KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > juddi > datastore > DataStoreFactory


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
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   // private reference to the jUDDI logger
38
private static Log log = LogFactory.getLog(DataStoreFactory.class);
39
40   // Authenticator property key & default implementation
41
private static final String JavaDoc IMPL_KEY = "juddi.dataStore";
42   private static final String JavaDoc DEFAULT_IMPL = "org.apache.juddi.datastore.jdbc.JDBCDataStore";
43
44   private static Class JavaDoc 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       // make sure we know what class to create
58
if (implClass == null)
59         implClass = loadImplClass();
60
61       // true if a configuration problem exists
62
if (implClass == null)
63         throw new RegistryException("The registry is not configured " +
64             "correctly.");
65         
66       // try to instantiate a new DataStore
67
dataStore = (DataStore)implClass.newInstance();
68     }
69     catch(Exception JavaDoc 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 JavaDoc loadImplClass()
86   {
87     if (implClass != null)
88       return implClass;
89     
90     // grab class name of the DataStore implementation to create
91
String JavaDoc className = Config.getStringProperty(IMPL_KEY,DEFAULT_IMPL);
92
93     // write the DataStore implementation name to the log
94
log.debug("DataStore Implementation = " + className);
95
96     try
97     {
98       // Use Loader to locate & load the DataStore implementation
99
implClass = Loader.getClassForName(className);
100     }
101     catch(ClassNotFoundException JavaDoc 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 JavaDoc[] args)
119   {
120     // delecare work variables
121
int connCount = 10;
122     DataStore[] stores = new DataStore[connCount];
123
124     // aquire some connections
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     // release those connections
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 }
Popular Tags