aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: package com.sun.tools.internal.ws.processor.util; aoqi@0: aoqi@0: import com.sun.tools.internal.ws.processor.model.*; aoqi@0: import com.sun.tools.internal.ws.processor.model.java.JavaInterface; aoqi@0: import com.sun.tools.internal.ws.processor.model.jaxb.JAXBType; aoqi@0: import com.sun.tools.internal.ws.processor.model.jaxb.JAXBTypeVisitor; aoqi@0: import com.sun.tools.internal.ws.processor.model.jaxb.RpcLitStructure; aoqi@0: aoqi@0: import javax.xml.namespace.QName; aoqi@0: import java.util.HashSet; aoqi@0: import java.util.Iterator; aoqi@0: import java.util.Set; aoqi@0: aoqi@0: /** aoqi@0: * This class writes out a Model as an XML document. aoqi@0: * aoqi@0: * @author WS Development Team aoqi@0: */ aoqi@0: public class ClassNameCollector extends ExtendedModelVisitor aoqi@0: implements JAXBTypeVisitor { aoqi@0: aoqi@0: public ClassNameCollector() { aoqi@0: } aoqi@0: aoqi@0: public void process(Model model) { aoqi@0: try { aoqi@0: _allClassNames = new HashSet(); aoqi@0: _exceptions = new HashSet(); aoqi@0: _wsdlBindingNames = new HashSet(); aoqi@0: _conflictingClassNames = new HashSet(); aoqi@0: _seiClassNames = new HashSet(); aoqi@0: _jaxbGeneratedClassNames = new HashSet(); aoqi@0: _exceptionClassNames = new HashSet(); aoqi@0: _portTypeNames = new HashSet(); aoqi@0: visit(model); aoqi@0: } catch (Exception e) { aoqi@0: e.printStackTrace(); aoqi@0: // fail silently aoqi@0: } finally { aoqi@0: _allClassNames = null; aoqi@0: _exceptions = null; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: public Set getConflictingClassNames() { aoqi@0: return _conflictingClassNames; aoqi@0: } aoqi@0: aoqi@0: protected void postVisit(Model model) throws Exception { aoqi@0: for (Iterator iter = model.getExtraTypes(); iter.hasNext();) { aoqi@0: visitType((AbstractType)iter.next()); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: protected void preVisit(Service service) throws Exception { aoqi@0: registerClassName( aoqi@0: ((JavaInterface)service.getJavaInterface()).getName()); aoqi@0: // We don't generate Impl classes, commenting it out. aoqi@0: // Otherwise, it would cause naming conflicts aoqi@0: //registerClassName( aoqi@0: // ((JavaInterface)service.getJavaInterface()).getImpl()); aoqi@0: } aoqi@0: aoqi@0: protected void processPort11x(Port port){ aoqi@0: QName wsdlBindingName = (QName) port.getProperty( aoqi@0: ModelProperties.PROPERTY_WSDL_BINDING_NAME); aoqi@0: if (!_wsdlBindingNames.contains(wsdlBindingName)) { aoqi@0: aoqi@0: // multiple ports can share a binding without causing a conflict aoqi@0: registerClassName(port.getJavaInterface().getName()); aoqi@0: } aoqi@0: registerClassName((String) port.getProperty( aoqi@0: ModelProperties.PROPERTY_STUB_CLASS_NAME)); aoqi@0: registerClassName((String) port.getProperty( aoqi@0: ModelProperties.PROPERTY_TIE_CLASS_NAME)); aoqi@0: } aoqi@0: aoqi@0: protected void preVisit(Port port) throws Exception { aoqi@0: QName portTypeName = (QName)port.getProperty(ModelProperties.PROPERTY_WSDL_PORT_TYPE_NAME); aoqi@0: if(_portTypeNames.contains(portTypeName)) aoqi@0: return; aoqi@0: aoqi@0: //in 2.0, stub/tie class are binding agnostic so they should be per port, that is multiple aoqi@0: // bindings can share the same port aoqi@0: aoqi@0: addSEIClassName(port.getJavaInterface().getName()); aoqi@0: } aoqi@0: aoqi@0: private void addSEIClassName(String s) { aoqi@0: _seiClassNames.add(s); aoqi@0: registerClassName(s); aoqi@0: } aoqi@0: aoqi@0: protected void postVisit(Port port) throws Exception { aoqi@0: QName wsdlBindingName = (QName) port.getProperty( aoqi@0: ModelProperties.PROPERTY_WSDL_BINDING_NAME); aoqi@0: if (!_wsdlBindingNames.contains(wsdlBindingName)) { aoqi@0: _wsdlBindingNames.add(wsdlBindingName); aoqi@0: } aoqi@0: aoqi@0: QName portTypeName = (QName)port.getProperty(ModelProperties.PROPERTY_WSDL_PORT_TYPE_NAME); aoqi@0: if(!_portTypeNames.contains(portTypeName)){ aoqi@0: _portTypeNames.add(portTypeName); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: protected boolean shouldVisit(Port port) { aoqi@0: QName wsdlBindingName = (QName) port.getProperty( aoqi@0: ModelProperties.PROPERTY_WSDL_BINDING_NAME); aoqi@0: return !_wsdlBindingNames.contains(wsdlBindingName); aoqi@0: } aoqi@0: aoqi@0: protected void preVisit(Fault fault) throws Exception { aoqi@0: if (!_exceptions.contains(fault.getJavaException())) { aoqi@0: aoqi@0: /* the same exception can be used in several faults, but that aoqi@0: * doesn't mean that there is a conflict aoqi@0: */ aoqi@0: _exceptions.add(fault.getJavaException()); aoqi@0: addExceptionClassName(fault.getJavaException().getName()); aoqi@0: aoqi@0: for (Iterator iter = fault.getSubfaults(); aoqi@0: iter != null && iter.hasNext();) { aoqi@0: aoqi@0: Fault subfault = (Fault) iter.next(); aoqi@0: preVisit(subfault); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: private void addExceptionClassName(String name) { aoqi@0: if(_allClassNames.contains(name)) aoqi@0: _exceptionClassNames.add(name); aoqi@0: registerClassName(name); aoqi@0: //To change body of created methods use File | Settings | File Templates. aoqi@0: } aoqi@0: aoqi@0: protected void visitBodyBlock(Block block) throws Exception { aoqi@0: visitBlock(block); aoqi@0: } aoqi@0: aoqi@0: protected void visitHeaderBlock(Block block) throws Exception { aoqi@0: visitBlock(block); aoqi@0: } aoqi@0: aoqi@0: protected void visitFaultBlock(Block block) throws Exception { aoqi@0: } aoqi@0: aoqi@0: protected void visitBlock(Block block) throws Exception { aoqi@0: visitType(block.getType()); aoqi@0: } aoqi@0: aoqi@0: protected void visit(Parameter parameter) throws Exception { aoqi@0: visitType(parameter.getType()); aoqi@0: } aoqi@0: aoqi@0: private void visitType(AbstractType type) throws Exception { aoqi@0: if (type != null) { aoqi@0: if (type instanceof JAXBType) aoqi@0: visitType((JAXBType)type); aoqi@0: else if (type instanceof RpcLitStructure) aoqi@0: visitType((RpcLitStructure)type); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: private void visitType(JAXBType type) throws Exception { aoqi@0: type.accept(this); aoqi@0: } aoqi@0: aoqi@0: private void visitType(RpcLitStructure type) throws Exception { aoqi@0: type.accept(this); aoqi@0: } aoqi@0: private void registerClassName(String name) { aoqi@0: if (name == null || name.equals("")) { aoqi@0: return; aoqi@0: } aoqi@0: if (_allClassNames.contains(name)) { aoqi@0: _conflictingClassNames.add(name); aoqi@0: } else { aoqi@0: _allClassNames.add(name); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: public Set getSeiClassNames() { aoqi@0: return _seiClassNames; aoqi@0: } aoqi@0: aoqi@0: private Set _seiClassNames; aoqi@0: aoqi@0: public Set getJaxbGeneratedClassNames() { aoqi@0: return _jaxbGeneratedClassNames; aoqi@0: } aoqi@0: aoqi@0: private Set _jaxbGeneratedClassNames; aoqi@0: aoqi@0: aoqi@0: public Set getExceptionClassNames() { aoqi@0: return _exceptionClassNames; aoqi@0: } aoqi@0: aoqi@0: private Set _exceptionClassNames; aoqi@0: boolean doneVisitingJAXBModel = false; aoqi@0: public void visit(JAXBType type) throws Exception { aoqi@0: if(!doneVisitingJAXBModel && type.getJaxbModel() != null){ aoqi@0: Set classNames = type.getJaxbModel().getGeneratedClassNames(); aoqi@0: for(String className : classNames){ aoqi@0: addJAXBGeneratedClassName(className); aoqi@0: } aoqi@0: doneVisitingJAXBModel = true; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: public void visit(RpcLitStructure type) throws Exception { aoqi@0: if(!doneVisitingJAXBModel){ aoqi@0: Set classNames = type.getJaxbModel().getGeneratedClassNames(); aoqi@0: for(String className : classNames){ aoqi@0: addJAXBGeneratedClassName(className); aoqi@0: } aoqi@0: doneVisitingJAXBModel = true; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: private void addJAXBGeneratedClassName(String name) { aoqi@0: _jaxbGeneratedClassNames.add(name); aoqi@0: registerClassName(name); aoqi@0: } aoqi@0: aoqi@0: private Set _allClassNames; aoqi@0: private Set _exceptions; aoqi@0: private Set _wsdlBindingNames; aoqi@0: private Set _conflictingClassNames; aoqi@0: private Set _portTypeNames; aoqi@0: }