KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > config > plugins > property > PropertyConfiguration


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2006, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.config.plugins.property;
23
24 import java.util.Properties JavaDoc;
25 import java.util.StringTokenizer JavaDoc;
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 /**
37  * PropertyConfiguration.
38  *
39  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
40  * @version $Revision: 45764 $
41  */

42 public class PropertyConfiguration extends AbstractConfiguration
43 {
44    /** The log */
45    private static final Logger log = Logger.getLogger(PropertyConfiguration.class);
46
47    /** The properties */
48    protected Properties JavaDoc properties;
49
50    /**
51     * Create a configuration
52     */

53    public PropertyConfiguration()
54    {
55       this(null);
56    }
57
58    /**
59     * Create a configuration
60     *
61     * @param properties the properties
62     */

63    public PropertyConfiguration(Properties JavaDoc properties)
64    {
65       if (properties == null)
66          properties = System.getProperties();
67       this.properties = properties;
68    }
69
70    /**
71     * Get the properties
72     *
73     * @return the properties
74     */

75    public Properties JavaDoc getProperties()
76    {
77       return properties;
78    }
79    
80    protected BeanInfoFactory createDefaultBeanInfoFactory() throws Throwable JavaDoc
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 JavaDoc
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 JavaDoc
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 JavaDoc
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 JavaDoc
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 JavaDoc
108    {
109       return (DependencyBuilder) loadFromProperties(PropertyConfigurationConstants.DEPENDENCY_BUILDER_NAME, PropertyConfigurationConstants.DEPENDENCY_BUILDER_DEFAULT, DependencyBuilder.class);
110    }
111
112    /**
113     * Load an object from the specified properties
114     *
115     * @param propertyName the property name
116     * @param defaultValue the default value
117     * @param targetClass the target class
118     * @return the object
119     * @throws Throwable for any error
120     */

121    protected Object JavaDoc loadFromProperties(String JavaDoc propertyName, String JavaDoc defaultValue, Class JavaDoc<? extends Object JavaDoc> targetClass) throws Throwable JavaDoc
122    {
123       String JavaDoc value = properties.getProperty(propertyName, defaultValue);
124       StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(value, ":");
125       Class JavaDoc clazz = null;
126       ClassNotFoundException JavaDoc error = null;
127       while (tokenizer.hasMoreTokens())
128       {
129          String JavaDoc className = tokenizer.nextToken();
130          try
131          {
132             clazz = getClass().getClassLoader().loadClass(className);
133             break;
134          }
135          catch (ClassNotFoundException JavaDoc 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 JavaDoc("Invalid configuration for property " + propertyName + " expected a class name that implements " + targetClass.getName());
145       
146       if (targetClass.isAssignableFrom(clazz) == false)
147          throw new RuntimeException JavaDoc("Class " + clazz.getName() + " specified in property " + propertyName + " does not implement " + targetClass.getName());
148
149       return clazz.newInstance();
150    }
151 }
152
Popular Tags