|
|||||||||||||||||||
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 | |||||||||||||||
ProxyBuilder.java | 100% | 100% | 100% | 100% |
|
1 |
// Copyright 2004, 2005 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.io.Serializable;
|
|
18 |
import java.lang.reflect.Modifier;
|
|
19 |
|
|
20 |
import org.apache.hivemind.internal.Module;
|
|
21 |
import org.apache.hivemind.internal.ServicePoint;
|
|
22 |
import org.apache.hivemind.internal.ser.ServiceSerializationHelper;
|
|
23 |
import org.apache.hivemind.service.BodyBuilder;
|
|
24 |
import org.apache.hivemind.service.ClassFab;
|
|
25 |
import org.apache.hivemind.service.ClassFabUtils;
|
|
26 |
import org.apache.hivemind.service.ClassFactory;
|
|
27 |
import org.apache.hivemind.service.MethodIterator;
|
|
28 |
import org.apache.hivemind.service.MethodSignature;
|
|
29 |
|
|
30 |
/**
|
|
31 |
* Class used to assist service extension points in creating proxies.
|
|
32 |
*
|
|
33 |
* @author Howard Lewis Ship
|
|
34 |
*/
|
|
35 |
public final class ProxyBuilder |
|
36 |
{ |
|
37 |
private ServicePoint _point;
|
|
38 |
|
|
39 |
private Class _serviceInterface;
|
|
40 |
|
|
41 |
private ClassFab _classFab;
|
|
42 |
|
|
43 |
private String _type;
|
|
44 |
|
|
45 | 967 |
public ProxyBuilder(String type, ServicePoint point)
|
46 |
{ |
|
47 | 967 |
this(type, point, false); |
48 |
} |
|
49 |
|
|
50 |
/**
|
|
51 |
* Constructs a new builder. The type will be incorporated into value returned by the
|
|
52 |
* <code>toString()</code> method. The service extension point is used to identify the service
|
|
53 |
* interface and service id.
|
|
54 |
*
|
|
55 |
* @param type
|
|
56 |
* used as part of the <code>toString()</code> method's return value
|
|
57 |
* @param point
|
|
58 |
* the service point for which this proxy is being constructed
|
|
59 |
* @param outerProxy
|
|
60 |
* if false, then the proxy can extend the service points service interface always.
|
|
61 |
* If true and the service point's declared interface is actually a bean class (not
|
|
62 |
* an interface), then the proxy will be a subclass of that bean.
|
|
63 |
*/
|
|
64 | 1928 |
public ProxyBuilder(String type, ServicePoint point, boolean outerProxy) |
65 |
{ |
|
66 | 1928 |
_point = point; |
67 | 1928 |
_type = type; |
68 | 1928 |
_serviceInterface = point.getServiceInterface(); |
69 |
|
|
70 | 1928 |
Class declaredInterface = point.getDeclaredInterface(); |
71 |
|
|
72 | 1928 |
Module module = point.getModule(); |
73 | 1928 |
ClassFactory factory = (ClassFactory) module.getService( |
74 |
"hivemind.ClassFactory",
|
|
75 |
ClassFactory.class);
|
|
76 |
|
|
77 | 1928 |
boolean extendBeanClass = outerProxy && !declaredInterface.isInterface();
|
78 | 1928 |
Class baseClass = extendBeanClass ? declaredInterface : Object.class;
|
79 |
|
|
80 | 1928 |
_classFab = factory.newClass(ClassFabUtils.generateClassName(_serviceInterface), baseClass); |
81 |
|
|
82 | 1928 |
if (!extendBeanClass)
|
83 | 1922 |
_classFab.addInterface(_serviceInterface); |
84 |
|
|
85 |
// Not exactly certain this will work with non-interface beans that already
|
|
86 |
// are serializable!
|
|
87 |
|
|
88 | 1928 |
if (outerProxy)
|
89 | 961 |
addSerializable(point.getExtensionPointId()); |
90 |
} |
|
91 |
|
|
92 |
/** @since 1.1 */
|
|
93 | 961 |
private void addSerializable(String pointId) |
94 |
{ |
|
95 | 961 |
_classFab.addInterface(Serializable.class);
|
96 |
|
|
97 | 961 |
BodyBuilder bb = new BodyBuilder();
|
98 |
|
|
99 | 961 |
bb.add( |
100 |
"return {0}.getServiceSerializationSupport().getServiceTokenForService(\"{1}\");",
|
|
101 |
ServiceSerializationHelper.class.getName(),
|
|
102 |
pointId); |
|
103 |
|
|
104 | 961 |
MethodSignature sig = new MethodSignature(Object.class, "writeReplace", null, null); |
105 |
|
|
106 | 961 |
_classFab.addMethod(Modifier.PRIVATE, sig, bb.toString()); |
107 |
} |
|
108 |
|
|
109 | 1928 |
public ClassFab getClassFab()
|
110 |
{ |
|
111 | 1928 |
return _classFab;
|
112 |
} |
|
113 |
|
|
114 |
/**
|
|
115 |
* Creates the service methods for the class.
|
|
116 |
*
|
|
117 |
* @param indirection
|
|
118 |
* the name of a variable, or a method invocation snippet, used to redirect the
|
|
119 |
* invocation on the proxy to the actual service implementation.
|
|
120 |
*/
|
|
121 | 1928 |
public void addServiceMethods(String indirection) |
122 |
{ |
|
123 | 1928 |
BodyBuilder builder = new BodyBuilder();
|
124 |
|
|
125 | 1928 |
MethodIterator mi = new MethodIterator(_serviceInterface);
|
126 |
|
|
127 | 1928 |
while (mi.hasNext())
|
128 |
{ |
|
129 | 2261 |
MethodSignature m = mi.next(); |
130 |
|
|
131 | 2261 |
builder.clear(); |
132 | 2261 |
builder.begin(); |
133 | 2261 |
builder.add("return ($r) ");
|
134 | 2261 |
builder.add(indirection); |
135 | 2261 |
builder.add(".");
|
136 | 2261 |
builder.add(m.getName()); |
137 | 2261 |
builder.addln("($$);");
|
138 | 2261 |
builder.end(); |
139 |
|
|
140 | 2261 |
_classFab.addMethod(Modifier.PUBLIC, m, builder.toString()); |
141 |
} |
|
142 |
|
|
143 | 1928 |
if (!mi.getToString())
|
144 | 1926 |
ClassFabUtils.addToStringMethod(_classFab, "<" + _type + " for " |
145 |
+ _point.getExtensionPointId() + "(" + _serviceInterface.getName() + ")>"); |
|
146 |
} |
|
147 |
} |
|