now distinguishing between provided and required service and added
[utils] / system / general / src / main / java / org / wamblee / system / CompositeSystem.java
1 package org.wamblee.system;
2
3 import java.util.ArrayList;
4 import java.util.Arrays;
5 import java.util.List;
6
7 import org.apache.commons.logging.Log;
8 import org.apache.commons.logging.LogFactory;
9
10 import sun.util.LocaleServiceProviderPool.LocalizedObjectGetter;
11
12 /**
13  * Composite system consisting of multiple subsystems. 
14  */
15 public class CompositeSystem extends AbstractSubSystem {
16
17         private static final Log LOG = LogFactory.getLog(CompositeSystem.class);
18
19         private SubSystem[] _systems;
20
21         /**
22          * Construcst the composite system. 
23          * @param aName Name of the system. 
24          * @param aRegistry Service registry.
25          * @param aSystems Subsystems. 
26          * @param aProvided Provided services of the system. 
27          * @param aRequired Required services by the system. 
28          */
29         public CompositeSystem(String aName, ServiceRegistry aRegistry, SubSystem[] aSystems,
30                         ServiceDescriptor[] aProvided, ServiceDescriptor[] aRequired) {
31                 super(aName, aRegistry, aProvided, aRequired);
32                 _systems = aSystems;
33                 validate();
34         }
35
36         /**
37          * Validates the subsystems together to check that there are
38          * no required services not in the required list and 
39          * no services in the provided list that cannot be provided. 
40          * Also logs a warning in case of superfluous requirements.  
41          */
42         private void validate() {
43                 List<ServiceDescriptor> provided = new ArrayList<ServiceDescriptor>();
44                 for (SubSystem system : _systems) {
45                         provided.addAll(Arrays.asList(system.getProvidedServices()));
46                 }
47
48                 List<ServiceDescriptor> required = new ArrayList<ServiceDescriptor>();
49                 for (SubSystem system : _systems) {
50                         required.addAll(Arrays.asList(system.getRequiredServices()));
51                 }
52
53                 for (ServiceDescriptor service : getProvidedServices()) {
54                         if (!(provided.contains(service))) {
55                                 throw new SystemAssemblyException(getName() + ": Service '" + service
56                                                 + "' is not provided by any of the subsystems");
57                         }
58                 }
59
60                 for (ServiceDescriptor service : getRequiredServices()) {
61                         if (!(required.contains(service))) {
62                                 info("Service '"
63                                                 + service
64                                                 + "' indicated as required is not actually required by any of the subsystems");
65                         }
66                 }
67
68                 List<ServiceDescriptor> reallyRequired = new ArrayList<ServiceDescriptor>(
69                                 required);
70                 for (ServiceDescriptor service : provided) {
71                         reallyRequired.remove(service); 
72                 }
73                 for (ServiceDescriptor service: getRequiredServices()) { 
74                         reallyRequired.remove(service); 
75                 }
76                 for (ServiceDescriptor service: reallyRequired) { 
77                         throw new SystemAssemblyException(getName() + ": " + "Service '" + service + "' is not provided internally and is not indicated as required for this sub system");
78                 }
79         }
80
81         @Override
82         protected void doStart(String aContext, Service[] aRequiredServices) {
83                 List<ProvidedServiceDescriptor> descriptors = new ArrayList<ProvidedServiceDescriptor>();
84                 for (Service service : aRequiredServices) {
85                         descriptors.add(service.getDescriptor());
86                 }
87                 SystemAssembler assembler = new SystemAssembler(aContext + "." + getName(), _systems,
88                                 descriptors.toArray(new ProvidedServiceDescriptor[0]));
89                 assembler.start(getRegistry(), aRequiredServices);
90         }
91         
92         @Override
93         protected void doStop() {
94                 // Empty.       
95         }
96
97         private void info(String aMsg) {
98                 LOG.info(getName() + ": " + aMsg);
99         }
100
101 }