1 /*
   2  * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.xml.internal.ws.model;
  27 
  28 import com.sun.istack.internal.NotNull;
  29 import com.sun.xml.internal.bind.api.Bridge;
  30 import com.sun.xml.internal.bind.api.JAXBRIContext;
  31 import com.sun.xml.internal.bind.api.TypeReference;
  32 import com.sun.xml.internal.ws.api.BindingID;
  33 import com.sun.xml.internal.ws.api.WSBinding;
  34 import com.sun.xml.internal.ws.api.databinding.Databinding;
  35 import com.sun.xml.internal.ws.api.model.JavaMethod;
  36 import com.sun.xml.internal.ws.api.model.ParameterBinding;
  37 import com.sun.xml.internal.ws.api.model.SEIModel;
  38 import com.sun.xml.internal.ws.api.model.wsdl.WSDLModel;
  39 import com.sun.xml.internal.ws.api.model.wsdl.WSDLPort;
  40 import com.sun.xml.internal.ws.api.model.wsdl.WSDLPart;
  41 import com.sun.xml.internal.ws.api.model.wsdl.WSDLBoundPortType;
  42 import com.sun.xml.internal.ws.api.model.wsdl.WSDLBoundOperation;
  43 import com.sun.xml.internal.ws.encoding.soap.streaming.SOAPNamespaceConstants;
  44 import com.sun.xml.internal.ws.resources.ModelerMessages;
  45 import com.sun.xml.internal.ws.spi.db.BindingContext;
  46 import com.sun.xml.internal.ws.spi.db.BindingContextFactory;
  47 import com.sun.xml.internal.ws.spi.db.BindingInfo;
  48 import com.sun.xml.internal.ws.spi.db.XMLBridge;
  49 import com.sun.xml.internal.ws.spi.db.TypeInfo;
  50 import com.sun.xml.internal.ws.util.Pool;
  51 import com.sun.xml.internal.ws.developer.UsesJAXBContextFeature;
  52 import com.sun.xml.internal.ws.developer.JAXBContextFactory;
  53 import com.sun.xml.internal.ws.binding.WebServiceFeatureList;
  54 
  55 import javax.jws.WebParam.Mode;
  56 import javax.xml.bind.JAXBContext;
  57 import javax.xml.bind.annotation.XmlSeeAlso;
  58 import javax.xml.namespace.QName;
  59 import javax.xml.ws.WebServiceException;
  60 
  61 
  62 import java.lang.reflect.Method;
  63 import java.security.AccessController;
  64 import java.security.PrivilegedActionException;
  65 import java.security.PrivilegedExceptionAction;
  66 import java.util.ArrayList;
  67 import java.util.Collection;
  68 import java.util.Collections;
  69 import java.util.HashMap;
  70 import java.util.List;
  71 import java.util.Map;
  72 import java.util.logging.Level;
  73 import java.util.logging.Logger;
  74 
  75 /**
  76  * model of the web service.  Used by the runtime marshall/unmarshall
  77  * web service invocations
  78  *
  79  * @author JAXWS Development Team
  80  */
  81 public abstract class AbstractSEIModelImpl implements SEIModel {
  82 
  83     protected AbstractSEIModelImpl(WebServiceFeatureList features) {
  84         this.features = features;
  85         databindingInfo = new BindingInfo();
  86         databindingInfo.setSEIModel(this);
  87     }
  88 
  89     void postProcess() {
  90         // should be called only once.
  91         if (jaxbContext != null) {
  92             return;
  93         }
  94         populateMaps();
  95         createJAXBContext();
  96     }
  97 
  98     public BindingInfo databindingInfo() {
  99         return databindingInfo;
 100     }
 101 
 102     /**
 103      * Link {@link SEIModel} to {@link WSDLModel}.
 104      * Merge it with {@link #postProcess()}.
 105      */
 106     public void freeze(WSDLPort port) {
 107         this.port = port;
 108         for (JavaMethodImpl m : javaMethods) {
 109             m.freeze(port);
 110             putOp(m.getOperationQName(),m);
 111 
 112         }
 113         if (databinding != null) {
 114             ((com.sun.xml.internal.ws.db.DatabindingImpl)databinding).freeze(port);
 115         }
 116     }
 117 
 118     /**
 119      * Populate methodToJM and nameToJM maps.
 120      */
 121     abstract protected void populateMaps();
 122 
 123     @Override
 124     public Pool.Marshaller getMarshallerPool() {
 125         return marshallers;
 126     }
 127 
 128     /**
 129      * @return the <code>JAXBRIContext</code>
 130      * @deprecated
 131      */
 132     @Override
 133     public JAXBContext getJAXBContext() {
 134         JAXBContext jc = bindingContext.getJAXBContext();
 135         if (jc != null) {
 136             return jc;
 137         }
 138         return jaxbContext;
 139     }
 140 
 141     public BindingContext getBindingContext() {
 142         return bindingContext;
 143     }
 144 
 145     /**
 146      * @return the known namespaces from JAXBRIContext
 147      */
 148     public List<String> getKnownNamespaceURIs() {
 149         return knownNamespaceURIs;
 150     }
 151 
 152     /**
 153      * @return the <code>Bridge</code> for the <code>type</code>
 154      * @deprecated use getBond
 155      */
 156     public final Bridge getBridge(TypeReference type) {
 157         Bridge b = bridgeMap.get(type);
 158         assert b!=null; // we should have created Bridge for all TypeReferences known to this model
 159         return b;
 160     }
 161 
 162     public final XMLBridge getXMLBridge(TypeInfo type) {
 163         XMLBridge b = xmlBridgeMap.get(type);
 164         assert b!=null; // we should have created Bridge for all TypeReferences known to this model
 165         return b;
 166     }
 167 
 168     private void /*JAXBRIContext*/ createJAXBContext() {
 169         final List<TypeInfo> types = getAllTypeInfos();
 170         final List<Class> cls = new ArrayList<Class>(types.size() + additionalClasses.size());
 171 
 172         cls.addAll(additionalClasses);
 173         for (TypeInfo type : types) {
 174             cls.add((Class) type.type);
 175         }
 176 
 177         try {
 178             //jaxbContext = JAXBRIContext.newInstance(cls, types, targetNamespace, false);
 179             // Need to avoid doPriv block once JAXB is fixed. Afterwards, use the above
 180             bindingContext = AccessController.doPrivileged(new PrivilegedExceptionAction<BindingContext>() {
 181                 public BindingContext run() throws Exception {
 182                     if(LOGGER.isLoggable(Level.FINEST)) {
 183                         LOGGER.log(Level.FINEST, "Creating JAXBContext with classes={0} and types={1}", new Object[]{cls, types});
 184                     }
 185                     UsesJAXBContextFeature f = features.get(UsesJAXBContextFeature.class);
 186                     com.oracle.webservices.internal.api.databinding.DatabindingModeFeature dmf =
 187                             features.get(com.oracle.webservices.internal.api.databinding.DatabindingModeFeature.class);
 188                     JAXBContextFactory factory = f!=null ? f.getFactory() : null;
 189                     if(factory==null)   factory=JAXBContextFactory.DEFAULT;
 190 
 191 //                    return factory.createJAXBContext(AbstractSEIModelImpl.this,cls,types);
 192 
 193                     databindingInfo.properties().put(JAXBContextFactory.class.getName(), factory);
 194                     if (dmf != null) {
 195                         if (LOGGER.isLoggable(Level.FINE))
 196                             LOGGER.log(Level.FINE, "DatabindingModeFeature in SEI specifies mode: {0}", dmf.getMode());
 197                         databindingInfo.setDatabindingMode(dmf
 198                                 .getMode());
 199                     }
 200 
 201                         if (f!=null) databindingInfo.setDatabindingMode(BindingContextFactory.DefaultDatabindingMode);
 202                         databindingInfo.setClassLoader(classLoader);
 203                         databindingInfo.contentClasses().addAll(cls);
 204                         databindingInfo.typeInfos().addAll(types);
 205                         databindingInfo.properties().put("c14nSupport", Boolean.FALSE);
 206                         databindingInfo.setDefaultNamespace(AbstractSEIModelImpl.this.getDefaultSchemaNamespace());
 207                         BindingContext bc =  BindingContextFactory.create(databindingInfo);
 208                             if (LOGGER.isLoggable(Level.FINE))
 209                                 LOGGER.log(Level.FINE,
 210                                         "Created binding context: "
 211                                                 + bc.getClass().getName());
 212 //                      System.out.println("---------------------- databinding " + bc);
 213                         return bc;
 214                 }
 215             });
 216 //          createBridgeMap(types);
 217             createBondMap(types);
 218         } catch (PrivilegedActionException e) {
 219             throw new WebServiceException(ModelerMessages.UNABLE_TO_CREATE_JAXB_CONTEXT(), e);
 220         }
 221         knownNamespaceURIs = new ArrayList<String>();
 222         for (String namespace : bindingContext.getKnownNamespaceURIs()) {
 223             if (namespace.length() > 0) {
 224                 if (!namespace.equals(SOAPNamespaceConstants.XSD) && !namespace.equals(SOAPNamespaceConstants.XMLNS))
 225                     knownNamespaceURIs.add(namespace);
 226             }
 227         }
 228 
 229         marshallers = new Pool.Marshaller(jaxbContext);
 230 
 231         //return getJAXBContext();
 232     }
 233 
 234     /**
 235      * @return returns non-null list of TypeReference
 236      */
 237     private List<TypeInfo> getAllTypeInfos() {
 238         List<TypeInfo> types = new ArrayList<TypeInfo>();
 239         Collection<JavaMethodImpl> methods = methodToJM.values();
 240         for (JavaMethodImpl m : methods) {
 241             m.fillTypes(types);
 242         }
 243         return types;
 244     }
 245 
 246     private void createBridgeMap(List<TypeReference> types) {
 247         for (TypeReference type : types) {
 248             Bridge bridge = jaxbContext.createBridge(type);
 249             bridgeMap.put(type, bridge);
 250         }
 251     }
 252     private void createBondMap(List<TypeInfo> types) {
 253         for (TypeInfo type : types) {
 254             XMLBridge binding = bindingContext.createBridge(type);
 255             xmlBridgeMap.put(type, binding);
 256         }
 257     }
 258 
 259 
 260     /**
 261      * @return true if <code>name</code> is the name
 262      * of a known fault name for the <code>Method method</code>
 263      */
 264     public boolean isKnownFault(QName name, Method method) {
 265         JavaMethodImpl m = getJavaMethod(method);
 266         for (CheckedExceptionImpl ce : m.getCheckedExceptions()) {
 267             if (ce.getDetailType().tagName.equals(name))
 268                 return true;
 269         }
 270         return false;
 271     }
 272 
 273     /**
 274      * @return true if <code>ex</code> is a Checked Exception
 275      * for <code>Method m</code>
 276      */
 277     public boolean isCheckedException(Method m, Class ex) {
 278         JavaMethodImpl jm = getJavaMethod(m);
 279         for (CheckedExceptionImpl ce : jm.getCheckedExceptions()) {
 280             if (ce.getExceptionClass().equals(ex))
 281                 return true;
 282         }
 283         return false;
 284     }
 285 
 286     /**
 287      * @return the <code>JavaMethod</code> representing the <code>method</code>
 288      */
 289     public JavaMethodImpl getJavaMethod(Method method) {
 290         return methodToJM.get(method);
 291     }
 292 
 293     /**
 294      * @return the <code>JavaMethod</code> associated with the
 295      * operation named name
 296      */
 297     public JavaMethodImpl getJavaMethod(QName name) {
 298         return nameToJM.get(name);
 299     }
 300 
 301     public JavaMethod getJavaMethodForWsdlOperation(QName operationName) {
 302         return wsdlOpToJM.get(operationName);
 303     }
 304 
 305 
 306     /**
 307      * @return the <code>QName</code> associated with the
 308      * JavaMethod jm.
 309      *
 310      * @deprecated
 311      *      Use {@link JavaMethod#getOperationName()}.
 312      */
 313     public QName getQNameForJM(JavaMethodImpl jm) {
 314         for (QName key : nameToJM.keySet()) {
 315             JavaMethodImpl jmethod = nameToJM.get(key);
 316             if (jmethod.getOperationName().equals(jm.getOperationName())){
 317                return key;
 318             }
 319         }
 320         return null;
 321     }
 322 
 323     /**
 324      * @return a <code>Collection</code> of <code>JavaMethods</code>
 325      * associated with this <code>RuntimeModel</code>
 326      */
 327     public final Collection<JavaMethodImpl> getJavaMethods() {
 328         return Collections.unmodifiableList(javaMethods);
 329     }
 330 
 331     void addJavaMethod(JavaMethodImpl jm) {
 332         if (jm != null)
 333             javaMethods.add(jm);
 334     }
 335 
 336     /**
 337      * Applies binding related information to the RpcLitPayload. The payload map is populated correctly
 338      * @return
 339      * Returns attachment parameters if/any.
 340      */
 341     private List<ParameterImpl> applyRpcLitParamBinding(JavaMethodImpl method, WrapperParameter wrapperParameter, WSDLBoundPortType boundPortType, Mode mode) {
 342         QName opName = new QName(boundPortType.getPortTypeName().getNamespaceURI(), method.getOperationName());
 343         WSDLBoundOperation bo = boundPortType.get(opName);
 344         Map<Integer, ParameterImpl> bodyParams = new HashMap<Integer, ParameterImpl>();
 345         List<ParameterImpl> unboundParams = new ArrayList<ParameterImpl>();
 346         List<ParameterImpl> attachParams = new ArrayList<ParameterImpl>();
 347         for(ParameterImpl param : wrapperParameter.wrapperChildren){
 348             String partName = param.getPartName();
 349             if(partName == null)
 350                 continue;
 351 
 352             ParameterBinding paramBinding = boundPortType.getBinding(opName,
 353                     partName, mode);
 354             if(paramBinding != null){
 355                 if(mode == Mode.IN)
 356                     param.setInBinding(paramBinding);
 357                 else if(mode == Mode.OUT || mode == Mode.INOUT)
 358                     param.setOutBinding(paramBinding);
 359 
 360                 if(paramBinding.isUnbound()){
 361                         unboundParams.add(param);
 362                 } else if(paramBinding.isAttachment()){
 363                     attachParams.add(param);
 364                 }else if(paramBinding.isBody()){
 365                     if(bo != null){
 366                         WSDLPart p = bo.getPart(param.getPartName(), mode);
 367                         if(p != null)
 368                             bodyParams.put(p.getIndex(), param);
 369                         else
 370                             bodyParams.put(bodyParams.size(), param);
 371                     }else{
 372                         bodyParams.put(bodyParams.size(), param);
 373                     }
 374                 }
 375             }
 376 
 377         }
 378         wrapperParameter.clear();
 379         for(int i = 0; i <  bodyParams.size();i++){
 380             ParameterImpl p = bodyParams.get(i);
 381             wrapperParameter.addWrapperChild(p);
 382         }
 383 
 384         //add unbounded parts
 385         for(ParameterImpl p:unboundParams){
 386             wrapperParameter.addWrapperChild(p);
 387         }
 388         return attachParams;
 389     }
 390 
 391 
 392     void put(QName name, JavaMethodImpl jm) {
 393         nameToJM.put(name, jm);
 394     }
 395 
 396     void put(Method method, JavaMethodImpl jm) {
 397         methodToJM.put(method, jm);
 398     }
 399 
 400     void putOp(QName opName, JavaMethodImpl jm) {
 401         wsdlOpToJM.put(opName, jm);
 402     }
 403     public String getWSDLLocation() {
 404         return wsdlLocation;
 405     }
 406 
 407     void setWSDLLocation(String location) {
 408         wsdlLocation = location;
 409     }
 410 
 411     public QName getServiceQName() {
 412         return serviceName;
 413     }
 414 
 415     public WSDLPort getPort() {
 416         return port;
 417     }
 418 
 419     public QName getPortName() {
 420         return portName;
 421     }
 422 
 423     public QName getPortTypeName() {
 424         return portTypeName;
 425     }
 426 
 427     void setServiceQName(QName name) {
 428         serviceName = name;
 429     }
 430 
 431     void setPortName(QName name) {
 432         portName = name;
 433     }
 434 
 435     void setPortTypeName(QName name) {
 436         portTypeName = name;
 437     }
 438 
 439     /**
 440      * This is the targetNamespace for the WSDL containing the PortType
 441      * definition
 442      */
 443     void setTargetNamespace(String namespace) {
 444         targetNamespace = namespace;
 445     }
 446 
 447     /**
 448      * This is the targetNamespace for the WSDL containing the PortType
 449      * definition
 450      */
 451     public String getTargetNamespace() {
 452         return targetNamespace;
 453     }
 454 
 455     String getDefaultSchemaNamespace() {
 456         String defaultNamespace = getTargetNamespace();
 457         if (defaultSchemaNamespaceSuffix == null) return defaultNamespace;
 458         if (!defaultNamespace.endsWith("/")) {
 459             defaultNamespace += "/";
 460         }
 461         return (defaultNamespace + defaultSchemaNamespaceSuffix);
 462     }
 463 
 464     @NotNull
 465     public QName getBoundPortTypeName() {
 466         assert portName != null;
 467         return new QName(portName.getNamespaceURI(), portName.getLocalPart()+"Binding");
 468     }
 469 
 470     /**
 471      * Adds additional classes obtained from {@link XmlSeeAlso} annotation. In starting
 472      * from wsdl case these classes would most likely be JAXB ObjectFactory that references other classes.
 473      */
 474     public void addAdditionalClasses(Class... additionalClasses) {
 475         for(Class cls : additionalClasses)
 476             this.additionalClasses.add(cls);
 477     }
 478 
 479     public Databinding getDatabinding() {
 480                 return databinding;
 481         }
 482 
 483         public void setDatabinding(Databinding wsRuntime) {
 484                 this.databinding = wsRuntime;
 485         }
 486 
 487         public WSBinding getWSBinding() {
 488                 return wsBinding;
 489         }
 490 
 491     public Class getContractClass() {
 492                 return contractClass;
 493         }
 494 
 495         public Class getEndpointClass() {
 496                 return endpointClass;
 497         }
 498 
 499         private List<Class> additionalClasses = new ArrayList<Class>();
 500 
 501     private Pool.Marshaller marshallers;
 502     /**
 503      * @deprecated
 504      */
 505     protected JAXBRIContext jaxbContext;
 506     protected BindingContext bindingContext;
 507     private String wsdlLocation;
 508     private QName serviceName;
 509     private QName portName;
 510     private QName portTypeName;
 511     private Map<Method,JavaMethodImpl> methodToJM = new HashMap<Method, JavaMethodImpl>();
 512     /**
 513      * Payload QName to the method that handles it.
 514      */
 515     private Map<QName,JavaMethodImpl> nameToJM = new HashMap<QName, JavaMethodImpl>();
 516     /**
 517      * Wsdl Operation QName to the method that handles it.
 518      */
 519     private Map<QName, JavaMethodImpl> wsdlOpToJM = new HashMap<QName, JavaMethodImpl>();
 520 
 521     private List<JavaMethodImpl> javaMethods = new ArrayList<JavaMethodImpl>();
 522     private final Map<TypeReference, Bridge> bridgeMap = new HashMap<TypeReference, Bridge>();
 523     private final Map<TypeInfo, XMLBridge> xmlBridgeMap = new HashMap<TypeInfo, XMLBridge>();
 524     protected final QName emptyBodyName = new QName("");
 525     private String targetNamespace = "";
 526     private List<String> knownNamespaceURIs = null;
 527     private WSDLPort port;
 528     private final WebServiceFeatureList features;
 529     private Databinding databinding;
 530     BindingID bindingId;
 531     protected Class contractClass;
 532         protected Class endpointClass;
 533         protected ClassLoader classLoader = null;
 534         protected WSBinding wsBinding;
 535         protected BindingInfo databindingInfo;
 536         protected String defaultSchemaNamespaceSuffix;
 537         private static final Logger LOGGER = Logger.getLogger(AbstractSEIModelImpl.class.getName());
 538 }