1 16 17 package org.apache.axis; 18 19 import org.apache.axis.components.logger.LogFactory; 20 import org.apache.axis.utils.Messages; 21 import org.apache.commons.discovery.ResourceClassIterator; 22 import org.apache.commons.discovery.ResourceNameDiscover; 23 import org.apache.commons.discovery.ResourceNameIterator; 24 import org.apache.commons.discovery.resource.ClassLoaders; 25 import org.apache.commons.discovery.resource.classes.DiscoverClasses; 26 import org.apache.commons.discovery.resource.names.DiscoverMappedNames; 27 import org.apache.commons.discovery.resource.names.DiscoverNamesInAlternateManagedProperties; 28 import org.apache.commons.discovery.resource.names.DiscoverNamesInManagedProperties; 29 import org.apache.commons.discovery.resource.names.DiscoverServiceNames; 30 import org.apache.commons.discovery.resource.names.NameDiscoverers; 31 import org.apache.commons.discovery.tools.ClassUtils; 32 import org.apache.commons.discovery.tools.DefaultClassHolder; 33 import org.apache.commons.discovery.tools.DiscoverClass; 34 import org.apache.commons.discovery.tools.ManagedProperties; 35 import org.apache.commons.discovery.tools.PropertiesHolder; 36 import org.apache.commons.discovery.tools.SPInterface; 37 import org.apache.commons.logging.Log; 38 39 import java.lang.reflect.InvocationTargetException ; 40 import java.security.AccessController ; 41 import java.security.PrivilegedAction ; 42 import java.util.Enumeration ; 43 import java.util.Map ; 44 import java.util.Properties ; 45 46 47 77 public class AxisProperties { 78 79 protected static Log log = 80 LogFactory.getLog(AxisProperties.class.getName()); 81 82 private static DiscoverNamesInAlternateManagedProperties altNameDiscoverer; 83 private static DiscoverMappedNames mappedNames; 84 private static NameDiscoverers nameDiscoverer; 85 private static ClassLoaders loaders; 86 87 public static void setClassOverrideProperty(Class clazz, String propertyName) { 88 getAlternatePropertyNameDiscoverer() 89 .addClassToPropertyNameMapping(clazz.getName(), propertyName); 90 } 91 92 public static void setClassDefault(Class clazz, String defaultName) { 93 getMappedNames().map(clazz.getName(), defaultName); 94 } 95 96 public static void setClassDefaults(Class clazz, String [] defaultNames) { 97 getMappedNames().map(clazz.getName(), defaultNames); 98 } 99 100 public static synchronized ResourceNameDiscover getNameDiscoverer() { 101 if (nameDiscoverer == null) { 102 nameDiscoverer = new NameDiscoverers(); 103 nameDiscoverer.addResourceNameDiscover(getAlternatePropertyNameDiscoverer()); 104 nameDiscoverer.addResourceNameDiscover(new DiscoverNamesInManagedProperties()); 105 nameDiscoverer.addResourceNameDiscover(new DiscoverServiceNames(getClassLoaders())); 106 nameDiscoverer.addResourceNameDiscover(getMappedNames()); 107 } 108 return nameDiscoverer; 109 } 110 111 public static ResourceClassIterator getResourceClassIterator(Class spi) { 112 ResourceNameIterator it = getNameDiscoverer().findResourceNames(spi.getName()); 113 return new DiscoverClasses(loaders).findResourceClasses(it); 114 } 115 116 private static synchronized ClassLoaders getClassLoaders() { 117 if (loaders == null) { 118 loaders = ClassLoaders.getAppLoaders(AxisProperties.class, null, true); 119 } 120 return loaders; 121 } 122 123 private static synchronized DiscoverMappedNames getMappedNames() { 124 if (mappedNames == null) { 125 mappedNames = new DiscoverMappedNames(); 126 } 127 return mappedNames; 128 } 129 130 private static synchronized DiscoverNamesInAlternateManagedProperties getAlternatePropertyNameDiscoverer() { 131 if (altNameDiscoverer == null) { 132 altNameDiscoverer = new DiscoverNamesInAlternateManagedProperties(); 133 } 134 135 return altNameDiscoverer; 136 } 137 138 157 158 public static Object newInstance(Class spiClass) 159 { 160 return newInstance(spiClass, null, null); 161 } 162 163 public static Object newInstance(final Class spiClass, 164 final Class constructorParamTypes[], 165 final Object constructorParams[]) { 166 return AccessController.doPrivileged( 167 new PrivilegedAction () { 168 public Object run() { 169 ResourceClassIterator services = getResourceClassIterator(spiClass); 170 171 Object obj = null; 172 while (obj == null && services.hasNext()) { 173 Class service = services.nextResourceClass().loadClass(); 174 175 178 if (service != null) { 179 181 try { 182 ClassUtils.verifyAncestory(spiClass, service); 183 obj = ClassUtils.newInstance(service, constructorParamTypes, constructorParams); 184 } catch (InvocationTargetException e) { 185 if (e.getTargetException() instanceof java.lang.NoClassDefFoundError ) { 186 log.debug(Messages.getMessage("exception00"), e); 187 } else { 188 log.warn(Messages.getMessage("exception00"), e); 189 } 190 } catch (Exception e) { 191 log.warn(Messages.getMessage("exception00"), e); 192 } 193 } 194 } 195 196 return obj; 197 } 198 }); 199 } 200 201 202 208 public static String getProperty(String propertyName) { 209 return ManagedProperties.getProperty(propertyName); 210 } 211 212 220 public static String getProperty(String propertyName, String dephault) { 221 return ManagedProperties.getProperty(propertyName, dephault); 222 } 223 224 229 public static void setProperty(String propertyName, String value) { 230 ManagedProperties.setProperty(propertyName, value); 231 } 232 233 243 public static void setProperty(String propertyName, String value, boolean isDefault) { 244 ManagedProperties.setProperty(propertyName, value, isDefault); 245 } 246 247 253 public static void setProperties(Map newProperties) { 254 ManagedProperties.setProperties(newProperties); 255 } 256 257 258 269 public static void setProperties(Map newProperties, boolean isDefault) { 270 ManagedProperties.setProperties(newProperties, isDefault); 271 } 272 273 274 public static Enumeration propertyNames() { 275 return ManagedProperties.propertyNames(); 276 } 277 278 287 public static Properties getProperties() { 288 return ManagedProperties.getProperties(); 289 } 290 291 292 public static Object newInstance(Class spiClass, Class defaultClass) 293 { 294 return newInstance(new SPInterface(spiClass), new DefaultClassHolder(defaultClass)); 295 } 296 297 311 private static Object newInstance(final SPInterface spi, 312 final DefaultClassHolder defaultClass) 313 { 314 return AccessController.doPrivileged( 315 new PrivilegedAction () { 316 public Object run() { 317 try { 318 return DiscoverClass.newInstance(null, 319 spi, 320 (PropertiesHolder)null, 321 defaultClass); 322 } catch (Exception e) { 323 log.error(Messages.getMessage("exception00"), e); 324 } 325 return null; 326 } 327 }); 328 } 329 } 330 | Popular Tags |