KickJava   Java API By Example, From Geeks To Geeks.

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


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.HashMap JavaDoc;
22 import java.util.Map JavaDoc;
23 import java.util.Set JavaDoc;
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     /**
72      * @param runtimeContext The runtimeContext to set.
73      */

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

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

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

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 JavaDoc<Method JavaDoc, InvocationConfiguration> iConfigMap = new HashMap JavaDoc<Method JavaDoc, InvocationConfiguration>();
123         ProxyFactory proxyFactory = proxyFactoryFactory.createProxyFactory();
124         Set JavaDoc<Method JavaDoc> javaMethods = JavaIntrospectionHelper.getAllUniqueMethods(serviceContract.getInterface());
125         for (Method JavaDoc 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             // invoke the reference builder to handle additional policy metadata
137
policyBuilder.build(configuredService);
138         }
139         // add tail interceptor
140
for (InvocationConfiguration iConfig : (Collection JavaDoc<InvocationConfiguration>) iConfigMap.values()) {
141             iConfig.addTargetInterceptor(new EntryPointInvokerInterceptor());
142         }
143         entryPoint.getConfiguredReference().setContextFactory(config);
144     }
145     
146     //FIXME same as the InvokerInterceptor except that it doesn't throw an exception in setNext
147
// For some reason another InvokerInterceptor is added after this one, need Jim to look into it
148
// and figure out why.
149
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