Author: ppoddar
Date: Thu Apr 15 18:33:24 2010
New Revision: 934511

URL: http://svn.apache.org/viewvc?rev=934511&view=rev
Log:
OPENJPA-1628: Usability changes. Merged from trunk revision 934507

Removed:
    openjpa/branches/2.0.x/scripts/mmg.options
Modified:
    
openjpa/branches/2.0.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/meta/AnnotationProcessor6.java
    
openjpa/branches/2.0.x/openjpa-project/src/doc/manual/jpa_overview_criteria.xml
    openjpa/branches/2.0.x/scripts/mmg.bat

Modified: 
openjpa/branches/2.0.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/meta/AnnotationProcessor6.java
URL: 
http://svn.apache.org/viewvc/openjpa/branches/2.0.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/meta/AnnotationProcessor6.java?rev=934511&r1=934510&r2=934511&view=diff
==============================================================================
--- 
openjpa/branches/2.0.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/meta/AnnotationProcessor6.java
 (original)
+++ 
openjpa/branches/2.0.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/meta/AnnotationProcessor6.java
 Thu Apr 15 18:33:24 2010
@@ -20,14 +20,10 @@ package org.apache.openjpa.persistence.m
 
 import static javax.lang.model.SourceVersion.RELEASE_6;
 
-import java.io.File;
 import java.io.IOException;
-import java.io.OutputStream;
 import java.io.PrintWriter;
-import java.net.URI;
 import java.net.URL;
 import java.util.Arrays;
-import java.util.Collections;
 import java.util.Date;
 import java.util.List;
 import java.util.Map;
@@ -47,11 +43,7 @@ import javax.lang.model.type.TypeMirror;
 import javax.lang.model.util.Elements;
 import javax.persistence.metamodel.StaticMetamodel;
 import javax.tools.Diagnostic;
-import javax.tools.JavaCompiler;
 import javax.tools.JavaFileObject;
-import javax.tools.StandardJavaFileManager;
-import javax.tools.StandardLocation;
-import javax.tools.ToolProvider;
 
 import org.apache.openjpa.lib.util.Localizer;
 import org.apache.openjpa.meta.MetaDataFactory;
@@ -66,39 +58,29 @@ import org.apache.openjpa.persistence.ut
  * This tool is invoked during compilation for JDK6 compiler if 
  * <UL>
  * <LI>OpenJPA and JPA libraries are available in the compiler classpath
- * and <LI>Annotation Processor option <code>-Aopenjpa.generate=true</code> is 
specified.
+ * and <LI>Annotation Processor option <code>-Aopenjpa.metamodel=true</code> 
is specified.
  * </UL>
  * <br>
  * <B>Usage</B><br>
- * <code>$ javac -classpath path/to/openjpa-all.jar -Aopenjpa.generated=true 
mypackage/MyEntity.java</code><br>
+ * <code>$ javac -classpath path/to/openjpa-all.jar -Aopenjpa.metamodel=true 
mypackage/MyEntity.java</code><br>
  * will generate source code for canonical meta-model class 
<code>mypackage.MyEntity_.java</code>.
+ * The source code is generated relative to the directory specified in 
<code>-s</code> option
+ * of <code>javac</code> compiler and defaulted to the current directory.
  * <p>
- * The Annotation Processor also recognizes the following options (none of 
them are mandatory).
- * Each of the following option key can also be prefixed with 
<code>openjpa.</code> to distinguish if multiple 
- * annotation processors are active during compilation:<br>
+ * The Annotation Processor also recognizes the following options (none of 
them are mandatory):<br>
  * <TABLE border="1">
- * <TR><TD>-Alog={log level}<TD>The logging level. Default is 
<code>WARN</code>. Permissible values are 
+ * <TR><TD>-Aopenjpa.log={log level}<TD>The logging level. Default is 
<code>WARN</code>. Permissible values are 
  *     <code>TRACE</code>, <code>INFO</code>, <code>WARN</code> or <code> 
ERROR</code>.
- * <TR><TD>-Asource={n}         <TD>Java source version of the generated code. 
Default is <code>6</code>.
- * <TR><TD>-Anaming={class name}        <TD>fully-qualified name of a class 
implementing 
+ * <TR><TD>-Aopenjpa.source={n}          <TD>Java source version of the 
generated code. Default is <code>6</code>.
+ * <TR><TD>-Aopenjpa.naming={class name} <TD>fully-qualified name of a class 
implementing 
  * <code>org.apache.openjpa.meta.MetaDataFactory</code> that determines
  * the name of a meta-class given the name of the original persistent Java 
