tomee-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From andygumbre...@apache.org
Subject [59/77] [abbrv] tomee git commit: TOMEE-1666 clean up of PersistenceBootstrap setup
Date Tue, 01 Dec 2015 22:04:11 GMT
TOMEE-1666 clean up of PersistenceBootstrap setup


Project: http://git-wip-us.apache.org/repos/asf/tomee/repo
Commit: http://git-wip-us.apache.org/repos/asf/tomee/commit/360ceefe
Tree: http://git-wip-us.apache.org/repos/asf/tomee/tree/360ceefe
Diff: http://git-wip-us.apache.org/repos/asf/tomee/diff/360ceefe

Branch: refs/heads/tomee-7.0.0-M1
Commit: 360ceefe96090bb2b2c118735a9b211df7219aab
Parents: ede8b74
Author: Romain Manni-Bucau <rmannibu@gmail.com>
Authored: Wed Nov 25 00:15:31 2015 +0100
Committer: Romain Manni-Bucau <rmannibu@gmail.com>
Committed: Wed Nov 25 00:15:31 2015 +0100

----------------------------------------------------------------------
 .../persistence/PersistenceBootstrap.java       | 63 ++++++++++++--------
 .../org/apache/openejb/javaagent/Agent.java     | 32 ++++++----
 2 files changed, 58 insertions(+), 37 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tomee/blob/360ceefe/container/openejb-core/src/main/java/org/apache/openejb/persistence/PersistenceBootstrap.java
----------------------------------------------------------------------
diff --git a/container/openejb-core/src/main/java/org/apache/openejb/persistence/PersistenceBootstrap.java
b/container/openejb-core/src/main/java/org/apache/openejb/persistence/PersistenceBootstrap.java
index 697396e..f2de2a0 100644
--- a/container/openejb-core/src/main/java/org/apache/openejb/persistence/PersistenceBootstrap.java
+++ b/container/openejb-core/src/main/java/org/apache/openejb/persistence/PersistenceBootstrap.java
@@ -17,17 +17,24 @@
 
 package org.apache.openejb.persistence;
 
+import org.apache.openejb.config.NewLoaderLogic;
 import org.apache.openejb.core.TempClassLoader;
 import org.apache.openejb.javaagent.Agent;
 import org.apache.openejb.loader.IO;
 import org.apache.openejb.loader.SystemInstance;
 import org.apache.openejb.util.Saxs;
 import org.apache.xbean.finder.ClassLoaders;
+import org.apache.xbean.finder.UrlSet;
 import org.xml.sax.Attributes;
 import org.xml.sax.InputSource;
 import org.xml.sax.SAXException;
 import org.xml.sax.helpers.DefaultHandler;
 
+import javax.persistence.EntityManagerFactory;
+import javax.persistence.spi.PersistenceProvider;
+import javax.sql.DataSource;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.parsers.SAXParser;
 import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
@@ -54,11 +61,6 @@ import java.util.Properties;
 import java.util.Set;
 import java.util.logging.Level;
 import java.util.logging.Logger;
-import javax.persistence.EntityManagerFactory;
-import javax.persistence.spi.PersistenceProvider;
-import javax.sql.DataSource;
-import javax.xml.parsers.ParserConfigurationException;
-import javax.xml.parsers.SAXParser;
 
 import static org.apache.openejb.loader.JarLocation.decode;
 
