package org.jboss.axis;
import org.apache.commons.discovery.ResourceClassIterator;
import org.apache.commons.discovery.ResourceNameDiscover;
import org.apache.commons.discovery.ResourceNameIterator;
import org.apache.commons.discovery.resource.ClassLoaders;
import org.apache.commons.discovery.resource.classes.DiscoverClasses;
import org.apache.commons.discovery.resource.names.DiscoverMappedNames;
import org.apache.commons.discovery.resource.names.DiscoverNamesInAlternateManagedProperties;
import org.apache.commons.discovery.resource.names.DiscoverNamesInManagedProperties;
import org.apache.commons.discovery.resource.names.DiscoverServiceNames;
import org.apache.commons.discovery.resource.names.NameDiscoverers;
import org.apache.commons.discovery.tools.ClassUtils;
import org.apache.commons.discovery.tools.DefaultClassHolder;
import org.apache.commons.discovery.tools.DiscoverClass;
import org.apache.commons.discovery.tools.ManagedProperties;
import org.apache.commons.discovery.tools.PropertiesHolder;
import org.apache.commons.discovery.tools.SPInterface;
import org.jboss.axis.utils.Messages;
import org.jboss.logging.Logger;
import java.lang.reflect.InvocationTargetException;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Enumeration;
import java.util.Map;
import java.util.Properties;
public class AxisProperties
{
private static Logger log = Logger.getLogger(AxisProperties.class.getName());
private static DiscoverNamesInAlternateManagedProperties altNameDiscoverer;
private static DiscoverMappedNames mappedNames;
private static NameDiscoverers nameDiscoverer;
public static void setClassOverrideProperty(Class clazz, String propertyName)
{
getAlternatePropertyNameDiscoverer()
.addClassToPropertyNameMapping(clazz.getName(), propertyName);
}
public static void setClassDefault(Class clazz, String defaultName)
{
getMappedNames().map(clazz.getName(), defaultName);
}
public static void setClassDefaults(Class clazz, String[] defaultNames)
{
getMappedNames().map(clazz.getName(), defaultNames);
}
public static ResourceNameDiscover getNameDiscoverer()
{
if (nameDiscoverer == null)
{
nameDiscoverer = new NameDiscoverers();
nameDiscoverer.addResourceNameDiscover(getAlternatePropertyNameDiscoverer());
nameDiscoverer.addResourceNameDiscover(new DiscoverNamesInManagedProperties());
nameDiscoverer.addResourceNameDiscover(new DiscoverServiceNames(getClassLoaders()));
nameDiscoverer.addResourceNameDiscover(getMappedNames());
}
return nameDiscoverer;
}
public static ResourceClassIterator getResourceClassIterator(Class spi)
{
ResourceNameIterator it = getNameDiscoverer().findResourceNames(spi.getName());
return new DiscoverClasses(getClassLoaders()).findResourceClasses(it);
}
private static ClassLoaders getClassLoaders()
{
ClassLoaders loaders = ClassLoaders.getLibLoaders(AxisEngine.class, null, true);
return loaders;
}
private static DiscoverMappedNames getMappedNames()
{
if (mappedNames == null)
{
mappedNames = new DiscoverMappedNames();
}
return mappedNames;
}
private static DiscoverNamesInAlternateManagedProperties getAlternatePropertyNameDiscoverer()
{
if (altNameDiscoverer == null)
{
altNameDiscoverer = new DiscoverNamesInAlternateManagedProperties();
}
return altNameDiscoverer;
}
public static Object newInstance(Class spiClass)
{
return newInstance(spiClass, null, null);
}
public static Object newInstance(final Class spiClass,
final Class constructorParamTypes[],
final Object constructorParams[])
{
return AccessController.doPrivileged(new PrivilegedAction()
{
public Object run()
{
ResourceClassIterator services = getResourceClassIterator(spiClass);
Object obj = null;
while (obj == null && services.hasNext())
{
Class service = services.nextResourceClass().loadClass();
if (service != null)
{
try
{
ClassUtils.verifyAncestory(spiClass, service);
obj = ClassUtils.newInstance(service, constructorParamTypes, constructorParams);
}
catch (InvocationTargetException e)
{
if (e.getTargetException() instanceof java.lang.NoClassDefFoundError)
{
log.debug(Messages.getMessage("exception00"), e);
}
else
{
log.warn(Messages.getMessage("exception00"), e);
}
}
catch (Exception e)
{
log.warn(Messages.getMessage("exception00"), e);
}
}
}
return obj;
}
});
}
public static String getProperty(String propertyName)
{
return ManagedProperties.getProperty(propertyName);
}
public static String getProperty(String propertyName, String dephault)
{
return ManagedProperties.getProperty(propertyName, dephault);
}
public static void setProperty(String propertyName, String value)
{
ManagedProperties.setProperty(propertyName, value);
}
public static void setProperty(String propertyName, String value, boolean isDefault)
{
ManagedProperties.setProperty(propertyName, value, isDefault);
}
public static void setProperties(Map newProperties)
{
ManagedProperties.setProperties(newProperties);
}
public static void setProperties(Map newProperties, boolean isDefault)
{
ManagedProperties.setProperties(newProperties, isDefault);
}
public static Enumeration propertyNames()
{
return ManagedProperties.propertyNames();
}
public static Properties getProperties()
{
return ManagedProperties.getProperties();
}
public static Object newInstance(Class spiClass, Class defaultClass)
{
return newInstance(new SPInterface(spiClass), new DefaultClassHolder(defaultClass));
}
private static Object newInstance(final SPInterface spi,
final DefaultClassHolder defaultClass)
{
return AccessController.doPrivileged(new PrivilegedAction()
{
public Object run()
{
try
{
return DiscoverClass.newInstance(null,
spi,
(PropertiesHolder)null,
defaultClass);
}
catch (Exception e)
{
log.error(Messages.getMessage("exception00"), e);
}
return null;
}
});
}
}