1 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 40 public class ServiceManager implements Serializable { 41 42 static ServiceManager manager; 43 44 static Logger xlogmon = null; 45 46 private static String name = null; 47 48 public final static String getName() { 49 if (name == null) 50 name = AgentServer.getName() + ".ServiceManager"; 51 return name; 52 } 53 54 60 static void init() throws Exception { 61 xlogmon = Debug.getLogger(Debug.A3Service); 63 64 manager = ServiceManager.load(); 65 if (manager == null) { 66 manager = new ServiceManager(); 67 } 68 } 69 70 80 static ServiceManager load() throws IOException, ClassNotFoundException { 81 return (ServiceManager) AgentServer.getTransaction().load("serviceManager"); 82 } 83 84 87 static void save() throws IOException { 88 AgentServer.getTransaction().save(manager, "serviceManager"); 89 } 90 91 92 Hashtable registry; 93 94 97 private ServiceManager() { 98 registry = new Hashtable(); 99 } 100 101 106 static void start(ServiceDesc desc) throws Exception { 107 xlogmon.log(BasicLevel.DEBUG, 108 getName() +" start service: " + desc); 109 110 if (desc.running) 111 throw new Exception ("Service already running"); 112 Class ptypes[] = new Class [2]; 113 Object args[] = new Object [2]; 114 115 ptypes[0] = Class.forName("java.lang.String"); 116 ptypes[1] = Boolean.TYPE; 117 Class service = Class.forName(desc.getClassName()); 118 Method init = service.getMethod("init", ptypes); 119 args[0] = desc.getArguments(); 120 args[1] = new Boolean (! 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 134 static void start(String scname) throws Exception { 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 144 static void start() throws Exception { 145 for (Enumeration e = manager.registry.elements(); 147 e.hasMoreElements() ;) { 148 ServiceDesc desc = (ServiceDesc) e.nextElement(); 149 try { 150 start(desc); 151 } catch (Exception exc) { 152 xlogmon.log(BasicLevel.ERROR, 153 getName() + ", cannot start service:" + 154 desc.getClassName(), exc); 155 } 156 } 157 } 158 159 164 static void stop(ServiceDesc desc) throws Exception { 165 if (! desc.running) return; 167 Class service = Class.forName(desc.getClassName()); 169 Method stop = service.getMethod("stopService", new Class [0]); 170 stop.invoke(null, new Object [0]); 171 desc.running = false; 172 } 173 174 179 static void stop(String scname) throws Exception { 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 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 exc) { 207 xlogmon.log(BasicLevel.WARN, 208 getName() + ", cannot stop service: " + desc, exc); 209 } 210 } 211 } 212 213 219 static void register(String scname, String 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 238 static void unregister(String 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 return services; 260 } 261 262 267 public String toString() { 268 StringBuffer output = new StringBuffer (); 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 |