(no commit message)
[utils] / system / general / src / main / java / org / wamblee / system / Container.java
1 /*
2  * Copyright 2007 the original author or authors.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */ 
16 package org.wamblee.system;
17
18 import java.util.ArrayList;
19 import java.util.Arrays;
20 import java.util.List;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24
25 import sun.util.LocaleServiceProviderPool.LocalizedObjectGetter;
26
27 /**
28  * Composite system consisting of multiple subsystems. 
29  *
30  * @author Erik Brakkee
31  */
32 public class Container extends AbstractComponent {
33
34         private static final Log LOG = LogFactory.getLog(Container.class);
35
36         private Component[] _systems;
37
38         /**
39          * Construcst the composite system. 
40          * @param aName Name of the system. 
41          * @param aRegistry Service registry.
42          * @param aSystems Subsystems. 
43          * @param aProvided Provided services of the system. 
44          * @param aRequired Required services by the system. 
45          */
46         public Container(String aName, Component[] aSystems,
47                         ProvidedInterface[] aProvided, RequiredInterface[] aRequired) {
48                 super(aName, aProvided, aRequired);
49                 _systems = aSystems;
50                 validate(aRequired);
51         }
52
53         /**
54          * Validates the subsystems together to check that there are
55          * no required services not in the required list and 
56          * no services in the provided list that cannot be provided. 
57          * Also logs a warning in case of superfluous requirements.  
58          */
59         private void validate(RequiredInterface[] aRequired) {
60                 List<ProvidedInterface> provided = new ArrayList<ProvidedInterface>();
61                 for (Component system : _systems) {
62                         provided.addAll(Arrays.asList(system.getProvidedServices()));
63                 }
64
65                 List<RequiredInterface> required = new ArrayList<RequiredInterface>();
66                 for (Component system : _systems) {
67                         required.addAll(Arrays.asList(system.getRequiredServices()));
68                 }
69
70                 for (ProvidedInterface service : getProvidedServices()) {
71                         if (!(provided.contains(service))) {
72                                 throw new SystemAssemblyException(getName() + ": Service '" + service
73                                                 + "' is not provided by any of the subsystems");
74                         }
75                 }
76
77                 for (RequiredInterface service : getRequiredServices()) {
78                         if (!(required.contains(service))) {
79                                 info("Service '"
80                                                 + service
81                                                 + "' indicated as required is not actually required by any of the subsystems");
82                         }
83                 }
84
85                 List<RequiredInterface> reallyRequired = new ArrayList<RequiredInterface>(
86                                 required);
87                 // Compute all required interfaces that are not provided
88                 for (ProvidedInterface service : provided) {
89                         List<RequiredInterface> fulfilled = 
90                                 Arrays.asList(SystemAssembler.filterRequiredServices(service, 
91                                         reallyRequired));
92                         reallyRequired.removeAll(fulfilled); 
93                 }
94                 // Now the remaining interfaces should be covered by the required
95                 // list. 
96                 reallyRequired.removeAll(Arrays.asList(aRequired));
97                 
98                 String missingRequired = "";
99                 for (RequiredInterface service: reallyRequired) {
100                         missingRequired += service + "\n";
101                 }
102                 if ( missingRequired.length() > 0 ) { 
103                         throw new SystemAssemblyException(getName() + ": missing required services\n" + missingRequired);
104                 }
105         }
106
107         @Override
108         protected void doStart(String aContext) {
109                 List<ProvidedInterface> provided = new ArrayList<ProvidedInterface>();
110                 
111                 // all interfaces from the required list of this container are
112                 // provided to the components inside it.
113                 RequiredInterface[] required = getRequiredServices();
114                 for (RequiredInterface intf: required) { 
115                     ProvidedInterface provider = intf.getProvider(); 
116                     if ( provider == null ) { 
117                         throw new SystemAssemblyException(aContext + ": required interface '" + intf +"' is not provided");
118                     }
119                         provided.add(intf.getProvider());
120                 }
121                 
122                 SystemAssembler assembler = new SystemAssembler( _systems, 
123                                 provided.toArray(new ProvidedInterface[0]));
124                 assembler.start();
125         }
126         
127         @Override
128         protected void doStop() {
129                 for (int i = _systems.length-1; i >= 0; i--) { 
130                         _systems[i].stop();
131                 }
132         }
133
134         private void info(String aMsg) {
135                 LOG.info(getName() + ": " + aMsg);
136         }
137
138 }