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.Arrays;
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.CompositeEdgeFilter;
35 import org.wamblee.system.graph.component.ComponentGraph;
36 import org.wamblee.system.graph.component.ConnectExternalProvidedProvidedFilter;
37 import org.wamblee.system.graph.component.ConnectRequiredExternallyRequiredEdgeFilter;
38 import org.wamblee.system.graph.component.ConnectRequiredProvidedEdgeFilter;
41 * Container consisting of multiple components.
43 * @author Erik Brakkee
45 public class Container extends AbstractComponent<Scope> {
47 private static final Log LOG = LogFactory.getLog(Container.class);
49 private List<Component> _components;
50 private Set<String> _componentNames;
51 private CompositeEdgeFilter _edgeFilter;
52 private boolean _sealed;
55 * Constructs the container
58 * Name of the container
62 * Provided services of the container
64 * Required services by the container.
66 public Container(String aName, Component[] aComponents,
67 List<ProvidedInterface> aProvided, List<RequiredInterface> aRequired) {
68 super(aName, aProvided, aRequired);
69 _components = new ArrayList<Component>();
71 _componentNames = new HashSet<String>();
72 _edgeFilter = new CompositeEdgeFilter();
74 for (Component component : aComponents) {
75 addComponent(component);
80 * Constructs the container
83 * Name of the container
87 * Provided services of the container
89 * Required services by the container.
91 public Container(String aName, Component[] aComponents,
92 ProvidedInterface[] aProvided, RequiredInterface[] aRequired) {
93 this(aName, aComponents, Arrays.asList(aProvided), Arrays.asList(aRequired));
96 public Container(String aName) {
97 this(aName, new Component[0], new ProvidedInterface[0],
98 new RequiredInterface[0]);
101 public Container addComponent(Component aComponent) {
103 if (aComponent.getContext() != null) {
104 throw new SystemAssemblyException(
105 "Inconsistent hierarchy, component '"
106 + aComponent.getName()
107 + "' is already part of another hierarchy");
109 if (_componentNames.contains(aComponent.getName())) {
110 throw new SystemAssemblyException("Duplicate component '"
111 + aComponent.getName() + "'");
113 _components.add(aComponent);
114 _componentNames.add(aComponent.getName());
115 aComponent.addContext(getQualifiedName());
120 * Explictly connects required and provided interfaces.
121 * @param aClientComponent Client component, may not be null.
122 * @param aRequiredInterface Required interface. If null it means all required interfaces.
123 * @param aServerComponent Server component to connect to. If null, it means that no server components
124 * may be connected to and the provider of the required interface will be null.
125 * @param aProvidedInterface Provided interface. If null, it means that there is no restriction on the
126 * name of the provided interface and that it is automatically selected.
128 public void connectRequiredProvided(String aClientComponent, String aRequiredInterface,
129 String aServerComponent, String aProvidedInterface) {
132 _edgeFilter.add(new ConnectRequiredProvidedEdgeFilter(aClientComponent, aRequiredInterface, aServerComponent, aProvidedInterface));
136 * Explicitly connects a externally required interface to an internally required interface.
137 * @param aComponent Component requiring the interface (must be non-null).
138 * @param aRequiredInterface Required interface of the component (must be non-null).
139 * @param aExternalRequiredInterface Externally required interface (must be non-null).
141 public void connectExternalRequired(String aComponent, String aRequiredInterface,
142 String aExternalRequiredInterface) {
145 _edgeFilter.add(new ConnectRequiredExternallyRequiredEdgeFilter(
146 aComponent, aRequiredInterface, aExternalRequiredInterface));
149 public void connectExternalProvided(String aExternalProvided, String aComponent, String aProvidedInterface) {
152 _edgeFilter.add(new ConnectExternalProvidedProvidedFilter(aExternalProvided, aComponent, aProvidedInterface));
157 public Container addProvidedInterface(ProvidedInterface aProvided) {
159 super.addProvidedInterface(aProvided);
164 public Container addRequiredInterface(RequiredInterface aRequired) {
166 super.addRequiredInterface(aRequired);
171 public void addContext(String aContext) {
172 super.addContext(aContext);
173 for (Component component : _components) {
174 component.addContext(aContext);
179 * Validates the components together to check that there are no required
180 * services not in the required list and no services in the provided list
181 * that cannot be provided. Also logs a warning in case of superfluous
184 * @throws SystemAssemblyException
185 * in case of any validation problems.
187 public void validate() {
188 doStartOptionalDryRun(null, true);
192 * Seal the container, meaning that no further components or interfaces may
200 * Checks if the container is sealed.
202 * @return True iff the container is sealed.
204 public boolean isSealed() {
209 * Utility method to start with an empty external scope. This is useful for
210 * top-level containers which are not part of another container.
214 public Scope start() {
215 Scope scope = new DefaultScope(getProvidedInterfaces());
216 return super.start(scope);
220 protected Scope doStart(Scope aExternalScope) {
223 Scope scope = new DefaultScope(getProvidedInterfaces().toArray(new ProvidedInterface[0]), aExternalScope);
224 ComponentGraph graph = doStartOptionalDryRun(scope, false);
225 exposeProvidedInterfaces(graph, aExternalScope, scope);
230 private void exposeProvidedInterfaces(ComponentGraph aGraph, Scope aExternalScope,
231 Scope aInternalScope) {
232 for (Pair<ProvidedInterface,ProvidedInterface> mapping:
233 aGraph.findExternalProvidedInterfaceMapping()) {
234 Object svc = aInternalScope.getInterfaceImplementation(mapping.getSecond(), Object.class);
235 addInterface(mapping.getFirst(), svc, aExternalScope);
239 private ComponentGraph doStartOptionalDryRun(Scope aScope, boolean aDryRun) {
240 ComponentGraph graph = createComponentGraph();
246 LOG.info("Starting '" + getQualifiedName() + "'");
248 List<Component> started = new ArrayList<Component>();
249 for (Component component : _components) {
251 // Start the service.
253 Object runtime = component.start(aScope);
254 aScope.addRuntime(component, runtime);
255 started.add(component);
257 } catch (SystemAssemblyException e) {
259 } catch (RuntimeException e) {
260 LOG.error(getQualifiedName() + ": could not start '"
261 + component.getQualifiedName() + "'", e);
262 stopAlreadyStartedComponents(started, aScope);
269 private ComponentGraph createComponentGraph() {
270 ComponentGraph graph = new ComponentGraph();
271 for (RequiredInterface req : getRequiredInterfaces()) {
272 graph.addRequiredInterface(this, req);
274 for (Component comp : _components) {
275 graph.addComponent(comp);
277 for (ProvidedInterface prov: getProvidedInterfaces()) {
278 graph.addProvidedInterface(this, prov);
281 graph.addEdgeFilter(_edgeFilter);
285 private void stopAlreadyStartedComponents(List<Component> aStarted,
287 // an exception occurred, stop the successfully started
289 for (int i = aStarted.size() - 1; i >= 0; i--) {
291 Component component = aStarted.get(i);
292 aStarted.get(i).stop(aScope.getRuntime(component));
293 } catch (Throwable t) {
294 LOG.error(getQualifiedName() + ": error stopping "
295 + aStarted.get(i).getQualifiedName());
301 protected void doStop(Scope aScope) {
302 for (int i = _components.size() - 1; i >= 0; i--) {
303 Component component = _components.get(i);
304 Object runtime = aScope.getRuntime(component);
305 component.stop(runtime);
309 private void checkSealed() {
311 throw new SystemAssemblyException("Container is sealed");