|
|||||||||||||||||||
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 | |||||||||||||||
AbstractServiceModelImpl.java | 83.3% | 93.5% | 100% | 91.8% |
|
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.servicemodel;
|
|
16 |
|
|
17 |
import java.util.List;
|
|
18 |
|
|
19 |
import org.apache.commons.logging.Log;
|
|
20 |
import org.apache.commons.logging.LogFactory;
|
|
21 |
import org.apache.hivemind.ApplicationRuntimeException;
|
|
22 |
import org.apache.hivemind.HiveMind;
|
|
23 |
import org.apache.hivemind.impl.ConstructableServicePoint;
|
|
24 |
import org.apache.hivemind.impl.InterceptorStackImpl;
|
|
25 |
import org.apache.hivemind.internal.ServiceImplementationConstructor;
|
|
26 |
import org.apache.hivemind.internal.ServiceInterceptorContribution;
|
|
27 |
import org.apache.hivemind.internal.ServiceModel;
|
|
28 |
|
|
29 |
/**
|
|
30 |
* Base class for implementing {@link org.apache.hivemind.ServiceModel}.
|
|
31 |
*
|
|
32 |
* @author Howard Lewis Ship
|
|
33 |
*/
|
|
34 |
public abstract class AbstractServiceModelImpl implements ServiceModel |
|
35 |
{ |
|
36 |
/**
|
|
37 |
* This log is created from the log's service id, which is the appropriate
|
|
38 |
* place to log any messages related to creating (or managing) the
|
|
39 |
* service implementation, proxy, etc. Subclasses should make
|
|
40 |
* use of this Log as well.
|
|
41 |
*/
|
|
42 |
protected final Log _log;
|
|
43 |
|
|
44 |
private ConstructableServicePoint _servicePoint;
|
|
45 |
|
|
46 | 981 |
public AbstractServiceModelImpl(ConstructableServicePoint servicePoint)
|
47 |
{ |
|
48 | 981 |
_log = LogFactory.getLog(servicePoint.getExtensionPointId()); |
49 |
|
|
50 | 981 |
_servicePoint = servicePoint; |
51 |
} |
|
52 |
|
|
53 | 682 |
protected Object addInterceptors(Object core)
|
54 |
{ |
|
55 | 682 |
List interceptors = _servicePoint.getOrderedInterceptorContributions(); |
56 |
|
|
57 | 682 |
int count = interceptors == null ? 0 : interceptors.size(); |
58 |
|
|
59 | 682 |
if (count == 0)
|
60 | 666 |
return core;
|
61 |
|
|
62 | 16 |
InterceptorStackImpl stack = new InterceptorStackImpl(_log, _servicePoint, core);
|
63 |
|
|
64 |
// They are sorted into runtime execution order. Since we build from the
|
|
65 |
// core service impl outwarads, we have to reverse the runtime execution
|
|
66 |
// order to get the build order.
|
|
67 |
// That is, if user expects interceptors in order A B C (perhaps using
|
|
68 |
// the rules: A before B, C after B).
|
|
69 |
// Then that's the order for interceptors list: A B C
|
|
70 |
// To get that runtime execution order, we wrap C around the core,
|
|
71 |
// wrap B around C, and wrap A around B.
|
|
72 |
|
|
73 | 16 |
for (int i = count - 1; i >= 0; i--) |
74 |
{ |
|
75 | 18 |
ServiceInterceptorContribution ic = |
76 |
(ServiceInterceptorContribution) interceptors.get(i); |
|
77 |
|
|
78 | 18 |
stack.process(ic); |
79 |
} |
|
80 |
|
|
81 |
// Whatever's on top is the final service.
|
|
82 |
|
|
83 | 13 |
return stack.peek();
|
84 |
} |
|
85 |
|
|
86 |
/**
|
|
87 |
* Constructs the core service implementation (by invoking the
|
|
88 |
* {@link ServiceImplementationConstructor}), and checks
|
|
89 |
* that the result is non-null and assignable
|
|
90 |
* to the service interface.
|
|
91 |
*/
|
|
92 | 720 |
protected Object constructCoreServiceImplementation()
|
93 |
{ |
|
94 | 720 |
if (_log.isDebugEnabled())
|
95 | 23 |
_log.debug( |
96 |
"Constructing core service implementation for service " + _servicePoint.getExtensionPointId());
|
|
97 |
|
|
98 | 720 |
Class serviceType = _servicePoint.getServiceInterface(); |
99 | 720 |
ServiceImplementationConstructor constructor = _servicePoint.getServiceConstructor(); |
100 | 720 |
Object result = constructor.constructCoreServiceImplementation(); |
101 |
|
|
102 | 720 |
if (result == null) |
103 | 0 |
throw new ApplicationRuntimeException( |
104 |
ServiceModelMessages.factoryReturnedNull(_servicePoint), |
|
105 |
constructor.getLocation(), |
|
106 |
null);
|
|
107 |
|
|
108 | 720 |
if (!serviceType.isAssignableFrom(result.getClass()))
|
109 | 0 |
throw new ApplicationRuntimeException( |
110 |
ServiceModelMessages.factoryWrongInterface(_servicePoint, result, serviceType), |
|
111 |
constructor.getLocation(), |
|
112 |
null);
|
|
113 |
|
|
114 | 720 |
HiveMind.setLocation(result, constructor.getLocation()); |
115 |
|
|
116 | 720 |
return result;
|
117 |
} |
|
118 |
|
|
119 |
/**
|
|
120 |
* Constructs the service implementation; this is invoked
|
|
121 |
* from {@link org.apache.hivemind.ServicePoint#getService(Class)}
|
|
122 |
* (for singletons), or from the generated
|
|
123 |
* deferrable proxy (for most service models). Primarily, invokes
|
|
124 |
* {@link #constructNewServiceImplementation()} from
|
|
125 |
* within a block that checks for recursive builds.
|
|
126 |
*/
|
|
127 |
|
|
128 | 671 |
protected Object constructServiceImplementation()
|
129 |
{ |
|
130 | 671 |
Object result = constructNewServiceImplementation(); |
131 |
|
|
132 |
// After succesfully building, we don't need
|
|
133 |
// some of the definition stuff again.
|
|
134 |
|
|
135 | 668 |
_servicePoint.clearConstructorInformation(); |
136 |
|
|
137 | 668 |
return result;
|
138 |
} |
|
139 |
|
|
140 |
/**
|
|
141 |
* Constructs a new implementation of the service, starting with
|
|
142 |
* a core implementation, then adding any interceptors.
|
|
143 |
*/
|
|
144 | 671 |
protected Object constructNewServiceImplementation()
|
145 |
{ |
|
146 | 671 |
try
|
147 |
{ |
|
148 | 671 |
Object core = constructCoreServiceImplementation(); |
149 |
|
|
150 | 671 |
Object intercepted = addInterceptors(core); |
151 |
|
|
152 | 668 |
return intercepted;
|
153 |
} |
|
154 |
catch (Exception ex)
|
|
155 |
{ |
|
156 | 3 |
throw new ApplicationRuntimeException( |
157 |
ServiceModelMessages.unableToConstructService(_servicePoint, ex), |
|
158 |
ex); |
|
159 |
} |
|
160 |
|
|
161 |
} |
|
162 |
|
|
163 | 2040 |
public ConstructableServicePoint getServicePoint()
|
164 |
{ |
|
165 | 2040 |
return _servicePoint;
|
166 |
} |
|
167 |
|
|
168 |
} |
|
169 |
|
|