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

Print this page

        

@@ -1,7 +1,7 @@
 /*
- * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License version 2 only, as
  * published by the Free Software Foundation.  Oracle designates this

@@ -23,10 +23,11 @@
  * questions.
  */
 
 package com.sun.tools.internal.ws.processor.modeler.annotation;
 
+import com.sun.istack.internal.logging.Logger;
 import com.sun.tools.internal.ws.processor.generator.GeneratorUtil;
 import com.sun.tools.internal.ws.processor.modeler.ModelerException;
 import com.sun.tools.internal.ws.resources.WebserviceapMessages;
 import com.sun.tools.internal.ws.wscompile.AbortException;
 import com.sun.tools.internal.ws.wscompile.WsgenOptions;

@@ -34,11 +35,10 @@
 import javax.annotation.processing.AbstractProcessor;
 import javax.annotation.processing.ProcessingEnvironment;
 import javax.annotation.processing.RoundEnvironment;
 import javax.annotation.processing.SupportedAnnotationTypes;
 import javax.annotation.processing.SupportedOptions;
-import javax.annotation.processing.SupportedSourceVersion;
 import javax.jws.WebService;
 import javax.lang.model.SourceVersion;
 import javax.lang.model.element.Element;
 import javax.lang.model.element.ElementKind;
 import javax.lang.model.element.Name;

@@ -49,16 +49,19 @@
 import javax.xml.ws.Holder;
 import javax.xml.ws.WebServiceProvider;
 import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.PrintStream;
+import java.lang.reflect.Method;
 import java.rmi.Remote;
 import java.rmi.RemoteException;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.HashSet;
+import java.util.Scanner;
 import java.util.Set;
