src/share/jaxws_classes/com/sun/xml/internal/ws/model/JavaMethodImpl.java

changeset 286
f50545b5e2f1
child 368
0989ad8c0860
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/ws/model/JavaMethodImpl.java	Tue Mar 06 16:09:35 2012 -0800
     1.3 @@ -0,0 +1,416 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package com.sun.xml.internal.ws.model;
    1.30 +
    1.31 +import com.sun.istack.internal.NotNull;
    1.32 +import com.sun.xml.internal.bind.api.TypeReference;
    1.33 +import com.sun.xml.internal.ws.api.databinding.MetadataReader;
    1.34 +import com.sun.xml.internal.ws.api.model.JavaMethod;
    1.35 +import com.sun.xml.internal.ws.api.model.MEP;
    1.36 +import com.sun.xml.internal.ws.api.model.SEIModel;
    1.37 +import com.sun.xml.internal.ws.api.model.wsdl.WSDLPort;
    1.38 +import com.sun.xml.internal.ws.api.model.wsdl.WSDLBoundOperation;
    1.39 +import com.sun.xml.internal.ws.api.model.wsdl.WSDLFault;
    1.40 +import com.sun.xml.internal.ws.api.model.soap.SOAPBinding;
    1.41 +import com.sun.xml.internal.ws.model.soap.SOAPBindingImpl;
    1.42 +import com.sun.xml.internal.ws.spi.db.TypeInfo;
    1.43 +import com.sun.xml.internal.ws.wsdl.ActionBasedOperationSignature;
    1.44 +import com.sun.istack.internal.Nullable;
    1.45 +
    1.46 +import javax.xml.namespace.QName;
    1.47 +import javax.xml.ws.Action;
    1.48 +import javax.xml.ws.WebServiceException;
    1.49 +import javax.jws.WebMethod;
    1.50 +import java.lang.reflect.Method;
    1.51 +import java.util.ArrayList;
    1.52 +import java.util.Collections;
    1.53 +import java.util.List;
    1.54 +import java.util.logging.Logger;
    1.55 +
    1.56 +/**
    1.57 + * Build this runtime model using java SEI and annotations
    1.58 + *
    1.59 + * @author Vivek Pandey
    1.60 + */
    1.61 +public final class JavaMethodImpl implements JavaMethod {
    1.62 +
    1.63 +    private String inputAction = "";
    1.64 +    private String outputAction = "";
    1.65 +    private final List<CheckedExceptionImpl> exceptions = new ArrayList<CheckedExceptionImpl>();
    1.66 +    private final Method method;
    1.67 +    /*package*/ final List<ParameterImpl> requestParams = new ArrayList<ParameterImpl>();
    1.68 +    /*package*/ final List<ParameterImpl> responseParams = new ArrayList<ParameterImpl>();
    1.69 +    private final List<ParameterImpl> unmReqParams = Collections.unmodifiableList(requestParams);
    1.70 +    private final List<ParameterImpl> unmResParams = Collections.unmodifiableList(responseParams);
    1.71 +    private SOAPBinding binding;
    1.72 +    private MEP mep;
    1.73 +    private QName operationName;
    1.74 +    private WSDLBoundOperation wsdlOperation;
    1.75 +    /*package*/ final AbstractSEIModelImpl owner;
    1.76 +    private final Method seiMethod;
    1.77 +    private QName requestPayloadName;
    1.78 +    private String soapAction;
    1.79 +
    1.80 +    /**
    1.81 +     * @param owner
    1.82 +     * @param method : Implementation class method
    1.83 +     * @param seiMethod : corresponding SEI Method.
    1.84 +     *                  Is there is no SEI, it should be Implementation class method
    1.85 +     */
    1.86 +    public JavaMethodImpl(AbstractSEIModelImpl owner, Method method, Method seiMethod, MetadataReader metadataReader) {
    1.87 +        this.owner = owner;
    1.88 +        this.method = method;
    1.89 +        this.seiMethod = seiMethod;
    1.90 +        setWsaActions(metadataReader);
    1.91 +    }
    1.92 +
    1.93 +    private void setWsaActions(MetadataReader metadataReader) {
    1.94 +        Action action = (metadataReader != null)? metadataReader.getAnnotation(Action.class, seiMethod):seiMethod.getAnnotation(Action.class);
    1.95 +        if(action != null) {
    1.96 +            inputAction = action.input();
    1.97 +            outputAction = action.output();
    1.98 +        }
    1.99 +
   1.100 +        //@Action(input) =="", get it from @WebMethod(action)
   1.101 +        WebMethod webMethod = (metadataReader != null)? metadataReader.getAnnotation(WebMethod.class, seiMethod):seiMethod.getAnnotation(WebMethod.class);
   1.102 +        soapAction = "";
   1.103 +        if (webMethod != null )
   1.104 +            soapAction = webMethod.action();
   1.105 +        if(!soapAction.equals("")) {
   1.106 +            //non-empty soapAction
   1.107 +            if(inputAction.equals(""))
   1.108 +                // set input action to non-empty soapAction
   1.109 +                inputAction = soapAction;
   1.110 +            else if(!inputAction.equals(soapAction)){
   1.111 +                //both are explicitly set via annotations, make sure @Action == @WebMethod.action
   1.112 +                throw new WebServiceException("@Action and @WebMethod(action=\"\" does not match on operation "+ method.getName());
   1.113 +            }
   1.114 +        }
   1.115 +    }
   1.116 +
   1.117 +    public ActionBasedOperationSignature getOperationSignature() {
   1.118 +        QName qname = getRequestPayloadName();
   1.119 +        if (qname == null) qname = new QName("", "");
   1.120 +        return new ActionBasedOperationSignature(getInputAction(), qname);
   1.121 +    }
   1.122 +
   1.123 +    public SEIModel getOwner() {
   1.124 +        return owner;
   1.125 +    }
   1.126 +
   1.127 +    /**
   1.128 +     * @see {@link JavaMethod}
   1.129 +     *
   1.130 +     * @return Returns the method.
   1.131 +     */
   1.132 +    public Method getMethod() {
   1.133 +        return method;
   1.134 +    }
   1.135 +
   1.136 +    /**
   1.137 +     * @see {@link JavaMethod}
   1.138 +     *
   1.139 +     * @return Returns the SEI method where annotations are present
   1.140 +     */
   1.141 +    public Method getSEIMethod() {
   1.142 +        return seiMethod;
   1.143 +    }
   1.144 +
   1.145 +    /**
   1.146 +     * @return Returns the mep.
   1.147 +     */
   1.148 +    public MEP getMEP() {
   1.149 +        return mep;
   1.150 +    }
   1.151 +
   1.152 +    /**
   1.153 +     * @param mep
   1.154 +     *            The mep to set.
   1.155 +     */
   1.156 +    void setMEP(MEP mep) {
   1.157 +        this.mep = mep;
   1.158 +    }
   1.159 +
   1.160 +    /**
   1.161 +     * @return the Binding object
   1.162 +     */
   1.163 +    public SOAPBinding getBinding() {
   1.164 +        if (binding == null)
   1.165 +            return new SOAPBindingImpl();
   1.166 +        return binding;
   1.167 +    }
   1.168 +
   1.169 +    /**
   1.170 +     * @param binding
   1.171 +     */
   1.172 +    void setBinding(SOAPBinding binding) {
   1.173 +        this.binding = binding;
   1.174 +    }
   1.175 +
   1.176 +    /**
   1.177 +     * Returns the {@link WSDLBoundOperation} Operation associated with {@link this}
   1.178 +     * operation.
   1.179 +     * @deprecated
   1.180 +     * @return the WSDLBoundOperation for this JavaMethod
   1.181 +     */
   1.182 +    public WSDLBoundOperation getOperation() {
   1.183 +//        assert wsdlOperation != null;
   1.184 +        return wsdlOperation;
   1.185 +    }
   1.186 +
   1.187 +    public void setOperationQName(QName name) {
   1.188 +        this.operationName = name;
   1.189 +    }
   1.190 +
   1.191 +    public QName getOperationQName() {
   1.192 +        return (wsdlOperation != null)? wsdlOperation.getName(): operationName;
   1.193 +    }
   1.194 +
   1.195 +    public String getSOAPAction() {
   1.196 +        return (wsdlOperation != null)? wsdlOperation.getSOAPAction(): soapAction;
   1.197 +    }
   1.198 +
   1.199 +    public String getOperationName() {
   1.200 +        return operationName.getLocalPart();
   1.201 +    }
   1.202 +
   1.203 +    public String getRequestMessageName() {
   1.204 +        return getOperationName();
   1.205 +    }
   1.206 +
   1.207 +    public String getResponseMessageName() {
   1.208 +        if(mep.isOneWay())
   1.209 +            return null;
   1.210 +        return getOperationName()+"Response";
   1.211 +    }
   1.212 +
   1.213 +    public void setRequestPayloadName(QName n)  {
   1.214 +        requestPayloadName = n;
   1.215 +    }
   1.216 +
   1.217 +    /**
   1.218 +     * @return soap:Body's first child name for request message.
   1.219 +     */
   1.220 +    public @Nullable QName getRequestPayloadName() {
   1.221 +        return (wsdlOperation != null)? wsdlOperation.getReqPayloadName(): requestPayloadName;
   1.222 +    }
   1.223 +
   1.224 +    /**
   1.225 +     * @return soap:Body's first child name for response message.
   1.226 +     */
   1.227 +    public @Nullable QName getResponsePayloadName() {
   1.228 +        return (mep == MEP.ONE_WAY) ? null : wsdlOperation.getResPayloadName();
   1.229 +    }
   1.230 +
   1.231 +    /**
   1.232 +     * @return returns unmodifiable list of request parameters
   1.233 +     */
   1.234 +    public List<ParameterImpl> getRequestParameters() {
   1.235 +        return unmReqParams;
   1.236 +    }
   1.237 +
   1.238 +    /**
   1.239 +     * @return returns unmodifiable list of response parameters
   1.240 +     */
   1.241 +    public List<ParameterImpl> getResponseParameters() {
   1.242 +        return unmResParams;
   1.243 +    }
   1.244 +
   1.245 +    void addParameter(ParameterImpl p) {
   1.246 +        if (p.isIN() || p.isINOUT()) {
   1.247 +            assert !requestParams.contains(p);
   1.248 +            requestParams.add(p);
   1.249 +        }
   1.250 +
   1.251 +        if (p.isOUT() || p.isINOUT()) {
   1.252 +            // this check is only for out parameters
   1.253 +            assert !responseParams.contains(p);
   1.254 +            responseParams.add(p);
   1.255 +        }
   1.256 +    }
   1.257 +
   1.258 +    void addRequestParameter(ParameterImpl p){
   1.259 +        if (p.isIN() || p.isINOUT()) {
   1.260 +            requestParams.add(p);
   1.261 +        }
   1.262 +    }
   1.263 +
   1.264 +    void addResponseParameter(ParameterImpl p){
   1.265 +        if (p.isOUT() || p.isINOUT()) {
   1.266 +            responseParams.add(p);
   1.267 +        }
   1.268 +    }
   1.269 +
   1.270 +    /**
   1.271 +     * @return Returns number of java method parameters - that will be all the
   1.272 +     *         IN, INOUT and OUT holders
   1.273 +     *
   1.274 +     * @deprecated no longer use in the new architecture
   1.275 +     */
   1.276 +    public int getInputParametersCount() {
   1.277 +        int count = 0;
   1.278 +        for (ParameterImpl param : requestParams) {
   1.279 +            if (param.isWrapperStyle()) {
   1.280 +                count += ((WrapperParameter) param).getWrapperChildren().size();
   1.281 +            } else {
   1.282 +                count++;
   1.283 +            }
   1.284 +        }
   1.285 +
   1.286 +        for (ParameterImpl param : responseParams) {
   1.287 +            if (param.isWrapperStyle()) {
   1.288 +                for (ParameterImpl wc : ((WrapperParameter) param).getWrapperChildren()) {
   1.289 +                    if (!wc.isResponse() && wc.isOUT()) {
   1.290 +                        count++;
   1.291 +                    }
   1.292 +                }
   1.293 +            } else if (!param.isResponse() && param.isOUT()) {
   1.294 +                count++;
   1.295 +            }
   1.296 +        }
   1.297 +
   1.298 +        return count;
   1.299 +    }
   1.300 +
   1.301 +    /**
   1.302 +     * @param ce
   1.303 +     */
   1.304 +    void addException(CheckedExceptionImpl ce) {
   1.305 +        if (!exceptions.contains(ce))
   1.306 +            exceptions.add(ce);
   1.307 +    }
   1.308 +
   1.309 +    /**
   1.310 +     * @param exceptionClass
   1.311 +     * @return CheckedException corresponding to the exceptionClass. Returns
   1.312 +     *         null if not found.
   1.313 +     */
   1.314 +    public CheckedExceptionImpl getCheckedException(Class exceptionClass) {
   1.315 +        for (CheckedExceptionImpl ce : exceptions) {
   1.316 +            if (ce.getExceptionClass()==exceptionClass)
   1.317 +                return ce;
   1.318 +        }
   1.319 +        return null;
   1.320 +    }
   1.321 +
   1.322 +
   1.323 +    /**
   1.324 +     * @return a list of checked Exceptions thrown by this method
   1.325 +     */
   1.326 +    public List<CheckedExceptionImpl> getCheckedExceptions(){
   1.327 +        return Collections.unmodifiableList(exceptions);
   1.328 +    }
   1.329 +
   1.330 +    public String getInputAction() {
   1.331 +//        return (wsdlOperation != null)? wsdlOperation.getOperation().getInput().getAction(): inputAction;
   1.332 +        return inputAction;
   1.333 +    }
   1.334 +
   1.335 +    public String getOutputAction() {
   1.336 +//        return (wsdlOperation != null)? wsdlOperation.getOperation().getOutput().getAction(): outputAction;
   1.337 +        return outputAction;
   1.338 +    }
   1.339 +
   1.340 +    /**
   1.341 +     * @deprecated
   1.342 +     * @param detailType
   1.343 +     * @return Gets the CheckedException corresponding to detailType. Returns
   1.344 +     *         null if no CheckedExcpetion with the detailType found.
   1.345 +     */
   1.346 +    public CheckedExceptionImpl getCheckedException(TypeReference detailType) {
   1.347 +        for (CheckedExceptionImpl ce : exceptions) {
   1.348 +            TypeInfo actual = ce.getDetailType();
   1.349 +            if (actual.tagName.equals(detailType.tagName) && actual.type==detailType.type) {
   1.350 +                return ce;
   1.351 +            }
   1.352 +        }
   1.353 +        return null;
   1.354 +    }
   1.355 +
   1.356 +
   1.357 +
   1.358 +    /**
   1.359 +     * Returns if the java method  is async
   1.360 +     * @return if this is an Asynch
   1.361 +     */
   1.362 +    public boolean isAsync(){
   1.363 +        return mep.isAsync;
   1.364 +    }
   1.365 +
   1.366 +    /*package*/ void freeze(WSDLPort portType) {
   1.367 +        this.wsdlOperation = portType.getBinding().get(new QName(portType.getBinding().getPortType().getName().getNamespaceURI(),getOperationName()));
   1.368 +        // TODO: replace this with proper error handling
   1.369 +        if(wsdlOperation ==null)
   1.370 +            throw new WebServiceException("Method "+seiMethod.getName()+" is exposed as WebMethod, but there is no corresponding wsdl operation with name "+operationName+" in the wsdl:portType" + portType.getBinding().getPortType().getName());
   1.371 +
   1.372 +        //so far, the inputAction, outputAction and fault actions are set from the @Action and @FaultAction
   1.373 +        //set the values from WSDLModel, if such annotations are not present or defaulted
   1.374 +        if(inputAction.equals("")) {
   1.375 +                inputAction = wsdlOperation.getOperation().getInput().getAction();
   1.376 +        } else if(!inputAction.equals(wsdlOperation.getOperation().getInput().getAction()))
   1.377 +                //TODO input action might be from @Action or WebMethod(action)
   1.378 +                LOGGER.warning("Input Action on WSDL operation "+wsdlOperation.getName().getLocalPart() + " and @Action on its associated Web Method " + seiMethod.getName() +" did not match and will cause problems in dispatching the requests");
   1.379 +
   1.380 +        if (!mep.isOneWay()) {
   1.381 +            if (outputAction.equals(""))
   1.382 +                outputAction = wsdlOperation.getOperation().getOutput().getAction();
   1.383 +
   1.384 +            for (CheckedExceptionImpl ce : exceptions) {
   1.385 +                if (ce.getFaultAction().equals("")) {
   1.386 +                    QName detailQName = ce.getDetailType().tagName;
   1.387 +                    WSDLFault wsdlfault = wsdlOperation.getOperation().getFault(detailQName);
   1.388 +                    if(wsdlfault == null) {
   1.389 +                        // mismatch between wsdl model and SEI model, log a warning and use  SEI model for Action determination
   1.390 +                        LOGGER.warning("Mismatch between Java model and WSDL model found, For wsdl operation " +
   1.391 +                                wsdlOperation.getName() + ",There is no matching wsdl fault with detail QName " +
   1.392 +                                ce.getDetailType().tagName);
   1.393 +                        ce.setFaultAction(ce.getDefaultFaultAction());
   1.394 +                    } else {
   1.395 +                        ce.setFaultAction(wsdlfault.getAction());
   1.396 +                    }
   1.397 +                }
   1.398 +            }
   1.399 +        }
   1.400 +    }
   1.401 +
   1.402 +    final void fillTypes(List<TypeInfo> types) {
   1.403 +        fillTypes(requestParams, types);
   1.404 +        fillTypes(responseParams, types);
   1.405 +
   1.406 +        for (CheckedExceptionImpl ce : exceptions) {
   1.407 +            types.add(ce.getDetailType());
   1.408 +        }
   1.409 +    }
   1.410 +
   1.411 +    private void fillTypes(List<ParameterImpl> params, List<TypeInfo> types) {
   1.412 +        for (ParameterImpl p : params) {
   1.413 +            p.fillTypes(types);
   1.414 +        }
   1.415 +    }
   1.416 +
   1.417 +    private static final Logger LOGGER = Logger.getLogger(com.sun.xml.internal.ws.model.JavaMethodImpl.class.getName());
   1.418 +
   1.419 +}

mercurial