1 15 package org.apache.hivemind.service.impl; 16 17 import java.lang.reflect.Method ; 18 19 import net.sf.cglib.proxy.Callback; 20 import net.sf.cglib.proxy.CallbackFilter; 21 import net.sf.cglib.proxy.Enhancer; 22 import net.sf.cglib.proxy.FixedValue; 23 import net.sf.cglib.proxy.NoOp; 24 25 import org.apache.hivemind.Location; 26 import org.apache.hivemind.definition.ImplementationConstructionContext; 27 import org.apache.hivemind.internal.AbstractServiceImplementationConstructor; 28 29 33 public class CglibBeanInterfaceFactory extends AbstractServiceImplementationConstructor 34 { 35 36 public CglibBeanInterfaceFactory(Location location, String contributingModuleId) 37 { 38 super(location); 39 } 40 41 public Object constructCoreServiceImplementation(ImplementationConstructionContext context) 42 { 43 return createCglibBean(); 44 } 45 46 47 50 public static BeanInterface createCglibBean() { 51 Enhancer enhancer = new Enhancer(); 52 enhancer.setClassLoader( BeanInterface.class.getClassLoader() ); 53 enhancer.setInterfaces( new Class [] { BeanInterface.class } ); 54 enhancer.setCallbackFilter( new InterfaceMethodFilter() ); 55 enhancer.setCallbacks( new Callback[] { new FixedValue() 56 { 57 public Object loadObject() throws Exception { 58 59 return "Hello, World!"; 60 } 61 62 }, NoOp.INSTANCE } ); 63 64 return (BeanInterface)enhancer.create(); 65 } 66 67 68 private static class InterfaceMethodFilter implements CallbackFilter 69 { 70 71 public int accept(Method method) 72 { 73 if( method.getName().equals( "interfaceMethod" ) ) 74 { 75 return 0; 76 } 77 return 1; 78 } 79 80 } 81 82 } 83 | Popular Tags |