|
|||||||||||||||||||
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 |
/**
|
|
46 |
* Constructs a new builder. The type will be incorporated into value returned by the
|
|
47 |
* <code>toString()</code> method. The service extension point is used to identify the service
|
|
48 |
* interface and service id.
|
|
49 |
*/
|
|
50 | 1425 |
public ProxyBuilder(String type, ServicePoint point)
|
51 |
{ |
|
52 | 1425 |
_point = point; |
53 | 1425 |
_type = type; |
54 | 1425 |
_serviceInterface = point.getServiceInterface(); |
55 |
|
|
56 | 1425 |
Module module = point.getModule(); |
57 | 1425 |
ClassFactory factory = (ClassFactory) module.getService( |
58 |
"hivemind.ClassFactory",
|
|
59 |
ClassFactory.class);
|
|
60 |
|
|
61 | 1425 |
_classFab = factory.newClass( |
62 |
ClassFabUtils.generateClassName(_serviceInterface), |
|
63 |
Object.class);
|
|
64 |
|
|
65 | 1425 |
_classFab.addInterface(_serviceInterface); |
66 |
|
|
67 | 1425 |
addSerializable(point.getExtensionPointId()); |
68 |
} |
|
69 |
|
|
70 |
/** @since 1.1 */
|
|
71 | 1425 |
private void addSerializable(String pointId) |
72 |
{ |
|
73 | 1425 |
_classFab.addInterface(Serializable.class);
|
74 |
|
|
75 | 1425 |
BodyBuilder bb = new BodyBuilder();
|
76 |
|
|
77 | 1425 |
bb.add( |
78 |
"return {0}.getServiceSerializationSupport().getServiceTokenForService(\"{1}\");",
|
|
79 |
ServiceSerializationHelper.class.getName(),
|
|
80 |
pointId); |
|
81 |
|
|
82 | 1425 |
MethodSignature sig = new MethodSignature(Object.class, "writeReplace", null, null); |
83 |
|
|
84 | 1425 |
_classFab.addMethod(Modifier.PRIVATE, sig, bb.toString()); |
85 |
} |
|
86 |
|
|
87 | 1425 |
public ClassFab getClassFab()
|
88 |
{ |
|
89 | 1425 |
return _classFab;
|
90 |
} |
|
91 |
|
|
92 |
/**
|
|
93 |
* Creates the service methods for the class.
|
|
94 |
*
|
|
95 |
* @param indirection
|
|
96 |
* the name of a variable, or a method invocation snippet, used to redirect the
|
|
97 |
* invocation on the proxy to the actual service implementation.
|
|
98 |
*/
|
|
99 | 1425 |
public void addServiceMethods(String indirection) |
100 |
{ |
|
101 | 1425 |
BodyBuilder builder = new BodyBuilder();
|
102 |
|
|
103 | 1425 |
MethodIterator mi = new MethodIterator(_serviceInterface);
|
104 |
|
|
105 | 1425 |
while (mi.hasNext())
|
106 |
{ |
|
107 | 1648 |
MethodSignature m = mi.next(); |
108 |
|
|
109 | 1648 |
builder.clear(); |
110 | 1648 |
builder.begin(); |
111 | 1648 |
builder.add("return ($r) ");
|
112 | 1648 |
builder.add(indirection); |
113 | 1648 |
builder.add(".");
|
114 | 1648 |
builder.add(m.getName()); |
115 | 1648 |
builder.addln("($$);");
|
116 | 1648 |
builder.end(); |
117 |
|
|
118 | 1648 |
_classFab.addMethod(Modifier.PUBLIC, m, builder.toString()); |
119 |
} |
|
120 |
|
|
121 | 1425 |
if (!mi.getToString())
|
122 | 1423 |
ClassFabUtils.addToStringMethod(_classFab, "<" + _type + " for " |
123 |
+ _point.getExtensionPointId() + "(" + _serviceInterface.getName() + ")>"); |
|
124 |
} |
|
125 |
} |
|