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

DBConnectionManager.java

Go to the documentation of this file.
00001 package cedar.jetweb.db;
00002 
00003 import java.sql.*;
00004 import java.util.Vector;
00005 import java.util.HashMap;
00013 
00014 public class DBConnectionManager {
00015 
00016     private HashMap<String, HashMap< String, String> > URLs
00017     = new HashMap< String, HashMap< String, String> >();
00018     private HashMap< String, String> defaultUsers_ 
00019     = new HashMap< String, String>();
00020     private HashMap<String, String> adminUsers_
00021     = new HashMap<String, String>();
00022     private HashMap<String, HashMap<String, Connection> > logConn 
00023     = new HashMap<String, HashMap<String, Connection> >();
00024 
00025     boolean driverLoaded=false;
00026 
00028 
00035     public DBConnectionManager init(String[] args){
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     };
00113 
00115 
00118     public DBConnectionManager init(String arg){
00119     String[] args = {arg};
00120     return(init(args));
00121     };
00123 
00127     public DBConnectionManager init(){
00128     return(init(DBConfig.JETWEB));
00129     };
00130 
00132 
00138     public synchronized Connection getConnection(String dbName, String user){
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     };
00170 
00171     public synchronized Connection getConnectionAsAdmin(String dbName){
00172     String userName = adminUsers_.get(dbName);
00173     return(getConnection(dbName, userName));
00174     }
00175 
00176     public synchronized Connection getConnection(String dbName){
00177     String userName = defaultUsers_.get(dbName);
00178 
00179     return(getConnection(dbName, userName));
00180     }
00182 
00185     public synchronized Connection getConnection(){
00186 
00187     return(getConnection
00188            (DBConfig.JETWEB, defaultUsers_.get(DBConfig.JETWEB)));
00189     };
00191 
00192     public synchronized Connection getConnectionAsAdmin(){
00193     return(getConnection
00194            (DBConfig.JETWEB, adminUsers_.get(DBConfig.JETWEB)));
00195     }
00197 
00201     private void openConnection(String dbName, String user){
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     };
00226 }

Generated Wed Jan 17 09:14:27 GMT 2007