|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
ConfigurationPointImpl.java | 94.4% | 94.1% | 83.3% | 92.6% |
|
1 |
// Copyright 2004 The Apache Software Foundation
|
|
2 |
//
|
|
3 |
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4 |
// you may not use this file except in compliance with the License.
|
|
5 |
// You may obtain a copy of the License at
|
|
6 |
//
|
|
7 |
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8 |
//
|
|
9 |
// Unless required by applicable law or agreed to in writing, software
|
|
10 |
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12 |
// See the License for the specific language governing permissions and
|
|
13 |
// limitations under the License.
|
|
14 |
|
|
15 |
package org.apache.hivemind.impl;
|
|
16 |
|
|
17 |
import java.util.ArrayList;
|
|
18 |
import java.util.Collections;
|
|
19 |
import java.util.List;
|
|
20 |
|
|
21 |
import org.apache.commons.logging.Log;
|
|
22 |
import org.apache.commons.logging.LogFactory;
|
|
23 |
import org.apache.hivemind.*;
|
|
24 |
import org.apache.hivemind.ApplicationRuntimeException;
|
|
25 |
import org.apache.hivemind.Occurances;
|
|
26 |
import org.apache.hivemind.internal.ConfigurationPoint;
|
|
27 |
import org.apache.hivemind.internal.Contribution;
|
|
28 |
import org.apache.hivemind.schema.Schema;
|
|
29 |
import org.apache.hivemind.util.ToStringBuilder;
|
|
30 |
|
|
31 |
/**
|
|
32 |
* Implementation of the {@link org.apache.hivemind.internal.ConfigurationPoint} interface; a container
|
|
33 |
* for {@link org.apache.hivemind.internal.Contribution}s.
|
|
34 |
*
|
|
35 |
* @author Howard Lewis Ship
|
|
36 |
*/
|
|
37 |
public final class ConfigurationPointImpl |
|
38 |
extends AbstractExtensionPoint
|
|
39 |
implements ConfigurationPoint
|
|
40 |
{ |
|
41 |
private static final Log LOG = LogFactory.getLog(ConfigurationPointImpl.class); |
|
42 |
|
|
43 |
/**
|
|
44 |
* The cached elements for the extension point (if caching is enabled).
|
|
45 |
*/
|
|
46 |
private List _elements;
|
|
47 |
private List _elementsProxy;
|
|
48 |
private Occurances _expectedCount;
|
|
49 |
private List _contributions;
|
|
50 |
private boolean _building; |
|
51 |
private Schema _contributionsSchema;
|
|
52 |
private ShutdownCoordinator _shutdownCoordinator;
|
|
53 |
|
|
54 | 1 |
protected void extendDescription(ToStringBuilder builder) |
55 |
{ |
|
56 | 1 |
builder.append("expectedCount", _expectedCount);
|
57 | 1 |
builder.append("contributions", _contributions);
|
58 | 1 |
builder.append("schema", _contributionsSchema);
|
59 |
} |
|
60 |
|
|
61 |
/**
|
|
62 |
* Returns the number of contributions; it is expected
|
|
63 |
* that each top-level {@link org.apache.hivemind.Element}
|
|
64 |
* in each {@link Contribution} will convert to one element instance;
|
|
65 |
* the value returned is the total number of top-level elements
|
|
66 |
* in all contributed Extensions.
|
|
67 |
*/
|
|
68 | 2 |
public int getContributionCount() |
69 |
{ |
|
70 | 2 |
if (_contributions == null) |
71 | 1 |
return 0;
|
72 |
|
|
73 | 1 |
int total = 0;
|
74 |
|
|
75 | 1 |
int count = _contributions.size();
|
76 | 1 |
for (int i = 0; i < count; i++) |
77 |
{ |
|
78 | 1 |
Contribution c = (Contribution) _contributions.get(i); |
79 | 1 |
total += c.getElements().size(); |
80 |
} |
|
81 |
|
|
82 | 1 |
return total;
|
83 |
} |
|
84 |
|
|
85 | 353 |
public void addContribution(Contribution c) |
86 |
{ |
|
87 | 353 |
if (_contributions == null) |
88 | 350 |
_contributions = new ArrayList();
|
89 |
|
|
90 | 353 |
_contributions.add(c); |
91 |
} |
|
92 |
|
|
93 | 582 |
public Occurances getExpectedCount()
|
94 |
{ |
|
95 | 582 |
return _expectedCount;
|
96 |
} |
|
97 |
|
|
98 | 582 |
public void setExpectedCount(Occurances occurances) |
99 |
{ |
|
100 | 582 |
_expectedCount = occurances; |
101 |
} |
|
102 |
|
|
103 |
/**
|
|
104 |
* Returns the contributed elements as an unmodifiable {@link List}.
|
|
105 |
* Internally, a proxy to the real list is returned, such that the
|
|
106 |
* real list may not be constructed until actually needed.
|
|
107 |
*/
|
|
108 | 354 |
public synchronized List getElements() |
109 |
{ |
|
110 | 354 |
if (_elements != null) |
111 | 1 |
return _elements;
|
112 |
|
|
113 | 353 |
if (_elementsProxy == null) |
114 |
{ |
|
115 | 352 |
ElementsProxyList outerProxy = new ElementsProxyList();
|
116 |
|
|
117 | 352 |
new ElementsInnerProxyList(this, outerProxy); |
118 |
|
|
119 | 352 |
_shutdownCoordinator.addRegistryShutdownListener(outerProxy); |
120 |
|
|
121 | 352 |
_elementsProxy = outerProxy; |
122 |
} |
|
123 |
|
|
124 | 353 |
return _elementsProxy;
|
125 |
} |
|
126 |
|
|
127 |
/**
|
|
128 |
* Invoked by {@link ElementsInnerProxyList} when the actual list
|
|
129 |
* is needed. Returns the List (which is modifiable, but
|
|
130 |
* that's OK because ElementsInnerProxyList is unmodifiable) and,
|
|
131 |
* as a side effect, keeps a reference to an unmodifiable
|
|
132 |
* version of the result for future invocations
|
|
133 |
* of {@link #getElements()}.
|
|
134 |
*/
|
|
135 |
|
|
136 | 348 |
synchronized List constructElements()
|
137 |
{ |
|
138 |
// It's nice to have this protection, but (unlike services), you
|
|
139 |
// would really have to go out of your way to provoke
|
|
140 |
// a recursive configuration.
|
|
141 |
|
|
142 | 348 |
if (_building)
|
143 | 0 |
throw new ApplicationRuntimeException( |
144 |
ImplMessages.recursiveConfiguration(getExtensionPointId())); |
|
145 |
|
|
146 | 348 |
try
|
147 |
{ |
|
148 | 348 |
_building = true;
|
149 |
|
|
150 | 348 |
List result = constructElementsList(); |
151 |
|
|
152 |
// After constructing the result, if the result
|
|
153 |
// will be cached, then there's no need to keep
|
|
154 |
// the schema and extensions (used to build the
|
|
155 |
// result); it can all be released to the GC.
|
|
156 |
|
|
157 | 346 |
_elements = Collections.unmodifiableList(result); |
158 |
|
|
159 | 346 |
_contributionsSchema = null;
|
160 | 346 |
_contributions = null;
|
161 |
|
|
162 |
// Now that we have the real list, we don't need the proxy
|
|
163 |
// anymore, either.
|
|
164 |
|
|
165 | 346 |
_elementsProxy = null;
|
166 |
|
|
167 | 346 |
return result;
|
168 |
} |
|
169 |
finally
|
|
170 |
{ |
|
171 | 348 |
_building = false;
|
172 |
} |
|
173 |
} |
|
174 |
|
|
175 |
/**
|
|
176 |
* Returns the list of elements assembled from all contributions.
|
|
177 |
* The list is modifiable, and should be wrapped into an unmodifiable
|
|
178 |
* list. May return the empty list, but won't return null.
|
|
179 |
*/
|
|
180 | 348 |
private List constructElementsList()
|
181 |
{ |
|
182 | 348 |
if (LOG.isDebugEnabled())
|
183 | 43 |
LOG.debug("Constructing extension point " + getExtensionPointId());
|
184 |
|
|
185 | 348 |
if (_contributions == null) |
186 | 76 |
return Collections.EMPTY_LIST;
|
187 |
|
|
188 | 272 |
SchemaProcessorImpl processor = |
189 |
new SchemaProcessorImpl(getModule().getErrorHandler(), _contributionsSchema);
|
|
190 |
|
|
191 | 272 |
int count = _contributions.size();
|
192 |
|
|
193 | 272 |
try
|
194 |
{ |
|
195 | 272 |
for (int i = 0; i < count; i++) |
196 |
{ |
|
197 | 275 |
Contribution extension = (Contribution) _contributions.get(i); |
198 |
|
|
199 | 275 |
processor.process(extension.getElements(), extension.getContributingModule()); |
200 |
} |
|
201 |
|
|
202 | 270 |
return processor.getElements();
|
203 |
} |
|
204 |
catch (Exception ex)
|
|
205 |
{ |
|
206 | 2 |
throw new ApplicationRuntimeException( |
207 |
ImplMessages.unableToConstructConfiguration(getExtensionPointId(), ex), |
|
208 |
ex); |
|
209 |
} |
|
210 |
|
|
211 |
} |
|
212 |
|
|
213 | 0 |
public Schema getSchema()
|
214 |
{ |
|
215 | 0 |
return _contributionsSchema;
|
216 |
} |
|
217 |
|
|
218 | 582 |
public void setContributionsSchema(Schema schema) |
219 |
{ |
|
220 | 582 |
_contributionsSchema = schema; |
221 |
} |
|
222 |
|
|
223 | 0 |
public Schema getContributionsSchema()
|
224 |
{ |
|
225 | 0 |
return _contributionsSchema;
|
226 |
} |
|
227 |
|
|
228 | 582 |
public void setShutdownCoordinator(ShutdownCoordinator coordinator) |
229 |
{ |
|
230 | 582 |
_shutdownCoordinator = coordinator; |
231 |
} |
|
232 |
|
|
233 |
} |
|
234 |
|
|