1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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 /***
25 * Implementation of Factory pattern responsible for instantiating
26 * the DataStore interface implementation.
27 *
28 * The name of the class to instantiate should exist as a property
29 * in the juddi.properties configuration file with a property name
30 * of juddi.datasource.datastoreClassName. If the property is not
31 * found an Exception is thrown.
32 *
33 * @author Steve Viens (sviens@apache.org)
34 */
35 public class DataStoreFactory
36 {
37
38 private static Log log = LogFactory.getLog(DataStoreFactory.class);
39
40
41 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 /***
47 * Returns a new instance of a DataStore.
48 *
49 * @return DataStore
50 */
51 public static DataStore getDataStore()
52 {
53 DataStore dataStore = null;
54
55 try
56 {
57
58 if (implClass == null)
59 implClass = loadImplClass();
60
61
62 if (implClass == null)
63 throw new RegistryException("The registry is not configured " +
64 "correctly.");
65
66
67 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 /***
81 * Returns a new instance of a Authenticator.
82 *
83 * @return Authenticator
84 */
85 private static synchronized Class loadImplClass()
86 {
87 if (implClass != null)
88 return implClass;
89
90
91 String className = Config.getStringProperty(IMPL_KEY,DEFAULT_IMPL);
92
93
94 log.debug("DataStore Implementation = " + className);
95
96 try
97 {
98
99 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 /****************************** TEST DRIVER *********************************/
115 /****************************************************************************/
116
117
118 public static void main(String[] args)
119 {
120
121 int connCount = 10;
122 DataStore[] stores = new DataStore[connCount];
123
124
125 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
135 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 }