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.NamedInterface;
31 import org.wamblee.system.core.ProvidedInterface;
32 import org.wamblee.system.core.RequiredInterface;
33 import org.wamblee.system.core.Scope;
34 import org.wamblee.system.core.SystemAssemblyException;
35 import org.wamblee.system.graph.CompositeEdgeFilter;
36 import org.wamblee.system.graph.component.ComponentGraph;
37 import org.wamblee.system.graph.component.ConnectExternalProvidedProvidedFilter;
38 import org.wamblee.system.graph.component.ConnectRequiredExternallyRequiredEdgeFilter;
39 import org.wamblee.system.graph.component.ConnectRequiredProvidedEdgeFilter;
42 * Container consisting of multiple components.
44 * @author Erik Brakkee
46 public class Container extends AbstractComponent<Scope> {
48 private static final Log LOG = LogFactory.getLog(Container.class);
50 private List<Component> _components;
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 _edgeFilter = new CompositeEdgeFilter();
73 for (Component component : aComponents) {
74 addComponent(component);
79 * Constructs the container
82 * Name of the container
86 * Provided services of the container
88 * Required services by the container.
90 public Container(String aName, Component[] aComponents,
91 ProvidedInterface[] aProvided, RequiredInterface[] aRequired) {
92 this(aName, aComponents, Arrays.asList(aProvided), Arrays.asList(aRequired));
95 public Container(String aName) {
96 this(aName, new Component[0], new ProvidedInterface[0],
97 new RequiredInterface[0]);
100 public Container addComponent(Component aComponent) {
102 if (aComponent.getContext() != null) {
103 throw new SystemAssemblyException(
104 "Inconsistent hierarchy, component '"
105 + aComponent.getName()
106 + "' is already part of another hierarchy");
108 if ( findComponent(aComponent.getName()) != null ) {
109 throw new SystemAssemblyException("Duplicate component '"
110 + aComponent.getName() + "'");
112 _components.add(aComponent);
113 aComponent.addContext(getQualifiedName());
118 * Explictly connects required and provided interfaces.
119 * @param aClientComponent Client component, may not be null.
120 * @param aRequiredInterface Required interface. If null it means all required interfaces.
121 * @param aServerComponent Server component to connect to. If null, it means that no server components
122 * may be connected to and the provider of the required interface will be null.
123 * @param aProvidedInterface Provided interface. If null, it means that there is no restriction on the
124 * name of the provided interface and that it is automatically selected.
126 public void connectRequiredProvided(String aClientComponent, String aRequiredInterface,
127 String aServerComponent, String aProvidedInterface) {
129 Component client = findComponent(aClientComponent);
130 Component server = findComponent(aServerComponent);
131 if ( client == null ) {
132 throw new SystemAssemblyException(getQualifiedName() + ": No component '" + aClientComponent + "' in the container");
134 if ( aRequiredInterface != null ) {
135 if ( findInterface(client.getRequiredInterfaces(), aRequiredInterface) == null ) {
136 throw new SystemAssemblyException(
137 getQualifiedName() + ": Component '" + aClientComponent + "' does not have a required interface named '"
138 + aRequiredInterface + "'");
141 if ( server == null ) {
142 throw new SystemAssemblyException("No component '" + aClientComponent + "' in the container");
144 if ( aProvidedInterface != null ) {
145 if ( findInterface(server.getProvidedInterfaces(), aProvidedInterface) == null) {
146 throw new SystemAssemblyException(
147 getQualifiedName() + ": Component '" + aServerComponent + "' does not have a provided interface named '"
148 + aProvidedInterface + "'");
151 _edgeFilter.add(new ConnectRequiredProvidedEdgeFilter(aClientComponent, aRequiredInterface, aServerComponent, aProvidedInterface));
155 * Explicitly connects a externally required interface to an internally required interface.
156 * @param aComponent Component requiring the interface (must be non-null).
157 * @param aRequiredInterface Required interface of the component (must be non-null).
158 * @param aExternalRequiredInterface Externally required interface (must be non-null).
160 public void connectExternalRequired(String aComponent, String aRequiredInterface,
161 String aExternalRequiredInterface) {
163 Component client = findComponent(aComponent);
164 if ( client == null ) {
165 throw new SystemAssemblyException(getQualifiedName() + ": No component '" + aComponent + "' in the container");
167 if ( aRequiredInterface != null ) {
168 if ( findInterface(client.getRequiredInterfaces(), aRequiredInterface) == null ) {
169 throw new SystemAssemblyException(
170 getQualifiedName() + ": Component '" + aComponent + "' does not have a required interface named '"
171 + aRequiredInterface + "'");
174 if ( aExternalRequiredInterface != null) {
175 if ( findInterface(getRequiredInterfaces(), aExternalRequiredInterface) == null ) {
176 throw new SystemAssemblyException(
177 getQualifiedName() + ": container does not have a required interface named '"
178 + aExternalRequiredInterface + "'");
181 _edgeFilter.add(new ConnectRequiredExternallyRequiredEdgeFilter(
182 aComponent, aRequiredInterface, aExternalRequiredInterface));
185 public void connectExternalProvided(String aExternalProvided, String aComponent, String aProvidedInterface) {
187 Component server = findComponent(aComponent);
190 if ( server == null ) {
191 throw new SystemAssemblyException("No component '" + aComponent + "' in the container");
193 if ( aProvidedInterface != null ) {
194 if ( findInterface(server.getProvidedInterfaces(), aProvidedInterface) == null) {
195 throw new SystemAssemblyException(
196 getQualifiedName() + ": Component '" + aComponent + "' does not have a provided interface named '"
197 + aProvidedInterface + "'");
200 if ( aExternalProvided != null ) {
201 if ( findInterface(getProvidedInterfaces(), aExternalProvided) == null) {
202 throw new SystemAssemblyException(
203 getQualifiedName() + ": Container does not have a provided interface named '"
204 + aExternalProvided + "'");
207 _edgeFilter.add(new ConnectExternalProvidedProvidedFilter(aExternalProvided, aComponent, aProvidedInterface));
212 public Container addProvidedInterface(ProvidedInterface aProvided) {
214 super.addProvidedInterface(aProvided);
219 public Container addRequiredInterface(RequiredInterface aRequired) {
221 super.addRequiredInterface(aRequired);
226 public void addContext(String aContext) {
227 super.addContext(aContext);
228 for (Component component : _components) {
229 component.addContext(aContext);
234 * Validates the components together to check that there are no required
235 * services not in the required list and no services in the provided list
236 * that cannot be provided. Also logs a warning in case of superfluous
239 * @throws SystemAssemblyException
240 * in case of any validation problems.
242 public void validate() {
243 doStartOptionalDryRun(null, true);
247 * Seal the container, meaning that no further components or interfaces may
255 * Checks if the container is sealed.
257 * @return True iff the container is sealed.
259 public boolean isSealed() {
264 * Utility method to start with an empty external scope. This is useful for
265 * top-level containers which are not part of another container.
269 public Scope start() {
270 Scope scope = new DefaultScope(getProvidedInterfaces());
271 return super.start(scope);
275 protected Scope doStart(Scope aExternalScope) {
277 Scope scope = new DefaultScope(getProvidedInterfaces().toArray(new ProvidedInterface[0]), aExternalScope);
278 ComponentGraph graph = doStartOptionalDryRun(scope, false);
279 exposeProvidedInterfaces(graph, aExternalScope, scope);
284 private void exposeProvidedInterfaces(ComponentGraph aGraph, Scope aExternalScope,
285 Scope aInternalScope) {
286 for (Pair<ProvidedInterface,ProvidedInterface> mapping:
287 aGraph.findExternalProvidedInterfaceMapping()) {
288 Object svc = aInternalScope.getInterfaceImplementation(mapping.getSecond(), Object.class);
289 addInterface(mapping.getFirst(), svc, aExternalScope);
293 private ComponentGraph doStartOptionalDryRun(Scope aScope, boolean aDryRun) {
294 ComponentGraph graph = createComponentGraph();
298 LOG.info("Starting '" + getQualifiedName() + "'");
300 List<Component> started = new ArrayList<Component>();
301 for (Component component : _components) {
303 // Start the service.
305 Object runtime = component.start(aScope);
306 aScope.addRuntime(component, runtime);
307 started.add(component);
309 } catch (SystemAssemblyException e) {
311 } catch (RuntimeException e) {
312 LOG.error(getQualifiedName() + ": could not start '"
313 + component.getQualifiedName() + "'", e);
314 stopAlreadyStartedComponents(started, aScope);
321 private ComponentGraph createComponentGraph() {
322 ComponentGraph graph = new ComponentGraph();
323 for (RequiredInterface req : getRequiredInterfaces()) {
324 graph.addRequiredInterface(this, req);
326 for (Component comp : _components) {
327 graph.addComponent(comp);
329 for (ProvidedInterface prov: getProvidedInterfaces()) {
330 graph.addProvidedInterface(this, prov);
333 graph.addEdgeFilter(_edgeFilter);
337 private void stopAlreadyStartedComponents(List<Component> aStarted,
339 // an exception occurred, stop the successfully started
341 for (int i = aStarted.size() - 1; i >= 0; i--) {
343 Component component = aStarted.get(i);
344 aStarted.get(i).stop(aScope.getRuntime(component));
345 } catch (Throwable t) {
346 LOG.error(getQualifiedName() + ": error stopping "
347 + aStarted.get(i).getQualifiedName());
353 protected void doStop(Scope aScope) {
354 for (int i = _components.size() - 1; i >= 0; i--) {
355 Component component = _components.get(i);
356 Object runtime = aScope.getRuntime(component);
357 component.stop(runtime);
361 private void checkSealed() {
363 throw new SystemAssemblyException("Container is sealed");
368 * Finds a component based on the non-qualified name of the component.
369 * @param aName Component name.
370 * @return Component or null if not found.
372 public Component findComponent(String aName) {
373 for (Component<?> component: _components) {
374 if ( component.getName().equals(aName)) {
381 private static <T extends NamedInterface> T findInterface(List<T> aInterfaces, String aInterfaceName) {
382 for (T intf: aInterfaces) {
383 if ( intf.getName().equals(aInterfaceName)) {