KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > java > plugin > standard > StandardObjectFactory


1 /*****************************************************************************
2  * Java Plug-in Framework (JPF)
3  * Copyright (C) 2004-2006 Dmitry Olshansky
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *****************************************************************************/

19 package org.java.plugin.standard;
20
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23 import org.java.plugin.ObjectFactory;
24 import org.java.plugin.PathResolver;
25 import org.java.plugin.PluginManager;
26 import org.java.plugin.registry.PluginRegistry;
27 import org.java.plugin.util.ExtendedProperties;
28
29 /**
30  * Standard object factory implementation.
31  * @version $Id: StandardObjectFactory.java,v 1.8 2006/06/05 18:16:43 ddimon Exp $
32  */

33 public class StandardObjectFactory extends ObjectFactory {
34     static final String JavaDoc PACKAGE_NAME = "org.java.plugin.standard"; //$NON-NLS-1$
35

36     protected Log log = LogFactory.getLog(getClass());
37     protected ExtendedProperties config;
38
39     /**
40      * @see org.java.plugin.ObjectFactory#configure(ExtendedProperties)
41      */

42     protected void configure(final ExtendedProperties configuration) {
43         config = (configuration != null) ? configuration
44                 : new ExtendedProperties();
45     }
46     
47     protected String JavaDoc getImplClassName(final Class JavaDoc cls) {
48         String JavaDoc result = config.getProperty(cls.getName(), null);
49         if (log.isDebugEnabled()) {
50             log.debug("implementation class for " + cls.getName() //$NON-NLS-1$
51
+ " is " + result); //$NON-NLS-1$
52
}
53         return result;
54     }
55     
56     protected Object JavaDoc createClassInstance(final String JavaDoc className)
57             throws InstantiationException JavaDoc, IllegalAccessException JavaDoc,
58             ClassNotFoundException JavaDoc {
59         ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader();
60         if (cl != null) {
61             try {
62                 return cl.loadClass(className).newInstance();
63             } catch (ClassNotFoundException JavaDoc cnfe) {
64                 // ignore
65
}
66         }
67         cl = getClass().getClassLoader();
68         if (cl != null) {
69             try {
70                 return cl.loadClass(className).newInstance();
71             } catch (ClassNotFoundException JavaDoc cnfe) {
72                 // ignore
73
}
74         }
75         return ClassLoader.getSystemClassLoader().loadClass(
76                 className).newInstance();
77     }
78
79     /**
80      * @see org.java.plugin.ObjectFactory#createRegistry()
81      */

82     public PluginRegistry createRegistry() {
83         String JavaDoc className = getImplClassName(PluginRegistry.class);
84         PluginRegistry result;
85         if (className == null) {
86             className = "org.java.plugin.registry.xml.PluginRegistryImpl"; //$NON-NLS-1$
87
}
88         try {
89             result = (PluginRegistry) createClassInstance(className);
90         } catch (Exception JavaDoc e) {
91             log.fatal("failed creating registry instance " //$NON-NLS-1$
92
+ className, e);
93             throw new Error JavaDoc("failed creating registry instance " //$NON-NLS-1$
94
+ className, e);
95         }
96         result.configure(config.getSubset(className + ".")); //$NON-NLS-1$
97
log.debug("registry instance created - " + result); //$NON-NLS-1$
98
return result;
99     }
100
101     /**
102      * @see org.java.plugin.ObjectFactory#createPathResolver()
103      */

104     public PathResolver createPathResolver() {
105         String JavaDoc className = getImplClassName(PathResolver.class);
106         PathResolver result;
107         if (className == null) {
108             className = "org.java.plugin.standard.StandardPathResolver"; //$NON-NLS-1$
109
}
110         try {
111             result = (PathResolver) createClassInstance(className);
112         } catch (Exception JavaDoc e) {
113             log.fatal("failed creating path resolver instance " //$NON-NLS-1$
114
+ className, e);
115             throw new Error JavaDoc("failed creating path resolver instance " //$NON-NLS-1$
116
+ className, e);
117         }
118         try {
119             result.configure(config.getSubset(className + ".")); //$NON-NLS-1$
120
} catch (Exception JavaDoc e) {
121             log.fatal("failed configuring path resolver instance " //$NON-NLS-1$
122
+ result, e);
123             throw new Error JavaDoc("failed configuring path resolver instance " //$NON-NLS-1$
124
+ result, e);
125         }
126         log.debug("path resolver instance created - " + result); //$NON-NLS-1$
127
return result;
128     }
129
130     /**
131      * Creates new instance of plug-in life cycle handler implementation class
132      * using standard discovery algorithm to determine which handler
133      * implementation class should be instantiated.
134      * @return new plug-in life cycle handler instance
135      */

136     protected PluginLifecycleHandler createLifecycleHandler() {
137         String JavaDoc className = getImplClassName(PluginLifecycleHandler.class);
138         PluginLifecycleHandler result;
139         if (className == null) {
140             className =
141                 "org.java.plugin.standard.StandardPluginLifecycleHandler"; //$NON-NLS-1$
142
}
143         try {
144             result = (PluginLifecycleHandler) createClassInstance(className);
145         } catch (Exception JavaDoc e) {
146             log.fatal("failed creating plug-in life cycle handler instance " //$NON-NLS-1$
147
+ className, e);
148             throw new Error JavaDoc(
149                     "failed creating plug-in life cycle handler instance " //$NON-NLS-1$
150
+ className, e);
151         }
152         result.configure(config.getSubset(className + ".")); //$NON-NLS-1$
153
log.debug("life cycle handler instance created - " + result); //$NON-NLS-1$
154
return result;
155     }
156
157     /**
158      * @see org.java.plugin.ObjectFactory#createManager(
159      * org.java.plugin.registry.PluginRegistry,
160      * org.java.plugin.PathResolver)
161      */

162     public PluginManager createManager(final PluginRegistry registry,
163             final PathResolver pathResolver) {
164         return new StandardPluginManager(registry, pathResolver,
165                 createLifecycleHandler());
166     }
167 }
168
Popular Tags