KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > axis > client > ServiceEndpointMethodInterceptor


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.geronimo.axis.client;
18
19 import java.lang.reflect.Method JavaDoc;
20 import java.rmi.RemoteException JavaDoc;
21 import java.util.Arrays JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.Set JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.Map JavaDoc;
26 import javax.security.auth.Subject JavaDoc;
27 import javax.xml.rpc.holders.Holder JavaDoc;
28 import javax.wsdl.OperationType;
29
30 import net.sf.cglib.proxy.MethodInterceptor;
31 import net.sf.cglib.proxy.MethodProxy;
32 import org.apache.axis.client.Call;
33 import org.apache.axis.description.ParameterDesc;
34 import org.apache.axis.utils.JavaUtils;
35 import org.apache.geronimo.security.ContextManager;
36 import org.apache.geronimo.security.jaas.NamedUsernamePasswordCredential;
37
38 /**
39  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
40  */

41 public class ServiceEndpointMethodInterceptor implements MethodInterceptor {
42
43     private final GenericServiceEndpoint stub;
44     private final OperationInfo[] operations;
45     private final String JavaDoc credentialsName;
46
47     public ServiceEndpointMethodInterceptor(GenericServiceEndpoint stub, OperationInfo[] operations, String JavaDoc credentialsName) {
48         this.stub = stub;
49         this.operations = operations;
50         this.credentialsName = credentialsName;
51     }
52
53     public Object JavaDoc intercept(Object JavaDoc o, Method JavaDoc method, Object JavaDoc[] objects, MethodProxy methodProxy) throws Throwable JavaDoc {
54         int index = methodProxy.getSuperIndex();
55         OperationInfo operationInfo = operations[index];
56         if (operationInfo == null) {
57             throw new RuntimeException JavaDoc("Operation not mapped: " + method.getName() + " index: " + index + "\n OperationInfos: " + Arrays.asList(operations));
58         }
59         stub.checkCachedEndpoint();
60
61         Call call = stub.createCall();
62
63         operationInfo.prepareCall(call);
64
65         stub.setUpCall(call);
66         if (credentialsName != null) {
67             Subject JavaDoc subject = ContextManager.getNextCaller();
68             if (subject == null) {
69                 throw new IllegalStateException JavaDoc("Subject missing but authentication turned on");
70             } else {
71                 Set JavaDoc creds = subject.getPrivateCredentials(NamedUsernamePasswordCredential.class);
72                 boolean found = false;
73                 for (Iterator JavaDoc iterator = creds.iterator(); iterator.hasNext();) {
74                     NamedUsernamePasswordCredential namedUsernamePasswordCredential = (NamedUsernamePasswordCredential) iterator.next();
75                     if (credentialsName.equals(namedUsernamePasswordCredential.getName())) {
76                         call.setUsername(namedUsernamePasswordCredential.getUsername());
77                         call.setPassword(new String JavaDoc(namedUsernamePasswordCredential.getPassword()));
78                         found = true;
79                         break;
80                     }
81                 }
82                 if (!found) {
83                     throw new IllegalStateException JavaDoc("no NamedUsernamePasswordCredential found for name " + credentialsName);
84                 }
85             }
86         }
87         Object JavaDoc response = null;
88         List JavaDoc parameterDescs = operationInfo.getOperationDesc().getParameters();
89         Object JavaDoc[] unwrapped = extractFromHolders(objects, parameterDescs, operationInfo.getOperationDesc().getNumInParams());
90         if (operationInfo.getOperationDesc().getMep() == OperationType.REQUEST_RESPONSE) {
91             try {
92                 response = call.invoke(unwrapped);
93             } catch (RemoteException JavaDoc e) {
94                 throw operationInfo.unwrapFault(e);
95             }
96
97             if (response instanceof RemoteException JavaDoc) {
98                 throw operationInfo.unwrapFault((RemoteException JavaDoc) response);
99             } else {
100                 stub.extractAttachments(call);
101                 Map JavaDoc outputParameters = call.getOutputParams();
102                 putInHolders(outputParameters, objects, parameterDescs);
103                 Class JavaDoc returnType = operationInfo.getOperationDesc().getReturnClass();
104                 //return type should never be null... but we are not objecting if wsdl-return-value-mapping is not set.
105
if (response == null || returnType == null || returnType.isAssignableFrom(response.getClass())) {
106                     return response;
107                 } else {
108                     return JavaUtils.convert(response, returnType);
109                 }
110             }
111         } else if (operationInfo.getOperationDesc().getMep() == OperationType.ONE_WAY) {
112             //one way
113
call.invokeOneWay(unwrapped);
114             return null;
115         } else {
116             throw new RuntimeException JavaDoc("Invalid messaging style: " + operationInfo.getOperationDesc().getMep());
117         }
118     }
119
120     private Object JavaDoc[] extractFromHolders(Object JavaDoc[] objects, List JavaDoc parameterDescs, int inParameterCount) throws JavaUtils.HolderException {
121         if (objects.length != parameterDescs.size()) {
122             throw new IllegalArgumentException JavaDoc("Mismatch parameter count: expected: " + parameterDescs.size() + ", actual: " + objects.length);
123         }
124         Object JavaDoc[] unwrapped = new Object JavaDoc[inParameterCount];
125         int j = 0;
126         for (int i = 0; objects != null && i < objects.length; i++) {
127             Object JavaDoc parameter = objects[i];
128             ParameterDesc parameterDesc = (ParameterDesc) parameterDescs.get(i);
129
130             if (parameterDesc.getMode() == ParameterDesc.INOUT) {
131                 unwrapped[j++] = JavaUtils.getHolderValue((Holder JavaDoc) parameter);
132             } else if (parameterDesc.getMode() == ParameterDesc.IN) {
133                 unwrapped[j++] = parameter;
134             }
135         }
136         return unwrapped;
137     }
138
139     private void putInHolders(Map JavaDoc outputParameters, Object JavaDoc[] objects, List JavaDoc parameterDescs) throws JavaUtils.HolderException {
140         for (int i = 0; i < objects.length; i++) {
141             Object JavaDoc parameter = objects[i];
142             ParameterDesc parameterDesc = (ParameterDesc) parameterDescs.get(i);
143             if ((parameterDesc.getMode() == ParameterDesc.INOUT) ||
144                     (parameterDesc.getMode() == ParameterDesc.OUT)) {
145                 Object JavaDoc returned = outputParameters.get(parameterDesc.getQName());
146                 if (returned instanceof Holder JavaDoc) {
147                     //TODO this must be a bug somewhere!!!!
148
returned = JavaUtils.getHolderValue(returned);
149                 }
150                 JavaUtils.setHolderValue((Holder JavaDoc) parameter, returned);
151             }
152         }
153     }
154
155 }
156
Popular Tags