@@ -130,27 +132,29 @@ public class PersistenceBootstrap {
             final List<URL> urls = new LinkedList<URL>();
             for (final String pXmlName : pXmlNames) { // find persistence.xml in the classloader
and in WEB-INF
                 urls.addAll(Collections.list(classLoader.getResources("META-INF/" + pXmlName)));
-                try {
-                    final Collection<URL> loaderUrls = ClassLoaders.findUrls(classLoader);
-                    for (final URL url : loaderUrls) {
-                        final File file = toFile(url);
-                        if ("classes".equals(file.getName()) && "WEB-INF".equals(file.getParentFile().getName()))
{
-                            final File pXml = new File(file.getParentFile(), pXmlName);
-                            if (pXml.exists()) {
-                                urls.add(pXml.toURI().toURL());
+                if ("true".equals(args.getProperty("web-scan", "false"))) { // findUrls is
slow for small tests and rarely needed
+                    try {
+                        final Collection<URL> loaderUrls = findUrls(classLoader, args);
+                        for (final URL url : loaderUrls) {
+                            final File file = toFile(url);
+                            if ("classes".equals(file.getName()) && "WEB-INF".equals(file.getParentFile().getName()))
{
+                                final File pXml = new File(file.getParentFile(), pXmlName);
+                                if (pXml.exists()) {
+                                    urls.add(pXml.toURI().toURL());
+                                }
+                                break;
                             }
-                            break;
-                        }
-                        if (file.getName().endsWith(".jar") && file.getParentFile().getName().equals("lib")
&& "WEB-INF".equals(file.getParentFile().getParentFile().getName())) {
-                            final File pXml = new File(file.getParentFile().getParentFile(),
pXmlName);
-                            if (pXml.exists()) {
-                                urls.add(pXml.toURI().toURL());
+                            if (file.getName().endsWith(".jar") && file.getParentFile().getName().equals("lib")
&& "WEB-INF".equals(file.getParentFile().getParentFile().getName())) {
+                                final File pXml = new File(file.getParentFile().getParentFile(),
pXmlName);
+                                if (pXml.exists()) {
+                                    urls.add(pXml.toURI().toURL());
+                                }
+                                break;
                             }
-                            break;
                         }
+                    } catch (final Throwable th) {
+                        // no-op
                     }
-                } catch (final Throwable th) {
-                    // no-op
                 }
             }
 
@@ -234,6 +238,17 @@ public class PersistenceBootstrap {
         }
     }
 
+    private static Set<URL> findUrls(final ClassLoader classLoader, final Properties
args) throws IOException {
+        if ("true".equals(args.getProperty("fast-scan", "true"))) {
+            try {
+                return new HashSet<>(NewLoaderLogic.applyBuiltinExcludes(new UrlSet(ClassLoaders.findUrls(classLoader)).excludeJvm()).getUrls());
+            } catch (final Throwable fallback) {
+                // let it fallback
+            }
+        }
+        return ClassLoaders.findUrls(classLoader);
+    }
+
     // don't force eager init
     private static String getAltDD() {
         final String property = "openejb.altdd.prefix";
@@ -290,11 +305,11 @@ public class PersistenceBootstrap {
         final SAXParser parser = Saxs.namespaceAwareFactory().newSAXParser();
 
         parser.parse(inputSource, new DefaultHandler() {
-            private StringBuilder characters = new StringBuilder();
+            private final StringBuilder characters = new StringBuilder(100);
             private Unit unit;
 
             public void startElement(final String uri, final String localName, final String
qName, final Attributes attributes) {
-                characters = new StringBuilder(100);
+                characters.setLength(0);
 
                 if (localName.equals("persistence-unit")) {
                     startPersistenceUnit(uri, localName, qName, attributes);

http://git-wip-us.apache.org/repos/asf/tomee/blob/360ceefe/container/openejb-javaagent/src/main/java/org/apache/openejb/javaagent/Agent.java
----------------------------------------------------------------------
diff --git a/container/openejb-javaagent/src/main/java/org/apache/openejb/javaagent/Agent.java
b/container/openejb-javaagent/src/main/java/org/apache/openejb/javaagent/Agent.java
index b249848..43be7b3 100644
--- a/container/openejb-javaagent/src/main/java/org/apache/openejb/javaagent/Agent.java
+++ b/container/openejb-javaagent/src/main/java/org/apache/openejb/javaagent/Agent.java
@@ -17,7 +17,13 @@
 
 package org.apache.openejb.javaagent;
 
-import java.io.*;
+import java.io.Closeable;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStreamWriter;
+import java.io.PrintWriter;
 import java.lang.instrument.ClassFileTransformer;
 import java.lang.instrument.IllegalClassFormatException;
 import java.lang.instrument.Instrumentation;
@@ -252,23 +258,26 @@ public class Agent {
     }
 
     private static class BootstrapTransformer implements ClassFileTransformer {
-
-        private boolean done;
-
         @Override
         public byte[] transform(final ClassLoader loader, final String className, final Class<?>
classBeingRedefined, final ProtectionDomain protectionDomain, final byte[] classfileBuffer)
throws IllegalClassFormatException {
-
             try {
                 bootstrap(loader);
             } catch (final Throwable e) {
-                done = true;
+                removeThis();
             }
-
             return classfileBuffer;
         }
 
+        private void removeThis() {
+            try {
+                instrumentation.removeTransformer(this);
+            } catch (final Throwable th) {
+                // no-op
+            }
+        }
+
         private void bootstrap(final ClassLoader loader) {
-            if (loader == null || done) {
+            if (loader == null) {
                 return;
             }
 
@@ -279,16 +288,13 @@ public class Agent {
                 return;
             }
 
-            // We found the classloader that has the openejb-core jar
-            // we need to mark ourselves as "done" so that when we attempt to load
-            // the PersistenceBootstrap class it doesn't cause an infinite loop
-            done = true;
-
             try {
                 final Class<?> bootstrapClass = loader.loadClass(bootstrapClassName);
                 final Method bootstrap = bootstrapClass.getMethod("bootstrap", ClassLoader.class);
                 bootstrap.invoke(null, loader);
+                removeThis();
             } catch (final Throwable e) {
+                removeThis();
                 Logger.getLogger(Agent.class.getName()).log(Level.WARNING, "Failed to invoke
bootstrap: " + e.getMessage());
             }
         }


Mime
View raw message