KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > axis > AxisProperties


1 /*
2  * Copyright (C) The Apache Software Foundation. All rights reserved.
3  *
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the docs/licenses/apache-1.1.txt file.
7  */

8
9 package org.jboss.axis;
10
11 import org.apache.commons.discovery.ResourceClassIterator;
12 import org.apache.commons.discovery.ResourceNameDiscover;
13 import org.apache.commons.discovery.ResourceNameIterator;
14 import org.apache.commons.discovery.resource.ClassLoaders;
15 import org.apache.commons.discovery.resource.classes.DiscoverClasses;
16 import org.apache.commons.discovery.resource.names.DiscoverMappedNames;
17 import org.apache.commons.discovery.resource.names.DiscoverNamesInAlternateManagedProperties;
18 import org.apache.commons.discovery.resource.names.DiscoverNamesInManagedProperties;
19 import org.apache.commons.discovery.resource.names.DiscoverServiceNames;
20 import org.apache.commons.discovery.resource.names.NameDiscoverers;
21 import org.apache.commons.discovery.tools.ClassUtils;
22 import org.apache.commons.discovery.tools.DefaultClassHolder;
23 import org.apache.commons.discovery.tools.DiscoverClass;
24 import org.apache.commons.discovery.tools.ManagedProperties;
25 import org.apache.commons.discovery.tools.PropertiesHolder;
26 import org.apache.commons.discovery.tools.SPInterface;
27 import org.jboss.axis.utils.Messages;
28 import org.jboss.logging.Logger;
29
30 import java.lang.reflect.InvocationTargetException JavaDoc;
31 import java.security.AccessController JavaDoc;
32 import java.security.PrivilegedAction JavaDoc;
33 import java.util.Enumeration JavaDoc;
34 import java.util.Map JavaDoc;
35 import java.util.Properties JavaDoc;
36
37
38 /**
39  * <p>Configuration properties for AXIS.
40  * </p>
41  * <p/>
42  * <p>Manage configuration properties according to a secure
43  * scheme similar to that used by classloaders:
44  * <ul>
45  * <li><code>ClassLoader</code>s are organized in a tree hierarchy.</li>
46  * <li>each <code>ClassLoader</code> has a reference
47  * to a parent <code>ClassLoader</code>.</li>
48  * <li>the root of the tree is the bootstrap <code>ClassLoader</code>er.</li>
49  * <li>the youngest decendent is the thread context class loader.</li>
50  * <li>properties are bound to a <code>ClassLoader</code> instance
51  * <ul>
52  * <li><i>non-default</i> properties bound to a parent <code>ClassLoader</code>
53  * instance take precedence over all properties of the same name bound
54  * to any decendent.
55  * Just to confuse the issue, this is the default case.</li>
56  * <li><i>default</i> properties bound to a parent <code>ClassLoader</code>
57  * instance may be overriden by (default or non-default) properties of
58  * the same name bound to any decendent.
59  * </li>
60  * </ul>
61  * </li>
62  * <li>System properties take precedence over all other properties</li>
63  * </ul>
64  * </p>
65  *
66  * @author Richard A. Sitze
67  */