entity class. Defaults to
  * <code>org.apache.openjpa.persistence.PersistenceMetaDataFactory</code> 
which appends a underscore character
  * (<code>_</code>) to the original Java class name. 
- * <TR><TD>-Aheader={url}           <TD>
+ * <TR><TD>-Aopenjpa.header={url}        <TD>
  * A url whose content will appear as comment header to the generated file(s). 
Recognizes special value
  * <code>ASL</code> for Apache Source License header as comment. By default 
adds a OpenJPA proprietary   
  * text.
- * <TR><TD>-Aout={dir}                      <TD>
- * A directory in the local file system. The generated files will be written 
<em>relative</em> to this directory
- * according to the package structure i.e. if <code>dir</code> is specified as 
<code>/myproject/generated-src</code>
- * then the generated source code will be written to 
<code>/myproject/generated-src/mypackage/MyEntity_.java</code>.
- * If this option is not specified, then an attempt will be made to write the 
generated source file in the same
- * directory of the source code of original class 
<code>mypackage.MyEntity</code>. The source code location for 
- * <code>mypackage.MyEntity</code> can only be determined for Sun JDK6 and 
<code>tools.jar</code> being available 
- * to the compiler classpath. If the source code location for the original 
class can not be determined, and the 
- * option is not specified, then the generated source code is written relative 
to the current directory according 
- * to the package structure.  
  * </TABLE>
  * <br>
  *
@@ -111,19 +93,16 @@ import org.apache.openjpa.persistence.ut
     "javax.persistence.Entity",
     "javax.persistence.Embeddable", 
     "javax.persistence.MappedSuperclass" })
