You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by db...@apache.org on 2012/02/27 00:52:35 UTC

svn commit: r1293969 - in /openejb/trunk/openejb/container: ./ openejb-jaxbagent/ openejb-jaxbagent/src/ openejb-jaxbagent/src/main/ openejb-jaxbagent/src/main/java/ openejb-jaxbagent/src/main/java/org/ openejb-jaxbagent/src/main/java/org/apache/ opene...

Author: dblevins
Date: Sun Feb 26 23:52:34 2012
New Revision: 1293969

URL: http://svn.apache.org/viewvc?rev=1293969&view=rev
Log:
TOMEE-143: JAXB Performance boost via saving generated accessors

Added:
    openejb/trunk/openejb/container/openejb-jaxbagent/
    openejb/trunk/openejb/container/openejb-jaxbagent/pom.xml
      - copied, changed from r1293453, openejb/trunk/openejb/container/openejb-javaagent/pom.xml
    openejb/trunk/openejb/container/openejb-jaxbagent/src/
    openejb/trunk/openejb/container/openejb-jaxbagent/src/main/
    openejb/trunk/openejb/container/openejb-jaxbagent/src/main/java/
    openejb/trunk/openejb/container/openejb-jaxbagent/src/main/java/org/
    openejb/trunk/openejb/container/openejb-jaxbagent/src/main/java/org/apache/
    openejb/trunk/openejb/container/openejb-jaxbagent/src/main/java/org/apache/openejb/
    openejb/trunk/openejb/container/openejb-jaxbagent/src/main/java/org/apache/openejb/jaxbagent/
    openejb/trunk/openejb/container/openejb-jaxbagent/src/main/java/org/apache/openejb/jaxbagent/Agent.java
    openejb/trunk/openejb/container/openejb-jee-accessors/
    openejb/trunk/openejb/container/openejb-jee-accessors/pom.xml   (with props)
    openejb/trunk/openejb/container/openejb-jee-accessors/src/
    openejb/trunk/openejb/container/openejb-jee-accessors/src/test/
    openejb/trunk/openejb/container/openejb-jee-accessors/src/test/java/
    openejb/trunk/openejb/container/openejb-jee-accessors/src/test/java/org/
    openejb/trunk/openejb/container/openejb-jee-accessors/src/test/java/org/apache/
    openejb/trunk/openejb/container/openejb-jee-accessors/src/test/java/org/apache/openejb/
    openejb/trunk/openejb/container/openejb-jee-accessors/src/test/java/org/apache/openejb/jee/
    openejb/trunk/openejb/container/openejb-jee-accessors/src/test/java/org/apache/openejb/jee/accessors/
    openejb/trunk/openejb/container/openejb-jee-accessors/src/test/java/org/apache/openejb/jee/accessors/GenerateAccessorsTest.java
Modified:
    openejb/trunk/openejb/container/openejb-jee/src/main/java/org/apache/openejb/jee/JAXBContextFactory.java
    openejb/trunk/openejb/container/pom.xml

Copied: openejb/trunk/openejb/container/openejb-jaxbagent/pom.xml (from r1293453, openejb/trunk/openejb/container/openejb-javaagent/pom.xml)
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-jaxbagent/pom.xml?p2=openejb/trunk/openejb/container/openejb-jaxbagent/pom.xml&p1=openejb/trunk/openejb/container/openejb-javaagent/pom.xml&r1=1293453&r2=1293969&rev=1293969&view=diff
==============================================================================
--- openejb/trunk/openejb/container/openejb-javaagent/pom.xml (original)
+++ openejb/trunk/openejb/container/openejb-jaxbagent/pom.xml Sun Feb 26 23:52:34 2012
@@ -23,8 +23,8 @@
     <version>4.0.0-beta-3-SNAPSHOT</version>
   </parent>
   <modelVersion>4.0.0</modelVersion>
-  <artifactId>openejb-javaagent</artifactId>
-  <name>OpenEJB :: Container :: Java Agent</name>
+  <artifactId>openejb-jaxbagent</artifactId>
+  <name>OpenEJB :: Container :: JAXB Java Agent</name>
   <build>
     <plugins>
       <plugin>
@@ -33,8 +33,8 @@
         <configuration>
           <archive>
             <manifestEntries>
-              <Premain-Class>org.apache.openejb.javaagent.Agent</Premain-Class>
-              <Agent-Class>org.apache.openejb.javaagent.Agent</Agent-Class>
+              <Premain-Class>org.apache.openejb.jaxbagent.Agent</Premain-Class>
+              <Agent-Class>org.apache.openejb.jaxbagent.Agent</Agent-Class>
               <Can-Redefine-Classes>true</Can-Redefine-Classes>
               <Can-Retransform-Classes>true</Can-Retransform-Classes>
             </manifestEntries>

