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.List;
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24 import org.wamblee.general.Pair;
25 import org.wamblee.system.core.AbstractComponent;
26 import org.wamblee.system.core.Component;
27 import org.wamblee.system.core.DefaultScope;
28 import org.wamblee.system.core.NamedInterface;
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.component.ComponentGraph;
35 import org.wamblee.system.graph.component.ConnectExternalProvidedProvidedFilter;
36 import org.wamblee.system.graph.component.ConnectRequiredExternallyRequiredEdgeFilter;
37 import org.wamblee.system.graph.component.ConnectRequiredProvidedEdgeFilter;
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 CompositeEdgeFilter _edgeFilter;
50 private boolean _sealed;
53 * Constructs the container
56 * Name of the container
60 * Provided services of the container
62 * Required services by the container.
64 public Container(String aName, Component[] aComponents,
65 List<ProvidedInterface> aProvided, List<RequiredInterface> aRequired) {
66 super(aName, aProvided, aRequired);
67 _components = new ArrayList<Component>();
69 _edgeFilter = new CompositeEdgeFilter();
71 for (Component component : aComponents) {
72 addComponent(component);
77 * Constructs the container
80 * Name of the container
84 * Provided services of the container
86 * Required services by the container.
88 public Container(String aName, Component[] aComponents,
89 ProvidedInterface[] aProvided, RequiredInterface[] aRequired) {
90 this(aName, aComponents, Arrays.asList(aProvided), Arrays.asList(aRequired));
93 public Container(String aName) {
94 this(aName, new Component[0], new ProvidedInterface[0],
95 new RequiredInterface[0]);
98 public Container addComponent(Component aComponent) {
100 if (aComponent.getContext() != null) {
101 throw new SystemAssemblyException(
102 "Inconsistent hierarchy, component '"
103 + aComponent.getName()
104 + "' is already part of another hierarchy");
106 if ( findComponent(aComponent.getName()) != null ) {
107 throw new SystemAssemblyException("Duplicate component '"
108 + aComponent.getName() + "'");
110 _components.add(aComponent);
111 aComponent.addContext(getQualifiedName());
116 * Explictly connects required and provided interfaces.
117 * @param aClientComponent Client component, may not be null.
118 * @param aRequiredInterface Required interface. If null it means all required interfaces.
119 * @param aServerComponent Server component to connect to. If null, it means that no server components
120 * may be connected to and the provider of the required interface will be null.
121 * @param aProvidedInterface Provided interface. If null, it means that there is no restriction on the
122 * name of the provided interface and that it is automatically selected.
124 public void connectRequiredProvided(String aClientComponent, String aRequiredInterface,
125 String aServerComponent, String aProvidedInterface) {
127 Component client = findComponent(aClientComponent);
128 Component server = findComponent(aServerComponent);
129 if ( client == null ) {
130 throw new SystemAssemblyException(getQualifiedName() + ": No component '" + aClientComponent + "' in the container");
132 if ( aRequiredInterface != null ) {
133 if ( findInterface(client.getRequiredInterfaces(), aRequiredInterface) == null ) {
134 throw new SystemAssemblyException(
135 getQualifiedName() + ": Component '" + aClientComponent + "' does not have a required interface named '"
136 + aRequiredInterface + "'");
139 if ( server == null ) {
140 throw new SystemAssemblyException("No component '" + aClientComponent + "' in the container");
142 if ( aProvidedInterface != null ) {
143 if ( findInterface(server.getProvidedInterfaces(), aProvidedInterface) == null) {
144 throw new SystemAssemblyException(
145 getQualifiedName() + ": Component '" + aServerComponent + "' does not have a provided interface named '"
146 + aProvidedInterface + "'");
149 _edgeFilter.add(new ConnectRequiredProvidedEdgeFilter(aClientComponent, aRequiredInterface, aServerComponent, aProvidedInterface));
153 * Explicitly connects a externally required interface to an internally required interface.
154 * @param aComponent Component requiring the interface (must be non-null).
155 * @param aRequiredInterface Required interface of the component (must be non-null).
156 * @param aExternalRequiredInterface Externally required interface (must be non-null).
158 public void connectExternalRequired(String aComponent, String aRequiredInterface,
159 String aExternalRequiredInterface) {
161 Component client = findComponent(aComponent);
162 if ( client == null ) {
163 throw new SystemAssemblyException(getQualifiedName() + ": No component '" + aComponent + "' in the container");
165 if ( aRequiredInterface != null ) {
166 if ( findInterface(client.getRequiredInterfaces(), aRequiredInterface) == null ) {
167 throw new SystemAssemblyException(
168 getQualifiedName() + ": Component '" + aComponent + "' does not have a required interface named '"
169 + aRequiredInterface + "'");
172 if ( aExternalRequiredInterface != null) {
173 if ( findInterface(getRequiredInterfaces(), aExternalRequiredInterface) == null ) {
174 throw new SystemAssemblyException(
175 getQualifiedName() + ": container does not have a required interface named '"
176 + aExternalRequiredInterface + "'");
179 _edgeFilter.add(new ConnectRequiredExternallyRequiredEdgeFilter(
180 aComponent, aRequiredInterface, aExternalRequiredInterface));
183 public void connectExternalProvided(String aExternalProvided, String aComponent, String aProvidedInterface) {
185 Component server = findComponent(aComponent);
188 if ( server == null ) {
189 throw new SystemAssemblyException("No component '" + aComponent + "' in the container");
191 if ( aProvidedInterface != null ) {
192 if ( findInterface(server.getProvidedInterfaces(), aProvidedInterface) == null) {
193 throw new SystemAssemblyException(
194 getQualifiedName() + ": Component '" + aComponent + "' does not have a provided interface named '"
195 + aProvidedInterface + "'");
198 if ( aExternalProvided != null ) {
199 if ( findInterface(getProvidedInterfaces(), aExternalProvided) == null) {
200 throw new SystemAssemblyException(
201 getQualifiedName() + ": Container does not have a provided interface named '"
202 + aExternalProvided + "'");
205 _edgeFilter.add(new ConnectExternalProvidedProvidedFilter(aExternalProvided, aComponent, aProvidedInterface));
210 public Container addProvidedInterface(ProvidedInterface aProvided) {
212 super.addProvidedInterface(aProvided);
217 public Container addRequiredInterface(RequiredInterface aRequired) {
219 super.addRequiredInterface(aRequired);
224 public void addContext(String aContext) {
225 super.addContext(aContext);
226 for (Component component : _components) {
227 component.addContext(aContext);
232 * Validates the components together to check that there are no required
233 * services not in the required list and no services in the provided list
234 * that cannot be provided. Also logs a warning in case of superfluous
237 * @throws SystemAssemblyException
238 * in case of any validation problems.
240 public void validate() {
241 doStartOptionalDryRun(null, true);
245 * Seal the container, meaning that no further components or interfaces may
253 * Checks if the container is sealed.
255 * @return True iff the container is sealed.
257 public boolean isSealed() {
262 * Utility method to start with an empty external scope. This is useful for
263 * top-level containers which are not part of another container.
267 public Scope start() {
268 Scope scope = new DefaultScope(getProvidedInterfaces());
269 return super.start(scope);
273 protected Scope doStart(Scope aExternalScope) {
275 Scope scope = new DefaultScope(getProvidedInterfaces().toArray(new ProvidedInterface[0]), aExternalScope);
276 ComponentGraph graph = doStartOptionalDryRun(scope, false);
277 exposeProvidedInterfaces(graph, aExternalScope, scope);
282 private void exposeProvidedInterfaces(ComponentGraph aGraph, Scope aExternalScope,
283 Scope aInternalScope) {
284 for (Pair<ProvidedInterface,ProvidedInterface> mapping:
285 aGraph.findExternalProvidedInterfaceMapping()) {
286 Object svc = aInternalScope.getInterfaceImplementation(mapping.getSecond(), Object.class);
287 addInterface(mapping.getFirst(), svc, aExternalScope);
291 private ComponentGraph doStartOptionalDryRun(Scope aScope, boolean aDryRun) {
292 ComponentGraph graph = createComponentGraph();
296 LOG.info("Starting '" + getQualifiedName() + "'");
298 List<Component> started = new ArrayList<Component>();
299 for (Component component : _components) {
301 // Start the service.
303 Object runtime = component.start(aScope);
304 aScope.addRuntime(component, runtime);
305 started.add(component);
307 } catch (SystemAssemblyException e) {
309 } catch (RuntimeException e) {
310 LOG.error(getQualifiedName() + ": could not start '"
311 + component.getQualifiedName() + "'", e);
312 stopAlreadyStartedComponents(started, aScope);
319 private ComponentGraph createComponentGraph() {
320 ComponentGraph graph = new ComponentGraph();
321 for (RequiredInterface req : getRequiredInterfaces()) {
322 graph.addRequiredInterface(this, req);
324 for (Component comp : _components) {
325 graph.addComponent(comp);
327 for (ProvidedInterface prov: getProvidedInterfaces()) {
328 graph.addProvidedInterface(this, prov);
331 graph.addEdgeFilter(_edgeFilter);
335 private void stopAlreadyStartedComponents(List<Component> aStarted,
337 // an exception occurred, stop the successfully started
339 for (int i = aStarted.size() - 1; i >= 0; i--) {
341 Component component = aStarted.get(i);
342 aStarted.get(i).stop(aScope.getRuntime(component));
343 } catch (Throwable t) {
344 LOG.error(getQualifiedName() + ": error stopping "
345 + aStarted.get(i).getQualifiedName());
351 protected void doStop(Scope aScope) {
352 for (int i = _components.size() - 1; i >= 0; i--) {
353 Component component = _components.get(i);
354 Object runtime = aScope.getRuntime(component);
355 component.stop(runtime);
359 private void checkSealed() {
361 throw new SystemAssemblyException("Container is sealed");
366 * Finds a component based on the non-qualified name of the component.
367 * @param aName Component name.
368 * @return Component or null if not found.
370 public Component findComponent(String aName) {
371 for (Component<?> component: _components) {
372 if ( component.getName().equals(aName)) {
379 private static <T extends NamedInterface> T findInterface(List<T> aInterfaces, String aInterfaceName) {
380 for (T intf: aInterfaces) {
381 if ( intf.getName().equals(aInterfaceName)) {