KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > sca > handler > ExternalJbiServiceTargetInvoker


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.handler;
18
19 import java.lang.reflect.InvocationTargetException JavaDoc;
20 import java.lang.reflect.Method JavaDoc;
21
22 import org.apache.tuscany.core.context.ExternalServiceContext;
23 import org.apache.tuscany.core.context.InstanceContext;
24 import org.apache.tuscany.core.context.QualifiedName;
25 import org.apache.tuscany.core.context.ScopeContext;
26 import org.apache.tuscany.core.context.TargetException;
27 import org.apache.tuscany.core.invocation.Interceptor;
28 import org.apache.tuscany.core.invocation.TargetInvoker;
29 import org.apache.tuscany.core.message.Message;
30
31 public class ExternalJbiServiceTargetInvoker implements TargetInvoker {
32
33     private QualifiedName serviceName;
34     private String JavaDoc esName;
35     private Method JavaDoc method;
36     private ScopeContext container;
37
38     private ExternalServiceContext context;
39
40     /**
41      * Constructs a new ExternalJbiServiceTargetInvoker.
42      * @param esName
43      * @param container
44      */

45     public ExternalJbiServiceTargetInvoker(QualifiedName serviceName, Method JavaDoc method, ScopeContext container) {
46         assert (serviceName != null) : "No service name specified";
47         assert (method != null) : "No method specified";
48         assert (container != null) : "No scope container specified";
49         this.serviceName = serviceName;
50         this.esName = serviceName.getPartName();
51         this.method = method;
52         this.container = container;
53     }
54
55     public Object JavaDoc invokeTarget(Object JavaDoc payload) throws InvocationTargetException JavaDoc {
56         if (context == null) {
57             InstanceContext iContext = container.getContext(esName);
58             if (!(iContext instanceof ExternalServiceContext)) {
59                 TargetException JavaDoc te = new TargetException JavaDoc("Unexpected target context type");
60                 te.setIdentifier(iContext.getClass().getName());
61                 te.addContextName(iContext.getName());
62                 throw te;
63             }
64             context = (ExternalServiceContext) iContext;
65         }
66         ExternalJbiServiceClient client = (ExternalJbiServiceClient) context.getImplementationInstance(true);
67         if (payload != null) {
68             return client.invoke(method, (Object JavaDoc[])payload);
69         } else {
70             return client.invoke(method, null);
71         }
72     }
73
74     public boolean isCacheable() {
75         return false;
76     }
77
78     public Message invoke(Message msg) {
79         try {
80             Object JavaDoc resp = invokeTarget(msg.getBody());
81             msg.setBody(resp);
82         } catch (InvocationTargetException JavaDoc e) {
83             msg.setBody(e.getCause());
84         } catch (Throwable JavaDoc e) {
85             msg.setBody(e);
86         }
87         return msg;
88     }
89
90     public void setNext(Interceptor next) {
91         throw new UnsupportedOperationException JavaDoc();
92     }
93
94     public Object JavaDoc clone() {
95         try {
96             ExternalJbiServiceTargetInvoker invoker = (ExternalJbiServiceTargetInvoker) super.clone();
97             invoker.container = container;
98             invoker.context = this.context;
99             invoker.esName = this.esName;
100             invoker.method = this.method;
101             invoker.serviceName = this.serviceName;
102             return invoker;
103         } catch (CloneNotSupportedException JavaDoc e) {
104             return null; // will not happen
105
}
106     }
107     
108 }
109
Popular Tags