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

Tue, 06 Mar 2012 16:09:35 -0800

author
ohair
date
Tue, 06 Mar 2012 16:09:35 -0800
changeset 286
f50545b5e2f1
child 368
0989ad8c0860
permissions
-rw-r--r--

7150322: Stop using drop source bundles in jaxws
Reviewed-by: darcy, ohrstrom

ohair@286 1 /*
ohair@286 2 * Copyright (c) 1997, 2010, 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;
ohair@286 63
ohair@286 64 /**
ohair@286 65 *
ohair@286 66 * @author WS Development Team
ohair@286 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
ohair@286 99 public void visit(Model model) throws Exception {
ohair@286 100 for (Service service : model.getServices()) {
ohair@286 101 service.accept(this);
ohair@286 102 }
ohair@286 103 }
ohair@286 104
ohair@286 105 public void visit(Service service) throws Exception {
ohair@286 106 for (Port port : service.getPorts()) {
ohair@286 107 port.accept(this);
ohair@286 108 }
ohair@286 109 }
ohair@286 110
ohair@286 111 public void visit(Port port) throws Exception {
ohair@286 112 for (Operation operation : port.getOperations()) {
ohair@286 113 operation.accept(this);
ohair@286 114 }
ohair@286 115 }
ohair@286 116
ohair@286 117 public void visit(Operation operation) throws Exception {
ohair@286 118 operation.getRequest().accept(this);
ohair@286 119 if (operation.getResponse() != null)
ohair@286 120 operation.getResponse().accept(this);
ohair@286 121 Iterator faults = operation.getFaultsSet().iterator();
ohair@286 122 if (faults != null) {
ohair@286 123 Fault fault;
ohair@286 124 while (faults.hasNext()) {
ohair@286 125 fault = (Fault) faults.next();
ohair@286 126 fault.accept(this);
ohair@286 127 }
ohair@286 128 }
ohair@286 129 }
ohair@286 130
ohair@286 131 public void visit(Parameter param) throws Exception {
ohair@286 132 }
ohair@286 133
ohair@286 134 public void visit(Block block) throws Exception {
ohair@286 135 }
ohair@286 136
ohair@286 137 public void visit(Response response) throws Exception {
ohair@286 138 }
ohair@286 139
ohair@286 140
ohair@286 141 public void visit(Request request) throws Exception {
ohair@286 142 }
ohair@286 143
ohair@286 144 public void visit(Fault fault) throws Exception {
ohair@286 145 }
ohair@286 146
ohair@286 147 public List<String> getJAXWSClassComment(){
ohair@286 148 return getJAXWSClassComment(targetVersion);
ohair@286 149 }
ohair@286 150
ohair@286 151 public static List<String> getJAXWSClassComment(String targetVersion) {
ohair@286 152 List<String> comments = new ArrayList<String>();
ohair@286 153 comments.add("This class was generated by the JAX-WS RI.\n");
ohair@286 154 comments.add(ToolVersion.VERSION.BUILD_VERSION+"\n");
ohair@286 155 comments.add("Generated source version: " + targetVersion);
ohair@286 156 return comments;
ohair@286 157 }
ohair@286 158
ohair@286 159 protected JDefinedClass getClass(String className, ClassType type) throws JClassAlreadyExistsException {
ohair@286 160 JDefinedClass cls;
ohair@286 161 try {
ohair@286 162 cls = cm._class(className, type);
ohair@286 163 } catch (JClassAlreadyExistsException e){
ohair@286 164 cls = cm._getClass(className);
ohair@286 165 if(cls == null)
ohair@286 166 throw e;
ohair@286 167 }
ohair@286 168 return cls;
ohair@286 169 }
ohair@286 170
ohair@286 171 protected void log(String msg) {
ohair@286 172 if (options.verbose) {
ohair@286 173 System.out.println(
ohair@286 174 "["
ohair@286 175 + Names.stripQualifier(this.getClass().getName())
ohair@286 176 + ": "
ohair@286 177 + msg
ohair@286 178 + "]");
ohair@286 179 }
ohair@286 180 }
ohair@286 181
ohair@286 182 protected void writeHandlerConfig(String className, JDefinedClass cls, WsimportOptions options) {
ohair@286 183 Element e = options.getHandlerChainConfiguration();
ohair@286 184 if(e == null)
ohair@286 185 return;
ohair@286 186 JAnnotationUse handlerChainAnn = cls.annotate(cm.ref(HandlerChain.class));
ohair@286 187 NodeList nl = e.getElementsByTagNameNS(
ohair@286 188 "http://java.sun.com/xml/ns/javaee", "handler-chain");
ohair@286 189 if(nl.getLength() > 0){
ohair@286 190 String fName = getHandlerConfigFileName(className);
ohair@286 191 handlerChainAnn.param("file", fName);
ohair@286 192 generateHandlerChainFile(e, className);
ohair@286 193 }
ohair@286 194 }
ohair@286 195
ohair@286 196 private String getHandlerConfigFileName(String fullName){
ohair@286 197 String name = Names.stripQualifier(fullName);
ohair@286 198 return name+"_handler.xml";
ohair@286 199 }
ohair@286 200
ohair@286 201 private void generateHandlerChainFile(Element hChains, String name) {
ohair@286 202 String hcName = getHandlerConfigFileName(name);
ohair@286 203
ohair@286 204 File packageDir = DirectoryUtil.getOutputDirectoryFor(name, destDir);
ohair@286 205 File hcFile = new File(packageDir, hcName);
ohair@286 206
ohair@286 207 options.addGeneratedFile(hcFile);
ohair@286 208
ohair@286 209 try {
ohair@286 210 IndentingWriter p =
ohair@286 211 new IndentingWriter(
ohair@286 212 new OutputStreamWriter(new FileOutputStream(hcFile)));
ohair@286 213 Transformer it = XmlUtil.newTransformer();
ohair@286 214
ohair@286 215 it.setOutputProperty(OutputKeys.METHOD, "xml");
ohair@286 216 it.setOutputProperty(OutputKeys.INDENT, "yes");
ohair@286 217 it.setOutputProperty(
ohair@286 218 "{http://xml.apache.org/xslt}indent-amount",
ohair@286 219 "2");
ohair@286 220 it.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
ohair@286 221 it.transform( new DOMSource(hChains), new StreamResult(p) );
ohair@286 222 p.close();
ohair@286 223 } catch (Exception e) {
ohair@286 224 throw new GeneratorException(
ohair@286 225 "generator.nestedGeneratorError",
ohair@286 226 e);
ohair@286 227 }
ohair@286 228 }
ohair@286 229
ohair@286 230 }

mercurial