Main Page | Packages | Class Hierarchy | Class List | Directories | File List | Class Members | Related Pages

DBConnectionManager Class Reference

List of all members.

Detailed Description

Manager for database connection
Author:
J. Monk from J. Butterworth

Definition at line 14 of file DBConnectionManager.java.


Public Member Functions

DBConnectionManager init (String[] args)
DBConnectionManager init (String arg)
DBConnectionManager init ()
synchronized Connection getConnection (String dbName, String user)
synchronized Connection getConnectionAsAdmin (String dbName)
synchronized Connection getConnection (String dbName)
synchronized Connection getConnection ()
synchronized Connection getConnectionAsAdmin ()

Package Attributes

boolean driverLoaded = false

Private Member Functions

void openConnection (String dbName, String user)

Private Attributes

HashMap< String, HashMap<
String, String > > 
URLs = new HashMap< String HashMap< String String> >()
HashMap< String, String > defaultUsers_ = new HashMap< String String>()
HashMap< String, String > adminUsers_ = new HashMap<String String>()
HashMap< String, HashMap<
String, Connection > > 
logConn = new HashMap<String HashMap<String Connection> >()

Member Function Documentation

synchronized Connection getConnection  ) 
 

Gets a connection to the default jetweb DB

Definition at line 185 of file DBConnectionManager.java.

00185                                                   {
00186 
00187     return(getConnection
00188            (DBConfig.JETWEB, defaultUsers_.get(DBConfig.JETWEB)));
00189     };

synchronized Connection getConnection String  dbName  ) 
 

Definition at line 176 of file DBConnectionManager.java.

00176                                                                {
00177     String userName = defaultUsers_.get(dbName);
00178 
00179     return(getConnection(dbName, userName));
00180     }

synchronized Connection getConnection String  dbName,
String  user
 

Does exactly what it says on the tin!

Parameters:
dbName the name of the database to get the connection from
user the name of the user for this connection

Definition at line 138 of file DBConnectionManager.java.

Referenced by DBConfig.getConnection(), BlobUpload.main(), and DBObjectManager.writeObject().

00138                                                                             {
00139 
00140     HashMap<String, Connection> connList = logConn.get(dbName);
00141     Connection con = null;
00142     if(connList !=null){
00143         con = connList.get(user);
00144     }
00145 
00146     boolean conExists = !(con==null);
00147     if(conExists){
00148         try{
00149         conExists = !(con.isClosed());
00150         }catch(SQLException e){
00151         System.out.println
00152             ("Unable to determine state of connection to "+dbName);
00153         System.out.println("SQLException: "+e.getMessage());
00154         }
00155     } 
00156 
00157     if(!conExists){
00158 
00159         System.out.println("dbName = "+dbName);
00160         System.out.println("user = "+user);
00161 
00162         System.out.println("URLs = "+URLs);
00163 
00164         System.out.println
00165         ("Reconnecting to database "+URLs.get(dbName).get(user));
00166         openConnection(dbName, user);
00167     }
00168     return(con);
00169     };

synchronized Connection getConnectionAsAdmin  ) 
 

Definition at line 192 of file DBConnectionManager.java.

00192                                                          {
00193     return(getConnection
00194            (DBConfig.JETWEB, adminUsers_.get(DBConfig.JETWEB)));
00195     }

synchronized Connection getConnectionAsAdmin String  dbName  ) 
 

Definition at line 171 of file DBConnectionManager.java.

Referenced by DBObjectManager.createTable().

00171                                                                       {
00172     String userName = adminUsers_.get(dbName);
00173     return(getConnection(dbName, userName));
00174     }

DBConnectionManager init  ) 
 

default initialisation to the jetweb DB.

Definition at line 127 of file DBConnectionManager.java.

00127                                      {
00128     return(init(DBConfig.JETWEB));
00129     };

DBConnectionManager init String  arg  ) 
 

initialise just the single named DB

Definition at line 118 of file DBConnectionManager.java.

00118                                                {
00119     String[] args = {arg};
00120     return(init(args));
00121     };

DBConnectionManager init String[]  args  ) 
 

Initialises the known databases. Database names are based on those given in the jetweb.properties file, e.g. jetweb=jdbc:mysql://127.0.0.1/jetweb can add more databases later by re-calling

Parameters:
String[] the array of database names we will initialise

Definition at line 35 of file DBConnectionManager.java.

Referenced by DBConfig.init(), GenDefaults.main(), and BlobUpload.main().

