001: package net.javacoding.jspider.core.impl;
002:
003: import net.javacoding.jspider.core.logging.Log;
004: import net.javacoding.jspider.core.logging.LogFactory;
005: import net.javacoding.jspider.core.util.config.PropertySet;
006: import net.javacoding.jspider.spi.Plugin;
007: import net.javacoding.jspider.mod.plugin.console.ConsolePlugin;
008:
009: import java.lang.reflect.Constructor;
010: import java.lang.reflect.InvocationTargetException;
011:
012: /**
013: * $Id: PluginInstantiator.java,v 1.3 2003/04/03 16:24:51 vanrogu Exp $
014: */
015: public class PluginInstantiator {
016:
017: protected Log log;
018:
019: public PluginInstantiator() {
020: log = LogFactory.getLog(PluginInstantiator.class);
021: }
022:
023: public Plugin instantiate(Class pluginClass, String name,
024: PropertySet config) {
025: Plugin plugin = null;
026:
027: try {
028: log
029: .debug("first trying to instantiate via ctr with (name, config) params");
030: plugin = instantiateWithNameAndConfig(pluginClass, name,
031: config);
032:
033: if (plugin == null) {
034: log
035: .debug("plugin not yet instantiated, trying via ctr with (config) param");
036: plugin = instantiateWithConfig(pluginClass, name,
037: config);
038: }
039: if (plugin == null) {
040: log
041: .debug("plugin not yet instantiated, trying via ctr with (name) param");
042: plugin = instantiateWithName(pluginClass, name, config);
043: }
044: if (plugin == null) {
045: log
046: .debug("plugin not yet instantiated, trying via default constructor");
047: plugin = (Plugin) pluginClass.newInstance();
048: }
049: } catch (InstantiationException e) {
050: log
051: .error(
052: "cannot instantiate module - defaulting to console",
053: e);
054: plugin = new ConsolePlugin();
055: } catch (IllegalAccessException e) {
056: log
057: .error(
058: "cannot instantiate module - defaulting to console",
059: e);
060: plugin = new ConsolePlugin();
061: }
062: log.debug("plugin instantiated.");
063: return plugin;
064: }
065:
066: protected Plugin instantiateWithNameAndConfig(Class pluginClass,
067: String name, PropertySet config) {
068: Plugin plugin = null;
069: try {
070: Class[] paramClasses = new Class[2];
071: paramClasses[0] = String.class;
072: paramClasses[1] = PropertySet.class;
073: Object[] params = new Object[2];
074: params[0] = name;
075: params[1] = config;
076: Constructor constructor = pluginClass
077: .getDeclaredConstructor(paramClasses);
078: plugin = (Plugin) constructor.newInstance(params);
079: } catch (NoSuchMethodException e) {
080: log
081: .debug(
082: "cannot instantiate module - constructor with name and PropertySet params not found",
083: e);
084: } catch (InstantiationException e) {
085: log
086: .debug(
087: "cannot instantiate module - InstantiationException",
088: e);
089: } catch (InvocationTargetException e) {
090: log
091: .debug(
092: "cannot instantiate module - InvocationTargetException",
093: e);
094: } catch (IllegalAccessException e) {
095: log
096: .debug(
097: "cannot instantiate module - IllegalAccessException",
098: e);
099: }
100: return plugin;
101: }
102:
103: protected Plugin instantiateWithConfig(Class pluginClass,
104: String name, PropertySet config) {
105: Plugin plugin = null;
106: try {
107: Class[] paramClasses = new Class[1];
108: paramClasses[0] = PropertySet.class;
109: Object[] params = new Object[1];
110: params[0] = config;
111: Constructor constructor = pluginClass
112: .getDeclaredConstructor(paramClasses);
113: plugin = (Plugin) constructor.newInstance(params);
114: } catch (NoSuchMethodException e) {
115: log
116: .debug(
117: "cannot instantiate module - constructor with PropertySet param not found",
118: e);
119: } catch (InstantiationException e) {
120: log
121: .debug(
122: "cannot instantiate module - InstantiationException",
123: e);
124: } catch (InvocationTargetException e) {
125: log
126: .debug(
127: "cannot instantiate module - InvocationTargetException",
128: e);
129: } catch (IllegalAccessException e) {
130: log
131: .debug(
132: "cannot instantiate module - IllegalAccessException",
133: e);
134: }
135: return plugin;
136: }
137:
138: protected Plugin instantiateWithName(Class pluginClass,
139: String name, PropertySet config) {
140: Plugin plugin = null;
141: try {
142: Class[] paramClasses = new Class[1];
143: paramClasses[0] = String.class;
144: Object[] params = new Object[1];
145: params[0] = name;
146: Constructor constructor = pluginClass
147: .getDeclaredConstructor(paramClasses);
148: plugin = (Plugin) constructor.newInstance(params);
149: } catch (NoSuchMethodException e) {
150: log
151: .debug(
152: "cannot instantiate module - constructor with name param not found",
153: e);
154: } catch (InstantiationException e) {
155: log
156: .debug(
157: "cannot instantiate module - InstantiationException",
158: e);
159: } catch (InvocationTargetException e) {
160: log
161: .debug(
162: "cannot instantiate module - InvocationTargetException",
163: e);
164: } catch (IllegalAccessException e) {
165: log
166: .debug(
167: "cannot instantiate module - IllegalAccessException",
168: e);
169: }
170: return plugin;
171: }
172:
173: }
|