1 22 package org.jboss.config.plugins.property; 23 24 import java.util.Properties ; 25 import java.util.StringTokenizer ; 26 27 import org.jboss.beans.info.spi.BeanInfoFactory; 28 import org.jboss.classadapter.spi.ClassAdapterFactory; 29 import org.jboss.classadapter.spi.DependencyBuilder; 30 import org.jboss.config.plugins.AbstractConfiguration; 31 import org.jboss.joinpoint.spi.JoinpointFactoryBuilder; 32 import org.jboss.logging.Logger; 33 import org.jboss.reflect.spi.TypeInfoFactory; 34 import org.jboss.repository.spi.MetaDataContextFactory; 35 36 42 public class PropertyConfiguration extends AbstractConfiguration 43 { 44 45 private static final Logger log = Logger.getLogger(PropertyConfiguration.class); 46 47 48 protected Properties properties; 49 50 53 public PropertyConfiguration() 54 { 55 this(null); 56 } 57 58 63 public PropertyConfiguration(Properties properties) 64 { 65 if (properties == null) 66 properties = System.getProperties(); 67 this.properties = properties; 68 } 69 70 75 public Properties getProperties() 76 { 77 return properties; 78 } 79 80 protected BeanInfoFactory createDefaultBeanInfoFactory() throws Throwable 81 { 82 return (BeanInfoFactory) loadFromProperties(PropertyConfigurationConstants.BEAN_INFO_FACTORY_NAME, PropertyConfigurationConstants.BEAN_INFO_FACTORY_DEFAULT, BeanInfoFactory.class); 83 } 84 85 protected ClassAdapterFactory createDefaultClassAdapterFactory() throws Throwable 86 { 87 ClassAdapterFactory result = (ClassAdapterFactory) loadFromProperties(PropertyConfigurationConstants.CLASS_ADAPTER_FACTORY_NAME, PropertyConfigurationConstants.CLASS_ADAPTER_FACTORY_DEFAULT, ClassAdapterFactory.class); 88 result.setConfiguration(this); 89 return result; 90 } 91 92 protected TypeInfoFactory createDefaultTypeInfoFactory() throws Throwable 93 { 94 return (TypeInfoFactory) loadFromProperties(PropertyConfigurationConstants.TYPE_INFO_FACTORY_NAME, PropertyConfigurationConstants.TYPE_INFO_FACTORY_DEFAULT, TypeInfoFactory.class); 95 } 96 97 protected JoinpointFactoryBuilder createDefaultJoinpointFactoryBuilder() throws Throwable 98 { 99 return (JoinpointFactoryBuilder) loadFromProperties(PropertyConfigurationConstants.JOIN_POINT_FACTORY_BUILDER_NAME, PropertyConfigurationConstants.JOIN_POINT_FACTORY_BUILDER_DEFAULT, JoinpointFactoryBuilder.class); 100 } 101 102 protected MetaDataContextFactory createDefaultMetaDataContextFactory() throws Throwable 103 { 104 return (MetaDataContextFactory) loadFromProperties(PropertyConfigurationConstants.META_DATA_CONTEXT_FACTORY_BUILDER_NAME, PropertyConfigurationConstants.META_DATA_CONTEXT_FACTORY_BUILDER_DEFAULT, MetaDataContextFactory.class); 105 } 106 107 protected DependencyBuilder createDefaultDependencyBuilder() throws Throwable 108 { 109 return (DependencyBuilder) loadFromProperties(PropertyConfigurationConstants.DEPENDENCY_BUILDER_NAME, PropertyConfigurationConstants.DEPENDENCY_BUILDER_DEFAULT, DependencyBuilder.class); 110 } 111 112 121 protected Object loadFromProperties(String propertyName, String defaultValue, Class <? extends Object > targetClass) throws Throwable 122 { 123 String value = properties.getProperty(propertyName, defaultValue); 124 StringTokenizer tokenizer = new StringTokenizer (value, ":"); 125 Class clazz = null; 126 ClassNotFoundException error = null; 127 while (tokenizer.hasMoreTokens()) 128 { 129 String className = tokenizer.nextToken(); 130 try 131 { 132 clazz = getClass().getClassLoader().loadClass(className); 133 break; 134 } 135 catch (ClassNotFoundException ignored) 136 { 137 log.trace(className + " not found: " + ignored.getMessage()); 138 error = ignored; 139 } 140 } 141 if (clazz == null && error != null) 142 throw error; 143 if (clazz == null) 144 throw new RuntimeException ("Invalid configuration for property " + propertyName + " expected a class name that implements " + targetClass.getName()); 145 146 if (targetClass.isAssignableFrom(clazz) == false) 147 throw new RuntimeException ("Class " + clazz.getName() + " specified in property " + propertyName + " does not implement " + targetClass.getName()); 148 149 return clazz.newInstance(); 150 } 151 } 152 | Popular Tags |