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; |
|
55 import java.rmi.Remote; |
|
56 import java.rmi.RemoteException; |
|
57 import java.util.ArrayList; |
54 import java.util.ArrayList; |
58 import java.util.Collection; |
55 import java.util.Collection; |
59 import java.util.HashSet; |
56 import java.util.HashSet; |
60 import java.util.Scanner; |
|
61 import java.util.Set; |
57 import java.util.Set; |
62 import java.util.logging.Level; |
58 import java.util.logging.Level; |
63 |
59 |
64 /** |
60 /** |
65 * WebServiceAp is a AnnotationProcessor for processing javax.jws.* and |
61 * WebServiceAp is a AnnotationProcessor for processing javax.jws.* and |
87 "javax.xml.ws.WebFault", |
83 "javax.xml.ws.WebFault", |
88 "javax.xml.ws.WebServiceClient", |
84 "javax.xml.ws.WebServiceClient", |
89 "javax.xml.ws.WebServiceProvider", |
85 "javax.xml.ws.WebServiceProvider", |
90 "javax.xml.ws.WebServiceRef" |
86 "javax.xml.ws.WebServiceRef" |
91 }) |
87 }) |
92 @SupportedOptions({WebServiceAp.DO_NOT_OVERWRITE, WebServiceAp.IGNORE_NO_WEB_SERVICE_FOUND_WARNING}) |
88 @SupportedOptions({WebServiceAp.DO_NOT_OVERWRITE, WebServiceAp.IGNORE_NO_WEB_SERVICE_FOUND_WARNING, WebServiceAp.VERBOSE}) |
93 public class WebServiceAp extends AbstractProcessor implements ModelBuilder { |
89 public class WebServiceAp extends AbstractProcessor implements ModelBuilder { |
94 |
90 |
95 private static final Logger LOGGER = Logger.getLogger(WebServiceAp.class); |
91 private static final Logger LOGGER = Logger.getLogger(WebServiceAp.class); |
96 |
92 |
97 public static final String DO_NOT_OVERWRITE = "doNotOverWrite"; |
93 public static final String DO_NOT_OVERWRITE = "doNotOverWrite"; |
98 public static final String IGNORE_NO_WEB_SERVICE_FOUND_WARNING = "ignoreNoWebServiceFoundWarning"; |
94 public static final String IGNORE_NO_WEB_SERVICE_FOUND_WARNING = "ignoreNoWebServiceFoundWarning"; |
|
95 public static final String VERBOSE = "verbose"; |
99 |
96 |
100 private WsgenOptions options; |
97 private WsgenOptions options; |
101 protected AnnotationProcessorContext context; |
98 protected AnnotationProcessorContext context; |
102 private File sourceDir; |
99 private File sourceDir; |
103 private boolean doNotOverWrite; |
100 private boolean doNotOverWrite; |
104 private boolean ignoreNoWebServiceFoundWarning = false; |
101 private boolean ignoreNoWebServiceFoundWarning = false; |
105 private TypeElement remoteElement; |
|
106 private TypeMirror remoteExceptionElement; |
|
107 private TypeMirror exceptionElement; |
102 private TypeMirror exceptionElement; |
108 private TypeMirror runtimeExceptionElement; |
103 private TypeMirror runtimeExceptionElement; |
109 private TypeElement defHolderElement; |
104 private TypeElement defHolderElement; |
110 private boolean isCommandLineInvocation; |
105 private boolean isCommandLineInvocation; |
111 private PrintStream out; |
106 private PrintStream out; |
124 } |
119 } |
125 |
120 |
126 @Override |
121 @Override |
127 public synchronized void init(ProcessingEnvironment processingEnv) { |
122 public synchronized void init(ProcessingEnvironment processingEnv) { |
128 super.init(processingEnv); |
123 super.init(processingEnv); |
129 remoteElement = processingEnv.getElementUtils().getTypeElement(Remote.class.getName()); |
|
130 remoteExceptionElement = processingEnv.getElementUtils().getTypeElement(RemoteException.class.getName()).asType(); |
|
131 exceptionElement = processingEnv.getElementUtils().getTypeElement(Exception.class.getName()).asType(); |
124 exceptionElement = processingEnv.getElementUtils().getTypeElement(Exception.class.getName()).asType(); |
132 runtimeExceptionElement = processingEnv.getElementUtils().getTypeElement(RuntimeException.class.getName()).asType(); |
125 runtimeExceptionElement = processingEnv.getElementUtils().getTypeElement(RuntimeException.class.getName()).asType(); |
133 defHolderElement = processingEnv.getElementUtils().getTypeElement(Holder.class.getName()); |
126 defHolderElement = processingEnv.getElementUtils().getTypeElement(Holder.class.getName()); |
134 if (options == null) { |
127 if (options == null) { |
135 options = new WsgenOptions(); |
128 options = new WsgenOptions(); |
136 |
129 |
137 out = new PrintStream(new ByteArrayOutputStream()); |
130 out = new PrintStream(new ByteArrayOutputStream()); |
138 |
|
139 doNotOverWrite = getOption(DO_NOT_OVERWRITE); |
131 doNotOverWrite = getOption(DO_NOT_OVERWRITE); |
140 ignoreNoWebServiceFoundWarning = getOption(IGNORE_NO_WEB_SERVICE_FOUND_WARNING); |
132 ignoreNoWebServiceFoundWarning = getOption(IGNORE_NO_WEB_SERVICE_FOUND_WARNING); |
141 |
133 options.verbose = getOption(VERBOSE); |
142 String classDir = parseArguments(); |
|
143 String property = System.getProperty("java.class.path"); |
|
144 options.classpath = classDir + File.pathSeparator + (property != null ? property : ""); |
|
145 isCommandLineInvocation = true; |
134 isCommandLineInvocation = true; |
146 } |
135 } |
147 options.filer = processingEnv.getFiler(); |
136 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; |
|
201 } |
137 } |
202 |
138 |
203 private boolean getOption(String key) { |
139 private boolean getOption(String key) { |
204 String value = processingEnv.getOptions().get(key); |
140 String value = processingEnv.getOptions().get(key); |
205 if (value != null) { |
141 if (value != null) { |
307 return sourceDir; |
243 return sourceDir; |
308 } |
244 } |
309 |
245 |
310 @Override |
246 @Override |
311 public boolean isRemote(TypeElement typeElement) { |
247 public boolean isRemote(TypeElement typeElement) { |
312 return processingEnv.getTypeUtils().isSubtype(typeElement.asType(), remoteElement.asType()); |
248 return TypeModeler.isRemote(typeElement); |
313 } |
249 } |
314 |
250 |
315 @Override |
251 @Override |
316 public boolean isServiceException(TypeMirror typeMirror) { |
252 public boolean isServiceException(TypeMirror typeMirror) { |
317 return processingEnv.getTypeUtils().isSubtype(typeMirror, exceptionElement) |
253 return processingEnv.getTypeUtils().isSubtype(typeMirror, exceptionElement) |
318 && !processingEnv.getTypeUtils().isSubtype(typeMirror, runtimeExceptionElement) |
254 && !processingEnv.getTypeUtils().isSubtype(typeMirror, runtimeExceptionElement) |
319 && !processingEnv.getTypeUtils().isSubtype(typeMirror, remoteExceptionElement); |
255 && !TypeModeler.isRemoteException(processingEnv, typeMirror); |
320 } |
256 } |
321 |
257 |
322 @Override |
258 @Override |
323 public TypeMirror getHolderValueType(TypeMirror type) { |
259 public TypeMirror getHolderValueType(TypeMirror type) { |
324 return TypeModeler.getHolderValueType(type, defHolderElement, processingEnv); |
260 return TypeModeler.getHolderValueType(type, defHolderElement, processingEnv); |