(no commit message)
[utils] / system / general / src / main / java / org / wamblee / system / AbstractSubSystem.java
index 06fd5bb82c823aa3481e278753a6338504919c66..7a6724f32ea8af702df9d9f45527f1dc53b09c98 100644 (file)
@@ -10,43 +10,47 @@ import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
 /**
- * Abstract subsystem class making it easy to implement new 
- * subsystems.
+ * Abstract subsystem class making it easy to implement new subsystems.
  */
 public abstract class AbstractSubSystem implements SubSystem {
-       
+
        private static final Log LOG = LogFactory.getLog(AbstractSubSystem.class);
 
-       private String _name; 
+       private String _name;
        private List<ServiceDescriptor> _provided;
        private List<ServiceDescriptor> _required;
-       private Map<ServiceDescriptor, Service> _running; 
+       private Map<ServiceDescriptor, Service> _running;
        
        /**
-        * Constructs the subsystem. 
-        * @param aRegistry Registry of services. 
-        * @param aName Name of the system. 
-        * @param aProvided Provided services. 
-        * @param aRequired Required services. 
+        * Constructs the subsystem.
+        * 
+        * @param aRegistry
+        *            Registry of services.
+        * @param aName
+        *            Name of the system.
+        * @param aProvided
+        *            Provided services.
+        * @param aRequired
+        *            Required services.
         */
-       protected AbstractSubSystem(String aName, ServiceDescriptor[] aProvided, 
-                       ServiceDescriptor[] aRequired) { 
-               _name = aName; 
-               _provided = new ArrayList<ServiceDescriptor>(); 
+       protected AbstractSubSystem(String aName, ServiceDescriptor[] aProvided,
+                       ServiceDescriptor[] aRequired) {
+               _name = aName;
+               _provided = new ArrayList<ServiceDescriptor>();
                _provided.addAll(Arrays.asList(aProvided));
-               _required = new ArrayList<ServiceDescriptor>(); 
+               _required = new ArrayList<ServiceDescriptor>();
                _required.addAll(Arrays.asList(aRequired));
-               _running = new HashMap<ServiceDescriptor, Service>(); 
+               _running = new HashMap<ServiceDescriptor, Service>();
        }
 
        @Override
        public final String getName() {
-               return _name; 
+               return _name;
        }
 
        @Override
        public final ServiceDescriptor[] getProvidedServices() {
-               return _provided.toArray(new ServiceDescriptor[0]); 
+               return _provided.toArray(new ServiceDescriptor[0]);
        }
 
        @Override
@@ -55,30 +59,40 @@ public abstract class AbstractSubSystem implements SubSystem {
        }
 
        @Override
-       public final Service[] start(String aContext, ServiceRegistry aRegistry, Service[] aRequiredServices) {
-               LOG.info("Initializing '" + aContext + "." + _name + "' with " + Arrays.asList(aRequiredServices));
-               doStart(aContext + "." + getName(), aRegistry, aRequiredServices); 
+       public final Service[] start(String aContext, ServiceRegistry aRegistry,
+                       Service[] aRequiredServices) {
+               LOG.info("Initializing '" + aContext + "." + _name + "' with "
+                               + Arrays.asList(aRequiredServices));
+               doStart(aContext + "." + getName(), aRegistry, aRequiredServices);
                return _running.values().toArray(new Service[0]);
        }
-       
+
        /**
-        * Must be implemented for initializing the subsystem. 
-        * The implementation must call {@link #addService(Service)}
-        * for each service that is started. 
-        * @param aRegistry Registry to which the subsystem must register its services. 
-        * @param aRequiredServices Services that are already running
-        * from other subsystems that may be used. 
+        * Must be implemented for initializing the subsystem. The implementation
+        * must call {@link #addService(Service)} for each service that is started.
+        * 
+        * @param aRegistry Service registry. 
+        * @param aRequiredServices
+        *            Services that are already running from other subsystems that
+        *            may be used.
         */
-       protected abstract void doStart(String aContext, ServiceRegistry aRegistry, Service[] aRequiredServices); 
-       
+       protected abstract void doStart(String aContext,
+                       ServiceRegistry aRegistry,
+                       Service[] aRequiredServices);
+
        /**
-        * Implementations must call this method to indicate that
-        * a new service has been started. 
-        * @param aService Service. 
+        * Implementations must call this method to indicate that a new service has
+        * been started.
+        * 
+        * @param aService
+        *            Service.
         */
-       protected final void addService(String aContext, Service aService) { 
+       protected final void addService(String aContext,
+                       ServiceRegistry aRegistry, 
+                       ServiceDescriptor aDescriptor, Object aService) {
                LOG.info(aContext + ": service '" + aService + "' started.");
-               _running.put(aService.getDescriptor(), aService);
+               Service svc = aRegistry.register(aDescriptor, aService);
+               _running.put(svc.getDescriptor(), svc);
        }
 
        @Override
@@ -86,9 +100,19 @@ public abstract class AbstractSubSystem implements SubSystem {
                return _running.values().toArray(new Service[0]);
        }
        
+       @Override
+       public void stop(String aContext, ServiceRegistry aRegistry) {
+               doStop(aContext);       
+               for (Service svc: _running.values()) { 
+                       aRegistry.remove(svc);
+               }
+       }
+       
+       protected abstract void doStop(String aContext); 
+
        @Override
        public String toString() {
-               return _name; 
+               return _name;
        }
 
 }