Added: openejb/trunk/openejb/container/openejb-jaxbagent/src/main/java/org/apache/openejb/jaxbagent/Agent.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-jaxbagent/src/main/java/org/apache/openejb/jaxbagent/Agent.java?rev=1293969&view=auto
==============================================================================
--- openejb/trunk/openejb/container/openejb-jaxbagent/src/main/java/org/apache/openejb/jaxbagent/Agent.java (added)
+++ openejb/trunk/openejb/container/openejb-jaxbagent/src/main/java/org/apache/openejb/jaxbagent/Agent.java Sun Feb 26 23:52:34 2012
@@ -0,0 +1,96 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.openejb.jaxbagent;
+
+import java.io.BufferedOutputStream;
+import java.io.Closeable;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.Flushable;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.lang.instrument.ClassFileTransformer;
+import java.lang.instrument.IllegalClassFormatException;
+import java.lang.instrument.Instrumentation;
+import java.security.ProtectionDomain;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class Agent {
+    private static String agentArgs;
+    private static Instrumentation instrumentation;
+
+    public static void premain(String agentArgs, Instrumentation instrumentation) {
+        if (Agent.instrumentation != null) return;
+
+        Agent.agentArgs = agentArgs;
+        Agent.instrumentation = instrumentation;
+        final File output = new File(agentArgs);
+        instrumentation.addTransformer(new WriteClassesTransformer(output));
+    }
+
+    public static void agentmain(String agentArgs, Instrumentation instrumentation) {
+        if (Agent.instrumentation != null) return;
+
+        Agent.agentArgs = agentArgs;
+        Agent.instrumentation = instrumentation;
+    }
+
+    private static class WriteClassesTransformer implements ClassFileTransformer {
+
+        private final File output;
+
+        public WriteClassesTransformer(File output) {
+            this.output = output;
+        }
+
+        public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
+
+            if (!className.contains("$JaxbAccessor")) return classfileBuffer;
+
+            try {
+                final File file = new File(output, className.replace('.', File.separatorChar) + ".class");
+                file.getParentFile().mkdirs();
+                final OutputStream write = new BufferedOutputStream(new FileOutputStream(file), classfileBuffer.length);
+                try {
+                    write.write(classfileBuffer);
+                    write.flush();
+                } finally {
+                    close(write);
+                }
+            } catch (IOException e) {
+            }
+            return classfileBuffer;
+        }
+
+        public void close(Closeable closeable) {
+            if (closeable == null) return;
+            try {
+                if (closeable instanceof Flushable) {
+                    ((Flushable) closeable).flush();
+                }
+            } catch (IOException e) {
+            }
+            try {
+                closeable.close();
+            } catch (IOException e) {
+            }
+        }
+
+    }
+}

