Save This Page
Home » openejb-3.1.2-src » org.apache » openejb » server » axis » client » [javadoc | source]
    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.openejb.server.axis.client;
   18   
   19   import net.sf.cglib.proxy.MethodInterceptor;
   20   import net.sf.cglib.proxy.MethodProxy;
   21   import org.apache.axis.client.Call;
   22   import org.apache.axis.description.ParameterDesc;
   23   import org.apache.axis.utils.JavaUtils;
   24   import org.apache.openejb.server.webservices.saaj.SaajUniverse;
   25   
   26   import javax.wsdl.OperationType;
   27   import javax.xml.rpc.holders.Holder;
   28   import java.lang.reflect.Method;
   29   import java.rmi.RemoteException;
   30   import java.util.Arrays;
   31   import java.util.List;
   32   import java.util.Map;
   33   
   34   public class ServiceEndpointMethodInterceptor implements MethodInterceptor {
   35       private final GenericServiceEndpoint stub;
   36       private final OperationInfo[] operations;
   37       private final String credentialsName;
   38   
   39       public ServiceEndpointMethodInterceptor(GenericServiceEndpoint stub, OperationInfo[] operations, String credentialsName) {
   40           this.stub = stub;
   41           this.operations = operations;
   42           this.credentialsName = credentialsName;
   43       }
   44   
   45       public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
   46           SaajUniverse universe = new SaajUniverse();
   47           universe.set(SaajUniverse.AXIS1);
   48           try {
   49               return doIntercept(method, objects, methodProxy);
   50           } finally {
   51               universe.unset();
   52           }
   53       }
   54   
   55       private Object doIntercept(Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
   56           int index = methodProxy.getSuperIndex();
   57           OperationInfo operationInfo = operations[index];
   58           if (operationInfo == null) {
   59               throw new RuntimeException("Operation not mapped: " + method.getName() + " index: " + index + "\n OperationInfos: " + Arrays.asList(operations));
   60           }
   61           stub.checkCachedEndpoint();
   62   
   63           Call call = stub.createCall();
   64   
   65           operationInfo.prepareCall(call);
   66   
   67           stub.setUpCall(call);
   68           if (credentialsName != null) {
   69               throw new UnsupportedOperationException("Client side auth is not implementd");
   70   //            Subject subject = ContextManager.getNextCaller();
   71   //            if (subject == null) {
   72   //                throw new IllegalStateException("Subject missing but authentication turned on");
   73   //            } else {
   74   //                Set creds = subject.getPrivateCredentials(NamedUsernamePasswordCredential.class);
   75   //                boolean found = false;
   76   //                for (Iterator iterator = creds.iterator(); iterator.hasNext();) {
   77   //                    NamedUsernamePasswordCredential namedUsernamePasswordCredential = (NamedUsernamePasswordCredential) iterator.next();
   78   //                    if (credentialsName.equals(namedUsernamePasswordCredential.getName())) {
   79   //                        call.setUsername(namedUsernamePasswordCredential.getUsername());
   80   //                        call.setPassword(new String(namedUsernamePasswordCredential.getPassword()));
   81   //                        found = true;
   82   //                        break;
   83   //                    }
   84   //                }
   85   //                if (!found) {
   86   //                    throw new IllegalStateException("no NamedUsernamePasswordCredential found for name " + credentialsName);
   87   //                }
   88   //            }
   89           }
   90           Object response = null;
   91           List parameterDescs = operationInfo.getOperationDesc().getParameters();
   92           Object[] unwrapped = extractFromHolders(objects, parameterDescs, operationInfo.getOperationDesc().getNumInParams());
   93           if (operationInfo.getOperationDesc().getMep() == OperationType.REQUEST_RESPONSE) {
   94               try {
   95                   response = call.invoke(unwrapped);
   96               } catch (RemoteException e) {
   97                   throw operationInfo.unwrapFault(e);
   98               }
   99   
  100               if (response instanceof RemoteException) {
  101                   throw operationInfo.unwrapFault((RemoteException) response);
  102               } else {
  103                   stub.extractAttachments(call);
  104                   Map outputParameters = call.getOutputParams();
  105                   putInHolders(outputParameters, objects, parameterDescs);
  106                   Class returnType = operationInfo.getOperationDesc().getReturnClass();
  107                   //return type should never be null... but we are not objecting if wsdl-return-value-mapping is not set.
  108                   if (response == null || returnType == null || returnType.isAssignableFrom(response.getClass())) {
  109                       return response;
  110                   } else {
  111                       return JavaUtils.convert(response, returnType);
  112                   }
  113               }
  114           } else if (operationInfo.getOperationDesc().getMep() == OperationType.ONE_WAY) {
  115               //one way
  116               call.invokeOneWay(unwrapped);
  117               return null;
  118           } else {
  119               throw new RuntimeException("Invalid messaging style: " + operationInfo.getOperationDesc().getMep());
  120           }
  121       }
  122   
  123       private Object[] extractFromHolders(Object[] objects, List parameterDescs, int inParameterCount) throws JavaUtils.HolderException {
  124           if (objects.length != parameterDescs.size()) {
  125               throw new IllegalArgumentException("Mismatch parameter count: expected: " + parameterDescs.size() + ", actual: " + objects.length);
  126           }
  127           Object[] unwrapped = new Object[inParameterCount];
  128           int j = 0;
  129           for (int i = 0; objects != null && i < objects.length; i++) {
  130               Object parameter = objects[i];
  131               ParameterDesc parameterDesc = (ParameterDesc) parameterDescs.get(i);
  132   
  133               if (parameterDesc.getMode() == ParameterDesc.INOUT) {
  134                   unwrapped[j++] = JavaUtils.getHolderValue(parameter);
  135               } else if (parameterDesc.getMode() == ParameterDesc.IN) {
  136                   unwrapped[j++] = parameter;
  137               }
  138           }
  139           return unwrapped;
  140       }
  141   
  142       private void putInHolders(Map outputParameters, Object[] objects, List parameterDescs) throws JavaUtils.HolderException {
  143           for (int i = 0; i < objects.length; i++) {
  144               Object parameter = objects[i];
  145               ParameterDesc parameterDesc = (ParameterDesc) parameterDescs.get(i);
  146               if ((parameterDesc.getMode() == ParameterDesc.INOUT) || (parameterDesc.getMode() == ParameterDesc.OUT)) {
  147                   Object returned = outputParameters.get(parameterDesc.getQName());
  148                   if (returned instanceof Holder) {
  149                       //TODO this must be a bug somewhere!!!!
  150                       returned = JavaUtils.getHolderValue(returned);
  151                   }
  152                   JavaUtils.setHolderValue(parameter, returned);
  153               }
  154           }
  155       }
  156   }

Save This Page
Home » openejb-3.1.2-src » org.apache » openejb » server » axis » client » [javadoc | source]