Wednesday, September 18, 2013

(yet another) How to enhance OpenJPA classes with TomEE 1.5.2 using Eclipse

import java.io.File;
import java.util.Collection;

import org.apache.commons.io.FileUtils;

/**
 * OpenJPA relies on a class "enhancement" process that is not fully supported in a easy way for eclipse users.
 *
 * <p>Before writing this script, I've considered the following actions
 *
 * <ol>
 * <li>Trying the <a href="http://openjpa.apache.org/openjpaeclipseinstallation.html">eclipse openJPA plugin</a> - the plugin UI hides the last
 * check button and seems not to work properly.
 * <li>Trying the <a href="http://openjpa.apache.org/enhancement-with-eclipse.html">recommended ant builder</a>. I would have to change the
 * project structure
 * <li>Adding <a href="http://webspherepersistence.blogspot.com.br/2009/02/openjpa-enhancement.html">a user-agent parameter to the VM</a>
 * - but at the time of this writing, TomEE 1.5.2 (the "stable" one) had a bug that the javaagent doesn't work (according to Romain), so
 * they fixed in the 1.6.0 trunk, which does not seems acceptable for any project that goes in production for a client.
 * <li>Switching to IBM JDK 7 (J9) virtual machines. Although they default to subclass for enhancement, which is not recommended, the
 * system seems to work with them, for both Windows and Linux, but the gotcha here is that for Windows, IBM distributes the JDK in a
 * "<a href="http://www.ibm.com/developerworks/java/jdk/eclipse/">IBM Development Package for Eclipse</a>" which deals with the problem gracefully
 * . I consider this as an acceptable alternative, but remember that using the IBM JDK is no the same
 * as actually enhancing the classes, as recommended by OpenJPA.
 * </ol>
 *
 * <p>So please forgive me if this procedure is cumbersome.
 *
 * <p>Now, before running standalone tests and migration procedures from main() methods or from JUnit @Test annotated methods, you must
 * run this procedure below to enhance the classes. The same for the web app, but using the destination path used by the server you're
 * running from inside eclipse. And of course, before deploying...
 *
 * <p>September 2013
 *
 *
 */
public class Enhancer {

    public static void main(String[] args) {

        String baseClassDir = "/path/to/eclipse/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/yourapp/WEB-INF/classes/";
//        String baseClassDir = "/path/to/eclipse/workspace/yourapp/build/classes/";
        Collection<File> files = FileUtils.listFiles(new File(baseClassDir), null, true);
        for(File file:files){
            String[] a = new String[3];
            a[0] = "-d";
            a[1] = baseClassDir;
            a[2] = file.getAbsolutePath();
            org.apache.openjpa.enhance.PCEnhancer.main(a);
        }
    }

}

No comments:

Post a Comment