68 public class AxisProperties
69 {
70    private static Logger log = Logger.getLogger(AxisProperties.class.getName());
71
72    private static DiscoverNamesInAlternateManagedProperties altNameDiscoverer;
73    private static DiscoverMappedNames mappedNames;
74    private static NameDiscoverers nameDiscoverer;
75    //private static ClassLoaders loaders;
76

77    public static void setClassOverrideProperty(Class JavaDoc clazz, String JavaDoc propertyName)
78    {
79       getAlternatePropertyNameDiscoverer()
80               .addClassToPropertyNameMapping(clazz.getName(), propertyName);
81    }
82
83    public static void setClassDefault(Class JavaDoc clazz, String JavaDoc defaultName)
84    {
85       getMappedNames().map(clazz.getName(), defaultName);
86    }
87
88    public static void setClassDefaults(Class JavaDoc clazz, String JavaDoc[] defaultNames)
89    {
90       getMappedNames().map(clazz.getName(), defaultNames);
91    }
92
93    public static ResourceNameDiscover getNameDiscoverer()
94    {
95       if (nameDiscoverer == null)
96       {
97          nameDiscoverer = new NameDiscoverers();
98          nameDiscoverer.addResourceNameDiscover(getAlternatePropertyNameDiscoverer());
99          nameDiscoverer.addResourceNameDiscover(new DiscoverNamesInManagedProperties());
100          nameDiscoverer.addResourceNameDiscover(new DiscoverServiceNames(getClassLoaders()));
101          nameDiscoverer.addResourceNameDiscover(getMappedNames());
102       }
103       return nameDiscoverer;
104    }
105
106    public static ResourceClassIterator getResourceClassIterator(Class JavaDoc spi)
107    {
108       ResourceNameIterator it = getNameDiscoverer().findResourceNames(spi.getName());
109       return new DiscoverClasses(getClassLoaders()).findResourceClasses(it);
110    }
111
112    private static ClassLoaders getClassLoaders()
113    {
114       // [TDI 13-Aug-2004] Switched from getAppLoaders to getLibLoaders and even more importantly
115
// do not store the the result in a static member variable, because this will lead
116
// to references to stale context class loader of previous deployments
117
ClassLoaders loaders = ClassLoaders.getLibLoaders(AxisEngine.class, null, true);
118       return loaders;
119    }
120
121    private static DiscoverMappedNames getMappedNames()
122    {
123       if (mappedNames == null)
124       {
125          mappedNames = new DiscoverMappedNames();
126       }
127       return mappedNames;
128    }
129
130    private static DiscoverNamesInAlternateManagedProperties getAlternatePropertyNameDiscoverer()
131    {
132       if (altNameDiscoverer == null)
133       {
134          altNameDiscoverer = new DiscoverNamesInAlternateManagedProperties();
135       }
136
137       return altNameDiscoverer;
138    }
139
140    /**
141     * !WARNING!
142     * SECURITY issue.
143     * <p/>
144     * See bug 11874
145     * <p/>
146     * The solution to both is to move doPrivilege UP within AXIS to a
147     * class that is either private (cannot be reached by code outside
148     * AXIS) or that represents a secure public interface...
149     * <p/>
150     * This is going to require analysis and (probably) rearchitecting.
151     * So, I'm taking taking the easy way out until we are at a point
152     * where we can reasonably rearchitect for security.
153     */

154
155    public static Object JavaDoc newInstance(Class JavaDoc spiClass)
156    {
157       return newInstance(spiClass, null, null);
158    }
159
160    public static Object JavaDoc newInstance(final Class JavaDoc spiClass,
161                                     final Class JavaDoc constructorParamTypes[],
162                                     final Object JavaDoc constructorParams[])
163    {
164       return AccessController.doPrivileged(new PrivilegedAction JavaDoc()
165       {
166          public Object JavaDoc run()
167          {
168             ResourceClassIterator services = getResourceClassIterator(spiClass);
169
170             Object JavaDoc obj = null;
171             while (obj == null && services.hasNext())
172             {
173                Class JavaDoc service = services.nextResourceClass().loadClass();
174
175                /* service == null
176                 * if class resource wasn't loadable
177                 */

178                if (service != null)
179                {
180                   /* OK, class loaded.. attempt to instantiate it.
181                    */

182                   try
183                   {
184                      ClassUtils.verifyAncestory(spiClass, service);
185                      obj = ClassUtils.newInstance(service, constructorParamTypes, constructorParams);
186                   }
187                   catch (InvocationTargetException JavaDoc e)
188                   {
189                      if (e.getTargetException() instanceof java.lang.NoClassDefFoundError JavaDoc)
190                      {
191                         log.debug(Messages.getMessage("exception00"), e);
192                      }
193                      else
194                      {
195                         log.warn(Messages.getMessage("exception00"), e);
196                      }
197                   }
198                   catch (Exception JavaDoc e)
199                   {
200                      log.warn(Messages.getMessage("exception00"), e);
201                   }
202                }
203             }
204
205             return obj;
206          }
207       });
208    }
209
210
211    /**
212     * Get value for property bound to the current thread context class loader.
213     *
214     * @param propertyName property name.
215     * @return property value if found, otherwise default.
216     */

217    public static String JavaDoc getProperty(String JavaDoc propertyName)
218    {
219       return ManagedProperties.getProperty(propertyName);
220    }
221
222    /**
223     * Get value for property bound to the current thread context class loader.
224     * If not found, then return default.
225     *
226     * @param propertyName property name.
227     * @param dephault default value.
228     * @return property value if found, otherwise default.
229     */

230    public static String JavaDoc getProperty(String JavaDoc propertyName, String JavaDoc dephault)
231    {
232       return ManagedProperties.getProperty(propertyName, dephault);
233    }
234
235    /**
236     * Set value for property bound to the current thread context class loader.
237     *
238     * @param propertyName property name
239     * @param value property value (non-default) If null, remove the property.
240     */

241    public static void setProperty(String JavaDoc propertyName, String JavaDoc value)
242    {
243       ManagedProperties.setProperty(propertyName, value);
244    }
245
246    /**
247     * Set value for property bound to the current thread context class loader.
248     *
249     * @param propertyName property name
250     * @param value property value. If null, remove the property.
251     * @param isDefault determines if property is default or not.
252     * A non-default property cannot be overriden.
253     * A default property can be overriden by a property
254     * (default or non-default) of the same name bound to
255     * a decendent class loader.
256     */

257    public static void setProperty(String JavaDoc propertyName, String JavaDoc value, boolean isDefault)
258    {
259       ManagedProperties.setProperty(propertyName, value, isDefault);
260    }
261
262    /**
263     * Set property values for <code>Properties</code> bound to the
264     * current thread context class loader.
265     *
266     * @param newProperties name/value pairs to be bound
267     */

268    public static void setProperties(Map JavaDoc newProperties)
269    {
270       ManagedProperties.setProperties(newProperties);
271    }
272
273
274    /**
275     * Set property values for <code>Properties</code> bound to the
276     * current thread context class loader.
277     *
278     * @param newProperties name/value pairs to be bound
279     * @param isDefault determines if properties are default or not.
280     * A non-default property cannot be overriden.
281     * A default property can be overriden by a property
282     * (default or non-default) of the same name bound to
283     * a decendent class loader.
284     */

285    public static void setProperties(Map JavaDoc newProperties, boolean isDefault)
286    {
287       ManagedProperties.setProperties(newProperties, isDefault);
288    }
289
290
291    public static Enumeration JavaDoc propertyNames()
292    {
293       return ManagedProperties.propertyNames();
294    }
295
296    /**
297     * This is an expensive operation.
298     *
299     * @return Returns a <code>java.util.Properties</code> instance
300     * that is equivalent to the current state of the scoped
301     * properties, in that getProperty() will return the same value.
302     * However, this is a copy, so setProperty on the
303     * returned value will not effect the scoped properties.
304     */

305    public static Properties getProperties()
306    {
307       return ManagedProperties.getProperties();
308    }
309
310
311    public static Object JavaDoc newInstance(Class JavaDoc spiClass, Class JavaDoc defaultClass)
312    {
313       return newInstance(new SPInterface(spiClass), new DefaultClassHolder(defaultClass));
314    }
315
316    /**
317     * !WARNING!
318     * SECURITY issue.
319     * <p/>
320     * See bug 11874
321     * <p/>
322     * The solution to both is to move doPrivilege UP within AXIS to a
323     * class that is either private (cannot be reached by code outside
324     * AXIS) or that represents a secure public interface...
325     * <p/>
326     * This is going to require analysis and (probably) rearchitecting.
327     * So, I'm taking taking the easy way out until we are at a point
328     * where we can reasonably rearchitect for security.
329     */

330    private static Object JavaDoc newInstance(final SPInterface spi,
331                                      final DefaultClassHolder defaultClass)
332    {
333       return AccessController.doPrivileged(new PrivilegedAction JavaDoc()
334       {
335          public Object JavaDoc run()
336          {
337             try
338             {
339                return DiscoverClass.newInstance(null,
340                        spi,
341                        (PropertiesHolder)null,
342                        defaultClass);
343             }
344             catch (Exception JavaDoc e)
345             {
346                log.error(Messages.getMessage("exception00"), e);
347             }
348             return null;
349          }
350       });
351    }
352 }
353
Popular Tags