-...@supportedoptions({ "openjpa.log", "log", 
-                     "openjpa.out", "out", 
-                     "openjpa.source", "source",
-                     "openjpa.naming", "naming",
-                     "openjpa.header", "header",
-                     "openjpa.generate"
+...@supportedoptions({ "openjpa.log", 
+                    "openjpa.source",
+                    "openjpa.naming",
+                    "openjpa.header",
+                    "openjpa.metamodel"
                   })
 @SupportedSourceVersion(RELEASE_6)
 
 public class AnnotationProcessor6 extends AbstractProcessor {
     private SourceAnnotationHandler handler;
-    private StandardJavaFileManager fileManager;
-    private boolean isUserSpecifiedOutputLocation;
     private MetaDataFactory factory;
     private int generatedSourceVersion = 6;
     private CompileTimeLogger logger;
@@ -206,13 +185,12 @@ public class AnnotationProcessor6 extend
     @Override
     public synchronized void init(ProcessingEnvironment processingEnv) {
         super.init(processingEnv);
-        active = "true".equalsIgnoreCase(getOptionValue("openjpa.generate"));
+        active = "true".equalsIgnoreCase(getOptionValue("openjpa.metamodel"));
         if (!active)
             return;
         processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, 
_loc.get("mmg-tool-banner").toString());
-        logger = new CompileTimeLogger(processingEnv, 
getOptionValue("openjpa.log", "log"));
+        logger = new CompileTimeLogger(processingEnv, 
getOptionValue("openjpa.log"));
         setSourceVersion();
-        setFileManager();
         setNamingPolicy();
         setHeader();
         handler = new SourceAnnotationHandler(processingEnv, logger);
@@ -252,8 +230,7 @@ public class AnnotationProcessor6 extend
         annotate(source, originalClass);
         TypeElement supCls = handler.getPersistentSupertype(e);
         if (supCls != null) {
-            String superName = factory.getMetaModelClassName(
-                    supCls.toString());
+            String superName = 
factory.getMetaModelClassName(supCls.toString());
             source.getTopLevelClass().setSuper(superName);
         }
         try {
@@ -327,12 +304,12 @@ public class AnnotationProcessor6 extend
     }
     
     /**
-     * Parse annotation processor option <code>-Asource=n</code> to detect
+     * Parse annotation processor option <code>-Aopenjpa.source=n</code> to 
detect
      * the source version for the generated classes. 
      * n must be a integer. Default or wrong specification returns 6.
      */
     private void setSourceVersion() {
-        String version = getOptionValue("openjpa.source", "source");
+        String version = getOptionValue("openjpa.source");
         if (version != null) {
             try {
                 generatedSourceVersion = Integer.parseInt(version);
@@ -346,7 +323,7 @@ public class AnnotationProcessor6 extend
     }
     
     private void setNamingPolicy() {
-        String policy = getOptionValue("openjpa.naming","naming");
+        String policy = getOptionValue("openjpa.naming");
         if (policy != null) {
             try {
                 factory = (MetaDataFactory)Class.forName(policy).newInstance();
@@ -360,7 +337,7 @@ public class AnnotationProcessor6 extend
     }
     
     private void setHeader() {
-        String headerOption = getOptionValue("openjpa.header", "header");
+        String headerOption = getOptionValue("openjpa.header");
         if (headerOption == null) {
             return;
         }
@@ -376,54 +353,15 @@ public class AnnotationProcessor6 extend
         }
     }
     
-    private void setFileManager() {
-        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
-        fileManager = compiler.getStandardFileManager(null, null, null);
-        String outDir = getOptionValue("openjpa.out", "out");
-        if (outDir != null)
-           isUserSpecifiedOutputLocation = setSourceOutputDirectory(new 
File(outDir));
-    }
-
     /**
      * Creates a file where source code of the given metaClass will be written.
      * 
      */
     private PrintWriter createSourceFile(String originalClass, String 
metaClass, TypeElement e) 
         throws IOException {
-        if (!isUserSpecifiedOutputLocation) {
-            
setSourceOutputDirectory(OutputPath.getAbsoluteDirectory(processingEnv, e));
-        }
-        JavaFileObject javaFile = 
fileManager.getJavaFileForOutput(StandardLocation.SOURCE_OUTPUT, 
-            metaClass, JavaFileObject.Kind.SOURCE, 
-            null); // do not use sibling hint because of indeterminable 
behavior across JDK 
-        logger.info(_loc.get("mmg-process", javaFile.toUri()));
-        OutputStream out = javaFile.openOutputStream();
-        PrintWriter writer = new PrintWriter(out);
-        return writer;
-    }
-    
-    /**
-     * Sets the output directory for generated source files.
-     * Tries to create the directory structure if does not exist.
-     * 
-     * @return true if the output has been set successfully.
-     */
-    boolean setSourceOutputDirectory(File outDir) {
-        if (outDir == null)
-            return false;
-        if (!outDir.exists()) {
-            if (!outDir.mkdirs()) {
-                logger.warn(_loc.get("mmg-bad-out", outDir, 
StandardLocation.SOURCE_OUTPUT));
-                return false;
-            }
-        }
-        try {
-            fileManager.setLocation(StandardLocation.SOURCE_OUTPUT, 
Collections.singleton(outDir));
-            return true;
-        } catch (IOException e) {
-            logger.warn(_loc.get("mmg-bad-out", outDir, 
StandardLocation.SOURCE_OUTPUT));
-            return false;
-        }
+        JavaFileObject javaFile = 
processingEnv.getFiler().createSourceFile(metaClass, e);
+        logger.info(_loc.get("mmg-process", javaFile.toUri().normalize()));
+        return new PrintWriter(javaFile.openWriter());
     }
     
     /**
@@ -437,97 +375,4 @@ public class AnnotationProcessor6 extend
         }
         return null;
     }
-    
-    /**
-     * An utility class to determine the source file corresponding to a 
{...@link TypeElement}.
-     * The utility uses Sun JDK internal API (com.sun.tools.*) and hence works 
reflectively
-     * to avoid compile-time dependency.
-     *   
-     * @author Pinaki Poddar
-     *
-     */
-    public static class OutputPath {
-        private static Class<?> trees = null;
-        static {
-            try {
-                trees = Class.forName("com.sun.source.util.Trees");
-            } catch (Throwable t) {
-                
-            }
-        }
-        
-        /**
-         * Gets the directory relative to the Java source file corresponding 
to the TypeElement.
-         * 
-         * @return null if the com.sun.source.util.* package is not available 
or the given TypeElement
-         * does not correspond to a compilation unit associated to a source 
file.
-         */
-        public static File getAbsoluteDirectory(ProcessingEnvironment env, 
TypeElement e) {
-            if (trees == null)
-                return null;
-            try {
-                // Trees root = Trees.instance(env);
-                Object root = trees.getMethod("instance", new 
Class[]{ProcessingEnvironment.class})
-                    .invoke(null, env);
-                
-                // TreePath path = root.getPath(e);
-                Object path = root.getClass().getMethod("getPath", new 
Class[]{Element.class})
-                    .invoke(root, e);
-                
-                // CompilationUnitTree unit = path.getCompilationUnit();
-                Object unit = path.getClass().getMethod("getCompilationUnit", 
(Class[])null)
-                    .invoke(path, (Object[])null);
-                
-                // JavaFileObject f = unit.getSourceFile();
-                JavaFileObject f = 
(JavaFileObject)unit.getClass().getMethod("getSourceFile", (Class[])null)
-                    .invoke(unit, (Object[])null);
-                
-                URI uri = f.toUri();
-                File dir = getParentFile(new File(uri.toURL().getPath()), 
-                        packageDepth(e.getQualifiedName().toString()));
-                return dir;
-            } catch (Throwable t) {
-                return null;
-            }
-        }
-        
-        /**
-         * Gets the parent of the given file recursively traversing to given 
number of levels.
-         */
-        public static File getParentFile(File f, int n) {
-            if (n < 0)
-                return f;
-            if (n == 0)
-                return f.getParentFile();
-            return getParentFile(f.getParentFile(), n-1);
-        }
-        
-        public static int packageDepth(String s) {
-            String pkg = getPackageName(s);
-            if (pkg == null)
-                return 0;
-            int depth = 1;
-            int i = 0;
-            while ((i = pkg.indexOf('.')) != -1) {
-                depth++;
-                pkg = pkg.substring(i+1);
-            }
-            return depth;
-        }
-        
-        public static String getPackageName(String s) {
-            if (s == null)
-                return null;
-            int i = s.lastIndexOf('.');
-            return (i == -1) ? null : s.substring(0, i);
-        }
-        
-        public static String getSimpleName(String s) {
-            if (s == null)
-                return null;
-            int i = s.lastIndexOf('.');
-            return (i == -1) ? s : s.substring(i+1);
-        }
-    }
-
 }

Modified: 
openjpa/branches/2.0.x/openjpa-project/src/doc/manual/jpa_overview_criteria.xml
URL: 
http://svn.apache.org/viewvc/openjpa/branches/2.0.x/openjpa-project/src/doc/manual/jpa_overview_criteria.xml?rev=934511&r1=934510&r2=934511&view=diff
==============================================================================
--- 
openjpa/branches/2.0.x/openjpa-project/src/doc/manual/jpa_overview_criteria.xml 
(original)
+++ 
openjpa/branches/2.0.x/openjpa-project/src/doc/manual/jpa_overview_criteria.xml 
Thu Apr 15 18:33:24 2010
@@ -168,32 +168,31 @@ Annotation processing tool generates sou
 the annotated source code of persistent entity. 
 This tool is invoked during compilation for JDK6 compiler if OpenJPA and JPA 
 libraries are specified in the compiler <code>-classpath</code> option 
<emphasis>and</emphasis>
-Annotation processor option <code>-Aopenjpa.generate=true</code> is specified.
+Annotation processor option <code>-Aopenjpa.metamodel=true</code> is specified.
     <programlisting>
- $ javac -classpath path/to/openjpa-all.jar -Aopenjpa.generate=true 
mypackage/MyEntity.java
+ $ javac -classpath path/to/openjpa-all.jar -Aopenjpa.metamodel=true 
mypackage/MyEntity.java
     </programlisting>
 will generate source code for canonical meta-model class 
<code>mypackage.MyEntity_.java</code>.
       </para>
       
       <para>
 The Annotation Processor recognizes the following options specified in the 
command-line with <code>-A</code> 
-(none of them are mandatory). Each of the following option key can also be 
prefixed with <code>openjpa.</code> 
-to distinguish if multiple annotation processors are active during compilation.
+(none of them are mandatory).
     <itemizedlist>
         <listitem>
                        <para>
-              -Alog=TRACE|INFO|WARN|ERROR : The logging level. Default is 
<code>WARN</code>.
+              -Aopenjpa.log=TRACE|INFO|WARN|ERROR : The logging level. Default 
is <code>WARN</code>.
             </para>
         </listitem>
         <listitem>
                        <para>
-             -Asource=&lt;n&gt; : where &lt;n&gt; denotes the integral number 
for Java source 
+             -Aopenjpa.source=&lt;n&gt; : where &lt;n&gt; denotes the integral 
number for Java source 
              version of the generated code. Default is <code>6</code>.
              </para>
         </listitem>
         <listitem>
                        <para>
-               -Anaming=class name : fully-qualified name of a class 
implementing 
+               -Aopenjpa.naming=class name : fully-qualified name of a class 
implementing 
                <code>org.apache.openjpa.meta.MetaDataFactory</code> that 
determines
 the name of a meta-class given the name of the original persistent Java entity 
class. Defaults to
 <code>org.apache.openjpa.persistence.PersistenceMetaDataFactory</code> which 
appends a underscore character
@@ -202,7 +201,7 @@ the name of a meta-class given the name 
         </listitem>
         <listitem>
                        <para>
-               -Aheader=&lt;url&gt; : A url whose content will appear as 
comment header to the generated file(s). 
+               -Aopenjpa.header=&lt;url&gt; : A url whose content will appear 
as comment header to the generated file(s). 
                Recognizes special value <code>ASL</code> for Apache Source 
License header as comment. 
                By default, adds a OpenJPA proprietary text as comment block.
              </para>

Modified: openjpa/branches/2.0.x/scripts/mmg.bat
URL: 
http://svn.apache.org/viewvc/openjpa/branches/2.0.x/scripts/mmg.bat?rev=934511&r1=934510&r2=934511&view=diff
==============================================================================
--- openjpa/branches/2.0.x/scripts/mmg.bat (original)
+++ openjpa/branches/2.0.x/scripts/mmg.bat Thu Apr 15 18:33:24 2010
@@ -21,34 +21,45 @@
 @rem Example Batch script to generate canonical meta-model classes
 @rem
 @rem Usage
-...@rem   $ mmg.bat <options.file> <class.list>
+...@rem   $ mmg.bat <class.list>
 @rem 
 @rem The canonical meta-model classes can be generated during compilation of
 @rem domain classes. This batch file compiles a set of classes (X.java) listed 
-...@rem in <class.list> file. The compiler is invoked with an annotation
-...@rem processor which generates a meta-model class X_.java for each X.java. 
-...@rem The options for annotation processor is specified in <options.file>.
+...@rem in <class.list> file. The compiler discoveres the annotation
+...@rem processor if openjpa classes are in classpath. The discovered 
annotation
+...@rem processor, however, is active only if -Aopenjpa.metamodel=true is set. 
 
 @rem 
 @rem See also 
-...@rem    mmg.options       : The options to Javac compiler 
 @rem    domain-class.list : The domain classes to be compiled
 @rem 
---------------------------------------------------------------------------
 @echo off
 setlocal
-set JAVA_HOME=c:\java\jdk1.6.0_10
 set JAVAC=%JAVA_HOME%\bin\javac
 
+...@rem 
---------------------------------------------------------------------------
+...@rem Compiler classpath shown for a typical OpenJPA development environment 
in Windows. 
+...@rem The essential aspect is openjpa libraries must be in the compiler's 
classpath.
 set M_REPO="C:\Documents and Settings\Administrator\.m2\repository"
 set SPEC=geronimo-jpa_2.0_spec
 set VERSION=1.0-EA9-SNAPSHOT
 set 
JPA_LIB=%M_REPO%\org\apache\geronimo\specs\%SPEC%\%VERSION%\%SPEC%-%VERSION%.jar
 
 set CLASSPATH=%JPA_LIB%
-set CLASSPATH=%CLASSPATH%;.\openjpa-lib\target\classes
-set CLASSPATH=%CLASSPATH%;.\openjpa-persistence\src\main\resources
-set CLASSPATH=%CLASSPATH%;.\openjpa-persistence\target\classes
-set CLASSPATH=%CLASSPATH%;.\openjpa-kernel\target\classes
+set CLASSPATH=%CLASSPATH%;..\openjpa\src\main\resources
+set CLASSPATH=%CLASSPATH%;..\openjpa-persistence\target\classes
+set CLASSPATH=%CLASSPATH%;..\openjpa-kernel\target\classes
+set CLASSPATH=%CLASSPATH%;..\openjpa-lib\target\classes
+
+...@rem 
---------------------------------------------------------------------------
+echo Using Java Compiler %JAVAC%
+%JAVAC% -version
+
+...@rem 
---------------------------------------------------------------------------
+...@rem Root directory for of the generated source files. Specified as -s 
option 
+set GEN_DIR=../openjpa-persistence-jdbc/src/test/java
 
-%JAVAC% -cp %CLASSPATH% @%1 @%2
+...@rem Only one option is shown for logging. Other available options are 
documented in
+...@rem OpenJPA User Manual and JavaDoc
+%JAVAC% -cp %CLASSPATH% -s %GEN_DIR% -Aopenjpa.metamodel=true 
-Aopenjpa.log=TRACE @%1
 
 endlocal


Reply via email to