KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis > configuration > EngineConfigurationFactoryFinder


1 /*
2  * Copyright 2002-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
17 package org.apache.axis.configuration;
18
19 import org.apache.axis.AxisProperties;
20 import org.apache.axis.EngineConfigurationFactory;
21 import org.apache.axis.components.logger.LogFactory;
22 import org.apache.axis.utils.Messages;
23 import org.apache.commons.discovery.ResourceClassIterator;
24 import org.apache.commons.discovery.tools.ClassUtils;
25 import org.apache.commons.logging.Log;
26
27 import java.lang.reflect.InvocationTargetException JavaDoc;
28 import java.lang.reflect.Method JavaDoc;
29 import java.security.AccessController JavaDoc;
30 import java.security.PrivilegedAction JavaDoc;
31
32
33 /**
34  * This is a default implementation of EngineConfigurationFactory.
35  * It is user-overrideable by a system property without affecting
36  * the caller. If you decide to override it, use delegation if
37  * you want to inherit the behaviour of this class as using
38  * class extension will result in tight loops. That is, your
39  * class should implement EngineConfigurationFactory and keep
40  * an instance of this class in a member field and delegate
41  * methods to that instance when the default behaviour is
42  * required.
43  *
44  * @author Richard A. Sitze
45  */

46 public class EngineConfigurationFactoryFinder
47 {
48     protected static Log log =
49         LogFactory.getLog(EngineConfigurationFactoryFinder.class.getName());
50
51     private static final Class JavaDoc mySpi = EngineConfigurationFactory.class;
52
53     private static final Class JavaDoc[] newFactoryParamTypes =
54         new Class JavaDoc[] { Object JavaDoc.class };
55
56     private static final String JavaDoc requiredMethod =
57         "public static EngineConfigurationFactory newFactory(Object)";
58
59     static {
60         AxisProperties.setClassOverrideProperty(
61                 EngineConfigurationFactory.class,
62                 EngineConfigurationFactory.SYSTEM_PROPERTY_NAME);
63
64         AxisProperties.setClassDefaults(EngineConfigurationFactory.class,
65                             new String JavaDoc[] {
66                                 "org.apache.axis.configuration.EngineConfigurationFactoryServlet",
67                                 "org.apache.axis.configuration.EngineConfigurationFactoryDefault",
68                                 });
69     }
70
71     private EngineConfigurationFactoryFinder() {
72     }
73
74
75     /**
76      * Create the default engine configuration and detect whether the user
77      * has overridden this with their own.
78      *
79      * The discovery mechanism will use the following logic:
80      *
81      * - discover all available EngineConfigurationFactories
82      * - find all META-INF/services/org.apache.axis.EngineConfigurationFactory
83      * files available through class loaders.
84      * - read files (see Discovery) to obtain implementation(s) of that
85      * interface
86      * - For each impl, call 'newFactory(Object param)'
87      * - Each impl should examine the 'param' and return a new factory ONLY
88      * - if it knows what to do with it
89      * (i.e. it knows what to do with the 'real' type)
90      * - it can find it's configuration information
91      * - Return first non-null factory found.
92      * - Try EngineConfigurationFactoryServlet.newFactory(obj)
93      * - Try EngineConfigurationFactoryDefault.newFactory(obj)
94      * - If zero found (all return null), throw exception
95      *
96      * ***
97      * This needs more work: System.properties, etc.
98      * Discovery will have more tools to help with that
99      * (in the manner of use below) in the near future.
100      * ***
101      *
102      */

103     public static EngineConfigurationFactory newFactory(final Object JavaDoc obj) {
104         /**
105          * recreate on each call is critical to gaining
106          * the right class loaders. Do not cache.
107          */

108         final Object JavaDoc[] params = new Object JavaDoc[] { obj };
109
110         /**
111          * Find and examine each service
112          */

113         return (EngineConfigurationFactory)AccessController.doPrivileged(
114                 new PrivilegedAction JavaDoc() {
115                     public Object JavaDoc run() {
116                         ResourceClassIterator services = AxisProperties.getResourceClassIterator(mySpi);
117
118                         EngineConfigurationFactory factory = null;
119
120                         while (factory == null && services.hasNext()) {
121                           try {
122                             Class JavaDoc service = services.nextResourceClass().loadClass();
123                 
124                             /* service == null
125                              * if class resource wasn't loadable
126                              */

127                             if (service != null) {
128                                 factory = newFactory(service, newFactoryParamTypes, params);
129                             }
130                           } catch (Exception JavaDoc e) {
131                             // there was an exception creating the factory
132
// the most likely cause was the JDK 1.4 problem
133
// in the discovery code that requires servlet.jar
134
// to be in the client classpath. For now, fall
135
// through to the next factory
136
}
137                         }
138                 
139                         if (factory != null) {
140                             if(log.isDebugEnabled()) {
141                                 log.debug(Messages.getMessage("engineFactory", factory.getClass().getName()));
142                             }
143                         } else {
144                             log.error(Messages.getMessage("engineConfigFactoryMissing"));
145                             // we should be throwing an exception here,
146
//
147
// but again, requires more refactoring than we want to swallow
148
// at this point in time. Ifthis DOES occur, it's a coding error:
149
// factory should NEVER be null.
150
// Testing will find this, as NullPointerExceptions will be generated
151
// elsewhere.
152
}
153                 
154                         return factory;
155                     }
156                 });
157     }
158
159     public static EngineConfigurationFactory newFactory() {
160         return newFactory(null);
161     }
162
163     private static EngineConfigurationFactory newFactory(Class JavaDoc service,
164                                                          Class JavaDoc[] paramTypes,
165                                                          Object JavaDoc[] param) {
166         /**
167          * Some JDK's may link on method resolution (findPublicStaticMethod)
168          * and others on method call (method.invoke).
169          *
170          * Either way, catch class load/resolve problems and return null.
171          */

172         
173         try {
174             /**
175              * Verify that service implements:
176              * public static EngineConfigurationFactory newFactory(Object);
177              */

178             Method JavaDoc method = ClassUtils.findPublicStaticMethod(service,
179                                                   EngineConfigurationFactory.class,
180                                                   "newFactory",
181                                                   paramTypes);
182     
183             if (method == null) {
184                 log.warn(Messages.getMessage("engineConfigMissingNewFactory",
185                                               service.getName(),
186                                               requiredMethod));
187             } else {
188                 try {
189                     return (EngineConfigurationFactory)method.invoke(null, param);
190                 } catch (InvocationTargetException JavaDoc e) {
191                     if (e.getTargetException() instanceof NoClassDefFoundError JavaDoc) {
192                         log.debug(Messages.getMessage("engineConfigLoadFactory",
193                                                       service.getName()));
194                     } else {
195                         log.warn(Messages.getMessage("engineConfigInvokeNewFactory",
196                                                       service.getName(),
197                                                       requiredMethod), e);
198                     }
199                 } catch (Exception JavaDoc e) {
200                     log.warn(Messages.getMessage("engineConfigInvokeNewFactory",
201                                                   service.getName(),
202                                                   requiredMethod), e);
203                 }
204             }
205         } catch (NoClassDefFoundError JavaDoc e) {
206             log.debug(Messages.getMessage("engineConfigLoadFactory",
207                                           service.getName()));
208         }
209
210         return null;
211     }
212 }
213
Popular Tags