1 17 package org.apache.servicemix.jsr181.xfire; 18 19 import java.lang.reflect.InvocationHandler ; 20 import java.lang.reflect.Method ; 21 import java.lang.reflect.Proxy ; 22 23 import javax.jbi.JBIException; 24 import javax.jbi.component.ComponentContext; 25 import javax.naming.InitialContext ; 26 import javax.xml.namespace.QName ; 27 28 import org.apache.servicemix.client.ClientFactory; 29 import org.apache.servicemix.client.DefaultServiceMixClient; 30 import org.apache.servicemix.client.ServiceMixClient; 31 import org.apache.servicemix.client.ServiceMixClientFacade; 32 import org.apache.servicemix.jbi.container.JBIContainer; 33 import org.apache.servicemix.jsr181.Jsr181LifeCycle; 34 import org.codehaus.xfire.XFire; 35 import org.springframework.beans.factory.FactoryBean; 36 37 38 46 public class JbiProxyFactoryBean implements FactoryBean { 47 48 private String name = ClientFactory.DEFAULT_JNDI_NAME; 49 private JBIContainer container; 50 private ClientFactory factory; 51 private ComponentContext context; 52 private Class type; 53 private Object proxy; 54 private InvocationHandler jbiInvocationHandler; 55 private QName service; 56 private QName interfaceName; 57 private String endpoint; 58 59 public Object getObject() throws Exception { 60 if( proxy == null ) { 61 proxy = createProxy(); 62 } 63 return proxy; 64 } 65 66 private Object createProxy() throws Exception { 67 68 return Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), new Class []{type}, new InvocationHandler (){ 69 public Object invoke(Object proxy, Method method, Object [] args) throws Throwable { 70 InvocationHandler next = getJBIInvocationHandler(); 71 return next.invoke(proxy, method, args); 72 } 73 }); 74 75 } 76 77 synchronized private InvocationHandler getJBIInvocationHandler() throws Exception { 78 if( jbiInvocationHandler == null ) { 79 XFire xfire = Jsr181LifeCycle.createXFire(getInternalContext()); 80 Object o = JbiProxy.create(xfire, context, interfaceName, service, endpoint, type); 81 jbiInvocationHandler = Proxy.getInvocationHandler(o); 82 } 83 return jbiInvocationHandler; 84 } 85 86 public Class getObjectType() { 87 return type; 88 } 89 90 public boolean isSingleton() { 91 return true; 92 } 93 94 protected ComponentContext getInternalContext() throws Exception { 95 if (context == null) { 96 if (factory == null) { 97 if (context != null) { 98 factory = new ClientFactory() { 99 public ServiceMixClient createClient() throws JBIException { 100 return new ServiceMixClientFacade(context); 101 } 102 }; 103 } else if (container != null) { 104 factory = container.getClientFactory(); 105 } else { 106 factory = (ClientFactory) new InitialContext ().lookup(name); 107 } 108 } 109 DefaultServiceMixClient client = new DefaultServiceMixClient(container); 110 context = client.getContext(); 111 } 112 return context; 113 } 114 115 public Class getType() { 116 return type; 117 } 118 119 public void setType(Class type) { 120 this.type = type; 121 } 122 123 public String getEndpoint() { 124 return endpoint; 125 } 126 127 public void setEndpoint(String endpointName) { 128 this.endpoint = endpointName; 129 } 130 131 public QName getInterfaceName() { 132 return interfaceName; 133 } 134 135 public void setInterfaceName(QName interfaceName) { 136 this.interfaceName = interfaceName; 137 } 138 139 public QName getService() { 140 return service; 141 } 142 143 public void setService(QName service) { 144 this.service = service; 145 } 146 147 150 public ComponentContext getContext() { 151 return context; 152 } 153 154 157 public void setContext(ComponentContext context) { 158 this.context = context; 159 } 160 161 164 public JBIContainer getContainer() { 165 return container; 166 } 167 168 171 public void setContainer(JBIContainer container) { 172 this.container = container; 173 } 174 175 178 public ClientFactory getFactory() { 179 return factory; 180 } 181 182 185 public void setFactory(ClientFactory factory) { 186 this.factory = factory; 187 } 188 189 192 public String getName() { 193 return name; 194 } 195 196 199 public void setName(String name) { 200 this.name = name; 201 } 202 203 } 204 | Popular Tags |