Added: openejb/trunk/openejb/container/openejb-jee-accessors/pom.xml
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-jee-accessors/pom.xml?rev=1293969&view=auto
==============================================================================
--- openejb/trunk/openejb/container/openejb-jee-accessors/pom.xml (added)
+++ openejb/trunk/openejb/container/openejb-jee-accessors/pom.xml Sun Feb 26 23:52:34 2012
@@ -0,0 +1,101 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+    Licensed to the Apache Software Foundation (ASF) under one or more
+    contributor license agreements.  See the NOTICE file distributed with
+    this work for additional information regarding copyright ownership.
+    The ASF licenses this file to You under the Apache License, Version 2.0
+    (the "License"); you may not use this file except in compliance with
+    the License.  You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+    Unless required by applicable law or agreed to in writing, software
+    distributed under the License is distributed on an "AS IS" BASIS,
+    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+    See the License for the specific language governing permissions and
+    limitations under the License.
+-->
+
+<!-- $Rev: 1237516 $ $Date: 2012-01-29 16:48:17 -0800 (Sun, 29 Jan 2012) $ -->
+
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+  <parent>
+    <artifactId>container</artifactId>
+    <groupId>org.apache.openejb</groupId>
+    <version>4.0.0-beta-3-SNAPSHOT</version>
+  </parent>
+  <modelVersion>4.0.0</modelVersion>
+  <artifactId>openejb-jee-accessors</artifactId>
+  <packaging>jar</packaging>
+  <name>OpenEJB :: Container :: Java EE Accessors</name>
+
+  <properties>
+      <openejb.osgi.import.pkg>
+        org.apache.geronimo.specs.jpa;resolution:=optional,
+        org.apache.geronimo.specs.activation;resolution:=optional,
+        *
+    </openejb.osgi.import.pkg>
+  </properties>
+
+  <build>
+    <plugins>
+      <!-- this configures the surefire plugin to run your tests with the javaagent enabled -->
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-surefire-plugin</artifactId>
+        <configuration>
+          <forkMode>pertest</forkMode>
+          <argLine>-javaagent:${basedir}/target/openejb-jaxbagent-${project.version}.jar=${project.build.directory}/classes/</argLine>
+          <workingDirectory>${basedir}/target</workingDirectory>
+        </configuration>
+      </plugin>
+
+      <!-- this tells maven to copy the openejb-javaagent jar into your target/ directory -->
+      <!-- where surefire can see it -->
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-dependency-plugin</artifactId>
+        <executions>
+          <execution>
+            <id>copy</id>
+            <phase>process-resources</phase>
+            <goals>
+              <goal>copy</goal>
+            </goals>
+            <configuration>
+              <artifactItems>
+                <artifactItem>
+                  <groupId>org.apache.openejb</groupId>
+                  <artifactId>openejb-jaxbagent</artifactId>
+                  <version>${project.version}</version>
+                  <outputDirectory>${project.build.directory}</outputDirectory>
+                </artifactItem>
+              </artifactItems>
+            </configuration>
+          </execution>
+        </executions>
+      </plugin>
+
+    </plugins>
+  </build>
+  <dependencies>
+    <dependency>
+      <groupId>org.apache.openejb</groupId>
+      <artifactId>openejb-jee</artifactId>
+      <version>${project.version}</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.openejb</groupId>
+      <artifactId>javaee-api</artifactId>
+      <scope>provided</scope>
+    </dependency>
+    <dependency>
+      <groupId>junit</groupId>
+      <artifactId>junit</artifactId>
+      <scope>test</scope>
+    </dependency>
+  </dependencies>
+</project>
+

Propchange: openejb/trunk/openejb/container/openejb-jee-accessors/pom.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Added: openejb/trunk/openejb/container/openejb-jee-accessors/src/test/java/org/apache/openejb/jee/accessors/GenerateAccessorsTest.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-jee-accessors/src/test/java/org/apache/openejb/jee/accessors/GenerateAccessorsTest.java?rev=1293969&view=auto
==============================================================================
--- openejb/trunk/openejb/container/openejb-jee-accessors/src/test/java/org/apache/openejb/jee/accessors/GenerateAccessorsTest.java (added)
+++ openejb/trunk/openejb/container/openejb-jee-accessors/src/test/java/org/apache/openejb/jee/accessors/GenerateAccessorsTest.java Sun Feb 26 23:52:34 2012
@@ -0,0 +1,64 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.openejb.jee.accessors;
+
+import org.apache.openejb.jee.Application;
+import org.apache.openejb.jee.ApplicationClient;
+import org.apache.openejb.jee.Beans;
+import org.apache.openejb.jee.Connector;
+import org.apache.openejb.jee.EjbJar;
+import org.apache.openejb.jee.FacesConfig;
+import org.apache.openejb.jee.HandlerChains;
+import org.apache.openejb.jee.JAXBContextFactory;
+import org.apache.openejb.jee.JavaWsdlMapping;
+import org.apache.openejb.jee.Persistence;
+import org.apache.openejb.jee.TldTaglib;
+import org.apache.openejb.jee.WebApp;
+import org.apache.openejb.jee.WebFragment;
+import org.apache.openejb.jee.Webservices;
+import org.apache.openejb.jee.bval.ValidationConfigType;
+import org.apache.openejb.jee.jpa.EntityMappings;
+import org.apache.openejb.jee.jpa.fragment.PersistenceFragment;
+import org.apache.openejb.jee.oejb3.OpenejbJar;
+import org.junit.Test;
+
+public class GenerateAccessorsTest {
+
+    @Test
+    public void test() throws Exception {
+        JAXBContextFactory.newInstance(
+                Application.class,
+                ApplicationClient.class,
+                Beans.class,
+                Connector.class,
+                EjbJar.class,
+                FacesConfig.class,
+                HandlerChains.class,
+                JavaWsdlMapping.class,
+                Persistence.class,
+                TldTaglib.class,
+                WebApp.class,
+                WebFragment.class,
+                Webservices.class,
+                ValidationConfigType.class,
+                EntityMappings.class,
+                OpenejbJar.class,
+                org.apache.openejb.jee.jpa.unit.Persistence.class,
+                PersistenceFragment.class
+        );
+    }
+}

