2 * Copyright 2007 the original author or authors.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.wamblee.system.container;
18 import java.util.ArrayList;
19 import java.util.HashSet;
20 import java.util.List;
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.wamblee.general.Pair;
26 import org.wamblee.system.core.AbstractComponent;
27 import org.wamblee.system.core.Component;
28 import org.wamblee.system.core.DefaultScope;
29 import org.wamblee.system.core.ProvidedInterface;
30 import org.wamblee.system.core.RequiredInterface;
31 import org.wamblee.system.core.Scope;
32 import org.wamblee.system.core.SystemAssemblyException;
33 import org.wamblee.system.graph.CompositeEdgeFilter;
34 import org.wamblee.system.graph.EdgeFilter;
35 import org.wamblee.system.graph.component.ComponentGraph;
36 import org.wamblee.system.graph.component.ConnectRequiredProvidedEdgeFilter;
37 import org.wamblee.system.graph.component.RequiredProvidedEdgeFactory;
40 * Container consisting of multiple components.
42 * @author Erik Brakkee
44 public class Container extends AbstractComponent<Scope> {
46 private static final Log LOG = LogFactory.getLog(Container.class);
48 private List<Component> _components;
49 private Set<String> _componentNames;
50 private CompositeEdgeFilter _edgeFilter;
51 private boolean _sealed;
54 * Constructs the container
57 * Name of the container
61 * Provided services of the container
63 * Required services by the container.
65 public Container(String aName, Component[] aComponents,
66 ProvidedInterface[] aProvided, RequiredInterface[] aRequired) {
67 super(aName, aProvided, aRequired);
68 _components = new ArrayList<Component>();
70 _componentNames = new HashSet<String>();
71 _edgeFilter = new CompositeEdgeFilter();
73 for (Component component : aComponents) {
74 addComponent(component);
78 public Container(String aName) {
79 this(aName, new Component[0], new ProvidedInterface[0],
80 new RequiredInterface[0]);
83 public Container addComponent(Component aComponent) {
85 if (aComponent.getContext() != null) {
86 throw new SystemAssemblyException(
87 "Inconsistent hierarchy, component '"
88 + aComponent.getName()
89 + "' is already part of another hierarchy");
91 if (_componentNames.contains(aComponent.getName())) {
92 throw new SystemAssemblyException("Duplicate component '"
93 + aComponent.getName() + "'");
95 _components.add(aComponent);
96 _componentNames.add(aComponent.getName());
97 aComponent.addContext(getQualifiedName());
102 * Explictly connects required and provided interfaces.
103 * @param aClientComponent Client component, may not be null.
104 * @param aRequiredInterface Required interface. If null it means all required interfaces.
105 * @param aServerComponent Server component to connect to. If null, it means that no server components
106 * may be connected to and the provider of the required interface will be null.
107 * @param aProvidedInterface Provided interface. If null, it means that there is no restriction on the
108 * name of the provided interface and that it is automatically selected.
110 public void connectRequiredProvided(String aClientComponent, String aRequiredInterface,
111 String aServerComponent, String aProvidedInterface) {
113 _edgeFilter.add(new ConnectRequiredProvidedEdgeFilter(aClientComponent, aRequiredInterface, aServerComponent, aProvidedInterface));
116 public void connectExternalRequired(String aComponent, String aRequiredInterface,
117 String aExternalRequiredInterface) {
120 throw new RuntimeException("not implemented");
123 public void connectExternalProvided(String aExternalProvided, String aComponent, String aProvidedInterface) {
126 throw new RuntimeException("not implemented");
131 public Container addProvidedInterface(ProvidedInterface aProvided) {
133 super.addProvidedInterface(aProvided);
138 public Container addRequiredInterface(RequiredInterface aRequired) {
140 super.addRequiredInterface(aRequired);
145 public void addContext(String aContext) {
146 super.addContext(aContext);
147 for (Component component : _components) {
148 component.addContext(aContext);
153 * Validates the components together to check that there are no required
154 * services not in the required list and no services in the provided list
155 * that cannot be provided. Also logs a warning in case of superfluous
158 * @throws SystemAssemblyException
159 * in case of any validation problems.
161 public void validate() {
162 doStartOptionalDryRun(null, true);
166 * Seal the container, meaning that no further components or interfaces may
174 * Checks if the container is sealed.
176 * @return True iff the container is sealed.
178 public boolean isSealed() {
183 * Utility method to start with an empty external scope. This is useful for
184 * top-level containers which are not part of another container.
188 public Scope start() {
189 Scope scope = new DefaultScope(getProvidedInterfaces());
190 return super.start(scope);
194 protected Scope doStart(Scope aExternalScope) {
197 Scope scope = new DefaultScope(getProvidedInterfaces(), aExternalScope);
198 ComponentGraph graph = doStartOptionalDryRun(scope, false);
199 exposeProvidedInterfaces(graph, aExternalScope, scope);
204 private void exposeProvidedInterfaces(ComponentGraph aGraph, Scope aExternalScope,
205 Scope aInternalScope) {
206 for (Pair<ProvidedInterface,ProvidedInterface> mapping:
207 aGraph.findExternalProvidedInterfaceMapping()) {
208 Object svc = aInternalScope.getInterfaceImplementation(mapping.getSecond(), Object.class);
209 addInterface(mapping.getFirst(), svc, aExternalScope);
213 private ComponentGraph doStartOptionalDryRun(Scope aScope, boolean aDryRun) {
214 ComponentGraph graph = createComponentGraph();
220 LOG.info("Starting '" + getQualifiedName() + "'");
222 List<Component> started = new ArrayList<Component>();
223 for (Component component : _components) {
225 // Start the service.
227 Object runtime = component.start(aScope);
228 aScope.addRuntime(component, runtime);
229 started.add(component);
231 } catch (SystemAssemblyException e) {
233 } catch (RuntimeException e) {
234 LOG.error(getQualifiedName() + ": could not start '"
235 + component.getQualifiedName() + "'", e);
236 stopAlreadyStartedComponents(started, aScope);
243 private ComponentGraph createComponentGraph() {
244 ComponentGraph graph = new ComponentGraph();
245 for (RequiredInterface req : getRequiredInterfaces()) {
246 graph.addRequiredInterface(this, req);
248 for (Component comp : _components) {
249 graph.addComponent(comp);
251 for (ProvidedInterface prov: getProvidedInterfaces()) {
252 graph.addProvidedInterface(this, prov);
255 graph.addEdgeFilter(_edgeFilter);
259 private void stopAlreadyStartedComponents(List<Component> aStarted,
261 // an exception occurred, stop the successfully started
263 for (int i = aStarted.size() - 1; i >= 0; i--) {
265 Component component = aStarted.get(i);
266 aStarted.get(i).stop(aScope.getRuntime(component));
267 } catch (Throwable t) {
268 LOG.error(getQualifiedName() + ": error stopping "
269 + aStarted.get(i).getQualifiedName());
275 protected void doStop(Scope aScope) {
276 for (int i = _components.size() - 1; i >= 0; i--) {
277 Component component = _components.get(i);
278 Object runtime = aScope.getRuntime(component);
279 component.stop(runtime);
283 private void checkSealed() {
285 throw new SystemAssemblyException("Container is sealed");