Wed, 27 Apr 2016 01:27:09 +0800
Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/jaxws/
changeset: 657:d47a47f961ee
tag: jdk8u25-b17
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. Oracle designates this |
aoqi@0 | 8 | * particular file as subject to the "Classpath" exception as provided |
aoqi@0 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
aoqi@0 | 10 | * |
aoqi@0 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 15 | * accompanied this code). |
aoqi@0 | 16 | * |
aoqi@0 | 17 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 20 | * |
aoqi@0 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 22 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 23 | * questions. |
aoqi@0 | 24 | */ |
aoqi@0 | 25 | |
aoqi@0 | 26 | package com.sun.tools.internal.ws.processor.generator; |
aoqi@0 | 27 | |
aoqi@0 | 28 | import com.sun.codemodel.internal.ClassType; |
aoqi@0 | 29 | import com.sun.codemodel.internal.JAnnotationUse; |
aoqi@0 | 30 | import com.sun.codemodel.internal.JClassAlreadyExistsException; |
aoqi@0 | 31 | import com.sun.codemodel.internal.JCodeModel; |
aoqi@0 | 32 | import com.sun.codemodel.internal.JDefinedClass; |
aoqi@0 | 33 | import com.sun.tools.internal.ws.ToolVersion; |
aoqi@0 | 34 | import com.sun.tools.internal.ws.processor.model.Block; |
aoqi@0 | 35 | import com.sun.tools.internal.ws.processor.model.Fault; |
aoqi@0 | 36 | import com.sun.tools.internal.ws.processor.model.Model; |
aoqi@0 | 37 | import com.sun.tools.internal.ws.processor.model.ModelVisitor; |
aoqi@0 | 38 | import com.sun.tools.internal.ws.processor.model.Operation; |
aoqi@0 | 39 | import com.sun.tools.internal.ws.processor.model.Parameter; |
aoqi@0 | 40 | import com.sun.tools.internal.ws.processor.model.Port; |
aoqi@0 | 41 | import com.sun.tools.internal.ws.processor.model.Request; |
aoqi@0 | 42 | import com.sun.tools.internal.ws.processor.model.Response; |
aoqi@0 | 43 | import com.sun.tools.internal.ws.processor.model.Service; |
aoqi@0 | 44 | import com.sun.tools.internal.ws.processor.util.DirectoryUtil; |
aoqi@0 | 45 | import com.sun.tools.internal.ws.processor.util.IndentingWriter; |
aoqi@0 | 46 | import com.sun.tools.internal.ws.wscompile.ErrorReceiver; |
aoqi@0 | 47 | import com.sun.tools.internal.ws.wscompile.WsimportOptions; |
aoqi@0 | 48 | import com.sun.xml.internal.ws.util.xml.XmlUtil; |
aoqi@0 | 49 | import org.w3c.dom.Element; |
aoqi@0 | 50 | import org.w3c.dom.NodeList; |
aoqi@0 | 51 | |
aoqi@0 | 52 | import javax.jws.HandlerChain; |
aoqi@0 | 53 | import javax.xml.transform.OutputKeys; |
aoqi@0 | 54 | import javax.xml.transform.Transformer; |
aoqi@0 | 55 | import javax.xml.transform.dom.DOMSource; |
aoqi@0 | 56 | import javax.xml.transform.stream.StreamResult; |
aoqi@0 | 57 | import java.io.File; |
aoqi@0 | 58 | import java.io.FileOutputStream; |
aoqi@0 | 59 | import java.io.OutputStreamWriter; |
aoqi@0 | 60 | import java.util.ArrayList; |
aoqi@0 | 61 | import java.util.Iterator; |
aoqi@0 | 62 | import java.util.List; |
aoqi@0 | 63 | import javax.annotation.processing.Filer; |
aoqi@0 | 64 | import javax.tools.FileObject; |
aoqi@0 | 65 | |
aoqi@0 | 66 | import javax.tools.StandardLocation; |
aoqi@0 | 67 | |
aoqi@0 | 68 | public abstract class GeneratorBase implements ModelVisitor { |
aoqi@0 | 69 | private File destDir; |
aoqi@0 | 70 | private String targetVersion; |
aoqi@0 | 71 | protected boolean donotOverride; |
aoqi@0 | 72 | protected JCodeModel cm; |
aoqi@0 | 73 | protected Model model; |
aoqi@0 | 74 | protected String wsdlLocation; |
aoqi@0 | 75 | protected ErrorReceiver receiver; |
aoqi@0 | 76 | protected WsimportOptions options; |
aoqi@0 | 77 | |
aoqi@0 | 78 | protected GeneratorBase() { |
aoqi@0 | 79 | } |
aoqi@0 | 80 | |
aoqi@0 | 81 | public void init(Model model, WsimportOptions options, ErrorReceiver receiver){ |
aoqi@0 | 82 | this.model = model; |
aoqi@0 | 83 | this.options = options; |
aoqi@0 | 84 | this.destDir = options.destDir; |
aoqi@0 | 85 | this.receiver = receiver; |
aoqi@0 | 86 | this.wsdlLocation = options.wsdlLocation; |
aoqi@0 | 87 | this.targetVersion = options.target.getVersion(); |
aoqi@0 | 88 | this.cm = options.getCodeModel(); |
aoqi@0 | 89 | } |
aoqi@0 | 90 | |
aoqi@0 | 91 | public void doGeneration() { |
aoqi@0 | 92 | try { |
aoqi@0 | 93 | model.accept(this); |
aoqi@0 | 94 | } catch (Exception e) { |
aoqi@0 | 95 | receiver.error(e); |
aoqi@0 | 96 | } |
aoqi@0 | 97 | } |
aoqi@0 | 98 | |
aoqi@0 | 99 | @Override |
aoqi@0 | 100 | public void visit(Model model) throws Exception { |
aoqi@0 | 101 | for (Service service : model.getServices()) { |
aoqi@0 | 102 | service.accept(this); |
aoqi@0 | 103 | } |
aoqi@0 | 104 | } |
aoqi@0 | 105 | |
aoqi@0 | 106 | @Override |
aoqi@0 | 107 | public void visit(Service service) throws Exception { |
aoqi@0 | 108 | for (Port port : service.getPorts()) { |
aoqi@0 | 109 | port.accept(this); |
aoqi@0 | 110 | } |
aoqi@0 | 111 | } |
aoqi@0 | 112 | |
aoqi@0 | 113 | @Override |
aoqi@0 | 114 | public void visit(Port port) throws Exception { |
aoqi@0 | 115 | for (Operation operation : port.getOperations()) { |
aoqi@0 | 116 | operation.accept(this); |
aoqi@0 | 117 | } |
aoqi@0 | 118 | } |
aoqi@0 | 119 | |
aoqi@0 | 120 | @Override |
aoqi@0 | 121 | public void visit(Operation operation) throws Exception { |
aoqi@0 | 122 | operation.getRequest().accept(this); |
aoqi@0 | 123 | if (operation.getResponse() != null) { |
aoqi@0 | 124 | operation.getResponse().accept(this); |
aoqi@0 | 125 | } |
aoqi@0 | 126 | Iterator faults = operation.getFaultsSet().iterator(); |
aoqi@0 | 127 | if (faults != null) { |
aoqi@0 | 128 | Fault fault; |
aoqi@0 | 129 | while (faults.hasNext()) { |
aoqi@0 | 130 | fault = (Fault) faults.next(); |
aoqi@0 | 131 | fault.accept(this); |
aoqi@0 | 132 | } |
aoqi@0 | 133 | } |
aoqi@0 | 134 | } |
aoqi@0 | 135 | |
aoqi@0 | 136 | @Override |
aoqi@0 | 137 | public void visit(Parameter param) throws Exception {} |
aoqi@0 | 138 | |
aoqi@0 | 139 | @Override |
aoqi@0 | 140 | public void visit(Block block) throws Exception {} |
aoqi@0 | 141 | |
aoqi@0 | 142 | @Override |
aoqi@0 | 143 | public void visit(Response response) throws Exception {} |
aoqi@0 | 144 | |
aoqi@0 | 145 | @Override |
aoqi@0 | 146 | public void visit(Request request) throws Exception {} |
aoqi@0 | 147 | |
aoqi@0 | 148 | @Override |
aoqi@0 | 149 | public void visit(Fault fault) throws Exception {} |
aoqi@0 | 150 | |
aoqi@0 | 151 | public List<String> getJAXWSClassComment(){ |
aoqi@0 | 152 | return getJAXWSClassComment(targetVersion); |
aoqi@0 | 153 | } |
aoqi@0 | 154 | |
aoqi@0 | 155 | public static List<String> getJAXWSClassComment(String targetVersion) { |
aoqi@0 | 156 | List<String> comments = new ArrayList<String>(); |
aoqi@0 | 157 | comments.add("This class was generated by the JAX-WS RI.\n"); |
aoqi@0 | 158 | comments.add(ToolVersion.VERSION.BUILD_VERSION+"\n"); |
aoqi@0 | 159 | comments.add("Generated source version: " + targetVersion); |
aoqi@0 | 160 | return comments; |
aoqi@0 | 161 | } |
aoqi@0 | 162 | |
aoqi@0 | 163 | protected JDefinedClass getClass(String className, ClassType type) throws JClassAlreadyExistsException { |
aoqi@0 | 164 | JDefinedClass cls; |
aoqi@0 | 165 | try { |
aoqi@0 | 166 | cls = cm._class(className, type); |
aoqi@0 | 167 | } catch (JClassAlreadyExistsException e){ |
aoqi@0 | 168 | cls = cm._getClass(className); |
aoqi@0 | 169 | if (cls == null) { |
aoqi@0 | 170 | throw e; |
aoqi@0 | 171 | } |
aoqi@0 | 172 | } |
aoqi@0 | 173 | return cls; |
aoqi@0 | 174 | } |
aoqi@0 | 175 | |
aoqi@0 | 176 | protected void log(String msg) { |
aoqi@0 | 177 | if (options.verbose) { |
aoqi@0 | 178 | System.out.println( |
aoqi@0 | 179 | "[" |
aoqi@0 | 180 | + Names.stripQualifier(this.getClass().getName()) |
aoqi@0 | 181 | + ": " |
aoqi@0 | 182 | + msg |
aoqi@0 | 183 | + "]"); |
aoqi@0 | 184 | } |
aoqi@0 | 185 | } |
aoqi@0 | 186 | |
aoqi@0 | 187 | protected void writeHandlerConfig(String className, JDefinedClass cls, WsimportOptions options) { |
aoqi@0 | 188 | Element e = options.getHandlerChainConfiguration(); |
aoqi@0 | 189 | if (e == null) { |
aoqi@0 | 190 | return; |
aoqi@0 | 191 | } |
aoqi@0 | 192 | JAnnotationUse handlerChainAnn = cls.annotate(cm.ref(HandlerChain.class)); |
aoqi@0 | 193 | NodeList nl = e.getElementsByTagNameNS( |
aoqi@0 | 194 | "http://java.sun.com/xml/ns/javaee", "handler-chain"); |
aoqi@0 | 195 | if(nl.getLength() > 0){ |
aoqi@0 | 196 | String fName = getHandlerConfigFileName(className); |
aoqi@0 | 197 | handlerChainAnn.param("file", fName); |
aoqi@0 | 198 | generateHandlerChainFile(e, className); |
aoqi@0 | 199 | } |
aoqi@0 | 200 | } |
aoqi@0 | 201 | |
aoqi@0 | 202 | private String getHandlerConfigFileName(String fullName){ |
aoqi@0 | 203 | String name = Names.stripQualifier(fullName); |
aoqi@0 | 204 | return name+"_handler.xml"; |
aoqi@0 | 205 | } |
aoqi@0 | 206 | |
aoqi@0 | 207 | private void generateHandlerChainFile(Element hChains, String name) { |
aoqi@0 | 208 | |
aoqi@0 | 209 | Filer filer = options.filer; |
aoqi@0 | 210 | |
aoqi@0 | 211 | try { |
aoqi@0 | 212 | IndentingWriter p; |
aoqi@0 | 213 | FileObject jfo; |
aoqi@0 | 214 | if (filer != null) { |
aoqi@0 | 215 | jfo = filer.createResource(StandardLocation.SOURCE_OUTPUT, |
aoqi@0 | 216 | Names.getPackageName(name), getHandlerConfigFileName(name)); |
aoqi@0 | 217 | options.addGeneratedFile(new File(jfo.toUri())); |
aoqi@0 | 218 | p = new IndentingWriter(new OutputStreamWriter(jfo.openOutputStream())); |
aoqi@0 | 219 | } else { // leave for backw. compatibility now |
aoqi@0 | 220 | String hcName = getHandlerConfigFileName(name); |
aoqi@0 | 221 | File packageDir = DirectoryUtil.getOutputDirectoryFor(name, destDir); |
aoqi@0 | 222 | File hcFile = new File(packageDir, hcName); |
aoqi@0 | 223 | options.addGeneratedFile(hcFile); |
aoqi@0 | 224 | p = new IndentingWriter(new OutputStreamWriter(new FileOutputStream(hcFile))); |
aoqi@0 | 225 | } |
aoqi@0 | 226 | |
aoqi@0 | 227 | Transformer it = XmlUtil.newTransformer(); |
aoqi@0 | 228 | |
aoqi@0 | 229 | it.setOutputProperty(OutputKeys.METHOD, "xml"); |
aoqi@0 | 230 | it.setOutputProperty(OutputKeys.INDENT, "yes"); |
aoqi@0 | 231 | it.setOutputProperty( |
aoqi@0 | 232 | "{http://xml.apache.org/xslt}indent-amount", |
aoqi@0 | 233 | "2"); |
aoqi@0 | 234 | it.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); |
aoqi@0 | 235 | it.transform( new DOMSource(hChains), new StreamResult(p) ); |
aoqi@0 | 236 | p.close(); |
aoqi@0 | 237 | } catch (Exception e) { |
aoqi@0 | 238 | throw new GeneratorException( |
aoqi@0 | 239 | "generator.nestedGeneratorError", |
aoqi@0 | 240 | e); |
aoqi@0 | 241 | } |
aoqi@0 | 242 | } |
aoqi@0 | 243 | |
aoqi@0 | 244 | } |