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