src/share/jaxws_classes/com/sun/tools/internal/ws/processor/generator/GeneratorBase.java

Tue, 09 Apr 2013 14:51:13 +0100

author
alanb
date
Tue, 09 Apr 2013 14:51:13 +0100
changeset 368
0989ad8c0860
parent 286
f50545b5e2f1
child 637
9c07ef4934dd
permissions
-rw-r--r--

8010393: Update JAX-WS RI to 2.2.9-b12941
Reviewed-by: alanb, erikj
Contributed-by: miroslav.kos@oracle.com, martin.grebac@oracle.com

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

mercurial