src/share/jaxws_classes/com/sun/tools/internal/ws/processor/util/ClassNameCollector.java

changeset 0
373ffda63c9a
child 637
9c07ef4934dd
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/jaxws_classes/com/sun/tools/internal/ws/processor/util/ClassNameCollector.java	Wed Apr 27 01:27:09 2016 +0800
     1.3 @@ -0,0 +1,260 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2012, 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.tools.internal.ws.processor.util;
    1.30 +
    1.31 +import com.sun.tools.internal.ws.processor.model.*;
    1.32 +import com.sun.tools.internal.ws.processor.model.java.JavaInterface;
    1.33 +import com.sun.tools.internal.ws.processor.model.jaxb.JAXBType;
    1.34 +import com.sun.tools.internal.ws.processor.model.jaxb.JAXBTypeVisitor;
    1.35 +import com.sun.tools.internal.ws.processor.model.jaxb.RpcLitStructure;
    1.36 +
    1.37 +import javax.xml.namespace.QName;
    1.38 +import java.util.HashSet;
    1.39 +import java.util.Iterator;
    1.40 +import java.util.Set;
    1.41 +
    1.42 +/**
    1.43 + * This class writes out a Model as an XML document.
    1.44 + *
    1.45 + * @author WS Development Team
    1.46 + */
    1.47 +public class ClassNameCollector extends ExtendedModelVisitor
    1.48 +    implements JAXBTypeVisitor {
    1.49 +
    1.50 +    public ClassNameCollector() {
    1.51 +    }
    1.52 +
    1.53 +    public void process(Model model) {
    1.54 +        try {
    1.55 +            _allClassNames = new HashSet();
    1.56 +            _exceptions = new HashSet();
    1.57 +            _wsdlBindingNames = new HashSet();
    1.58 +            _conflictingClassNames = new HashSet();
    1.59 +            _seiClassNames = new HashSet<String>();
    1.60 +            _jaxbGeneratedClassNames = new HashSet<String>();
    1.61 +            _exceptionClassNames = new HashSet<String>();
    1.62 +            _portTypeNames = new HashSet<QName>();
    1.63 +            visit(model);
    1.64 +        } catch (Exception e) {
    1.65 +            e.printStackTrace();
    1.66 +            // fail silently
    1.67 +        } finally {
    1.68 +            _allClassNames = null;
    1.69 +            _exceptions = null;
    1.70 +        }
    1.71 +    }
    1.72 +
    1.73 +    public Set getConflictingClassNames() {
    1.74 +        return _conflictingClassNames;
    1.75 +    }
    1.76 +
    1.77 +    protected void postVisit(Model model) throws Exception {
    1.78 +        for (Iterator iter = model.getExtraTypes(); iter.hasNext();) {
    1.79 +            visitType((AbstractType)iter.next());
    1.80 +        }
    1.81 +    }
    1.82 +
    1.83 +    protected void preVisit(Service service) throws Exception {
    1.84 +        registerClassName(
    1.85 +            ((JavaInterface)service.getJavaInterface()).getName());
    1.86 +        // We don't generate Impl classes, commenting it out.
    1.87 +        // Otherwise, it would cause naming conflicts
    1.88 +        //registerClassName(
    1.89 +        //    ((JavaInterface)service.getJavaInterface()).getImpl());
    1.90 +    }
    1.91 +
    1.92 +    protected void processPort11x(Port port){
    1.93 +        QName wsdlBindingName = (QName) port.getProperty(
    1.94 +            ModelProperties.PROPERTY_WSDL_BINDING_NAME);
    1.95 +        if (!_wsdlBindingNames.contains(wsdlBindingName)) {
    1.96 +
    1.97 +            // multiple ports can share a binding without causing a conflict
    1.98 +            registerClassName(port.getJavaInterface().getName());
    1.99 +        }
   1.100 +        registerClassName((String) port.getProperty(
   1.101 +            ModelProperties.PROPERTY_STUB_CLASS_NAME));
   1.102 +        registerClassName((String) port.getProperty(
   1.103 +            ModelProperties.PROPERTY_TIE_CLASS_NAME));
   1.104 +    }
   1.105 +
   1.106 +    protected void preVisit(Port port) throws Exception {
   1.107 +        QName portTypeName = (QName)port.getProperty(ModelProperties.PROPERTY_WSDL_PORT_TYPE_NAME);
   1.108 +        if(_portTypeNames.contains(portTypeName))
   1.109 +            return;
   1.110 +
   1.111 +        //in 2.0, stub/tie class are binding agnostic so they should be per port, that is multiple
   1.112 +        // bindings can share the same port
   1.113 +
   1.114 +        addSEIClassName(port.getJavaInterface().getName());
   1.115 +    }
   1.116 +
   1.117 +    private void addSEIClassName(String s) {
   1.118 +        _seiClassNames.add(s);
   1.119 +        registerClassName(s);
   1.120 +    }
   1.121 +
   1.122 +    protected void postVisit(Port port) throws Exception {
   1.123 +        QName wsdlBindingName = (QName) port.getProperty(
   1.124 +            ModelProperties.PROPERTY_WSDL_BINDING_NAME);
   1.125 +        if (!_wsdlBindingNames.contains(wsdlBindingName)) {
   1.126 +            _wsdlBindingNames.add(wsdlBindingName);
   1.127 +        }
   1.128 +
   1.129 +        QName portTypeName = (QName)port.getProperty(ModelProperties.PROPERTY_WSDL_PORT_TYPE_NAME);
   1.130 +        if(!_portTypeNames.contains(portTypeName)){
   1.131 +            _portTypeNames.add(portTypeName);
   1.132 +        }
   1.133 +    }
   1.134 +
   1.135 +    protected boolean shouldVisit(Port port) {
   1.136 +        QName wsdlBindingName = (QName) port.getProperty(
   1.137 +            ModelProperties.PROPERTY_WSDL_BINDING_NAME);
   1.138 +        return !_wsdlBindingNames.contains(wsdlBindingName);
   1.139 +    }
   1.140 +
   1.141 +    protected void preVisit(Fault fault) throws Exception {
   1.142 +        if (!_exceptions.contains(fault.getJavaException())) {
   1.143 +
   1.144 +            /* the same exception can be used in several faults, but that
   1.145 +             * doesn't mean that there is a conflict
   1.146 +             */
   1.147 +            _exceptions.add(fault.getJavaException());
   1.148 +            addExceptionClassName(fault.getJavaException().getName());
   1.149 +
   1.150 +            for (Iterator iter = fault.getSubfaults();
   1.151 +                iter != null && iter.hasNext();) {
   1.152 +
   1.153 +                Fault subfault = (Fault) iter.next();
   1.154 +                preVisit(subfault);
   1.155 +            }
   1.156 +        }
   1.157 +    }
   1.158 +
   1.159 +    private void addExceptionClassName(String name) {
   1.160 +        if(_allClassNames.contains(name))
   1.161 +            _exceptionClassNames.add(name);
   1.162 +        registerClassName(name);
   1.163 +        //To change body of created methods use File | Settings | File Templates.
   1.164 +    }
   1.165 +
   1.166 +    protected void visitBodyBlock(Block block) throws Exception {
   1.167 +        visitBlock(block);
   1.168 +    }
   1.169 +
   1.170 +    protected void visitHeaderBlock(Block block) throws Exception {
   1.171 +        visitBlock(block);
   1.172 +    }
   1.173 +
   1.174 +    protected void visitFaultBlock(Block block) throws Exception {
   1.175 +    }
   1.176 +
   1.177 +    protected void visitBlock(Block block) throws Exception {
   1.178 +        visitType(block.getType());
   1.179 +    }
   1.180 +
   1.181 +    protected void visit(Parameter parameter) throws Exception {
   1.182 +        visitType(parameter.getType());
   1.183 +    }
   1.184 +
   1.185 +    private void visitType(AbstractType type) throws Exception {
   1.186 +        if (type != null) {
   1.187 +            if (type instanceof JAXBType)
   1.188 +                visitType((JAXBType)type);
   1.189 +            else if (type instanceof RpcLitStructure)
   1.190 +                visitType((RpcLitStructure)type);
   1.191 +        }
   1.192 +    }
   1.193 +
   1.194 +
   1.195 +    private void visitType(JAXBType type) throws Exception {
   1.196 +        type.accept(this);
   1.197 +    }
   1.198 +
   1.199 +    private void visitType(RpcLitStructure type) throws Exception {
   1.200 +        type.accept(this);
   1.201 +    }
   1.202 +    private void registerClassName(String name) {
   1.203 +        if (name == null || name.equals("")) {
   1.204 +            return;
   1.205 +        }
   1.206 +        if (_allClassNames.contains(name)) {
   1.207 +            _conflictingClassNames.add(name);
   1.208 +        } else {
   1.209 +            _allClassNames.add(name);
   1.210 +        }
   1.211 +    }
   1.212 +
   1.213 +    public Set<String> getSeiClassNames() {
   1.214 +        return _seiClassNames;
   1.215 +    }
   1.216 +
   1.217 +    private Set<String> _seiClassNames;
   1.218 +
   1.219 +    public Set<String> getJaxbGeneratedClassNames() {
   1.220 +        return _jaxbGeneratedClassNames;
   1.221 +    }
   1.222 +
   1.223 +    private Set<String> _jaxbGeneratedClassNames;
   1.224 +
   1.225 +
   1.226 +    public Set<String> getExceptionClassNames() {
   1.227 +        return _exceptionClassNames;
   1.228 +    }
   1.229 +
   1.230 +    private Set<String> _exceptionClassNames;
   1.231 +    boolean doneVisitingJAXBModel = false;
   1.232 +    public void visit(JAXBType type) throws Exception {
   1.233 +        if(!doneVisitingJAXBModel && type.getJaxbModel() != null){
   1.234 +            Set<String> classNames = type.getJaxbModel().getGeneratedClassNames();
   1.235 +            for(String className : classNames){
   1.236 +                addJAXBGeneratedClassName(className);
   1.237 +            }
   1.238 +            doneVisitingJAXBModel = true;
   1.239 +        }
   1.240 +    }
   1.241 +
   1.242 +    public void visit(RpcLitStructure type) throws Exception {
   1.243 +        if(!doneVisitingJAXBModel){
   1.244 +            Set<String> classNames = type.getJaxbModel().getGeneratedClassNames();
   1.245 +            for(String className : classNames){
   1.246 +                addJAXBGeneratedClassName(className);
   1.247 +            }
   1.248 +            doneVisitingJAXBModel = true;
   1.249 +        }
   1.250 +    }
   1.251 +
   1.252 +
   1.253 +    private void addJAXBGeneratedClassName(String name) {
   1.254 +        _jaxbGeneratedClassNames.add(name);
   1.255 +        registerClassName(name);
   1.256 +    }
   1.257 +
   1.258 +    private Set _allClassNames;
   1.259 +    private Set _exceptions;
   1.260 +    private Set _wsdlBindingNames;
   1.261 +    private Set _conflictingClassNames;
   1.262 +    private Set<QName> _portTypeNames;
   1.263 +}

mercurial