You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by an...@apache.org on 2012/12/17 17:52:13 UTC

svn commit: r1423017 - in /openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb: ClassLoaderUtil.java assembler/classic/CmpJarBuilder.java

Author: andygumbrecht
Date: Mon Dec 17 16:52:12 2012
New Revision: 1423017

URL: http://svn.apache.org/viewvc?rev=1423017&view=rev
Log:
Fix potential win paths.
CmpJarBuilder#addJarEntry
ClassLoaderUtil#configurer

Modified:
    openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/ClassLoaderUtil.java
    openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/CmpJarBuilder.java

Modified: openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/ClassLoaderUtil.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/ClassLoaderUtil.java?rev=1423017&r1=1423016&r2=1423017&view=diff
==============================================================================
--- openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/ClassLoaderUtil.java (original)
+++ openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/ClassLoaderUtil.java Mon Dec 17 16:52:12 2012
@@ -70,24 +70,24 @@ public class ClassLoaderUtil {
         });
     }
 
-    public static File getUrlCachedName(String appId, URL url) {
+    public static File getUrlCachedName(final String appId, final URL url) {
         return localUrlCache.getUrlCachedName(appId, url);
     }
 
-    public static boolean isUrlCached(String appId, URL url) {
+    public static boolean isUrlCached(final String appId, final URL url) {
         return localUrlCache.isUrlCached(appId, url);
     }
 
-    public static URL getUrlKeyCached(String appId, File file) {
-    	return localUrlCache.getUrlKeyCached(appId, file);
+    public static URL getUrlKeyCached(final String appId, final File file) {
+        return localUrlCache.getUrlKeyCached(appId, file);
     }
 
-    public static URLClassLoader createClassLoaderFirst(String appId, URL[] urls, ClassLoader parent) {
+    public static URLClassLoader createClassLoaderFirst(final String appId, final URL[] urls, final ClassLoader parent) {
         return cacheClassLoader(appId, new URLClassLoaderFirst(localUrlCache.cacheUrls(appId, urls), parent));
     }
 
-    public static URLClassLoader createClassLoader(String appId, URL[] urls, ClassLoader parent) {
-       return cacheClassLoader(appId, new URLClassLoader(localUrlCache.cacheUrls(appId, urls), parent));
+    public static URLClassLoader createClassLoader(final String appId, final URL[] urls, final ClassLoader parent) {
+        return cacheClassLoader(appId, new URLClassLoader(localUrlCache.cacheUrls(appId, urls), parent));
     }
 
     private static URLClassLoader cacheClassLoader(final String appId, final URLClassLoader classLoader) {
@@ -113,17 +113,17 @@ public class ClassLoaderUtil {
      *
      * @param classLoader ClassLoader to destroy.
      */
-    public static void destroyClassLoader(ClassLoader classLoader) {
+    public static void destroyClassLoader(final ClassLoader classLoader) {
         logger.debug("Destroying classLoader " + toString(classLoader));
 
         // remove from the indexes
-        Set<String> apps = appsByClassLoader.remove(classLoader);
+        final Set<String> apps = appsByClassLoader.remove(classLoader);
 
         if (apps != null) {
 
             List<ClassLoader> classLoaders;
 
-            for (String appId : apps) {
+            for (final String appId : apps) {
 
                 classLoaders = classLoadersByApp.get(appId);
 
@@ -145,8 +145,6 @@ public class ClassLoaderUtil {
         for (final String jar : getClosedJarFiles(classLoader)) {
             clearSunJarFileFactoryCache(jar);
         }
-
-        classLoader = null;
     }
 
     /**
@@ -162,16 +160,16 @@ public class ClassLoaderUtil {
         if (null != cl && cl instanceof URLClassLoader) {
 
             final URLClassLoader ucl = (URLClassLoader) cl;
-            Class clazz = java.net.URLClassLoader.class;
+            final Class clazz = java.net.URLClassLoader.class;
 
             try {
 
-                java.lang.reflect.Field ucp = clazz.getDeclaredField("ucp");
+                final java.lang.reflect.Field ucp = clazz.getDeclaredField("ucp");
                 ucp.setAccessible(true);
-                Object cp = ucp.get(ucl);
-                java.lang.reflect.Field loaders = cp.getClass().getDeclaredField("loaders");
+                final Object cp = ucp.get(ucl);
+                final java.lang.reflect.Field loaders = cp.getClass().getDeclaredField("loaders");
                 loaders.setAccessible(true);
-                java.util.Collection c = (java.util.Collection) loaders.get(cp);
+                final java.util.Collection c = (java.util.Collection) loaders.get(cp);
                 java.lang.reflect.Field loader;
                 java.util.jar.JarFile jf;
 
@@ -194,10 +192,10 @@ public class ClassLoaderUtil {
         return files;
     }
 
-    public boolean finalizeNativeLibs(ClassLoader cl) {
+    public boolean finalizeNativeLibs(final ClassLoader cl) {
 
         boolean res = false;
-        Class classClassLoader = ClassLoader.class;
+        final Class classClassLoader = ClassLoader.class;
         java.lang.reflect.Field nativeLibraries = null;
 
         try {
@@ -224,13 +222,11 @@ public class ClassLoaderUtil {
         }
 
         res = true;
-        Vector java_lang_ClassLoader_NativeLibrary = (Vector) obj;
+        final Vector java_lang_ClassLoader_NativeLibrary = (Vector) obj;
         java.lang.reflect.Method finalize;
 
         for (final Object lib : java_lang_ClassLoader_NativeLibrary) {
 
-            finalize = null;
-
             try {
                 finalize = lib.getClass().getDeclaredMethod("finalize", new Class[0]);
 
@@ -251,10 +247,10 @@ public class ClassLoaderUtil {
         return res;
     }
 
-    public static void destroyClassLoader(String appId) {
+    public static void destroyClassLoader(final String appId) {
 
         logger.debug("Destroying classLoaders for application " + appId);
-        List<ClassLoader> classLoaders = classLoadersByApp.remove(appId);
+        final List<ClassLoader> classLoaders = classLoadersByApp.remove(appId);
 
         if (classLoaders != null) {
 
@@ -277,8 +273,6 @@ public class ClassLoaderUtil {
                     it.remove();
                     appsByClassLoader.remove(cl);
                     destroyClassLoader(cl);
-                    cl = null;
-
                     System.gc();
                 } else {
                     logger.debug("ClassLoader " + toString(cl) + " held open by the applications: " + apps);
@@ -290,11 +284,11 @@ public class ClassLoaderUtil {
         clearSunJarFileFactoryCache(appId);
     }
 
-    public static URLClassLoader createTempClassLoader(ClassLoader parent) {
+    public static URLClassLoader createTempClassLoader(final ClassLoader parent) {
         return new TempClassLoader(parent);
     }
 
-    public static URLClassLoader createTempClassLoader(String appId, URL[] rawUrls, ClassLoader parent) {
+    public static URLClassLoader createTempClassLoader(final String appId, final URL[] rawUrls, final ClassLoader parent) {
         String updatedAppId = appId;
         if (appId != null) { // here we often get the full path of the app as id where later it is simply the name of the file/dir
             final File file = new File(appId);
@@ -313,7 +307,7 @@ public class ClassLoaderUtil {
         }
         if (configurer != null) {
             final Collection<URL> urlList = new ArrayList<URL>();
-            for (URL rawUrl : rawUrls) {
+            for (final URL rawUrl : rawUrls) {
                 if (configurer.accept(rawUrl)) {
                     urlList.add(rawUrl);
                 }
@@ -462,8 +456,8 @@ public class ClassLoaderUtil {
         }
     }
 
-    private static boolean isParent(String jarLocation, File file) {
-        File dir = new File(jarLocation);
+    private static boolean isParent(final String jarLocation, File file) {
+        final File dir = new File(jarLocation);
         while (file != null) {
             if (file.equals(dir)) {
                 return true;
@@ -482,10 +476,10 @@ public class ClassLoaderUtil {
      * @param clazz     the name of the class containing the cache field
      * @param fieldName the name of the cache field
      */
-    public static void clearSunSoftCache(final Class clazz, String fieldName) {
+    public static void clearSunSoftCache(final Class clazz, final String fieldName) {
         synchronized (clazz) {
             try {
-                Field field = clazz.getDeclaredField(fieldName);
+                final Field field = clazz.getDeclaredField(fieldName);
                 field.setAccessible(true);
                 final Map cache = (Map) field.get(null);
                 cache.clear();
@@ -495,17 +489,17 @@ public class ClassLoaderUtil {
         }
     }
 
-    public static void cleanOpenJPACache(ClassLoader classLoader) {
+    public static void cleanOpenJPACache(final ClassLoader classLoader) {
         try {
-            Class<?> pcRegistryClass = ClassLoaderUtil.class.getClassLoader().loadClass("org.apache.openjpa.enhance.PCRegistry");
-            Method deRegisterMethod = pcRegistryClass.getMethod("deRegister", ClassLoader.class);
+            final Class<?> pcRegistryClass = ClassLoaderUtil.class.getClassLoader().loadClass("org.apache.openjpa.enhance.PCRegistry");
+            final Method deRegisterMethod = pcRegistryClass.getMethod("deRegister", ClassLoader.class);
             deRegisterMethod.invoke(null, classLoader);
         } catch (Throwable ignored) {
             // there is nothing a user could do about this anyway
         }
     }
 
-    private static String toString(ClassLoader classLoader) {
+    private static String toString(final ClassLoader classLoader) {
         if (classLoader == null) {
             return "null";
         } else {
@@ -519,7 +513,7 @@ public class ClassLoaderUtil {
 
     public static ClassLoaderConfigurer configurer(final String rawId) {
         String id = rawId;
-        if (id != null && id.startsWith("/") && !new File(id).exists() && id.length() > 1) {
+        if (id != null && (id.startsWith("/") || id.startsWith("\\")) && !new File(id).exists() && id.length() > 1) {
             id = id.substring(1);
         }
 
@@ -567,10 +561,10 @@ public class ClassLoaderUtil {
     private static ClassLoaderConfigurer createConfigurer(final String key, final String impl) {
         try {
             final ObjectRecipe recipe = new ObjectRecipe(impl);
-            for (Map.Entry<Object, Object> entry : SystemInstance.get().getProperties().entrySet()) {
-                String entryKey = entry.getKey().toString();
+            for (final Map.Entry<Object, Object> entry : SystemInstance.get().getProperties().entrySet()) {
+                final String entryKey = entry.getKey().toString();
                 if (entryKey.startsWith(key)) {
-                    String newKey = entryKey.substring(key.length());
+                    final String newKey = entryKey.substring(key.length());
                     if (!"clazz".equals(newKey)) {
                         recipe.setProperty(newKey, entry.getValue());
                     }

Modified: openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/CmpJarBuilder.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/CmpJarBuilder.java?rev=1423017&r1=1423016&r2=1423017&view=diff
==============================================================================
--- openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/CmpJarBuilder.java (original)
+++ openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/CmpJarBuilder.java Mon Dec 17 16:52:12 2012
@@ -16,6 +16,14 @@
  */
 package org.apache.openejb.assembler.classic;
 
+import org.apache.openejb.ClassLoaderUtil;
+import org.apache.openejb.core.cmp.CmpUtil;
+import org.apache.openejb.core.cmp.cmp2.Cmp1Generator;
+import org.apache.openejb.core.cmp.cmp2.Cmp2Generator;
+import org.apache.openejb.core.cmp.cmp2.CmrField;
+import org.apache.openejb.loader.IO;
+import org.apache.openejb.util.UrlCache;
+
 import java.io.File;
 import java.io.IOException;
 import java.util.Set;
@@ -24,25 +32,18 @@ import java.util.TreeSet;
 import java.util.jar.JarEntry;
 import java.util.jar.JarOutputStream;
 
-import org.apache.openejb.core.cmp.cmp2.Cmp2Generator;
-import org.apache.openejb.core.cmp.cmp2.CmrField;
-import org.apache.openejb.core.cmp.cmp2.Cmp1Generator;
-import org.apache.openejb.core.cmp.CmpUtil;
-import org.apache.openejb.ClassLoaderUtil;
-import org.apache.openejb.loader.IO;
-import org.apache.openejb.util.UrlCache;
-
 /**
  * Creates a jar file which contains the CMP implementation classes and the cmp entity mappings xml file.
  */
 public class CmpJarBuilder {
+
     private final ClassLoader tempClassLoader;
 
     private File jarFile;
     private final Set<String> entries = new TreeSet<String>();
     private final AppInfo appInfo;
 
-    public CmpJarBuilder(AppInfo appInfo, ClassLoader classLoader) {
+    public CmpJarBuilder(final AppInfo appInfo, final ClassLoader classLoader) {
         this.appInfo = appInfo;
         tempClassLoader = ClassLoaderUtil.createTempClassLoader(classLoader);
     }
@@ -55,12 +56,12 @@ public class CmpJarBuilder {
     }
 
     /**
-     * Generate the CMP jar file associated with this 
-     * deployed application.  The generated jar file will 
-     * contain generated classes and metadata that will 
-     * allow the JPA engine to manage the bean persistence. 
-     * 
-     * @exception IOException
+     * Generate the CMP jar file associated with this
+     * deployed application.  The generated jar file will
+     * contain generated classes and metadata that will
+     * allow the JPA engine to manage the bean persistence.
+     *
+     * @throws IOException
      */
     private void generate() throws IOException {
         // Don't generate an empty jar.  If there are no container-managed beans defined in this 
@@ -70,13 +71,13 @@ public class CmpJarBuilder {
         }
 
         boolean threwException = false;
-        JarOutputStream jarOutputStream = openJarFile();
+        final JarOutputStream jarOutputStream = openJarFile();
         try {
             // Generate CMP implementation classes
-            for (EjbJarInfo ejbJar : appInfo.ejbJars) {
-                for (EnterpriseBeanInfo beanInfo : ejbJar.enterpriseBeans) {
+            for (final EjbJarInfo ejbJar : appInfo.ejbJars) {
+                for (final EnterpriseBeanInfo beanInfo : ejbJar.enterpriseBeans) {
                     if (beanInfo instanceof EntityBeanInfo) {
-                        EntityBeanInfo entityBeanInfo = (EntityBeanInfo) beanInfo;
+                        final EntityBeanInfo entityBeanInfo = (EntityBeanInfo) beanInfo;
                         if ("CONTAINER".equalsIgnoreCase(entityBeanInfo.persistenceType)) {
                             generateClass(jarOutputStream, entityBeanInfo);
                         }
@@ -93,27 +94,29 @@ public class CmpJarBuilder {
         } finally {
             close(jarOutputStream);
             if (threwException) {
-                jarFile.delete();
+                if (!jarFile.delete()) {
+                    jarFile.deleteOnExit();
+                }
                 jarFile = null;
             }
         }
     }
 
     /**
-     * Test if an application contains and CMP beans that 
-     * need to be mapped to the JPA persistence engine.  This 
-     * will search all of the ejb jars contained within 
-     * the application looking for Entity beans with 
-     * a CONTAINER persistence type. 
-     * 
-     * @return true if the application uses container managed beans, 
+     * Test if an application contains and CMP beans that
+     * need to be mapped to the JPA persistence engine.  This
+     * will search all of the ejb jars contained within
+     * the application looking for Entity beans with
+     * a CONTAINER persistence type.
+     *
+     * @return true if the application uses container managed beans,
      *         false if none are found.
      */
     private boolean hasCmpBeans() {
-        for (EjbJarInfo ejbJar : appInfo.ejbJars) {
-            for (EnterpriseBeanInfo beanInfo : ejbJar.enterpriseBeans) {
+        for (final EjbJarInfo ejbJar : appInfo.ejbJars) {
+            for (final EnterpriseBeanInfo beanInfo : ejbJar.enterpriseBeans) {
                 if (beanInfo instanceof EntityBeanInfo) {
-                    EntityBeanInfo entityBeanInfo = (EntityBeanInfo) beanInfo;
+                    final EntityBeanInfo entityBeanInfo = (EntityBeanInfo) beanInfo;
                     if ("CONTAINER".equalsIgnoreCase(entityBeanInfo.persistenceType)) {
                         return true;
                     }
@@ -124,21 +127,18 @@ public class CmpJarBuilder {
     }
 
     /**
-     * Generate a class file for a CMP bean, writing the 
-     * byte data for the generated class into the jar file 
-     * we're constructing. 
-     * 
-     * @param jarOutputStream
-     *               The target jarfile.
-     * @param entityBeanInfo
-     *               The descriptor for the entity bean we need to wrapper.
-     * 
-     * @exception IOException
+     * Generate a class file for a CMP bean, writing the
+     * byte data for the generated class into the jar file
+     * we're constructing.
+     *
+     * @param jarOutputStream The target jarfile.
+     * @param entityBeanInfo  The descriptor for the entity bean we need to wrapper.
+     * @throws IOException
      */
-    private void generateClass(JarOutputStream jarOutputStream, EntityBeanInfo entityBeanInfo) throws IOException {
+    private void generateClass(final JarOutputStream jarOutputStream, final EntityBeanInfo entityBeanInfo) throws IOException {
         // don't generate if there is aleady an implementation class
-        String cmpImplClass = CmpUtil.getCmpImplClassName(entityBeanInfo.abstractSchemaName, entityBeanInfo.ejbClass);
-        String entryName = cmpImplClass.replace(".", "/") + ".class";
+        final String cmpImplClass = CmpUtil.getCmpImplClassName(entityBeanInfo.abstractSchemaName, entityBeanInfo.ejbClass);
+        final String entryName = cmpImplClass.replace(".", "/") + ".class";
         if (entries.contains(entryName) || tempClassLoader.getResource(entryName) != null) {
             return;
         }
@@ -148,7 +148,7 @@ public class CmpJarBuilder {
         try {
             beanClass = tempClassLoader.loadClass(entityBeanInfo.ejbClass);
         } catch (ClassNotFoundException e) {
-            throw (IOException)new IOException("Could not find entity bean class " + beanClass).initCause(e);
+            throw (IOException) new IOException("Could not find entity bean class " + beanClass).initCause(e);
         }
 
         // and the primary key class, if defined.  
@@ -157,15 +157,15 @@ public class CmpJarBuilder {
             try {
                 primKeyClass = tempClassLoader.loadClass(entityBeanInfo.primKeyClass);
             } catch (ClassNotFoundException e) {
-                throw (IOException)new IOException("Could not find entity primary key class " + entityBeanInfo.primKeyClass).initCause(e);
+                throw (IOException) new IOException("Could not find entity primary key class " + entityBeanInfo.primKeyClass).initCause(e);
             }
         }
 
         // now generate a class file using the appropriate level of CMP generator.  
-        byte[] bytes;
+        final byte[] bytes;
         // NB:  We'll need to change this test of CMP 3 is ever defined!
         if (entityBeanInfo.cmpVersion != 2) {
-            Cmp1Generator cmp1Generator = new Cmp1Generator(cmpImplClass, beanClass);
+            final Cmp1Generator cmp1Generator = new Cmp1Generator(cmpImplClass, beanClass);
             // A primary key class defined as Object is an unknown key.  Mark it that 
             // way so the generator will create the automatically generated key. 
             if ("java.lang.Object".equals(entityBeanInfo.primKeyClass)) {
@@ -175,22 +175,22 @@ public class CmpJarBuilder {
         } else {
 
             // generate the implementation class
-            Cmp2Generator cmp2Generator = new Cmp2Generator(cmpImplClass,
-                    beanClass,
-                    entityBeanInfo.primKeyField,
-                    primKeyClass,
-                    entityBeanInfo.cmpFieldNames.toArray(new String[entityBeanInfo.cmpFieldNames.size()]));
+            final Cmp2Generator cmp2Generator = new Cmp2Generator(cmpImplClass,
+                                                                  beanClass,
+                                                                  entityBeanInfo.primKeyField,
+                                                                  primKeyClass,
+                                                                  entityBeanInfo.cmpFieldNames.toArray(new String[entityBeanInfo.cmpFieldNames.size()]));
 
             // we need to have a complete set of the defined CMR fields available for the 
             // generation process as well. 
-            for (CmrFieldInfo cmrFieldInfo : entityBeanInfo.cmrFields) {
-                EntityBeanInfo roleSource = cmrFieldInfo.mappedBy.roleSource;
-                CmrField cmrField = new CmrField(cmrFieldInfo.fieldName,
-                        cmrFieldInfo.fieldType,
-                        CmpUtil.getCmpImplClassName(roleSource.abstractSchemaName, roleSource.ejbClass),
-                        roleSource.local,
-                        cmrFieldInfo.mappedBy.fieldName,
-                        cmrFieldInfo.synthetic);
+            for (final CmrFieldInfo cmrFieldInfo : entityBeanInfo.cmrFields) {
+                final EntityBeanInfo roleSource = cmrFieldInfo.mappedBy.roleSource;
+                final CmrField cmrField = new CmrField(cmrFieldInfo.fieldName,
+                                                       cmrFieldInfo.fieldType,
+                                                       CmpUtil.getCmpImplClassName(roleSource.abstractSchemaName, roleSource.ejbClass),
+                                                       roleSource.local,
+                                                       cmrFieldInfo.mappedBy.fieldName,
+                                                       cmrFieldInfo.synthetic);
                 cmp2Generator.addCmrField(cmrField);
             }
             bytes = cmp2Generator.generate();
@@ -200,22 +200,20 @@ public class CmpJarBuilder {
         addJarEntry(jarOutputStream, entryName, bytes);
     }
 
-    
     /**
-     * Insert a file resource into the generated jar file. 
-     * 
-     * @param jarOutputStream
-     *                 The target jar file.
-     * @param fileName The name we're inserting.
-     * @param bytes    The file byte data.
-     * 
-     * @exception IOException
+     * Insert a file resource into the generated jar file.
+     *
+     * @param jarOutputStream The target jar file.
+     * @param fileName        The name we're inserting.
+     * @param bytes           The file byte data.
+     * @throws IOException
      */
-    private void addJarEntry(JarOutputStream jarOutputStream, String fileName, byte[] bytes) throws IOException {
-        // add all missing directory entried
+    private void addJarEntry(final JarOutputStream jarOutputStream, String fileName, final byte[] bytes) throws IOException {
+        // add all missing directory entries
+        fileName = fileName.replace('\\', '/');
         String path = "";
-        for (StringTokenizer tokenizer = new StringTokenizer(fileName, "/"); tokenizer.hasMoreTokens();) {
-            String part = tokenizer.nextToken();
+        for (StringTokenizer tokenizer = new StringTokenizer(fileName, "/"); tokenizer.hasMoreTokens(); ) {
+            final String part = tokenizer.nextToken();
             if (tokenizer.hasMoreTokens()) {
                 path += part + "/";
                 if (!entries.contains(path)) {
@@ -247,11 +245,10 @@ public class CmpJarBuilder {
         Thread.yield();
 
         jarFile.deleteOnExit();
-        JarOutputStream jarOutputStream = new JarOutputStream(IO.write(jarFile));
-        return jarOutputStream;
+        return new JarOutputStream(IO.write(jarFile));
     }
 
-    private void close(JarOutputStream jarOutputStream) {
+    private void close(final JarOutputStream jarOutputStream) {
         if (jarOutputStream != null) {
             try {
                 jarOutputStream.close();