f3b1bf752c08f0cbb8bdf2f3cb6ff2afc811a40f
[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 aSystems Subsystems. 
25          * @param aProvided Provided services of the system. 
26          * @param aRequired Required services by the system. 
27          */
28         public CompositeSystem(String aName, SubSystem[] aSystems,
29                         ServiceDescriptor[] aProvided, ServiceDescriptor[] aRequired) {
30                 super(aName, aProvided, aRequired);
31                 _systems = aSystems;
32                 validate();
33         }
34
35         /**
36          * Validates the subsystems together to check that there are
37          * no required services not in the required list and 
38          * no services in the provided list that cannot be provided. 
39          * Also logs a warning in case of superfluous requirements.  
40          */
41         private void validate() {
42                 List<ServiceDescriptor> provided = new ArrayList<ServiceDescriptor>();
43                 for (SubSystem system : _systems) {
44                         provided.addAll(Arrays.asList(system.getProvidedServices()));
45                 }
46
47                 List<ServiceDescriptor> required = new ArrayList<ServiceDescriptor>();
48                 for (SubSystem system : _systems) {
49                         required.addAll(Arrays.asList(system.getRequiredServices()));
50                 }
51
52                 for (ServiceDescriptor service : getProvidedServices()) {
53                         if (!(provided.contains(service))) {
54                                 throw new SystemAssemblyException(getName() + ": Service '" + service
55                                                 + "' is not provided by any of the subsystems");
56                         }
57                 }
58
59                 for (ServiceDescriptor service : getRequiredServices()) {
60                         if (!(required.contains(service))) {
61                                 info("Service '"
62                                                 + service
63                                                 + "' indicated as required is not actually required by any of the subsystems");
64                         }
65                 }
66
67                 List<ServiceDescriptor> reallyRequired = new ArrayList<ServiceDescriptor>(
68                                 required);
69                 for (ServiceDescriptor service : provided) {
70                         reallyRequired.remove(service); 
71                 }
72                 for (ServiceDescriptor service: getRequiredServices()) { 
73                         reallyRequired.remove(service); 
74                 }
75                 for (ServiceDescriptor service: reallyRequired) { 
76                         throw new SystemAssemblyException(getName() + ": " + "Service '" + service + "' is not provided internally and is not indicated as required for this sub system");
77                 }
78         }
79
80         @Override
81         protected void doInitialize(String aContext, Service[] aRequiredServices) {
82                 List<ServiceDescriptor> descriptors = new ArrayList<ServiceDescriptor>();
83                 for (Service service : aRequiredServices) {
84                         descriptors.add(service.getDescriptor());
85                 }
86                 SystemAssembler assembler = new SystemAssembler(getName(), _systems,
87                                 descriptors.toArray(new ServiceDescriptor[0]));
88                 assembler.start(aRequiredServices);
89         }
90
91         private void info(String aMsg) {
92                 LOG.info(getName() + ": " + aMsg);
93         }
94
95 }