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 Tue Mar 06 16:09:35 2012 -0800 1.3 @@ -0,0 +1,263 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2010, 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 + if (fault.getParentFault() != null) { 1.151 + preVisit(fault.getParentFault()); 1.152 + } 1.153 + for (Iterator iter = fault.getSubfaults(); 1.154 + iter != null && iter.hasNext();) { 1.155 + 1.156 + Fault subfault = (Fault) iter.next(); 1.157 + preVisit(subfault); 1.158 + } 1.159 + } 1.160 + } 1.161 + 1.162 + private void addExceptionClassName(String name) { 1.163 + if(_allClassNames.contains(name)) 1.164 + _exceptionClassNames.add(name); 1.165 + registerClassName(name); 1.166 + //To change body of created methods use File | Settings | File Templates. 1.167 + } 1.168 + 1.169 + protected void visitBodyBlock(Block block) throws Exception { 1.170 + visitBlock(block); 1.171 + } 1.172 + 1.173 + protected void visitHeaderBlock(Block block) throws Exception { 1.174 + visitBlock(block); 1.175 + } 1.176 + 1.177 + protected void visitFaultBlock(Block block) throws Exception { 1.178 + } 1.179 + 1.180 + protected void visitBlock(Block block) throws Exception { 1.181 + visitType(block.getType()); 1.182 + } 1.183 + 1.184 + protected void visit(Parameter parameter) throws Exception { 1.185 + visitType(parameter.getType()); 1.186 + } 1.187 + 1.188 + private void visitType(AbstractType type) throws Exception { 1.189 + if (type != null) { 1.190 + if (type instanceof JAXBType) 1.191 + visitType((JAXBType)type); 1.192 + else if (type instanceof RpcLitStructure) 1.193 + visitType((RpcLitStructure)type); 1.194 + } 1.195 + } 1.196 + 1.197 + 1.198 + private void visitType(JAXBType type) throws Exception { 1.199 + type.accept(this); 1.200 + } 1.201 + 1.202 + private void visitType(RpcLitStructure type) throws Exception { 1.203 + type.accept(this); 1.204 + } 1.205 + private void registerClassName(String name) { 1.206 + if (name == null || name.equals("")) { 1.207 + return; 1.208 + } 1.209 + if (_allClassNames.contains(name)) { 1.210 + _conflictingClassNames.add(name); 1.211 + } else { 1.212 + _allClassNames.add(name); 1.213 + } 1.214 + } 1.215 + 1.216 + public Set<String> getSeiClassNames() { 1.217 + return _seiClassNames; 1.218 + } 1.219 + 1.220 + private Set<String> _seiClassNames; 1.221 + 1.222 + public Set<String> getJaxbGeneratedClassNames() { 1.223 + return _jaxbGeneratedClassNames; 1.224 + } 1.225 + 1.226 + private Set<String> _jaxbGeneratedClassNames; 1.227 + 1.228 + 1.229 + public Set<String> getExceptionClassNames() { 1.230 + return _exceptionClassNames; 1.231 + } 1.232 + 1.233 + private Set<String> _exceptionClassNames; 1.234 + boolean doneVisitingJAXBModel = false; 1.235 + public void visit(JAXBType type) throws Exception { 1.236 + if(!doneVisitingJAXBModel && type.getJaxbModel() != null){ 1.237 + Set<String> classNames = type.getJaxbModel().getGeneratedClassNames(); 1.238 + for(String className : classNames){ 1.239 + addJAXBGeneratedClassName(className); 1.240 + } 1.241 + doneVisitingJAXBModel = true; 1.242 + } 1.243 + } 1.244 + 1.245 + public void visit(RpcLitStructure type) throws Exception { 1.246 + if(!doneVisitingJAXBModel){ 1.247 + Set<String> classNames = type.getJaxbModel().getGeneratedClassNames(); 1.248 + for(String className : classNames){ 1.249 + addJAXBGeneratedClassName(className); 1.250 + } 1.251 + doneVisitingJAXBModel = true; 1.252 + } 1.253 + } 1.254 + 1.255 + 1.256 + private void addJAXBGeneratedClassName(String name) { 1.257 + _jaxbGeneratedClassNames.add(name); 1.258 + registerClassName(name); 1.259 + } 1.260 + 1.261 + private Set _allClassNames; 1.262 + private Set _exceptions; 1.263 + private Set _wsdlBindingNames; 1.264 + private Set _conflictingClassNames; 1.265 + private Set<QName> _portTypeNames; 1.266 +}