00035                                                   {
00036 
00037     if(!driverLoaded){
00038         driverLoaded = true;
00039         String driverName = System.getProperty("driverName");
00040         System.out.println("The sql driver name is "+driverName);
00041         
00042         try{
00043         Class.forName(driverName).newInstance();
00044         System.out.println("Loaded " + driverName);
00045         }catch(Exception E){
00046         System.err.println("Unable to load driver " + driverName);
00047         E.printStackTrace();
00048         }
00049     }
00050 
00051     for(String arg: args){
00052         
00053         //for(int ii=0; ii!= args.length; ++ii){
00054         //if(!URLs.containsKey(args[ii])){
00055         if(!URLs.containsKey(arg)){
00056         String URL = System.getProperty(arg);
00057         
00058         Vector<String> users = new Vector<String>();
00059         String allUsers = System.getProperty(arg+"_users");
00060 
00061         System.out.println("allUsers = "+allUsers);
00062 
00063         String userType = System.getProperty(arg+"_default_user");
00064 
00065         defaultUsers_.put(arg, userType);
00066         
00067         userType = System.getProperty(arg + "_admin_user");
00068         
00069         adminUsers_.put(arg, userType);
00070         
00071         int ii = allUsers.indexOf(" ");
00072         while(ii>-1){
00073             System.out.println("ii = "+ii);
00074             String user = allUsers.substring(0, ii).trim();
00075             allUsers = allUsers.substring(ii+1).trim();
00076             ii = allUsers.indexOf(" ");
00077             if(user.length()>0)users.add(user);
00078         }
00079 
00080         users.add(allUsers.trim());
00081 
00082         HashMap<String, String> passwords = new HashMap();
00083                 
00084         HashMap<String, String> userURLs = 
00085             new HashMap<String, String>();
00086         URLs.put(arg, userURLs);
00087         
00088         String userList = "";
00089 
00090         for(String user: users){
00091             String password = 
00092             System.getProperty(user + "_" + arg + "_password");
00093             if(password==null) password = "";
00094 
00095             userList = userList + " " + user;
00096 
00097             String fullURL = URL + "?user=" + user;
00098             if(!password.equals(""))
00099             fullURL = fullURL + "&password="+password;
00100 
00101             userURLs.put(user, fullURL);
00102             openConnection(arg, user);
00103         }
00104 
00105         System.out.println("Using database " + 
00106                    URL + 
00107                    " with users: " + userList);
00108         }
00109     }
00110         
00111     return(this);
00112     };

void openConnection String  dbName,
String  user
[private]
 

opens a connection to the database

Parameters:
dbName the name of the database

Definition at line 201 of file DBConnectionManager.java.

00201                                                            {
00202 
00203     try{
00204 
00205         HashMap<String, Connection> existingUsers = 
00206         logConn.get(dbName);
00207         if(existingUsers == null){ 
00208         existingUsers = 
00209             new HashMap<String, Connection>();
00210         logConn.put(dbName, existingUsers);
00211         }
00212 
00213         existingUsers.put
00214         (user, DriverManager.getConnection
00215          (URLs.get(dbName).get(user)));
00216         System.out.println("Connected to "+URLs.get(dbName).get(user));
00217         //System.out.println(logConn.get(arg).toString());
00218 
00219     }catch(SQLException E) {
00220         System.out.println("SQLException: " + E.getMessage());
00221         System.out.println("SQLState:     " + E.getSQLState());
00222         System.out.println("VendorError:  " + E.getErrorCode());
00223     }
00224     return;
00225     };


Member Data Documentation

HashMap<String, String> adminUsers_ = new HashMap<String String>() [private]
 

Definition at line 21 of file DBConnectionManager.java.

HashMap< String, String> defaultUsers_ = new HashMap< String String>() [private]
 

Definition at line 19 of file DBConnectionManager.java.

boolean driverLoaded = false [package]
 

Definition at line 25 of file DBConnectionManager.java.

HashMap<String, HashMap<String, Connection> > logConn = new HashMap<String HashMap<String Connection> >() [private]
 

Definition at line 23 of file DBConnectionManager.java.

HashMap<String, HashMap< String, String> > URLs = new HashMap< String HashMap< String String> >() [private]
 

Definition at line 17 of file DBConnectionManager.java.


The documentation for this class was generated from the following file:

Generated Wed Jan 17 09:14:27 GMT 2007