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