1 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 ; 31 import java.security.AccessController ; 32 import java.security.PrivilegedAction ; 33 import java.util.Enumeration ; 34 import java.util.Map ; 35 import java.util.Properties ; 36 37 38 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 77 public static void setClassOverrideProperty(Class clazz, String propertyName) 78 { 79 getAlternatePropertyNameDiscoverer() 80 .addClassToPropertyNameMapping(clazz.getName(), propertyName); 81 } 82 83 public static void setClassDefault(Class clazz, String defaultName) 84 { 85 getMappedNames().map(clazz.getName(), defaultName); 86 } 87 88 public static void setClassDefaults(Class clazz, String [] 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 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 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 154 155 public static Object newInstance(Class spiClass) 156 { 157 return newInstance(spiClass, null, null); 158 } 159 160 public static Object newInstance(final Class spiClass, 161 final Class constructorParamTypes[], 162 final Object constructorParams[]) 163 { 164 return AccessController.doPrivileged(new PrivilegedAction () 165 { 166 public Object run() 167 { 168 ResourceClassIterator services = getResourceClassIterator(spiClass); 169 170 Object obj = null; 171 while (obj == null && services.hasNext()) 172 { 173 Class service = services.nextResourceClass().loadClass(); 174 175 178 if (service != null) 179 { 180 182 try 183 { 184 ClassUtils.verifyAncestory(spiClass, service); 185 obj = ClassUtils.newInstance(service, constructorParamTypes, constructorParams); 186 } 187 catch (InvocationTargetException e) 188 { 189 if (e.getTargetException() instanceof java.lang.NoClassDefFoundError ) 190 { 191 log.debug(Messages.getMessage("exception00"), e); 192 } 193 else 194 { 195 log.warn(Messages.getMessage("exception00"), e); 196 } 197 } 198 catch (Exception e) 199 { 200 log.warn(Messages.getMessage("exception00"), e); 201 } 202 } 203 } 204 205 return obj; 206 } 207 }); 208 } 209 210 211 217 public static String getProperty(String propertyName) 218 { 219 return ManagedProperties.getProperty(propertyName); 220 } 221 222 230 public static String getProperty(String propertyName, String dephault) 231 { 232 return ManagedProperties.getProperty(propertyName, dephault); 233 } 234 235 241 public static void setProperty(String propertyName, String value) 242 { 243 ManagedProperties.setProperty(propertyName, value); 244 } 245 246 257 public static void setProperty(String propertyName, String value, boolean isDefault) 258 { 259 ManagedProperties.setProperty(propertyName, value, isDefault); 260 } 261 262 268 public static void setProperties(Map newProperties) 269 { 270 ManagedProperties.setProperties(newProperties); 271 } 272 273 274 285 public static void setProperties(Map newProperties, boolean isDefault) 286 { 287 ManagedProperties.setProperties(newProperties, isDefault); 288 } 289 290 291 public static Enumeration propertyNames() 292 { 293 return ManagedProperties.propertyNames(); 294 } 295 296 305 public static Properties getProperties() 306 { 307 return ManagedProperties.getProperties(); 308 } 309 310 311 public static Object newInstance(Class spiClass, Class defaultClass) 312 { 313 return newInstance(new SPInterface(spiClass), new DefaultClassHolder(defaultClass)); 314 } 315 316 330 private static Object newInstance(final SPInterface spi, 331 final DefaultClassHolder defaultClass) 332 { 333 return AccessController.doPrivileged(new PrivilegedAction () 334 { 335 public Object run() 336 { 337 try 338 { 339 return DiscoverClass.newInstance(null, 340 spi, 341 (PropertiesHolder)null, 342 defaultClass); 343 } 344 catch (Exception e) 345 { 346 log.error(Messages.getMessage("exception00"), e); 347 } 348 return null; 349 } 350 }); 351 } 352 } 353 | Popular Tags |