1 17 package org.apache.servicemix.sca.builder; 18 19 import java.lang.reflect.Method ; 20 import java.util.Collection ; 21 import java.util.HashMap ; 22 import java.util.Map ; 23 import java.util.Set ; 24 25 import org.apache.servicemix.sca.assembly.JbiBinding; 26 import org.apache.servicemix.sca.config.JbiServiceEntryPointContextFactory; 27 import org.apache.tuscany.core.builder.BuilderException; 28 import org.apache.tuscany.core.builder.ContextFactoryBuilder; 29 import org.apache.tuscany.core.builder.impl.EntryPointContextFactory; 30 import org.apache.tuscany.core.config.JavaIntrospectionHelper; 31 import org.apache.tuscany.core.context.AggregateContext; 32 import org.apache.tuscany.core.context.QualifiedName; 33 import org.apache.tuscany.core.invocation.Interceptor; 34 import org.apache.tuscany.core.invocation.InvocationConfiguration; 35 import org.apache.tuscany.core.invocation.InvocationRuntimeException; 36 import org.apache.tuscany.core.invocation.ProxyConfiguration; 37 import org.apache.tuscany.core.invocation.TargetInvoker; 38 import org.apache.tuscany.core.invocation.spi.ProxyFactory; 39 import org.apache.tuscany.core.invocation.spi.ProxyFactoryFactory; 40 import org.apache.tuscany.core.message.Message; 41 import org.apache.tuscany.core.message.MessageFactory; 42 import org.apache.tuscany.core.runtime.RuntimeContext; 43 import org.apache.tuscany.core.system.annotation.Autowire; 44 import org.apache.tuscany.model.assembly.AssemblyModelObject; 45 import org.apache.tuscany.model.assembly.ConfiguredService; 46 import org.apache.tuscany.model.assembly.EntryPoint; 47 import org.apache.tuscany.model.assembly.Service; 48 import org.apache.tuscany.model.assembly.ServiceContract; 49 import org.osoa.sca.annotations.Init; 50 import org.osoa.sca.annotations.Scope; 51 52 @Scope("MODULE") 53 public class JbiServiceEntryPointBuilder implements ContextFactoryBuilder<AggregateContext> { 54 55 private RuntimeContext runtimeContext; 56 57 private ProxyFactoryFactory proxyFactoryFactory; 58 59 private MessageFactory messageFactory; 60 61 private ContextFactoryBuilder policyBuilder; 62 63 public JbiServiceEntryPointBuilder() { 64 } 65 66 @Init(eager = true) 67 public void init() { 68 runtimeContext.addBuilder(this); 69 } 70 71 74 @Autowire 75 public void setRuntimeContext(RuntimeContext runtimeContext) { 76 this.runtimeContext = runtimeContext; 77 } 78 79 82 @Autowire 83 public void setProxyFactoryFactory(ProxyFactoryFactory factory) { 84 this.proxyFactoryFactory = factory; 85 } 86 87 92 @Autowire 93 public void setMessageFactory(MessageFactory msgFactory) { 94 this.messageFactory = msgFactory; 95 } 96 97 104 public void setPolicyBuilder(ContextFactoryBuilder builder) { 105 policyBuilder = builder; 106 } 107 108 public void build(AssemblyModelObject object) throws BuilderException { 109 if (!(object instanceof EntryPoint)) { 110 return; 111 } 112 EntryPoint entryPoint = (EntryPoint) object; 113 if (entryPoint.getBindings().size() < 1 || !(entryPoint.getBindings().get(0) instanceof JbiBinding)) { 114 return; 115 } 116 117 EntryPointContextFactory config = new JbiServiceEntryPointContextFactory(entryPoint.getName(), entryPoint.getConfiguredService().getService().getName(), messageFactory); 118 119 ConfiguredService configuredService = entryPoint.getConfiguredService(); 120 Service service = configuredService.getService(); 121 ServiceContract serviceContract = service.getServiceContract(); 122 Map <Method , InvocationConfiguration> iConfigMap = new HashMap <Method , InvocationConfiguration>(); 123 ProxyFactory proxyFactory = proxyFactoryFactory.createProxyFactory(); 124 Set <Method > javaMethods = JavaIntrospectionHelper.getAllUniqueMethods(serviceContract.getInterface()); 125 for (Method method : javaMethods) { 126 InvocationConfiguration iConfig = new InvocationConfiguration(method); 127 iConfigMap.put(method, iConfig); 128 } 129 QualifiedName qName = new QualifiedName(entryPoint.getConfiguredReference().getTargetConfiguredServices().get(0).getAggregatePart().getName() + "/" + service.getName()); 130 ProxyConfiguration pConfiguration = new ProxyConfiguration(qName, iConfigMap, serviceContract.getInterface().getClassLoader(), messageFactory); 131 proxyFactory.setBusinessInterface(serviceContract.getInterface()); 132 proxyFactory.setProxyConfiguration(pConfiguration); 133 config.addSourceProxyFactory(service.getName(), proxyFactory); 134 configuredService.setProxyFactory(proxyFactory); 135 if (policyBuilder != null) { 136 policyBuilder.build(configuredService); 138 } 139 for (InvocationConfiguration iConfig : (Collection <InvocationConfiguration>) iConfigMap.values()) { 141 iConfig.addTargetInterceptor(new EntryPointInvokerInterceptor()); 142 } 143 entryPoint.getConfiguredReference().setContextFactory(config); 144 } 145 146 public class EntryPointInvokerInterceptor implements Interceptor { 150 151 public EntryPointInvokerInterceptor() { 152 } 153 154 public Message invoke(Message msg) throws InvocationRuntimeException { 155 TargetInvoker invoker = msg.getTargetInvoker(); 156 if (invoker == null) { 157 throw new InvocationRuntimeException("No target invoker specified on message"); 158 } 159 return invoker.invoke(msg); 160 } 161 162 public void setNext(Interceptor next) { 163 } 164 165 } 166 167 } 168 | Popular Tags |