KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > sca > builder > ExternalJbiServiceBuilder


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.sca.builder;
18
19 import java.lang.reflect.Method JavaDoc;
20 import java.util.Collection JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.Set JavaDoc;
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 /**
51  * Creates a <code>RuntimeConfigurationBuilder</code> for an external service configured with the {@link WebServiceBinding}
52  */

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     /**
73      * @param runtimeContext The runtimeContext to set.
74      */

75     @Autowire
76     public void setRuntimeContext(RuntimeContext runtimeContext) {
77         this.runtimeContext = runtimeContext;
78     }
79
80     /**
81      * Sets the factory used to construct proxies implmementing the business interface required by a reference
82      */

83     @Autowire
84     public void setProxyFactoryFactory(ProxyFactoryFactory factory) {
85         this.proxyFactoryFactory = factory;
86     }
87
88     /**
89      * Sets the factory used to construct invocation messages
90      *
91      * @param msgFactory
92      */

93     @Autowire
94     public void setMessageFactory(MessageFactory msgFactory) {
95         this.messageFactory = msgFactory;
96     }
97
98     /**
99      * Sets a builder responsible for creating source-side and target-side invocation chains for a reference. The
100      * reference builder may be hierarchical, containing other child reference builders that operate on specific
101      * metadata used to construct and invocation chain.
102      *
103      * @see org.apache.tuscany.core.builder.impl.HierarchicalBuilder
104      */

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 JavaDoc<Method JavaDoc, InvocationConfiguration> iConfigMap = new MethodHashMap();
125         ProxyFactory proxyFactory = proxyFactoryFactory.createProxyFactory();
126         Set JavaDoc<Method JavaDoc> javaMethods = JavaIntrospectionHelper.getAllUniqueMethods(serviceContract.getInterface());
127         for (Method JavaDoc 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             // invoke the reference builder to handle additional policy metadata
139
policyBuilder.build(configuredService);
140         }
141         // add tail interceptor
142
for (InvocationConfiguration iConfig : (Collection JavaDoc<InvocationConfiguration>) iConfigMap.values()) {
143             iConfig.addTargetInterceptor(new InvokerInterceptor());
144         }
145
146         externalService.getConfiguredService().setContextFactory(config);
147     }
148
149 }
150
Popular Tags