001: package org.columba.core.component;
002:
003: import java.util.Enumeration;
004: import java.util.logging.Logger;
005:
006: import org.apache.commons.cli.CommandLine;
007: import org.columba.api.plugin.IExtension;
008: import org.columba.api.plugin.IExtensionHandler;
009: import org.columba.api.plugin.IExtensionHandlerKeys;
010: import org.columba.api.plugin.IPluginManager;
011: import org.columba.api.plugin.PluginException;
012: import org.columba.api.plugin.PluginHandlerNotFoundException;
013: import org.columba.core.component.api.IComponentPlugin;
014: import org.columba.core.logging.Logging;
015: import org.columba.core.plugin.PluginManager;
016:
017: public class ComponentManager implements IComponentPlugin {
018:
019: private static final Logger LOG = Logger
020: .getLogger("org.columba.core.main"); //$NON-NLS-1$
021:
022: private static final ComponentManager instance = new ComponentManager();
023:
024: private IExtensionHandler extensionHandler;
025:
026: private ComponentManager() {
027: // nothing to do
028: }
029:
030: public static ComponentManager getInstance() {
031: return instance;
032: }
033:
034: private IExtensionHandler getExtensionHandler() {
035: if (extensionHandler == null) {
036: try {
037: // retrieve plugin manager instance
038: final IPluginManager pm = PluginManager.getInstance();
039:
040: extensionHandler = pm
041: .getExtensionHandler(IExtensionHandlerKeys.ORG_COLUMBA_CORE_COMPONENT);
042: } catch (final PluginHandlerNotFoundException e) {
043: LOG.severe(e.getMessage());
044:
045: if (Logging.DEBUG) {
046: e.printStackTrace();
047: }
048: }
049: }
050: return extensionHandler;
051: }
052:
053: public IComponentPlugin getPlugin(final String id) {
054: IComponentPlugin component = null;
055:
056: final IExtension extension = getExtensionHandler()
057: .getExtension(id);
058:
059: try {
060: component = (IComponentPlugin) extension
061: .instanciateExtension(null);
062: } catch (final PluginException e) {
063: LOG.severe(e.getMessage());
064:
065: if (Logging.DEBUG) {
066: e.printStackTrace();
067: }
068: }
069:
070: return component;
071: }
072:
073: /**
074: * @see org.columba.core.component.api.IComponentPlugin#init()
075: */
076: public void init() {
077: final Enumeration<IExtension> extensionEnumeration = getExtensionHandler()
078: .getExtensionEnumeration();
079:
080: while (extensionEnumeration.hasMoreElements()) {
081: final IExtension ext = extensionEnumeration.nextElement();
082: IComponentPlugin p;
083:
084: try {
085: p = (IComponentPlugin) ext.instanciateExtension(null);
086: p.init();
087: } catch (final PluginException e) {
088: LOG.severe(e.getMessage());
089:
090: if (Logging.DEBUG) {
091: e.printStackTrace();
092: }
093:
094: }
095:
096: }
097: }
098:
099: /**
100: * @see org.columba.core.component.api.IComponentPlugin#postStartup()
101: */
102: public void postStartup() {
103: final Enumeration<IExtension> extensionEnumeration = getExtensionHandler()
104: .getExtensionEnumeration();
105:
106: while (extensionEnumeration.hasMoreElements()) {
107: final IExtension ext = extensionEnumeration.nextElement();
108: IComponentPlugin p;
109: try {
110: p = (IComponentPlugin) ext.instanciateExtension(null);
111: p.postStartup();
112: } catch (final PluginException e) {
113: LOG.severe(e.getMessage());
114:
115: if (Logging.DEBUG) {
116: e.printStackTrace();
117: }
118: }
119: }
120: }
121:
122: /**
123: * @see org.columba.core.component.api.IComponentPlugin#registerCommandLineArguments()
124: */
125: public void registerCommandLineArguments() {
126:
127: final Enumeration<IExtension> extensionEnumeration = getExtensionHandler()
128: .getExtensionEnumeration();
129:
130: while (extensionEnumeration.hasMoreElements()) {
131: final IExtension ext = extensionEnumeration.nextElement();
132: IComponentPlugin p;
133: try {
134: p = (IComponentPlugin) ext.instanciateExtension(null);
135: p.registerCommandLineArguments();
136: } catch (final PluginException e) {
137: LOG.severe(e.getMessage());
138:
139: if (Logging.DEBUG) {
140: e.printStackTrace();
141: }
142: }
143:
144: }
145: }
146:
147: /**
148: * @see org.columba.core.component.api.IComponentPlugin#handleCommandLineParameters(org.apache.commons.cli.CommandLine)
149: */
150: public void handleCommandLineParameters(
151: final CommandLine commandLine) {
152: final Enumeration<IExtension> extensionEnumeration = getExtensionHandler()
153: .getExtensionEnumeration();
154:
155: while (extensionEnumeration.hasMoreElements()) {
156: final IExtension ext = extensionEnumeration.nextElement();
157: IComponentPlugin p;
158: try {
159: p = (IComponentPlugin) ext.instanciateExtension(null);
160: p.handleCommandLineParameters(commandLine);
161: } catch (final PluginException e) {
162: LOG.severe(e.getMessage());
163:
164: if (Logging.DEBUG) {
165: e.printStackTrace();
166: }
167: }
168:
169: }
170: }
171:
172: }
|