src/share/jaxws_classes/com/sun/tools/internal/ws/processor/modeler/annotation/WebServiceAp.java

changeset 368
0989ad8c0860
parent 286
f50545b5e2f1
child 637
9c07ef4934dd
equal deleted inserted replaced
366:8c0b6bccfe47 368:0989ad8c0860
1 /* 1 /*
2 * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved. 2 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 * 4 *
5 * This code is free software; you can redistribute it and/or modify it 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 6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this 7 * published by the Free Software Foundation. Oracle designates this
23 * questions. 23 * questions.
24 */ 24 */
25 25
26 package com.sun.tools.internal.ws.processor.modeler.annotation; 26 package com.sun.tools.internal.ws.processor.modeler.annotation;
27 27
28 import com.sun.istack.internal.logging.Logger;
28 import com.sun.tools.internal.ws.processor.generator.GeneratorUtil; 29 import com.sun.tools.internal.ws.processor.generator.GeneratorUtil;
29 import com.sun.tools.internal.ws.processor.modeler.ModelerException; 30 import com.sun.tools.internal.ws.processor.modeler.ModelerException;
30 import com.sun.tools.internal.ws.resources.WebserviceapMessages; 31 import com.sun.tools.internal.ws.resources.WebserviceapMessages;
31 import com.sun.tools.internal.ws.wscompile.AbortException; 32 import com.sun.tools.internal.ws.wscompile.AbortException;
32 import com.sun.tools.internal.ws.wscompile.WsgenOptions; 33 import com.sun.tools.internal.ws.wscompile.WsgenOptions;
34 import javax.annotation.processing.AbstractProcessor; 35 import javax.annotation.processing.AbstractProcessor;
35 import javax.annotation.processing.ProcessingEnvironment; 36 import javax.annotation.processing.ProcessingEnvironment;
36 import javax.annotation.processing.RoundEnvironment; 37 import javax.annotation.processing.RoundEnvironment;
37 import javax.annotation.processing.SupportedAnnotationTypes; 38 import javax.annotation.processing.SupportedAnnotationTypes;
38 import javax.annotation.processing.SupportedOptions; 39 import javax.annotation.processing.SupportedOptions;
39 import javax.annotation.processing.SupportedSourceVersion;
40 import javax.jws.WebService; 40 import javax.jws.WebService;
41 import javax.lang.model.SourceVersion; 41 import javax.lang.model.SourceVersion;
42 import javax.lang.model.element.Element; 42 import javax.lang.model.element.Element;
43 import javax.lang.model.element.ElementKind; 43 import javax.lang.model.element.ElementKind;
44 import javax.lang.model.element.Name; 44 import javax.lang.model.element.Name;
49 import javax.xml.ws.Holder; 49 import javax.xml.ws.Holder;
50 import javax.xml.ws.WebServiceProvider; 50 import javax.xml.ws.WebServiceProvider;
51 import java.io.ByteArrayOutputStream; 51 import java.io.ByteArrayOutputStream;
52 import java.io.File; 52 import java.io.File;
53 import java.io.PrintStream; 53 import java.io.PrintStream;
54 import java.lang.reflect.Method;
54 import java.rmi.Remote; 55 import java.rmi.Remote;
55 import java.rmi.RemoteException; 56 import java.rmi.RemoteException;
56 import java.util.ArrayList; 57 import java.util.ArrayList;
57 import java.util.Collection; 58 import java.util.Collection;
58 import java.util.HashSet; 59 import java.util.HashSet;
60 import java.util.Scanner;
59 import java.util.Set; 61 import java.util.Set;
62 import java.util.logging.Level;
60 63
61 /** 64 /**
62 * WebServiceAp is a AnnotationProcessor for processing javax.jws.* and 65 * WebServiceAp is a AnnotationProcessor for processing javax.jws.* and
63 * javax.xml.ws.* annotations. This class is used either by the WsGen (CompileTool) tool or 66 * javax.xml.ws.* annotations. This class is used either by the WsGen (CompileTool) tool or
64 * indirectly when invoked by javac. 67 * indirectly when invoked by javac.
85 "javax.xml.ws.WebServiceClient", 88 "javax.xml.ws.WebServiceClient",
86 "javax.xml.ws.WebServiceProvider", 89 "javax.xml.ws.WebServiceProvider",
87 "javax.xml.ws.WebServiceRef" 90 "javax.xml.ws.WebServiceRef"
88 }) 91 })
89 @SupportedOptions({WebServiceAp.DO_NOT_OVERWRITE, WebServiceAp.IGNORE_NO_WEB_SERVICE_FOUND_WARNING}) 92 @SupportedOptions({WebServiceAp.DO_NOT_OVERWRITE, WebServiceAp.IGNORE_NO_WEB_SERVICE_FOUND_WARNING})
90 @SupportedSourceVersion(SourceVersion.RELEASE_6)
91 public class WebServiceAp extends AbstractProcessor implements ModelBuilder { 93 public class WebServiceAp extends AbstractProcessor implements ModelBuilder {
94
95 private static final Logger LOGGER = Logger.getLogger(WebServiceAp.class);
92 96
93 public static final String DO_NOT_OVERWRITE = "doNotOverWrite"; 97 public static final String DO_NOT_OVERWRITE = "doNotOverWrite";
94 public static final String IGNORE_NO_WEB_SERVICE_FOUND_WARNING = "ignoreNoWebServiceFoundWarning"; 98 public static final String IGNORE_NO_WEB_SERVICE_FOUND_WARNING = "ignoreNoWebServiceFoundWarning";
95 99
96 private WsgenOptions options; 100 private WsgenOptions options;
118 this.context = new AnnotationProcessorContext(); 122 this.context = new AnnotationProcessorContext();
119 this.out = out; 123 this.out = out;
120 } 124 }
121 125
122 @Override 126 @Override
123 public void init(ProcessingEnvironment processingEnv) { 127 public synchronized void init(ProcessingEnvironment processingEnv) {
124 super.init(processingEnv); 128 super.init(processingEnv);
125 remoteElement = processingEnv.getElementUtils().getTypeElement(Remote.class.getName()); 129 remoteElement = processingEnv.getElementUtils().getTypeElement(Remote.class.getName());
126 remoteExceptionElement = processingEnv.getElementUtils().getTypeElement(RemoteException.class.getName()).asType(); 130 remoteExceptionElement = processingEnv.getElementUtils().getTypeElement(RemoteException.class.getName()).asType();
127 exceptionElement = processingEnv.getElementUtils().getTypeElement(Exception.class.getName()).asType(); 131 exceptionElement = processingEnv.getElementUtils().getTypeElement(Exception.class.getName()).asType();
128 runtimeExceptionElement = processingEnv.getElementUtils().getTypeElement(RuntimeException.class.getName()).asType(); 132 runtimeExceptionElement = processingEnv.getElementUtils().getTypeElement(RuntimeException.class.getName()).asType();
133 out = new PrintStream(new ByteArrayOutputStream()); 137 out = new PrintStream(new ByteArrayOutputStream());
134 138
135 doNotOverWrite = getOption(DO_NOT_OVERWRITE); 139 doNotOverWrite = getOption(DO_NOT_OVERWRITE);
136 ignoreNoWebServiceFoundWarning = getOption(IGNORE_NO_WEB_SERVICE_FOUND_WARNING); 140 ignoreNoWebServiceFoundWarning = getOption(IGNORE_NO_WEB_SERVICE_FOUND_WARNING);
137 141
138 String property = System.getProperty("sun.java.command"); // todo: check if property can be null 142 String classDir = parseArguments();
139 options.verbose = property != null && property.contains("-verbose"); 143 String property = System.getProperty("java.class.path");
140 // todo: check how to get -s and -d, -classpath options
141 String classDir = ".";
142 sourceDir = new File(classDir);
143 property = System.getProperty("java.class.path");
144 options.classpath = classDir + File.pathSeparator + (property != null ? property : ""); 144 options.classpath = classDir + File.pathSeparator + (property != null ? property : "");
145 isCommandLineInvocation = true; 145 isCommandLineInvocation = true;
146 } 146 }
147 options.filer = processingEnv.getFiler(); 147 options.filer = processingEnv.getFiler();
148 }
149
150 private String parseArguments() {
151 // let's try to parse JavacOptions
152
153 String classDir = null;
154 try {
155 ClassLoader cl = WebServiceAp.class.getClassLoader();
156 Class javacProcessingEnvironmentClass = Class.forName("com.sun.tools.javac.processing.JavacProcessingEnvironment", false, cl);
157 if (javacProcessingEnvironmentClass.isInstance(processingEnv)) {
158 Method getContextMethod = javacProcessingEnvironmentClass.getDeclaredMethod("getContext");
159 Object tmpContext = getContextMethod.invoke(processingEnv);
160 Class optionsClass = Class.forName("com.sun.tools.javac.util.Options", false, cl);
161 Class contextClass = Class.forName("com.sun.tools.javac.util.Context", false, cl);
162 Method instanceMethod = optionsClass.getDeclaredMethod("instance", new Class[]{contextClass});
163 Object tmpOptions = instanceMethod.invoke(null, tmpContext);
164 if (tmpOptions != null) {
165 Method getMethod = optionsClass.getDeclaredMethod("get", new Class[]{String.class});
166 Object result = getMethod.invoke(tmpOptions, "-s"); // todo: we have to check for -d also
167 if (result != null) {
168 classDir = (String) result;
169 }
170 this.options.verbose = getMethod.invoke(tmpOptions, "-verbose") != null;
171 }
172 }
173 } catch (Exception e) {
174 /// some Error was here - problems with reflection or security
175 processWarning(WebserviceapMessages.WEBSERVICEAP_PARSING_JAVAC_OPTIONS_ERROR());
176 report(e.getMessage());
177 }
178
179 if (classDir == null) { // some error within reflection block
180 String property = System.getProperty("sun.java.command");
181 if (property != null) {
182 Scanner scanner = new Scanner(property);
183 boolean sourceDirNext = false;
184 while (scanner.hasNext()) {
185 String token = scanner.next();
186 if (sourceDirNext) {
187 classDir = token;
188 sourceDirNext = false;
189 } else if ("-verbose".equals(token)) {
190 options.verbose = true;
191 } else if ("-s".equals(token)) {
192 sourceDirNext = true;
193 }
194 }
195 }
196 }
197 if (classDir != null) {
198 sourceDir = new File(classDir);
199 }
200 return classDir;
148 } 201 }
149 202
150 private boolean getOption(String key) { 203 private boolean getOption(String key) {
151 String value = processingEnv.getOptions().get(key); 204 String value = processingEnv.getOptions().get(key);
152 if (value != null) { 205 if (value != null) {
184 element.accept(webServiceVisitor, null); 237 element.accept(webServiceVisitor, null);
185 processedEndpoint = true; 238 processedEndpoint = true;
186 } 239 }
187 if (!processedEndpoint) { 240 if (!processedEndpoint) {
188 if (isCommandLineInvocation) { 241 if (isCommandLineInvocation) {
189 if (!ignoreNoWebServiceFoundWarning) 242 if (!ignoreNoWebServiceFoundWarning) {
190 processWarning(WebserviceapMessages.WEBSERVICEAP_NO_WEBSERVICE_ENDPOINT_FOUND()); 243 processWarning(WebserviceapMessages.WEBSERVICEAP_NO_WEBSERVICE_ENDPOINT_FOUND());
244 }
191 } else { 245 } else {
192 processError(WebserviceapMessages.WEBSERVICEAP_NO_WEBSERVICE_ENDPOINT_FOUND()); 246 processError(WebserviceapMessages.WEBSERVICEAP_NO_WEBSERVICE_ENDPOINT_FOUND());
193 } 247 }
194 } 248 }
195 return true; 249 return true;
212 report(message); 266 report(message);
213 } 267 }
214 } 268 }
215 269
216 protected void report(String msg) { 270 protected void report(String msg) {
217 PrintStream outStream = out != null ? out : new PrintStream(out, true); 271 if (out == null) {
218 outStream.println(msg); 272 if (LOGGER.isLoggable(Level.FINE)) {
219 outStream.flush(); 273 LOGGER.log(Level.FINE, "No output set for web service annotation processor reporting.");
274 }
275 return;
276 }
277 out.println(msg);
278 out.flush();
220 } 279 }
221 280
222 @Override 281 @Override
223 public void processError(String message) { 282 public void processError(String message) {
224 if (isCommandLineInvocation) { 283 if (isCommandLineInvocation) {
294 353
295 @Override 354 @Override
296 public String getOperationName(Name messageName) { 355 public String getOperationName(Name messageName) {
297 return messageName != null ? messageName.toString() : null; 356 return messageName != null ? messageName.toString() : null;
298 } 357 }
358
359 @Override
360 public SourceVersion getSupportedSourceVersion() {
361 return SourceVersion.latest();
362 }
299 } 363 }

mercurial