1 17 package org.apache.servicemix.sca.builder; 18 19 import java.lang.reflect.Method ; 20 import java.util.Collection ; 21 import java.util.Map ; 22 import java.util.Set ; 23 24 import org.apache.servicemix.sca.assembly.JbiBinding; 25 import org.apache.servicemix.sca.config.ExternalJbiServiceContextFactory; 26 import org.apache.servicemix.sca.handler.ExternalJbiServiceClient; 27 import org.apache.tuscany.core.builder.BuilderException; 28 import org.apache.tuscany.core.builder.ContextFactoryBuilder; 29 import org.apache.tuscany.core.config.JavaIntrospectionHelper; 30 import org.apache.tuscany.core.context.QualifiedName; 31 import org.apache.tuscany.core.injection.SingletonObjectFactory; 32 import org.apache.tuscany.core.invocation.InvocationConfiguration; 33 import org.apache.tuscany.core.invocation.MethodHashMap; 34 import org.apache.tuscany.core.invocation.ProxyConfiguration; 35 import org.apache.tuscany.core.invocation.impl.InvokerInterceptor; 36 import org.apache.tuscany.core.invocation.spi.ProxyFactory; 37 import org.apache.tuscany.core.invocation.spi.ProxyFactoryFactory; 38 import org.apache.tuscany.core.message.MessageFactory; 39 import org.apache.tuscany.core.runtime.RuntimeContext; 40 import org.apache.tuscany.core.system.annotation.Autowire; 41 import org.apache.tuscany.model.assembly.AssemblyModelObject; 42 import org.apache.tuscany.model.assembly.ConfiguredService; 43 import org.apache.tuscany.model.assembly.ExternalService; 44 import org.apache.tuscany.model.assembly.Service; 45 import org.apache.tuscany.model.assembly.ServiceContract; 46 import org.apache.tuscany.model.scdl.WebServiceBinding; 47 import org.osoa.sca.annotations.Init; 48 import org.osoa.sca.annotations.Scope; 49 50 53 @Scope("MODULE") 54 public class ExternalJbiServiceBuilder implements ContextFactoryBuilder { 55 56 private RuntimeContext runtimeContext; 57 58 private ProxyFactoryFactory proxyFactoryFactory; 59 60 private MessageFactory messageFactory; 61 62 private ContextFactoryBuilder policyBuilder; 63 64 public ExternalJbiServiceBuilder() { 65 } 66 67 @Init(eager = true) 68 public void init() { 69 runtimeContext.addBuilder(this); 70 } 71 72 75 @Autowire 76 public void setRuntimeContext(RuntimeContext runtimeContext) { 77 this.runtimeContext = runtimeContext; 78 } 79 80 83 @Autowire 84 public void setProxyFactoryFactory(ProxyFactoryFactory factory) { 85 this.proxyFactoryFactory = factory; 86 } 87 88 93 @Autowire 94 public void setMessageFactory(MessageFactory msgFactory) { 95 this.messageFactory = msgFactory; 96 } 97 98 105 public void setPolicyBuilder(ContextFactoryBuilder builder) { 106 policyBuilder = builder; 107 } 108 109 public void build(AssemblyModelObject object) throws BuilderException { 110 if (!(object instanceof ExternalService)) { 111 return; 112 } 113 ExternalService externalService = (ExternalService) object; 114 if (externalService.getBindings().size() < 1 || !(externalService.getBindings().get(0) instanceof JbiBinding)) { 115 return; 116 } 117 118 ExternalJbiServiceClient externalJbiServiceClient = new ExternalJbiServiceClient(externalService); 119 ExternalJbiServiceContextFactory config = new ExternalJbiServiceContextFactory(externalService.getName(), new SingletonObjectFactory<ExternalJbiServiceClient>(externalJbiServiceClient)); 120 121 ConfiguredService configuredService = externalService.getConfiguredService(); 122 Service service = configuredService.getService(); 123 ServiceContract serviceContract = service.getServiceContract(); 124 Map <Method , InvocationConfiguration> iConfigMap = new MethodHashMap(); 125 ProxyFactory proxyFactory = proxyFactoryFactory.createProxyFactory(); 126 Set <Method > javaMethods = JavaIntrospectionHelper.getAllUniqueMethods(serviceContract.getInterface()); 127 for (Method method : javaMethods) { 128 InvocationConfiguration iConfig = new InvocationConfiguration(method); 129 iConfigMap.put(method, iConfig); 130 } 131 QualifiedName qName = new QualifiedName(externalService.getName() + "/" + service.getName()); 132 ProxyConfiguration pConfiguration = new ProxyConfiguration(qName, iConfigMap, serviceContract.getInterface().getClassLoader(), messageFactory); 133 proxyFactory.setBusinessInterface(serviceContract.getInterface()); 134 proxyFactory.setProxyConfiguration(pConfiguration); 135 config.addTargetProxyFactory(service.getName(), proxyFactory); 136 configuredService.setProxyFactory(proxyFactory); 137 if (policyBuilder != null) { 138 policyBuilder.build(configuredService); 140 } 141 for (InvocationConfiguration iConfig : (Collection <InvocationConfiguration>) iConfigMap.values()) { 143 iConfig.addTargetInterceptor(new InvokerInterceptor()); 144 } 145 146 externalService.getConfiguredService().setContextFactory(config); 147 } 148 149 } 150 | Popular Tags |