KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > broker > util > factory > ConfigurableFactory


1 package org.apache.ojb.broker.util.factory;
2
3 /* Copyright 2002-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 import org.apache.commons.lang.SystemUtils;
19 import org.apache.ojb.broker.PersistenceBrokerException;
20 import org.apache.ojb.broker.util.ClassHelper;
21 import org.apache.ojb.broker.util.configuration.Configurable;
22 import org.apache.ojb.broker.util.configuration.Configuration;
23 import org.apache.ojb.broker.util.configuration.ConfigurationException;
24 import org.apache.ojb.broker.util.configuration.impl.OjbConfigurator;
25 import org.apache.ojb.broker.util.interceptor.InterceptorFactory;
26 import org.apache.ojb.broker.util.logging.Logger;
27 import org.apache.ojb.broker.util.logging.LoggerFactory;
28
29 /**
30  * ConfigurableFactory is an abstract baseclass for OJB factory classes.
31  * It provides all infrastructure for configuration through OJB.properties.
32  * A derived class must implement the getConfigurationKey() method.
33  * The returned configuration key is used to lookup the class to be instantiated
34  * by the derived factory.
35  * The lookup is performed in the configure() method and uses the OJB.properties
36  * information.
37  *
38  * @author Thomas Mahler
39  */

40 public abstract class ConfigurableFactory implements Configurable
41 {
42     private Logger log = LoggerFactory.getLogger(this.getClass());
43
44     /**
45      * the class to be served
46      */

47     private Class JavaDoc classToServe = null;
48
49
50     /**
51      * the public constructor calls configure() to perform configuration
52      * of the factory instance.
53      */

54     public ConfigurableFactory()
55     {
56         OjbConfigurator.getInstance().configure(this);
57     }
58
59     /**
60      * must be implemented in the concrete factory classes.
61      * the configuration key is used to lookup the Class to serve
62      * from the OjbConfiguration in configure().
63      */

64     protected abstract String JavaDoc getConfigurationKey();
65
66     /**
67      * @see org.apache.ojb.broker.util.configuration.Configurable#configure(Configuration)
68      * looks up the the key getConfigurationKey() in the OjbConfiguration
69      * to determine the Class to be served.
70      */

71     public void configure(Configuration pConfig) throws ConfigurationException
72     {
73         if (getConfigurationKey() == null)
74         {
75             getLogger().error("ConfigurableFactory configuration key is 'null'");
76             throw new PersistenceBrokerException("ConfigurableFactory configuration key is 'null'");
77         }
78         Class JavaDoc clazz = pConfig.getClass(getConfigurationKey(), null);
79         if (clazz == null)
80         {
81             getLogger().error("ConfigurableFactory configuration key class for key'" + getConfigurationKey() + "' does not exist.");
82             throw new PersistenceBrokerException(
83                     "ConfigurableFactory configuration key class for key'" + getConfigurationKey() + "' does not exist.");
84         }
85         this.setClassToServe(clazz);
86     }
87
88     /**
89      * factory method for creating new instances
90      * the Class to be instantiated is defined by getClassToServe().
91      * @return Object the created instance
92      */

93     public Object JavaDoc createNewInstance(Class JavaDoc[] types, Object JavaDoc[] args)
94     {
95         try
96         {
97             Object JavaDoc result;
98             // create an instance of the target class
99
if (types != null)
100             {
101                 result = ClassHelper.newInstance(getClassToServe(), types, args, true);
102             }
103             else
104             {
105                 result = ClassHelper.newInstance(getClassToServe(), true);
106             }
107             // if defined in OJB.properties all instances are wrapped by an interceptor
108
result = InterceptorFactory.getInstance().createInterceptorFor(result);
109             return result;
110
111         }
112         catch (InstantiationException JavaDoc e)
113         {
114             getLogger().error("ConfigurableFactory can't instantiate class " +
115                     getClassToServe() + buildArgumentString(types, args), e);
116             throw new PersistenceBrokerException(e);
117         }
118         catch (IllegalAccessException JavaDoc e)
119         {
120             getLogger().error("ConfigurableFactory can't access constructor for class " +
121                     getClassToServe() + buildArgumentString(types, args), e);
122             throw new PersistenceBrokerException(e);
123         }
124         catch (Exception JavaDoc e)
125         {
126             getLogger().error("ConfigurableFactory instantiation failed for class " +
127                     getClassToServe() + buildArgumentString(types, args), e);
128             throw new PersistenceBrokerException(e);
129         }
130     }
131
132     protected String JavaDoc buildArgumentString(Class JavaDoc[] types, Object JavaDoc[] args)
133     {
134         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
135         String JavaDoc eol = SystemUtils.LINE_SEPARATOR;
136         buf.append(eol + "* Factory types: ");
137         if (types != null)
138         {
139             for (int i = 0; i < types.length; i++)
140             {
141                 Class JavaDoc type = types[i];
142                 buf.append(eol + (i + 1) + " - Type: " + (type != null ? type.getName() : null));
143             }
144         }
145         else
146             buf.append(eol + "none");
147
148         buf.append(eol + "* Factory arguments: ");
149         if (args != null)
150         {
151             for (int i = 0; i < args.length; i++)
152             {
153                 Object JavaDoc obj = args[i];
154                 buf.append(eol + (i + 1) + " - Argument: " + obj);
155             }
156         }
157         else
158             buf.append(eol + "none");
159         return buf.toString();
160     }
161
162     /**
163      * factory method for creating new instances
164      * the Class to be instantiated is defined by getClassToServe().
165      * @return Object the created instance
166      */

167     public Object JavaDoc createNewInstance()
168     {
169         return createNewInstance((Class JavaDoc) null, (Object JavaDoc) null);
170     }
171
172
173     /**
174      * factory method for creating new instances
175      * the Class to be instantiated is defined by getClassToServe().
176      * @return Object the created instance
177      */

178     public Object JavaDoc createNewInstance(Class JavaDoc type, Object JavaDoc arg)
179     {
180         if (type != null)
181             return createNewInstance(new Class JavaDoc[]{type}, new Object JavaDoc[]{arg});
182         else
183             return createNewInstance((Class JavaDoc[]) null, (Object JavaDoc[]) null);
184     }
185
186     /**
187      * Returns the classToServe.
188      * @return Class
189      */

190     public Class JavaDoc getClassToServe()
191     {
192         return classToServe;
193     }
194
195     /**
196      * Sets the classToServe.
197      * <br/>
198      * Normally this is done by the factory using
199      * {@link #getConfigurationKey}.
200      * <br/>
201      * <b>Note:</b> For internal use only!
202      * @param classToServe The classToServe to set
203      */

204     public void setClassToServe(Class JavaDoc classToServe)
205     {
206         this.classToServe = classToServe;
207     }
208
209     /**
210      * the logger for the ConfigurableFactory
211      */

212     protected Logger getLogger()
213     {
214         return log;
215     }
216 }
217
Popular Tags