1 16 package org.apache.juddi.datastore; 17 18 import org.apache.commons.logging.Log; 19 import org.apache.commons.logging.LogFactory; 20 import org.apache.juddi.error.RegistryException; 21 import org.apache.juddi.util.Config; 22 import org.apache.juddi.util.Loader; 23 24 35 public class DataStoreFactory 36 { 37 private static Log log = LogFactory.getLog(DataStoreFactory.class); 39 40 private static final String IMPL_KEY = "juddi.dataStore"; 42 private static final String DEFAULT_IMPL = "org.apache.juddi.datastore.jdbc.JDBCDataStore"; 43 44 private static Class implClass = null; 45 46 51 public static DataStore getDataStore() 52 { 53 DataStore dataStore = null; 54 55 try 56 { 57 if (implClass == null) 59 implClass = loadImplClass(); 60 61 if (implClass == null) 63 throw new RegistryException("The registry is not configured " + 64 "correctly."); 65 66 dataStore = (DataStore)implClass.newInstance(); 68 } 69 catch(Exception e) 70 { 71 log.error("Exception while attempting to instantiate the " + 72 "implementation of DataStore: " + implClass.getName() + 73 "\n" + e.getMessage()); 74 log.error(e); 75 } 76 77 return dataStore; 78 } 79 80 85 private static synchronized Class loadImplClass() 86 { 87 if (implClass != null) 88 return implClass; 89 90 String className = Config.getStringProperty(IMPL_KEY,DEFAULT_IMPL); 92 93 log.debug("DataStore Implementation = " + className); 95 96 try 97 { 98 implClass = Loader.getClassForName(className); 100 } 101 catch(ClassNotFoundException e) 102 { 103 log.error("The registry is not configured correctly. The specified " + 104 "DataStore class '" + className + "' was not found in " + 105 "classpath."); 106 log.error(e); 107 } 108 109 return implClass; 110 } 111 112 113 114 115 116 117 118 public static void main(String [] args) 119 { 120 int connCount = 10; 122 DataStore[] stores = new DataStore[connCount]; 123 124 for (int i=0; i<connCount; i++) 126 { 127 stores[i] = (DataStore)DataStoreFactory.getDataStore(); 128 if (stores[i] != null) 129 System.out.println("Got a DataStore: "+stores[i].getClass().getName()); 130 else 131 System.out.println("Sorry - A DataStore object could not be created."); 132 } 133 134 for (int i=0; i<connCount; i++) 136 { 137 if (stores[i] != null) 138 { 139 stores[i].release(); 140 System.out.println("DataStore "+i+" released."); 141 } 142 else 143 System.out.println("DataStore "+i+" was never successfully created."); 144 } 145 } 146 } | Popular Tags |