Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: javatools/db/util/FirebirdUserCreator.java


1   /*
2    * FirebirdUserCreator.java
3    *
4    * Created on 27 febbraio 2003, 18.06
5       Javatools (modified version) - Some useful general classes.
6       Copyright (C) 2002-2003  Chris Bitmead (original) Antonio Petrelli (modified)
7   
8       This program is free software; you can redistribute it and/or modify
9       it under the terms of the GNU General Public License as published by
10      the Free Software Foundation; either version 2 of the License, or
11      (at your option) any later version.
12  
13      This program is distributed in the hope that it will be useful,
14      but WITHOUT ANY WARRANTY; without even the implied warranty of
15      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16      GNU General Public License for more details.
17  
18      You should have received a copy of the GNU General Public License
19      along with this program; if not, write to the Free Software
20      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  
22      Contact me at: brenmcguire@users.sourceforge.net
23   */
24  
25  package javatools.db.util;
26  
27  import java.io.*;
28  import java.sql.*;
29  import javatools.db.*;
30  import javatools.swing.dialog.JLogin;
31  import javatools.util.Registry;
32  import com.ice.jni.registry.*;
33  
34  /**
35   *
36   * @author  Antonio Petrelli
37   */
38  public class FirebirdUserCreator implements javatools.db.util.UserCreator {
39      
40      public String getRootDefaultPassword() {
41          return "masterkey";
42      }
43      
44      public String getRootUserName() {
45          return "SYSDBA";
46      }
47      
48      public void create(DbDatabase base, String userName, String password) throws DbException {
49          String osName, cmdLine, gsec, adminUserName, adminPassword, securityDir;
50          String[] cmdParms;
51          java.util.ResourceBundle javatoolsBundle;
52          Process proc;
53          int result;
54          boolean flag = true;
55          DbManager manager;
56          DbDatabase adminDB;
57          String connectString, s1, s2;
58          Connection conn;
59          PreparedStatement stmt;
60          Statement stmt2;
61          
62          javatoolsBundle = java.util.ResourceBundle.getBundle("res/JavatoolsBundle");
63          osName = System.getProperty("os.name");
64          securityDir = this.findSecurityDir();
65          manager = DbManager.singleton();
66          connectString = ConnectionString.build(manager, "firebird", "org.firebirdsql.jdbc.FBDriver", "localhost", -1, securityDir, "security", "", "");
67          try {
68              result = JLogin.showLoginDialog(
69                  javatoolsBundle.getString("Creating_new_user"),
70                  javatoolsBundle.getString("EnterDatabaseAdministrator"),
71                  "SYSDBA", "masterkey");
72              if (result == JLogin.OK_OPTION) {
73                  adminUserName = JLogin.getLoginUserName();
74                  adminPassword = JLogin.getLoginPassword();
75                  Class.forName("org.firebirdsql.jdbc.FBDriver");
76                  conn = DriverManager.getConnection(connectString, adminUserName, adminPassword);
77                  stmt = conn.prepareStatement("INSERT INTO USERS VALUES(?, null, null, null, null, ?, null, null, null, null, null)");
78                  stmt.setString(1, userName.toUpperCase());
79                  s1 = password;
80                  s1 = JCrypt.crypt("9z", password);
81                  s1 = s1.substring(2);
82                  s2 = JCrypt.crypt("9z", s1);
83                  s2 = s2.substring(2);
84                  password = s2;
85                  stmt.setString(2, password);
86                  stmt.execute();
87                  conn.close();
88              }
89          }
90          catch (Exception e) {
91              throw new DbException(e.getMessage());
92          }
93      }
94      
95      private String findSecurityDir() {
96          int i, numPos;
97          File tempFile, tempLibrary;
98          String tempString, tempLibraryString, osName;
99          String localConfigDir;
100         RegistryKey sharedDirKey;
101         
102         osName = System.getProperty("os.name");
103         tempString = null;
104         if (osName.equals("Linux")) {
105             numPos = positions.length;
106             for (i=0; i < numPos && tempString == null; i++) {
107                 tempFile = new File(positions[i]+"/security.fdb");
108                 if (tempFile.exists())
109                     tempString = positions[i];
110             }
111         }
112         else if (osName.startsWith("Windows")) {
113             try {
114                 sharedDirKey=javatools.util.Registry.HKEY_LOCAL_MACHINE.openSubKey(
115                     "SOFTWARE\\FirebirdSQL\\Firebird\\CurrentVersion");
116                 tempString = sharedDirKey.getStringValue("RootDirectory");
117             }
118             catch (NoSuchKeyException e) {
119                 System.out.println("Firebird Key not found!");
120             }
121             catch (NoSuchValueException e) {
122                 System.out.println("Firebird value not found!");
123             }
124             catch (RegistryException e) {
125                 System.out.println("Generic Registry Exception!");
126             }
127         }
128         return tempString;
129     }
130     
131     private static String[] positions = {"/opt/interbase", "/usr",
132         "/usr/local", "/usr/interbase", "/usr/local/interbase",
133         "/usr/local/firebird"};
134 }