001: /*****************************************************************************
002: * Java Plug-in Framework (JPF)
003: * Copyright (C) 2004-2007 Dmitry Olshansky
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: *****************************************************************************/package org.java.plugin.standard;
019:
020: import org.apache.commons.logging.Log;
021: import org.apache.commons.logging.LogFactory;
022: import org.java.plugin.ObjectFactory;
023: import org.java.plugin.PathResolver;
024: import org.java.plugin.PluginManager;
025: import org.java.plugin.registry.PluginRegistry;
026: import org.java.plugin.util.ExtendedProperties;
027:
028: /**
029: * Standard object factory implementation.
030: * @version $Id$
031: */
032: public class StandardObjectFactory extends ObjectFactory {
033: static final String PACKAGE_NAME = "org.java.plugin.standard"; //$NON-NLS-1$
034:
035: protected Log log = LogFactory.getLog(getClass());
036: protected ExtendedProperties config;
037:
038: /**
039: * @see org.java.plugin.ObjectFactory#configure(ExtendedProperties)
040: */
041: @Override
042: protected void configure(final ExtendedProperties configuration) {
043: config = (configuration != null) ? configuration
044: : new ExtendedProperties();
045: }
046:
047: protected String getImplClassName(final Class<?> cls) {
048: String result = config.getProperty(cls.getName(), null);
049: if (log.isDebugEnabled()) {
050: log.debug("implementation class for " + cls.getName() //$NON-NLS-1$
051: + " is " + result); //$NON-NLS-1$
052: }
053: return result;
054: }
055:
056: protected Object createClassInstance(final String className)
057: throws InstantiationException, IllegalAccessException,
058: ClassNotFoundException {
059: ClassLoader cl = Thread.currentThread().getContextClassLoader();
060: if (cl != null) {
061: try {
062: return cl.loadClass(className).newInstance();
063: } catch (ClassNotFoundException cnfe) {
064: // ignore
065: }
066: }
067: cl = getClass().getClassLoader();
068: if (cl != null) {
069: try {
070: return cl.loadClass(className).newInstance();
071: } catch (ClassNotFoundException cnfe) {
072: // ignore
073: }
074: }
075: return ClassLoader.getSystemClassLoader().loadClass(className)
076: .newInstance();
077: }
078:
079: /**
080: * @see org.java.plugin.ObjectFactory#createRegistry()
081: */
082: @Override
083: public PluginRegistry createRegistry() {
084: String className = getImplClassName(PluginRegistry.class);
085: PluginRegistry result;
086: if (className == null) {
087: className = "org.java.plugin.registry.xml.PluginRegistryImpl"; //$NON-NLS-1$
088: }
089: try {
090: result = (PluginRegistry) createClassInstance(className);
091: } catch (Exception e) {
092: log.fatal("failed creating registry instance " //$NON-NLS-1$
093: + className, e);
094: throw new Error("failed creating registry instance " //$NON-NLS-1$
095: + className, e);
096: }
097: result.configure(config.getSubset(className + ".")); //$NON-NLS-1$
098: log.debug("registry instance created - " + result); //$NON-NLS-1$
099: return result;
100: }
101:
102: /**
103: * @see org.java.plugin.ObjectFactory#createPathResolver()
104: */
105: @Override
106: public PathResolver createPathResolver() {
107: String className = getImplClassName(PathResolver.class);
108: PathResolver result;
109: if (className == null) {
110: className = "org.java.plugin.standard.StandardPathResolver"; //$NON-NLS-1$
111: }
112: try {
113: result = (PathResolver) createClassInstance(className);
114: } catch (Exception e) {
115: log.fatal("failed creating path resolver instance " //$NON-NLS-1$
116: + className, e);
117: throw new Error("failed creating path resolver instance " //$NON-NLS-1$
118: + className, e);
119: }
120: try {
121: result.configure(config.getSubset(className + ".")); //$NON-NLS-1$
122: } catch (Exception e) {
123: log.fatal("failed configuring path resolver instance " //$NON-NLS-1$
124: + result, e);
125: throw new Error(
126: "failed configuring path resolver instance " //$NON-NLS-1$
127: + result, e);
128: }
129: log.debug("path resolver instance created - " + result); //$NON-NLS-1$
130: return result;
131: }
132:
133: /**
134: * Creates new instance of plug-in life cycle handler implementation class
135: * using standard discovery algorithm to determine which handler
136: * implementation class should be instantiated.
137: * @return new plug-in life cycle handler instance
138: */
139: protected PluginLifecycleHandler createLifecycleHandler() {
140: String className = getImplClassName(PluginLifecycleHandler.class);
141: PluginLifecycleHandler result;
142: if (className == null) {
143: className = "org.java.plugin.standard.StandardPluginLifecycleHandler"; //$NON-NLS-1$
144: }
145: try {
146: result = (PluginLifecycleHandler) createClassInstance(className);
147: } catch (Exception e) {
148: log.fatal(
149: "failed creating plug-in life cycle handler instance " //$NON-NLS-1$
150: + className, e);
151: throw new Error(
152: "failed creating plug-in life cycle handler instance " //$NON-NLS-1$
153: + className, e);
154: }
155: result.configure(config.getSubset(className + ".")); //$NON-NLS-1$
156: log.debug("life cycle handler instance created - " + result); //$NON-NLS-1$
157: return result;
158: }
159:
160: /**
161: * @see org.java.plugin.ObjectFactory#createManager(
162: * org.java.plugin.registry.PluginRegistry,
163: * org.java.plugin.PathResolver)
164: */
165: @Override
166: public PluginManager createManager(final PluginRegistry registry,
167: final PathResolver pathResolver) {
168: return new StandardPluginManager(registry, pathResolver,
169: createLifecycleHandler());
170: }
171: }
|