KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > core > scripting > service > ServiceManager


1 // The contents of this file are subject to the Mozilla Public License Version
2
// 1.1
3
//(the "License"); you may not use this file except in compliance with the
4
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
5
//
6
//Software distributed under the License is distributed on an "AS IS" basis,
7
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
8
//for the specific language governing rights and
9
//limitations under the License.
10
//
11
//The Original Code is "The Columba Project"
12
//
13
//The Initial Developers of the Original Code are Frederik Dietz and Timo
14
// Stich.
15
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
16
//
17
//All Rights Reserved.
18
package org.columba.core.scripting.service;
19
20 import java.util.Enumeration JavaDoc;
21 import java.util.logging.Level JavaDoc;
22 import java.util.logging.Logger JavaDoc;
23
24 import org.columba.api.plugin.IExtensionHandler;
25 import org.columba.api.plugin.IExtensionHandlerKeys;
26 import org.columba.api.plugin.IExtensionInterface;
27 import org.columba.api.plugin.PluginException;
28 import org.columba.api.plugin.PluginHandlerNotFoundException;
29 import org.columba.core.logging.Logging;
30 import org.columba.core.plugin.Extension;
31 import org.columba.core.plugin.PluginManager;
32 import org.columba.core.scripting.service.api.IColumbaService;
33
34 public class ServiceManager {
35
36     private static final Logger JavaDoc LOG = Logger
37             .getLogger("org.columba.core.scripting.service.ServiceManager");
38
39     private static ServiceManager instance = new ServiceManager();
40
41     private IExtensionHandler handler;
42
43     private ServiceManager() {
44         try {
45             handler = PluginManager.getInstance().getExtensionHandler(
46                     IExtensionHandlerKeys.ORG_COLUMBA_CORE_SERVICE);
47         } catch (PluginHandlerNotFoundException e) {
48             e.printStackTrace();
49         }
50     }
51
52     public static ServiceManager getInstance() {
53         return instance;
54     }
55
56     /**
57      * Retrieve service instance. <code>ExtensionHandler</code> automatically
58      * handles singleton extensions. We don't need to cache instances.
59      *
60      * @param extension
61      * extension metadata
62      * @return instance of extension interface
63      */

64     private IColumbaService getServiceInstance(Extension extension) {
65
66         IExtensionInterface service = null;
67         try {
68             service = (IExtensionInterface) extension
69                     .instanciateExtension(new Object JavaDoc[] {});
70         } catch (PluginException e1) {
71             LOG.severe("Failed to load service: " + e1.getMessage());
72
73             if (Logging.DEBUG)
74                 e1.printStackTrace();
75
76             return null;
77         }
78
79         if (!(service instanceof IColumbaService)) {
80             LOG.log(Level.WARNING,
81                     "Service plugin doesn't explicitly declare an "
82                             + "IColumbaService interface. Service ignored...");
83             return null;
84         }
85
86         return (IColumbaService) service;
87
88     }
89
90     /**
91      * Instanciate all services.
92      *
93      */

94     public void initServices() {
95         Enumeration JavaDoc e = handler.getExtensionEnumeration();
96         while (e.hasMoreElements()) {
97             Extension extension = (Extension) e.nextElement();
98
99             // retrieving the instance for the first time
100
// creates an instance in ExtensionHandler subclass
101
//
102
// instance reference is kept in hashmap automatically
103
IColumbaService service = getServiceInstance(extension);
104             service.initService();
105
106         }
107
108     }
109
110     public void disposeServices() {
111         Enumeration JavaDoc e = handler.getExtensionEnumeration();
112         while (e.hasMoreElements()) {
113             Extension extension = (Extension) e.nextElement();
114             IColumbaService service = getServiceInstance(extension);
115             service.disposeService();
116         }
117     }
118
119     public void startServices() {
120         Enumeration JavaDoc e = handler.getExtensionEnumeration();
121         while (e.hasMoreElements()) {
122             Extension extension = (Extension) e.nextElement();
123             IColumbaService service = getServiceInstance(extension);
124             service.startService();
125         }
126
127     }
128
129     public void stopServices() {
130         Enumeration JavaDoc e = handler.getExtensionEnumeration();
131         while (e.hasMoreElements()) {
132             Extension extension = (Extension) e.nextElement();
133             IColumbaService service = getServiceInstance(extension);
134             service.stopService();
135         }
136     }
137 }
138
Popular Tags