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.Collection;
20 import java.util.HashSet;
21 import java.util.List;
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.wamblee.general.Pair;
27 import org.wamblee.system.core.AbstractComponent;
28 import org.wamblee.system.core.Component;
29 import org.wamblee.system.core.DefaultScope;
30 import org.wamblee.system.core.ProvidedInterface;
31 import org.wamblee.system.core.RequiredInterface;
32 import org.wamblee.system.core.Scope;
33 import org.wamblee.system.core.SystemAssemblyException;
34 import org.wamblee.system.graph.component.ComponentGraph;
37 * Container consisting of multiple components.
39 * @author Erik Brakkee
41 public class Container extends AbstractComponent<Scope> {
43 private static final Log LOG = LogFactory.getLog(Container.class);
45 private List<Component> _components;
46 private Set<String> _componentNames;
47 private CompositeInterfaceRestriction _restriction;
48 private boolean _sealed;
51 * Constructs the container
54 * Name of the container
58 * Provided services of the container
60 * Required services by the container.
62 public Container(String aName, Component[] aComponents,
63 ProvidedInterface[] aProvided, RequiredInterface[] aRequired) {
64 super(aName, aProvided, aRequired);
65 _components = new ArrayList<Component>();
67 _componentNames = new HashSet<String>();
68 _restriction = new CompositeInterfaceRestriction();
70 for (Component component : aComponents) {
71 addComponent(component);
75 public Container(String aName) {
76 this(aName, new Component[0], new ProvidedInterface[0],
77 new RequiredInterface[0]);
80 public Container addComponent(Component aComponent) {
82 if (aComponent.getContext() != null) {
83 throw new SystemAssemblyException(
84 "Inconsistent hierarchy, component '"
85 + aComponent.getName()
86 + "' is already part of another hierarchy");
88 if (_componentNames.contains(aComponent.getName())) {
89 throw new SystemAssemblyException("Duplicate component '"
90 + aComponent.getName() + "'");
92 _components.add(aComponent);
93 _componentNames.add(aComponent.getName());
94 aComponent.addContext(getQualifiedName());
99 * Adds an interface restriction for explicitly configuring the relations
100 * between components.
102 * @param aRestriction
103 * Restriction to add.
104 * @return Reference to this to allow call chaining.
106 public Container addRestriction(InterfaceRestriction aRestriction) {
108 _restriction.add(aRestriction);
113 public Container addProvidedInterface(ProvidedInterface aProvided) {
115 super.addProvidedInterface(aProvided);
120 public Container addRequiredInterface(RequiredInterface aRequired) {
122 super.addRequiredInterface(aRequired);
127 public void addContext(String aContext) {
128 super.addContext(aContext);
129 for (Component component : _components) {
130 component.addContext(aContext);
135 * Validates the components together to check that there are no required
136 * services not in the required list and no services in the provided list
137 * that cannot be provided. Also logs a warning in case of superfluous
140 * @throws SystemAssemblyException
141 * in case of any validation problems.
143 public void validate() {
144 doStartOptionalDryRun(null, true);
148 * Seal the container, meaning that no further components or interfaces may
156 * Checks if the container is sealed.
158 * @return True iff the container is sealed.
160 public boolean isSealed() {
165 * Utility method to start with an empty external scope. This is useful for
166 * top-level containers which are not part of another container.
170 public Scope start() {
171 Scope scope = new DefaultScope(getProvidedInterfaces());
172 return super.start(scope);
176 protected Scope doStart(Scope aExternalScope) {
179 Scope scope = new DefaultScope(getProvidedInterfaces(), aExternalScope);
180 ComponentGraph graph = doStartOptionalDryRun(scope, false);
181 exposeProvidedInterfaces(graph, aExternalScope, scope);
186 private void exposeProvidedInterfaces(ComponentGraph aGraph, Scope aExternalScope,
187 Scope aInternalScope) {
188 for (Pair<ProvidedInterface,ProvidedInterface> mapping:
189 aGraph.findExternalProvidedInterfaceMapping()) {
190 Object svc = aInternalScope.getInterfaceImplementation(mapping.getSecond(), Object.class);
191 addInterface(mapping.getFirst(), svc, aExternalScope);
195 private ComponentGraph doStartOptionalDryRun(Scope aScope, boolean aDryRun) {
196 ComponentGraph graph = createComponentGraph();
202 LOG.info("Starting '" + getQualifiedName() + "'");
204 List<Component> started = new ArrayList<Component>();
205 for (Component component : _components) {
207 // Start the service.
209 Object runtime = component.start(aScope);
210 aScope.addRuntime(component, runtime);
211 started.add(component);
213 } catch (SystemAssemblyException e) {
215 } catch (RuntimeException e) {
216 LOG.error(getQualifiedName() + ": could not start '"
217 + component.getQualifiedName() + "'", e);
218 stopAlreadyStartedComponents(started, aScope);
225 private ComponentGraph createComponentGraph() {
226 ComponentGraph graph = new ComponentGraph();
227 for (RequiredInterface req : getRequiredInterfaces()) {
228 graph.addRequiredInterface(req);
230 for (Component comp : _components) {
231 graph.addComponent(comp);
233 for (ProvidedInterface prov: getProvidedInterfaces()) {
234 graph.addProvidedInterface(prov);
237 graph.addRestriction(_restriction);
241 private void stopAlreadyStartedComponents(List<Component> aStarted,
243 // an exception occurred, stop the successfully started
245 for (int i = aStarted.size() - 1; i >= 0; i--) {
247 Component component = aStarted.get(i);
248 aStarted.get(i).stop(aScope.getRuntime(component));
249 } catch (Throwable t) {
250 LOG.error(getQualifiedName() + ": error stopping "
251 + aStarted.get(i).getQualifiedName());
257 protected void doStop(Scope aScope) {
258 for (int i = _components.size() - 1; i >= 0; i--) {
259 Component component = _components.get(i);
260 Object runtime = aScope.getRuntime(component);
261 component.stop(runtime);
265 private void checkSealed() {
267 throw new SystemAssemblyException("Container is sealed");