1 15 package org.apache.hivemind.impl; 16 17 import java.lang.reflect.Constructor ; 18 import java.lang.reflect.Modifier ; 19 20 import org.apache.hivemind.ApplicationRuntimeException; 21 import org.apache.hivemind.events.RegistryShutdownListener; 22 import org.apache.hivemind.internal.ServiceModel; 23 import org.apache.hivemind.internal.ServicePoint; 24 import org.apache.hivemind.service.BodyBuilder; 25 import org.apache.hivemind.service.ClassFab; 26 import org.apache.hivemind.service.ClassFabUtils; 27 import org.apache.hivemind.service.MethodSignature; 28 import org.apache.hivemind.util.ConstructorUtils; 29 30 36 public final class ProxyUtils 37 { 38 public static final String SERVICE_ACCESSOR_METHOD_NAME = "_service"; 39 40 public static final String DELEGATE_ACCESSOR_METHOD_NAME = "_delegate"; 41 42 private ProxyUtils() 43 { 44 } 46 47 53 public static Object createDelegatingProxy(String type, ServiceModel serviceModel, 54 String delegationMethodName, ServicePoint servicePoint) 55 { 56 ProxyBuilder builder = new ProxyBuilder(type, servicePoint.getModule(), 57 servicePoint.getServiceInterface(), servicePoint.getDeclaredInterface(), false); 58 59 ClassFab classFab = builder.getClassFab(); 60 61 addConstructor(classFab, serviceModel); 62 63 addServiceAccessor(classFab, delegationMethodName, servicePoint); 64 65 builder.addServiceMethods(SERVICE_ACCESSOR_METHOD_NAME + "()"); 66 67 Class proxyClass = classFab.createClass(); 68 69 try 70 { 71 Constructor c = proxyClass.getConstructor(new Class [] 72 { String .class, serviceModel.getClass() }); 73 74 return c.newInstance(new Object [] 75 { servicePoint.getExtensionPointId(), serviceModel }); 76 } 77 catch (Exception ex) 78 { 79 throw new ApplicationRuntimeException(ex); 80 } 81 } 82 83 97 98 public static RegistryShutdownListener createOuterProxy(Object delegate, 99 ServicePoint servicePoint) 100 { 101 ProxyBuilder builder = new ProxyBuilder("OuterProxy", servicePoint.getModule(), 102 servicePoint.getServiceInterface(), servicePoint.getDeclaredInterface(), true); 103 104 ClassFab classFab = builder.getClassFab(); 105 106 addDelegateAccessor(classFab, servicePoint, delegate); 107 108 builder.addServiceMethods(DELEGATE_ACCESSOR_METHOD_NAME + "()"); 109 110 Class proxyClass = classFab.createClass(); 111 112 try 113 { 114 return (RegistryShutdownListener) ConstructorUtils.invokeConstructor( 115 proxyClass, 116 new Object [] 117 { servicePoint.getExtensionPointId(), delegate }); 118 } 119 catch (Exception ex) 120 { 121 throw new ApplicationRuntimeException(ex); 122 } 123 } 124 125 126 127 private static void addDelegateAccessor(ClassFab classFab, ServicePoint servicePoint, 128 Object delegate) 129 { 130 classFab.addField("_shutdown", boolean.class); 131 132 Class delegateClass = ClassFabUtils.getInstanceClass(classFab, delegate, servicePoint 133 .getServiceInterface()); 134 135 classFab.addField("_delegate", delegateClass); 136 137 classFab.addConstructor(new Class [] 138 { String .class, delegateClass }, null, "{ this($1); _delegate = $2; }"); 139 140 classFab.addInterface(RegistryShutdownListener.class); 141 if( RegistryShutdownListener.class.isAssignableFrom( delegateClass ) ) 142 { 143 classFab.addMethod(Modifier.PUBLIC | Modifier.FINAL, new MethodSignature(void.class, 144 "registryDidShutdown", null, null), "{ _delegate.registryDidShutdown(); _delegate = null; _shutdown = true; }"); 145 } 146 else 147 { 148 classFab.addMethod(Modifier.PUBLIC | Modifier.FINAL, new MethodSignature(void.class, 149 "registryDidShutdown", null, null), "{ _delegate = null; _shutdown = true; }"); 150 } 151 BodyBuilder builder = new BodyBuilder(); 152 153 builder.begin(); 154 155 builder.addln("if (_shutdown)"); 156 builder.addln(" throw org.apache.hivemind.HiveMind#createRegistryShutdownException();"); 157 158 builder.add("return _delegate;"); 159 160 builder.end(); 161 162 classFab.addMethod(Modifier.FINAL | Modifier.PRIVATE, new MethodSignature(delegateClass, 163 DELEGATE_ACCESSOR_METHOD_NAME, null, null), builder.toString()); 164 } 165 166 170 private static void addConstructor(ClassFab classFab, ServiceModel model) 171 { 172 Class modelClass = model.getClass(); 173 174 classFab.addField("_serviceModel", modelClass); 175 176 classFab.addConstructor(new Class [] 177 { String .class, modelClass }, null, "{ this($1); _serviceModel = $2; }"); 178 } 179 180 184 private static void addServiceAccessor(ClassFab classFab, String serviceModelMethodName, 185 ServicePoint servicePoint) 186 { 187 Class serviceInterface = servicePoint.getServiceInterface(); 188 189 classFab.addField(SERVICE_ACCESSOR_METHOD_NAME, serviceInterface); 190 191 BodyBuilder builder = new BodyBuilder(); 192 builder.begin(); 193 194 builder.add("return ("); 195 builder.add(serviceInterface.getName()); 196 builder.add(") _serviceModel."); 197 builder.add(serviceModelMethodName); 198 builder.add("();"); 199 200 builder.end(); 201 202 classFab.addMethod(Modifier.PRIVATE | Modifier.FINAL, new MethodSignature(serviceInterface, 203 SERVICE_ACCESSOR_METHOD_NAME, null, null), builder.toString()); 204 } 205 } | Popular Tags |