+import java.util.logging.Level;
 
 /**
  * WebServiceAp is a AnnotationProcessor for processing javax.jws.* and
  * javax.xml.ws.* annotations. This class is used either by the WsGen (CompileTool) tool or
  * indirectly when invoked by javac.

@@ -85,13 +88,14 @@
         "javax.xml.ws.WebServiceClient",
         "javax.xml.ws.WebServiceProvider",
         "javax.xml.ws.WebServiceRef"
 })
 @SupportedOptions({WebServiceAp.DO_NOT_OVERWRITE, WebServiceAp.IGNORE_NO_WEB_SERVICE_FOUND_WARNING})
-@SupportedSourceVersion(SourceVersion.RELEASE_6)
 public class WebServiceAp extends AbstractProcessor implements ModelBuilder {
 
+    private static final Logger LOGGER = Logger.getLogger(WebServiceAp.class);
+
     public static final String DO_NOT_OVERWRITE = "doNotOverWrite";
     public static final String IGNORE_NO_WEB_SERVICE_FOUND_WARNING = "ignoreNoWebServiceFoundWarning";
 
     private WsgenOptions options;
     protected AnnotationProcessorContext context;

@@ -118,11 +122,11 @@
         this.context = new AnnotationProcessorContext();
         this.out = out;
     }
 
     @Override
-    public void init(ProcessingEnvironment processingEnv) {
+    public synchronized void init(ProcessingEnvironment processingEnv) {
         super.init(processingEnv);
         remoteElement = processingEnv.getElementUtils().getTypeElement(Remote.class.getName());
         remoteExceptionElement = processingEnv.getElementUtils().getTypeElement(RemoteException.class.getName()).asType();
         exceptionElement = processingEnv.getElementUtils().getTypeElement(Exception.class.getName()).asType();
         runtimeExceptionElement = processingEnv.getElementUtils().getTypeElement(RuntimeException.class.getName()).asType();

@@ -133,22 +137,71 @@
             out = new PrintStream(new ByteArrayOutputStream());
 
             doNotOverWrite = getOption(DO_NOT_OVERWRITE);
             ignoreNoWebServiceFoundWarning = getOption(IGNORE_NO_WEB_SERVICE_FOUND_WARNING);
 
-            String property = System.getProperty("sun.java.command"); // todo: check if property can be null
-            options.verbose = property != null && property.contains("-verbose");
-            // todo: check how to get -s and -d, -classpath options
-            String classDir = ".";
-            sourceDir = new File(classDir);
-            property = System.getProperty("java.class.path");
+            String classDir = parseArguments();
+            String property = System.getProperty("java.class.path");
             options.classpath = classDir + File.pathSeparator + (property != null ? property : "");
             isCommandLineInvocation = true;
         }
         options.filer = processingEnv.getFiler();
     }
 
+    private String parseArguments() {
+        // let's try to parse JavacOptions
+
+        String classDir = null;
+        try {
+            ClassLoader cl = WebServiceAp.class.getClassLoader();
+            Class javacProcessingEnvironmentClass = Class.forName("com.sun.tools.javac.processing.JavacProcessingEnvironment", false, cl);
+            if (javacProcessingEnvironmentClass.isInstance(processingEnv)) {
+                Method getContextMethod = javacProcessingEnvironmentClass.getDeclaredMethod("getContext");
+                Object tmpContext = getContextMethod.invoke(processingEnv);
+                Class optionsClass = Class.forName("com.sun.tools.javac.util.Options", false, cl);
+                Class contextClass = Class.forName("com.sun.tools.javac.util.Context", false, cl);
+                Method instanceMethod = optionsClass.getDeclaredMethod("instance", new Class[]{contextClass});
+                Object tmpOptions = instanceMethod.invoke(null, tmpContext);
+                if (tmpOptions != null) {
+                    Method getMethod = optionsClass.getDeclaredMethod("get", new Class[]{String.class});
+                    Object result = getMethod.invoke(tmpOptions, "-s"); // todo: we have to check for -d also
+                    if (result != null) {
+                        classDir = (String) result;
+                    }
+                    this.options.verbose = getMethod.invoke(tmpOptions, "-verbose") != null;
+                }
+            }
+        } catch (Exception e) {
+            /// some Error was here - problems with reflection or security
+            processWarning(WebserviceapMessages.WEBSERVICEAP_PARSING_JAVAC_OPTIONS_ERROR());
+            report(e.getMessage());
+        }
+
+        if (classDir == null) { // some error within reflection block
+            String property = System.getProperty("sun.java.command");
+            if (property != null) {
+                Scanner scanner = new Scanner(property);
+                boolean sourceDirNext = false;
+                while (scanner.hasNext()) {
+                    String token = scanner.next();
+                    if (sourceDirNext) {
+                        classDir = token;
+                        sourceDirNext = false;
+                    } else if ("-verbose".equals(token)) {
+                        options.verbose = true;
+                    } else if ("-s".equals(token)) {
+                        sourceDirNext = true;
+                    }
+                }
+            }
+        }
+        if (classDir != null) {
+            sourceDir = new File(classDir);
+        }
+        return classDir;
+    }
+
     private boolean getOption(String key) {
         String value = processingEnv.getOptions().get(key);
         if (value != null) {
             return Boolean.valueOf(value);
         }

@@ -184,12 +237,13 @@
             element.accept(webServiceVisitor, null);
             processedEndpoint = true;
         }
         if (!processedEndpoint) {
             if (isCommandLineInvocation) {
-                if (!ignoreNoWebServiceFoundWarning)
+                if (!ignoreNoWebServiceFoundWarning) {
                     processWarning(WebserviceapMessages.WEBSERVICEAP_NO_WEBSERVICE_ENDPOINT_FOUND());
+                }
             } else {
                 processError(WebserviceapMessages.WEBSERVICEAP_NO_WEBSERVICE_ENDPOINT_FOUND());
             }
         }
         return true;

@@ -212,13 +266,18 @@
             report(message);
         }
     }
 
     protected void report(String msg) {
-        PrintStream outStream = out != null ? out : new PrintStream(out, true);
-        outStream.println(msg);
-        outStream.flush();
+        if (out == null) {
+            if (LOGGER.isLoggable(Level.FINE)) {
+                LOGGER.log(Level.FINE, "No output set for web service annotation processor reporting.");
+            }
+            return;
+        }
+        out.println(msg);
+        out.flush();
     }
 
     @Override
     public void processError(String message) {
         if (isCommandLineInvocation) {

@@ -294,6 +353,11 @@
 
     @Override
     public String getOperationName(Name messageName) {
         return messageName != null ? messageName.toString() : null;
     }
+
+    @Override
+    public SourceVersion getSupportedSourceVersion() {
+        return SourceVersion.latest();
+    }
 }