(no commit message)
[utils] / system / general / src / main / java / org / wamblee / system / core / AbstractComponent.java
1 /*
2  * Copyright 2007 the original author or authors.
3  * 
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
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
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.
15  */
16 package org.wamblee.system.core;
17
18 import java.util.ArrayList;
19 import java.util.Arrays;
20 import java.util.HashSet;
21 import java.util.List;
22 import java.util.Set;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26
27 /**
28  * Abstract subsystem class making it easy to implement new subsystems.
29  */
30 public abstract class AbstractComponent<Type> implements Component<Type> {
31
32         private static final Log LOG = LogFactory.getLog(AbstractComponent.class);
33
34         private ThreadLocal<List<ProvidedInterface>> _remaining;
35
36         private String _context;
37         private String _name;
38         private List<ProvidedInterface> _provided;
39         private List<RequiredInterface> _required;
40
41         /**
42          * Constructs the subsystem.
43          * 
44          * @param aName
45          *            Name of the system.
46          * @param aProvided
47          *            Provided services.
48          * @param aRequired
49          *            Required services.
50          */
51         protected AbstractComponent(String aName, ProvidedInterface[] aProvided,
52                         RequiredInterface[] aRequired) {
53                 _remaining = new ThreadLocal<List<ProvidedInterface>>();
54                 _context = null;
55                 _name = aName;
56                 _provided = new ArrayList<ProvidedInterface>();
57                 _provided.addAll(Arrays.asList(aProvided));
58                 _required = new ArrayList<RequiredInterface>();
59                 _required.addAll(Arrays.asList(aRequired));
60         }
61
62         @Override
63         public final String getName() {
64                 return _name;
65         }
66
67         @Override
68         public void addContext(String aContext) {
69                 if (_context == null) {
70                         _context = aContext;
71                 } else {
72                         _context = aContext + "." + _context;
73                 }
74         }
75
76         @Override
77         public String getQualifiedName() {
78                 if (_context == null) {
79                         return getName();
80                 }
81                 return _context + "." + getName();
82         }
83
84         @Override
85         public final ProvidedInterface[] getProvidedInterfaces() {
86                 return _provided.toArray(new ProvidedInterface[0]);
87         }
88
89         @Override
90         public final RequiredInterface[] getRequiredInterfaces() {
91                 return _required.toArray(new RequiredInterface[0]);
92         }
93
94         @Override
95         public final Type start(Scope aScope) {
96                 LOG.info("Initializing '" + getQualifiedName() + "'");
97                 List<ProvidedInterface> oldRemaining = _remaining.get();
98                 _remaining.set(new ArrayList<ProvidedInterface>(Arrays.asList(getProvidedInterfaces())));
99                 try {
100                         Type runtime = doStart(aScope);
101                         checkNotStartedInterfaces();
102                         return runtime;
103                 } finally {
104                         _remaining.set(oldRemaining);
105                 }
106         }
107
108         private void checkNotStartedInterfaces() {
109                 if (_remaining.get().size() > 0) {
110                         String notProvided = "";
111                         for (ProvidedInterface provided : _remaining.get()) {
112                                 notProvided += "\nComponent " + getQualifiedName()
113                                                 + " did not start interface " + provided;
114                         }
115                         throw new SystemAssemblyException(notProvided);
116                 }
117         }
118
119         /**
120          * Must be implemented for initializing the subsystem. The implementation
121          * must call {@link #addService(Service)} for each service that is started.
122          * 
123          * @return Returns the runtime of the component.
124          */
125         protected abstract Type doStart(Scope aScope);
126
127         /**
128          * Implementations must call this method to indicate that a new service has
129          * been started.
130          * 
131          * @param aDescriptor
132          *            Provided interface.
133          * @param aService
134          *            Implementation of the interface.
135          * @param aScope
136          *            scope in which to publish the implementation.
137          */
138         protected final void addInterface(ProvidedInterface aDescriptor,
139                         Object aService, Scope aScope) {
140                 LOG.info("Interface '" + getQualifiedName() + "."
141                                 + aDescriptor.getName() + "' started.");
142                 _remaining.get().remove(aDescriptor);
143                 aScope.publishInterface(aDescriptor, aService);
144         }
145
146         @Override
147         public void stop(Type aRuntime) {
148                 doStop(aRuntime);
149         }
150
151         protected abstract void doStop(Type aRuntime);
152
153         @Override
154         public String toString() {
155                 return getQualifiedName();
156         }
157
158 }