Modified: openejb/trunk/openejb/container/openejb-jee/src/main/java/org/apache/openejb/jee/JAXBContextFactory.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-jee/src/main/java/org/apache/openejb/jee/JAXBContextFactory.java?rev=1293969&r1=1293968&r2=1293969&view=diff
==============================================================================
--- openejb/trunk/openejb/container/openejb-jee/src/main/java/org/apache/openejb/jee/JAXBContextFactory.java (original)
+++ openejb/trunk/openejb/container/openejb-jee/src/main/java/org/apache/openejb/jee/JAXBContextFactory.java Sun Feb 26 23:52:34 2012
@@ -17,7 +17,9 @@
  */
 package org.apache.openejb.jee;
 
+import java.io.PrintStream;
 import java.util.Map;
+import java.util.concurrent.TimeUnit;
 import javax.xml.bind.JAXBContext;
 import javax.xml.bind.JAXBException;
 
@@ -31,8 +33,12 @@ public final class JAXBContextFactory {
 //            } catch (NoClassDefFoundError e) {
 //            }
 //        }
-
-        return JAXBContext.newInstance(s);
+        final Event event = Event.start(s);
+        try {
+            return JAXBContext.newInstance(s);
+        } finally {
+            event.stop();
+        }
     }
 
     public static JAXBContext newInstance(String s, ClassLoader classLoader) throws JAXBException {
@@ -43,7 +49,12 @@ public final class JAXBContextFactory {
 //            }
 //        }
 
-        return JAXBContext.newInstance(s, classLoader);
+        final Event event = Event.start(s);
+        try {
+            return JAXBContext.newInstance(s, classLoader);
+        } finally {
+            event.stop();
+        }
     }
 
     public static JAXBContext newInstance(String s, ClassLoader classLoader, Map<String, ?> properties) throws JAXBException {
@@ -54,7 +65,12 @@ public final class JAXBContextFactory {
 //            }
 //        }
 
-        return JAXBContext.newInstance(s, classLoader, properties);
+        final Event event = Event.start(s);
+        try {
+            return JAXBContext.newInstance(s, classLoader, properties);
+        } finally {
+            event.stop();
+        }
     }
 
     public static JAXBContext newInstance(Class... classes) throws JAXBException {
@@ -64,8 +80,18 @@ public final class JAXBContextFactory {
 //            } catch (NoClassDefFoundError e) {
 //            }
 //        }
-
-        return JAXBContext.newInstance(classes);
+        final StringBuilder sb = new StringBuilder();
+        for (Class clazz : classes) {
+            sb.append(clazz.getName());
+            sb.append(",");
+        }
+        sb.deleteCharAt(sb.length() - 1);
+        final Event event = Event.start(sb.toString());
+        try {
+            return JAXBContext.newInstance(classes);
+        } finally {
+            event.stop();
+        }
     }
 
     public static JAXBContext newInstance(Class[] classes, Map<String, ?> properties) throws JAXBException {
@@ -76,7 +102,18 @@ public final class JAXBContextFactory {
 //            }
 //        }
 
-        return JAXBContext.newInstance(classes, properties);
+        final StringBuilder sb = new StringBuilder();
+        for (Class clazz : classes) {
+            sb.append(clazz.getName());
+            sb.append(",");
+        }
+        sb.deleteCharAt(sb.length() - 1);
+        final Event event = Event.start(sb.toString());
+        try {
+            return JAXBContext.newInstance(classes, properties);
+        } finally {
+            event.stop();
+        }
     }
 
 //    public static class Sxc {
@@ -111,4 +148,27 @@ public final class JAXBContextFactory {
 //        }
 //    }
 
+
+
+    private static class Event {
+        protected final long start = System.nanoTime();
+        private final String description;
+
+        private Event(String description) {
+            this.description = description;
+        }
+
+        public static Event start(String description) {
+            return new Event(description);
+        }
+
+        public void stop() {
+            stop(System.out);
+        }
+
+        public void stop(PrintStream out) {
+            out.printf("JAXBContext.newInstance %s  %s", TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - this.start), this.description);
+            out.println();
+        }
+    }
 }

Modified: openejb/trunk/openejb/container/pom.xml
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/container/pom.xml?rev=1293969&r1=1293968&r2=1293969&view=diff
==============================================================================
--- openejb/trunk/openejb/container/pom.xml (original)
+++ openejb/trunk/openejb/container/pom.xml Sun Feb 26 23:52:34 2012
@@ -31,7 +31,9 @@
     <module>openejb-core</module>
     <module>openejb-loader</module>
     <module>openejb-javaagent</module>
+    <module>openejb-jaxbagent</module>
     <module>openejb-jee</module>
+    <module>openejb-jee-accessors</module>
     <module>openejb-junit</module>
     <module>openejb-jsf</module>
     <module>openejb-doc-api</module>