KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > dyade > aaa > agent > ServiceManager


1 /*
2  * Copyright (C) 2001 - 2003 ScalAgent Distributed Technologies
3  * Copyright (C) 1996 - 2000 BULL
4  * Copyright (C) 1996 - 2000 INRIA
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA.
20  */

21 package fr.dyade.aaa.agent;
22
23 import java.io.*;
24 import java.util.*;
25 import java.lang.reflect.*;
26
27 import org.objectweb.util.monolog.api.BasicLevel;
28 import org.objectweb.util.monolog.api.Logger;
29
30 import fr.dyade.aaa.util.*;
31 import fr.dyade.aaa.agent.conf.*;
32
33 /**
34  * Object which manages services.
35  * There is only one <code>ServiceManager</code> object per agent server.
36  * The <code>ServiceManager</code> object is initialized in <code>init</code>,
37  * called from <code>AgentServer.init</code>. This classes reuses the
38  * persistency service provided by <code>Transaction</code>.
39  */

40 public class ServiceManager implements Serializable {
41   /** the unique <code>ServiceManager</code> in the agent server */
42   static ServiceManager manager;
43
44   static Logger xlogmon = null;
45
46   private static String JavaDoc name = null;
47
48   public final static String JavaDoc getName() {
49     if (name == null)
50       name = AgentServer.getName() + ".ServiceManager";
51     return name;
52   }
53   
54   /**
55    * Initializes the <code>ServiceManager</code> object. Synchronize the
56    * persistent image and the configuration file.
57    *
58    * @exception Exception unspecialized exception
59    */

60   static void init() throws Exception JavaDoc {
61     // Get the logging monitor from current server MonologMonitorFactory
62
xlogmon = Debug.getLogger(Debug.A3Service);
63
64     manager = ServiceManager.load();
65     if (manager == null) {
66       manager = new ServiceManager();
67     }
68   }
69
70   /**
71    * Builds object from persistent image.
72    *
73    * @return loaded object or null if no persistent image exists
74    *
75    * @exception IOException
76    * when accessing the stored image
77    * @exception ClassNotFoundException
78    * if the corresponding image class may not be found
79    */

80   static ServiceManager load() throws IOException, ClassNotFoundException JavaDoc {
81     return (ServiceManager) AgentServer.getTransaction().load("serviceManager");
82   }
83
84   /**
85    * Saves object in persistent storage.
86    */

87   static void save() throws IOException {
88     AgentServer.getTransaction().save(manager, "serviceManager");
89   }
90
91   /** repository holding <code>Service</code>s */
92   Hashtable registry;
93
94   /**
95    * Default constructor.
96    */

97   private ServiceManager() {
98     registry = new Hashtable();
99   }
100
101   /**
102    * Start a <code>Service</code> defined by its descriptor.
103    *
104    * @param desc service descriptor.
105    */

106   static void start(ServiceDesc desc) throws Exception JavaDoc {
107     xlogmon.log(BasicLevel.DEBUG,
108                 getName() +" start service: " + desc);
109
110     if (desc.running)
111       throw new Exception JavaDoc("Service already running");
112     Class JavaDoc ptypes[] = new Class JavaDoc[2];
113     Object JavaDoc args[] = new Object JavaDoc[2];
114
115     ptypes[0] = Class.forName("java.lang.String");
116     ptypes[1] = Boolean.TYPE;
117     Class JavaDoc service = Class.forName(desc.getClassName());
118     Method init = service.getMethod("init", ptypes);
119     args[0] = desc.getArguments();
120     args[1] = new Boolean JavaDoc(! desc.isInitialized());
121     init.invoke(null, args);
122     desc.running = true;
123     desc.initialized = true;
124
125     xlogmon.log(BasicLevel.DEBUG,
126                 getName() + " service started");
127   }
128
129   /**
130    * Start a <code>Service</code> identified by its name.
131    *
132    * @param scname service class name.
133    */

134   static void start(String JavaDoc scname) throws Exception JavaDoc {
135     ServiceDesc desc = (ServiceDesc) manager.registry.get(scname);
136     if (desc == null)
137       throw new NoSuchElementException("Unknown service: " + scname);
138     start(desc);
139   }
140
141   /**
142    * Starts all defined services.
143    */

144   static void start() throws Exception JavaDoc {
145     // Launch all services defined in A3CML file
146
for (Enumeration e = manager.registry.elements();
147      e.hasMoreElements() ;) {
148       ServiceDesc desc = (ServiceDesc) e.nextElement();
149       try {
150     start(desc);
151       } catch (Exception JavaDoc exc) {
152         xlogmon.log(BasicLevel.ERROR,
153                    getName() + ", cannot start service:" +
154                    desc.getClassName(), exc);
155       }
156     }
157   }
158
159   /**
160    * Stop a <code>Service</code> defined by its descriptor.
161    *
162    * @param desc service descriptor.
163    */

164   static void stop(ServiceDesc desc) throws Exception JavaDoc {
165     // DF: idempotency (could be done in AgentAdmin)
166
if (! desc.running) return;
167 // throw new Exception("Service already stopped");
168
Class JavaDoc service = Class.forName(desc.getClassName());
169     Method stop = service.getMethod("stopService", new Class JavaDoc[0]);
170     stop.invoke(null, new Object JavaDoc[0]);
171     desc.running = false;
172   }
173
174   /**
175    * Stop a <code>Service</code> identified by its name.
176    *
177    * @param scname service class name.
178    */

179   static void stop(String JavaDoc scname) throws Exception JavaDoc {
180     ServiceDesc desc = (ServiceDesc) manager.registry.get(scname);
181     if (desc == null)
182       throw new NoSuchElementException("Unknown service: " + scname);
183     stop(desc);
184   }
185
186   /**
187    * Stops all running services.
188    */

189   static void stop() {
190     if ((manager == null) ||
191         (manager.registry == null)) return;
192
193     for (Enumeration e = manager.registry.elements();
194      e.hasMoreElements() ;) {
195       ServiceDesc desc = (ServiceDesc) e.nextElement();
196       try {
197         if (xlogmon.isLoggable(BasicLevel.DEBUG))
198           xlogmon.log(BasicLevel.DEBUG,
199                       getName() + ", stops: " + desc);
200
201     if (desc.running) stop(desc);
202
203         if (xlogmon.isLoggable(BasicLevel.DEBUG))
204           xlogmon.log(BasicLevel.DEBUG,
205                       getName() + ", service stopped");
206       } catch (Throwable JavaDoc exc) {
207         xlogmon.log(BasicLevel.WARN,
208                    getName() + ", cannot stop service: " + desc, exc);
209       }
210     }
211   }
212
213   /**
214    * Registers a new <code>Service</code> object.
215    *
216    * @param scname service class name.
217    * @param args launching arguments.
218    */

219   static void register(String JavaDoc scname, String JavaDoc args) {
220     synchronized (manager) {
221       ServiceDesc desc = (ServiceDesc) manager.registry.get(scname);
222       xlogmon.log(BasicLevel.DEBUG,
223                   getName() + ", register " + scname + " -> " + desc);
224       if (desc == null) {
225         desc = new ServiceDesc(scname, args);
226         manager.registry.put(scname, desc);
227       } else {
228         desc.args = args;
229       }
230     }
231   }
232
233   /**
234    * Unregisters useless <code>Service</code>.
235    *
236    * @param scname service class name.
237    */

238   static void unregister(String JavaDoc scname) {
239     synchronized (manager) {
240       manager.registry.remove(scname);
241     }
242   }
243
244   static ServiceDesc[] getServices() {
245     ServiceDesc[] services = new ServiceDesc[manager.registry.size()];
246     int i = 0;
247     for (Enumeration e = manager.registry.elements(); e.hasMoreElements();) {
248       services[i++] = (ServiceDesc) e.nextElement();
249     }
250 // 1.2 Collection values = manager.registry.values();
251
// 1.2 ServiceDesc[] services = new ServiceDesc[values.size()];
252
// 1.2 try {
253
// 1.2 services = (ServiceDesc[]) values.toArray(services);
254
// 1.2 } catch (ArrayStoreException exc) {
255
// 1.2 xlogmon.log(BasicLevel.ERROR,
256
// 1.2 "AgentServer#" + AgentServer.getServerId() +
257
// 1.2 ".ServiceManager, can't get services.", exc);
258
// 1.2 }
259
return services;
260   }
261
262   /**
263    * Provides a string image for this object.
264    *
265    * @return a string image for this object
266    */

267   public String JavaDoc toString() {
268     StringBuffer JavaDoc output = new StringBuffer JavaDoc();
269     output.append('(');
270     output.append(super.toString());
271     output.append(",registry=").append(Strings.toString(registry));
272     output.append(')');
273     return output.toString();
274   }
275 }
276
Popular Tags