1 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.ser.ServiceSerializationHelper; 22 import org.apache.hivemind.service.BodyBuilder; 23 import org.apache.hivemind.service.ClassFab; 24 import org.apache.hivemind.service.ClassFabUtils; 25 import org.apache.hivemind.service.ClassFactory; 26 import org.apache.hivemind.service.MethodIterator; 27 import org.apache.hivemind.service.MethodSignature; 28 29 34 public final class ProxyBuilder 35 { 36 private Class _serviceInterface; 37 38 private ClassFab _classFab; 39 40 private String _type; 41 42 60 public ProxyBuilder(String type, Module module, Class serviceInterface, Class declaredInterface, boolean outerProxy) 61 { 62 _type = type; 63 _serviceInterface = serviceInterface; 64 65 ClassFactory factory = (ClassFactory) module.getService( 66 "hivemind.ClassFactory", 67 ClassFactory.class); 68 69 boolean extendBeanClass = outerProxy && !declaredInterface.isInterface(); 70 Class baseClass = extendBeanClass ? declaredInterface : Object .class; 71 72 _classFab = factory.newClass(ClassFabUtils.generateClassName(_serviceInterface), baseClass); 73 74 _classFab.addField("_extensionPointId", String .class); 75 76 _classFab.addConstructor(new Class [] { String .class }, null, "{ _extensionPointId = $1; }"); 77 78 if (!extendBeanClass) 79 _classFab.addInterface(_serviceInterface); 80 81 84 if (outerProxy) 85 addSerializable(); 86 } 87 88 89 private void addSerializable() 90 { 91 _classFab.addInterface(Serializable .class); 92 93 BodyBuilder bb = new BodyBuilder(); 94 95 bb.add( 96 "return {0}.getServiceSerializationSupport().getServiceTokenForService(_extensionPointId);", 97 ServiceSerializationHelper.class.getName()); 98 99 MethodSignature sig = new MethodSignature(Object .class, "writeReplace", null, null); 100 101 _classFab.addMethod(Modifier.PRIVATE, sig, bb.toString()); 102 } 103 104 public ClassFab getClassFab() 105 { 106 return _classFab; 107 } 108 109 112 public void addServiceMethods(String indirection) 113 { 114 addServiceMethods(indirection, true); 115 } 116 117 126 public void addServiceMethods(String indirection, boolean addToString) 127 { 128 BodyBuilder builder = new BodyBuilder(); 129 130 MethodIterator mi = new MethodIterator(_serviceInterface); 131 while (mi.hasNext()) 132 { 133 MethodSignature m = mi.next(); 134 if( !_classFab.containsMethod( m ) ) 135 { 136 builder.clear(); 137 builder.begin(); 138 builder.add("return ($r) "); 139 builder.add(indirection); 140 builder.add("."); 141 builder.add(m.getName()); 142 builder.addln("($$);"); 143 builder.end(); 144 _classFab.addMethod(Modifier.PUBLIC, m, builder.toString()); 145 } 146 } 147 148 if (!mi.getToString() && addToString) 149 ClassFabUtils.addToStringMethod(_classFab, "<" + _type + " for \" + _extensionPointId + \"" 150 + "(" + _serviceInterface.getName() + ")>"); 151 } 152 } | Popular Tags |