001: // The contents of this file are subject to the Mozilla Public License Version
002: // 1.1
003: //(the "License"); you may not use this file except in compliance with the
004: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
005: //
006: //Software distributed under the License is distributed on an "AS IS" basis,
007: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
008: //for the specific language governing rights and
009: //limitations under the License.
010: //
011: //The Original Code is "The Columba Project"
012: //
013: //The Initial Developers of the Original Code are Frederik Dietz and Timo
014: // Stich.
015: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
016: //
017: //All Rights Reserved.
018: package org.columba.core.scripting.service;
019:
020: import java.util.Enumeration;
021: import java.util.logging.Level;
022: import java.util.logging.Logger;
023:
024: import org.columba.api.plugin.IExtensionHandler;
025: import org.columba.api.plugin.IExtensionHandlerKeys;
026: import org.columba.api.plugin.IExtensionInterface;
027: import org.columba.api.plugin.PluginException;
028: import org.columba.api.plugin.PluginHandlerNotFoundException;
029: import org.columba.core.logging.Logging;
030: import org.columba.core.plugin.Extension;
031: import org.columba.core.plugin.PluginManager;
032: import org.columba.core.scripting.service.api.IColumbaService;
033:
034: public class ServiceManager {
035:
036: private static final Logger LOG = Logger
037: .getLogger("org.columba.core.scripting.service.ServiceManager");
038:
039: private static ServiceManager instance = new ServiceManager();
040:
041: private IExtensionHandler handler;
042:
043: private ServiceManager() {
044: try {
045: handler = PluginManager.getInstance().getExtensionHandler(
046: IExtensionHandlerKeys.ORG_COLUMBA_CORE_SERVICE);
047: } catch (PluginHandlerNotFoundException e) {
048: e.printStackTrace();
049: }
050: }
051:
052: public static ServiceManager getInstance() {
053: return instance;
054: }
055:
056: /**
057: * Retrieve service instance. <code>ExtensionHandler</code> automatically
058: * handles singleton extensions. We don't need to cache instances.
059: *
060: * @param extension
061: * extension metadata
062: * @return instance of extension interface
063: */
064: private IColumbaService getServiceInstance(Extension extension) {
065:
066: IExtensionInterface service = null;
067: try {
068: service = (IExtensionInterface) extension
069: .instanciateExtension(new Object[] {});
070: } catch (PluginException e1) {
071: LOG.severe("Failed to load service: " + e1.getMessage());
072:
073: if (Logging.DEBUG)
074: e1.printStackTrace();
075:
076: return null;
077: }
078:
079: if (!(service instanceof IColumbaService)) {
080: LOG
081: .log(
082: Level.WARNING,
083: "Service plugin doesn't explicitly declare an "
084: + "IColumbaService interface. Service ignored...");
085: return null;
086: }
087:
088: return (IColumbaService) service;
089:
090: }
091:
092: /**
093: * Instanciate all services.
094: *
095: */
096: public void initServices() {
097: Enumeration e = handler.getExtensionEnumeration();
098: while (e.hasMoreElements()) {
099: Extension extension = (Extension) e.nextElement();
100:
101: // retrieving the instance for the first time
102: // creates an instance in ExtensionHandler subclass
103: //
104: // instance reference is kept in hashmap automatically
105: IColumbaService service = getServiceInstance(extension);
106: service.initService();
107:
108: }
109:
110: }
111:
112: public void disposeServices() {
113: Enumeration e = handler.getExtensionEnumeration();
114: while (e.hasMoreElements()) {
115: Extension extension = (Extension) e.nextElement();
116: IColumbaService service = getServiceInstance(extension);
117: service.disposeService();
118: }
119: }
120:
121: public void startServices() {
122: Enumeration e = handler.getExtensionEnumeration();
123: while (e.hasMoreElements()) {
124: Extension extension = (Extension) e.nextElement();
125: IColumbaService service = getServiceInstance(extension);
126: service.startService();
127: }
128:
129: }
130:
131: public void stopServices() {
132: Enumeration e = handler.getExtensionEnumeration();
133: while (e.hasMoreElements()) {
134: Extension extension = (Extension) e.nextElement();
135: IColumbaService service = getServiceInstance(extension);
136: service.stopService();
137: }
138: }
139: }
|