Better solution for setting the context. The context is now known as soon as componen...
[utils] / system / general / src / main / java / org / wamblee / system / AbstractComponent.java
index 885cad6e1056980a5b2c769b2b9866ce8d7842fc..8b6109f233a951cf72edae3a6eb5765abc320e2a 100644 (file)
@@ -1,15 +1,28 @@
+/*
+ * Copyright 2007 the original author or authors.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */ 
 package org.wamblee.system;
 
 import java.util.ArrayList;
 import java.util.Arrays;
-import java.util.Collection;
-import java.util.HashMap;
+import java.util.HashSet;
 import java.util.List;
-import java.util.Map;
+import java.util.Set;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
-import org.wamblee.system.Component.Status;
 
 /**
  * Abstract subsystem class making it easy to implement new subsystems.
@@ -18,18 +31,16 @@ public abstract class AbstractComponent implements Component {
 
        private static final Log LOG = LogFactory.getLog(AbstractComponent.class);
 
-       private Status _status; 
-       private String _name;
-       private ServiceRegistry _registry
-       private List<ProvidedInterfaceDescriptor> _provided;
-       private List<RequiredInterfaceDescriptor> _required;
-       private Map<ProvidedInterfaceDescriptor, Service> _running;
+       private Status _status;
+       private String _context; 
+       private String _name
+       private List<ProvidedInterface> _provided;
+       private List<RequiredInterface> _required;
+       private Set<ProvidedInterface> _running;
        
        /**
         * Constructs the subsystem.
-        * 
-        * @param aRegistry
-        *            Registry of services.
+        *
         * @param aName
         *            Name of the system.
         * @param aProvided
@@ -37,16 +48,16 @@ public abstract class AbstractComponent implements Component {
         * @param aRequired
         *            Required services.
         */
-       protected AbstractComponent(String aName, ServiceRegistry aRegistry, ProvidedInterfaceDescriptor[] aProvided,
-                       RequiredInterfaceDescriptor[] aRequired) {
+       protected AbstractComponent(String aName, ProvidedInterface[] aProvided,
+                       RequiredInterface[] aRequired) {
                _status = Status.NOT_STARTED;
+               _context = null; 
                _name = aName;
-               _registry = aRegistry; 
-               _provided = new ArrayList<ProvidedInterfaceDescriptor>();
+               _provided = new ArrayList<ProvidedInterface>();
                _provided.addAll(Arrays.asList(aProvided));
-               _required = new ArrayList<RequiredInterfaceDescriptor>();
+               _required = new ArrayList<RequiredInterface>();
                _required.addAll(Arrays.asList(aRequired));
-               _running = new HashMap<ProvidedInterfaceDescriptor, Service>();
+               _running = new HashSet<ProvidedInterface>();
        }
        
        @Override
@@ -59,40 +70,52 @@ public abstract class AbstractComponent implements Component {
                return _name;
        }
        
-       public ServiceRegistry getRegistry() {
-               return _registry;
+       @Override
+       public void addContext(String aContext) {
+               if (_context == null ) { 
+                       _context = aContext; 
+               }
+               else { 
+                       _context = aContext + "." + _context;
+               }
+       }
+       
+       @Override
+       public String getQualifiedName() {
+               if ( _context == null ) { 
+                       return getName(); 
+               }
+               return _context + "." + getName(); 
        }
 
        @Override
-       public final ProvidedInterfaceDescriptor[] getProvidedServices() {
-               return _provided.toArray(new ProvidedInterfaceDescriptor[0]);
+       public final ProvidedInterface[] getProvidedServices() {
+               return _provided.toArray(new ProvidedInterface[0]);
        }
 
        @Override
-       public final RequiredInterfaceDescriptor[] getRequiredServices() {
-               return _required.toArray(new RequiredInterfaceDescriptor[0]);
+       public final RequiredInterface[] getRequiredServices() {
+               return _required.toArray(new RequiredInterface[0]);
        }
 
        @Override
-       public final Service[] start(String aContext,
-                       Service[] aRequiredServices) {
-               LOG.info("Initializing '" + aContext + "." + _name + "' with "
-                               + Arrays.asList(aRequiredServices));
-               doStart(aContext + "." + getName(), aRequiredServices);
+       public final void start() {
+               LOG.info("Initializing '" + getQualifiedName() + "'");
+               doStart();
                _status = Status.RUNNING;
-               return _running.values().toArray(new Service[0]);
+               if ( _running.size() != _provided.size()) { 
+                       List<ProvidedInterface> remaining = 
+                               new ArrayList<ProvidedInterface>(_provided);
+                       remaining.removeAll(_running);
+                       throw new SystemAssemblyException(getQualifiedName() + ": not all services were started, missing " + remaining);
+               }
        }
 
        /**
         * Must be implemented for initializing the subsystem. The implementation
         * must call {@link #addService(Service)} for each service that is started.
-        *  
-        * @param aRequiredServices
-        *            Services that are already running from other subsystems that
-        *            may be used.
         */
-       protected abstract void doStart(String aContext,
-                       Service[] aRequiredServices);
+       protected abstract void doStart();
 
        /**
         * Implementations must call this method to indicate that a new service has
@@ -101,24 +124,21 @@ public abstract class AbstractComponent implements Component {
         * @param aService
         *            Service.
         */
-       protected final void addService(String aContext,
-                       ProvidedInterfaceDescriptor aDescriptor, Object aService) {
-               LOG.info(aContext + ": service '" + aService + "' started.");
-               Service svc = getRegistry().register(aDescriptor, aService);
-               _running.put(svc.getDescriptor(), svc);
+       protected final void addService(
+                       ProvidedInterface aDescriptor, Object aService) {
+               LOG.info("Interface '" + getQualifiedName() + "." + aDescriptor.getName() + "' started.");
+               _running.add(aDescriptor);
+               aDescriptor.publish(aService);
        }
 
        @Override
-       public Service[] getRunningServices() {
-               return _running.values().toArray(new Service[0]);
+       public ProvidedInterface[] getRunningServices() {
+               return _running.toArray(new ProvidedInterface[0]);
        }
        
        @Override
        public void stop() {
                doStop();       
-               for (Service svc: _running.values()) { 
-                       getRegistry().remove(svc);
-               }
                _status = Status.STOPPED;
        }