You are viewing a plain text version of this content. The canonical link for it is here.
Posted to scm@geronimo.apache.org by xu...@apache.org on 2011/08/12 10:23:04 UTC

svn commit: r1157006 - in /geronimo/server/trunk: framework/modules/geronimo-deployment/src/main/java/org/apache/geronimo/deployment/ framework/modules/geronimo-kernel/src/main/java/org/apache/geronimo/kernel/util/ plugins/client/geronimo-client-builde...

Author: xuhaihong
Date: Fri Aug 12 08:23:04 2011
New Revision: 1157006

URL: http://svn.apache.org/viewvc?rev=1157006&view=rev
Log:
GERONIMO-6106 Clean up temp files created in the deployment process
Use a thread local in the FileUtils to hold all the temp files in the deployment process

Modified:
    geronimo/server/trunk/framework/modules/geronimo-deployment/src/main/java/org/apache/geronimo/deployment/Deployer.java
    geronimo/server/trunk/framework/modules/geronimo-deployment/src/main/java/org/apache/geronimo/deployment/DeploymentContext.java
    geronimo/server/trunk/framework/modules/geronimo-kernel/src/main/java/org/apache/geronimo/kernel/util/FileUtils.java
    geronimo/server/trunk/framework/modules/geronimo-kernel/src/main/java/org/apache/geronimo/kernel/util/JarUtils.java
    geronimo/server/trunk/plugins/client/geronimo-client-builder/src/main/java/org/apache/geronimo/client/builder/AppClientModuleBuilder.java
    geronimo/server/trunk/plugins/j2ee/geronimo-j2ee-builder/src/main/java/org/apache/geronimo/j2ee/deployment/EARConfigBuilder.java
    geronimo/server/trunk/plugins/jetty8/geronimo-jetty8-builder/src/main/java/org/apache/geronimo/jetty8/deployment/JettyModuleBuilder.java
    geronimo/server/trunk/plugins/tomcat/geronimo-tomcat7-builder/src/main/java/org/apache/geronimo/tomcat/deployment/TomcatModuleBuilder.java

Modified: geronimo/server/trunk/framework/modules/geronimo-deployment/src/main/java/org/apache/geronimo/deployment/Deployer.java
URL: http://svn.apache.org/viewvc/geronimo/server/trunk/framework/modules/geronimo-deployment/src/main/java/org/apache/geronimo/deployment/Deployer.java?rev=1157006&r1=1157005&r2=1157006&view=diff
==============================================================================
--- geronimo/server/trunk/framework/modules/geronimo-deployment/src/main/java/org/apache/geronimo/deployment/Deployer.java (original)
+++ geronimo/server/trunk/framework/modules/geronimo-deployment/src/main/java/org/apache/geronimo/deployment/Deployer.java Fri Aug 12 08:23:04 2011
@@ -228,6 +228,7 @@ public class Deployer implements GBeanLi
 
         DeploymentContext context = null;
         try {
+            FileUtils.beginRecordTempFiles();
             Object plan = null;
             ConfigurationBuilder builder = null;
             for (Iterator i = builders.iterator(); i.hasNext();) {
@@ -268,15 +269,6 @@ public class Deployer implements GBeanLi
 
             return install(targetFile, install, manifest, store, context);
         } catch (Throwable e) {
-            //TODO not clear all errors will result in total cleanup
-//            File configurationDir = configurationData.getConfigurationDir();
-//            if (!DeploymentUtil.recursiveDelete(configurationDir)) {
-//                pendingDeletionIndex.setProperty(configurationDir.getName(), new String("delete"));
-//                log.debug("Queued deployment directory to be reaped " + configurationDir);
-//            }
-//            if (targetFile != null) {
-//                targetFile.delete();
-//            }
             if (e instanceof Error) {
                 log.error("Deployment failed due to ", e);
                 throw (Error) e;
@@ -288,14 +280,16 @@ public class Deployer implements GBeanLi
             }
             throw new Error(e);
         } finally {
-            JarUtils.close(module);
-            if (context != null) {
-                List<File> deleteOnExitFiles = context.getDeleteOnExitFiles();
+            JarUtils.close(module);            
+            if (context != null) {               
                 try {
                     context.close();
                 } catch (Exception e) {
-                }
-                cleanUpTemporaryDirectories(deleteOnExitFiles);
+                }                
+            }
+            List<File> tempFiles = FileUtils.endRecordTempFiles();
+            if (tempFiles != null) {
+                cleanUpTemporaryDirectories(tempFiles);
             }
         }
     }

Modified: geronimo/server/trunk/framework/modules/geronimo-deployment/src/main/java/org/apache/geronimo/deployment/DeploymentContext.java
URL: http://svn.apache.org/viewvc/geronimo/server/trunk/framework/modules/geronimo-deployment/src/main/java/org/apache/geronimo/deployment/DeploymentContext.java?rev=1157006&r1=1157005&r2=1157006&view=diff
==============================================================================
--- geronimo/server/trunk/framework/modules/geronimo-deployment/src/main/java/org/apache/geronimo/deployment/DeploymentContext.java (original)
+++ geronimo/server/trunk/framework/modules/geronimo-deployment/src/main/java/org/apache/geronimo/deployment/DeploymentContext.java Fri Aug 12 08:23:04 2011
@@ -99,10 +99,6 @@ public class DeploymentContext {
     protected final LinkedHashSet<String> bundleClassPath = new LinkedHashSet<String>();
     protected final ConfigurationModuleType moduleType;
     protected final Environment environment;
-    //This temporary directories is used to hold all the folders created in the deployment process.
-    //Due to some folders are required until the target application is installed,  so for those creators (e.g. xxxBuilder) have no chance
-    //to delete them.
-    private List<File> deleteOnExitFiles;
     //This provides services such as loading more bundles, it is NOT for the configuration we are constructing here.
     //It should be a disposable nested framework so as to not pollute the main framework with stuff we load as deployment parents.
     private final BundleContext bundleContext;
@@ -686,40 +682,6 @@ public class DeploymentContext {
         return failures;
     }
 
-    public void deleteFileOnExit(File file) {
-        if (deleteOnExitFiles == null) {
-            deleteOnExitFiles = new LinkedList<File>();
-        }
-        deleteOnExitFiles.add(file);
-    }
-
-    public boolean deleteFileOnExit(URL fileURL) {
-        if (!"file".equals(fileURL.getProtocol())) {
-            if (log.isDebugEnabled()) {
-                log.debug("Only file protocol URL is suppor, fileURL = " + fileURL);
-            }
-            return false;
-        }
-        File file = null;
-        try {
-            file = new File(fileURL.toURI());
-        } catch (Exception e) {
-            file = new File(fileURL.getPath());
-        }
-        if (file.exists()) {
-            deleteFileOnExit(file);
-            return true;
-        }
-        if (log.isDebugEnabled()) {
-            log.debug("Could not locate the target file based on the fileURL " + fileURL);
-        }
-        return false;
-    }
-
-    public List<File> getDeleteOnExitFiles() {
-        return deleteOnExitFiles == null ? Collections.<File>emptyList() : deleteOnExitFiles;
-    }
-
     private String verifyReference(GBeanData gbean, String referenceName, ReferencePatterns referencePatterns, Configuration configuration) {
         GReferenceInfo referenceInfo = gbean.getGBeanInfo().getReference(referenceName);
 

Modified: geronimo/server/trunk/framework/modules/geronimo-kernel/src/main/java/org/apache/geronimo/kernel/util/FileUtils.java
URL: http://svn.apache.org/viewvc/geronimo/server/trunk/framework/modules/geronimo-kernel/src/main/java/org/apache/geronimo/kernel/util/FileUtils.java?rev=1157006&r1=1157005&r2=1157006&view=diff
==============================================================================
--- geronimo/server/trunk/framework/modules/geronimo-kernel/src/main/java/org/apache/geronimo/kernel/util/FileUtils.java (original)
+++ geronimo/server/trunk/framework/modules/geronimo-kernel/src/main/java/org/apache/geronimo/kernel/util/FileUtils.java Fri Aug 12 08:23:04 2011
@@ -29,7 +29,6 @@ import java.io.OutputStream;
 import java.io.OutputStreamWriter;
 import java.net.MalformedURLException;
 import java.net.URL;
-import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.Enumeration;
@@ -62,10 +61,31 @@ public class FileUtils {
 
     public static final String DEFAULT_TEMP_DIRECTORY_SUFFIX = ".tmpdir";
 
+    private static final ThreadLocal<List<File>> TEMPORARY_FILES = new ThreadLocal<List<File>>();
+    
     public static void copyFile(File source, File destination) throws IOException {
         copyFile(source, destination, IOUtils.DEFAULT_COPY_BUFFER_SIZE);
     }
 
+    public static void beginRecordTempFiles() {
+        TEMPORARY_FILES.set(new LinkedList<File>());
+    }
+    
+    public static List<File> endRecordTempFiles() {
+        List<File> tempFiles = TEMPORARY_FILES.get();
+        TEMPORARY_FILES.remove();
+        return tempFiles;
+    }
+    
+    public static void addTempFile(File tempFile) {
+        List<File> tempFiles = TEMPORARY_FILES.get();
+        if(tempFiles != null) {
+            tempFiles.add(tempFile);
+        } else if(logger.isDebugEnabled()) {
+            logger.debug("Unable to record temporary file " + tempFile.getAbsolutePath() + " ,  it is required to call beginRecordTempFiles first");
+        }
+    }
+    
     public static void copyFile(File source, File destination, int bufferSizeInBytes) throws IOException {
         if (!source.exists() || source.isDirectory()) {
             throw new IllegalArgumentException("Source does not exist or it is not a file");
@@ -91,22 +111,30 @@ public class FileUtils {
         File tempDir = File.createTempFile(DEFAULT_TEMP_PREFIX, DEFAULT_TEMP_DIRECTORY_SUFFIX);
         tempDir.delete();
         tempDir.mkdirs();
-        deleteOnExit(tempDir);
+        addTempFile(tempDir);
         return tempDir;
     }
 
-    // be careful to clean up the temp file... we tell the vm to delete this on exit
-    // but VMs can't be trusted to acutally delete the file
-    public static File createTempFile() throws IOException {
+    public static File createTempFile(boolean record) throws IOException {
         File tempFile = File.createTempFile(DEFAULT_TEMP_PREFIX, DEFAULT_TEMP_FILE_SUFFIX);
+        if (record) {
+            addTempFile(tempFile);
+        }
         tempFile.deleteOnExit();
         return tempFile;
     }
+    
+    // be careful to clean up the temp file... we tell the vm to delete this on exit
+    // but VMs can't be trusted to acutally delete the file
+    public static File createTempFile() throws IOException {
+        return createTempFile(true);
+    }
 
     // be careful to clean up the temp file... we tell the vm to delete this on exit
     // but VMs can't be trusted to acutally delete the file
     public static File createTempFile(String extension) throws IOException {
         File tempFile = File.createTempFile(DEFAULT_TEMP_PREFIX, extension == null ? DEFAULT_TEMP_DIRECTORY_SUFFIX : extension);
+        addTempFile(tempFile);
         tempFile.deleteOnExit();
         return tempFile;
     }
@@ -420,37 +448,4 @@ public class FileUtils {
 
     private FileUtils() {
     }
-
-    // Shutdown hook for recurssive delete on tmp directories
-    static final List<String> delete = new ArrayList<String>();
-
-    static {
-        Runtime.getRuntime().addShutdownHook(new Thread(){
-            @Override
-            public void run() {
-                delete();
-            }
-        });
-    }
-
-    private static void deleteOnExit(File file) {
-        delete.add(file.getAbsolutePath());
-    }
-
-    private static void delete() {
-        for (String path : delete) {
-            delete(new File(path));
-        }
-    }
-
-    private static void delete(File file) {
-        if (file.isDirectory()) {
-            for (File f : file.listFiles()) {
-                delete(f);
-            }
-        }
-
-        file.delete();
-    }
-
 }

Modified: geronimo/server/trunk/framework/modules/geronimo-kernel/src/main/java/org/apache/geronimo/kernel/util/JarUtils.java
URL: http://svn.apache.org/viewvc/geronimo/server/trunk/framework/modules/geronimo-kernel/src/main/java/org/apache/geronimo/kernel/util/JarUtils.java?rev=1157006&r1=1157005&r2=1157006&view=diff
==============================================================================
--- geronimo/server/trunk/framework/modules/geronimo-kernel/src/main/java/org/apache/geronimo/kernel/util/JarUtils.java (original)
+++ geronimo/server/trunk/framework/modules/geronimo-kernel/src/main/java/org/apache/geronimo/kernel/util/JarUtils.java Fri Aug 12 08:23:04 2011
@@ -61,7 +61,7 @@ public final class JarUtils {
         //Why not always set this with true ? Online deployer also lock the jar files
         jarUrlRewrite = new Boolean(System.getProperty("org.apache.geronimo.kernel.util.JarUtils.jarUrlRewrite", "true"));
         try {
-            DUMMY_JAR_FILE = FileUtils.createTempFile();
+            DUMMY_JAR_FILE = FileUtils.createTempFile(false);
             new JarOutputStream(new FileOutputStream(JarUtils.DUMMY_JAR_FILE), new Manifest()).close();
         } catch (IOException e) {
             throw new ExceptionInInitializerError(e);
@@ -205,35 +205,6 @@ public final class JarUtils {
         }
     }
 
-    public static void deleteJarFileURL(JarFile jarFile, URL jarFileUrl) {
-        try {
-            if (jarFile instanceof NestedJarFile) {
-                NestedJarFile nestedJar = (NestedJarFile) jarFile;
-                if (nestedJar.isUnpacked()) {
-                    JarFile baseJar = nestedJar.getBaseJar();
-                    if (baseJar instanceof UnpackedJarFile) {
-                        return;
-                    }
-                }
-            }
-            if (jarFile instanceof UnpackedJarFile) {
-                //Leave it there, as the URL is from a unpacked jar file
-                return;
-            } else {
-                if (jarUrlRewrite) {
-                    new File(jarFileUrl.getFile()).delete();
-                } else {
-                    //Leave it there, as it is a jar entry URL
-                    return;
-                }
-            }
-        } catch (Exception e) {
-            if (logger.isDebugEnabled()) {
-                logger.debug("unable to delete jar file URL " + jarFileUrl + " created from " + jarFile.getName(), e);
-            }
-        }
-    }
-
     public static JarFile createJarFile(File jarFile) throws IOException {
         if (jarFile.isDirectory()) {
             return new UnpackedJarFile(jarFile);

Modified: geronimo/server/trunk/plugins/client/geronimo-client-builder/src/main/java/org/apache/geronimo/client/builder/AppClientModuleBuilder.java
URL: http://svn.apache.org/viewvc/geronimo/server/trunk/plugins/client/geronimo-client-builder/src/main/java/org/apache/geronimo/client/builder/AppClientModuleBuilder.java?rev=1157006&r1=1157005&r2=1157006&view=diff
==============================================================================
--- geronimo/server/trunk/plugins/client/geronimo-client-builder/src/main/java/org/apache/geronimo/client/builder/AppClientModuleBuilder.java (original)
+++ geronimo/server/trunk/plugins/client/geronimo-client-builder/src/main/java/org/apache/geronimo/client/builder/AppClientModuleBuilder.java Fri Aug 12 08:23:04 2011
@@ -305,11 +305,9 @@ public class AppClientModuleBuilder impl
 
         String specDD = null;
         ApplicationClient appClient = null;
-        boolean cleanUpSpecDDUrlRequired = false;
         try {
             if (specDDUrl == null) {
                 specDDUrl = JarUtils.createJarURL(moduleFile, "META-INF/application-client.xml");
-                cleanUpSpecDDUrlRequired = true;
             }
 
             // read in the entire specDD as a string, we need this for getDeploymentDescriptor
@@ -453,10 +451,6 @@ public class AppClientModuleBuilder impl
         for (ModuleBuilderExtension mbe : moduleBuilderExtensions) {
             mbe.createModule(module, plan, moduleFile, targetPath, specDDUrl, clientEnvironment, null, earName, naming, idBuilder);
         }
-
-        if(cleanUpSpecDDUrlRequired && specDDUrl != null) {
-            JarUtils.deleteJarFileURL(moduleFile, specDDUrl);
-        }
         if (standAlone) {
             Map<JndiKey, Map<String, Object>> appJndiContext = Module.share(Module.APP, module.getJndiContext());
 
@@ -594,7 +588,6 @@ public class AppClientModuleBuilder impl
                     connectionTrackerObjectName,
                     corbaGBeanObjectName,
                     earContext);
-            earContext.deleteFileOnExit(tempDirectory);
             appClientModule.setEarContext(appClientDeploymentContext);
             appClientModule.setRootEarContext(earContext);
 

Modified: geronimo/server/trunk/plugins/j2ee/geronimo-j2ee-builder/src/main/java/org/apache/geronimo/j2ee/deployment/EARConfigBuilder.java
URL: http://svn.apache.org/viewvc/geronimo/server/trunk/plugins/j2ee/geronimo-j2ee-builder/src/main/java/org/apache/geronimo/j2ee/deployment/EARConfigBuilder.java?rev=1157006&r1=1157005&r2=1157006&view=diff
==============================================================================
--- geronimo/server/trunk/plugins/j2ee/geronimo-j2ee-builder/src/main/java/org/apache/geronimo/j2ee/deployment/EARConfigBuilder.java (original)
+++ geronimo/server/trunk/plugins/j2ee/geronimo-j2ee-builder/src/main/java/org/apache/geronimo/j2ee/deployment/EARConfigBuilder.java Fri Aug 12 08:23:04 2011
@@ -398,10 +398,6 @@ public class EARConfigBuilder implements
                     return null;
                 }
                 application = new Application();
-            } finally {
-                if (applicationXmlUrl != null) {
-                    JarUtils.deleteJarFileURL(earFile, applicationXmlUrl);
-                }
             }
         }
 
@@ -590,7 +586,6 @@ public class EARConfigBuilder implements
                     corbaGBeanObjectName,
                     new HashMap()
             );
-            earContext.deleteFileOnExit(tempDirectory);
             applicationInfo.setEarContext(earContext);
             applicationInfo.setRootEarContext(earContext);
             earContext.getGeneralData().put(EARContext.MODULE_LIST_KEY, applicationInfo.getModuleLocations());
@@ -799,16 +794,6 @@ public class EARConfigBuilder implements
         } finally {
             for (Module<?, ?> module : applicationInfo.getModules()) {
                 module.close();
-                //In the non in-place deployment, a temporary file for each module in the ear will be created
-                if (!inPlaceDeployment && ConfigurationModuleType.EAR == applicationType) {
-                    try {
-                        FileUtils.recursiveDelete(new File(module.getModuleFile().getName()));
-                    } catch (Exception e) {
-                         if(log.isDebugEnabled()) {
-                             log.debug("Unable to delete the temporary file for module " + module.getName());
-                         }
-                    }
-                }
             }
         }
     }

Modified: geronimo/server/trunk/plugins/jetty8/geronimo-jetty8-builder/src/main/java/org/apache/geronimo/jetty8/deployment/JettyModuleBuilder.java
URL: http://svn.apache.org/viewvc/geronimo/server/trunk/plugins/jetty8/geronimo-jetty8-builder/src/main/java/org/apache/geronimo/jetty8/deployment/JettyModuleBuilder.java?rev=1157006&r1=1157005&r2=1157006&view=diff
==============================================================================
--- geronimo/server/trunk/plugins/jetty8/geronimo-jetty8-builder/src/main/java/org/apache/geronimo/jetty8/deployment/JettyModuleBuilder.java (original)
+++ geronimo/server/trunk/plugins/jetty8/geronimo-jetty8-builder/src/main/java/org/apache/geronimo/jetty8/deployment/JettyModuleBuilder.java Fri Aug 12 08:23:04 2011
@@ -268,11 +268,9 @@ public class JettyModuleBuilder extends 
         // parse the spec dd
         String specDD = null;
         WebApp webApp = null;
-        boolean specDDUrlCleanUpRequired = false;
         try {
             if (specDDUrl == null) {
                 specDDUrl = JarUtils.createJarURL(moduleFile, "WEB-INF/web.xml");
-                specDDUrlCleanUpRequired = true;
             }
 
             // read in the entire specDD as a string, we need this for getDeploymentDescriptor
@@ -352,10 +350,6 @@ public class JettyModuleBuilder extends 
         for (ModuleBuilderExtension mbe : moduleBuilderExtensions) {
             mbe.createModule(module, plan, moduleFile, targetPath, specDDUrl, environment, contextRoot, earName, naming, idBuilder);
         }
-
-        if (specDDUrlCleanUpRequired && specDDUrl != null) {
-            JarUtils.deleteJarFileURL(moduleFile, specDDUrl);
-        }
         return module;
     }
 

Modified: geronimo/server/trunk/plugins/tomcat/geronimo-tomcat7-builder/src/main/java/org/apache/geronimo/tomcat/deployment/TomcatModuleBuilder.java
URL: http://svn.apache.org/viewvc/geronimo/server/trunk/plugins/tomcat/geronimo-tomcat7-builder/src/main/java/org/apache/geronimo/tomcat/deployment/TomcatModuleBuilder.java?rev=1157006&r1=1157005&r2=1157006&view=diff
==============================================================================
--- geronimo/server/trunk/plugins/tomcat/geronimo-tomcat7-builder/src/main/java/org/apache/geronimo/tomcat/deployment/TomcatModuleBuilder.java (original)
+++ geronimo/server/trunk/plugins/tomcat/geronimo-tomcat7-builder/src/main/java/org/apache/geronimo/tomcat/deployment/TomcatModuleBuilder.java Fri Aug 12 08:23:04 2011
@@ -279,11 +279,9 @@ public class TomcatModuleBuilder extends
         // parse the spec dd
         String specDD = null;
         WebApp webApp = null;
-        boolean specDDUrlCleanUpRequired = false;
         try {
             if (specDDUrl == null) {
                 specDDUrl = JarUtils.createJarURL(moduleFile, "WEB-INF/web.xml");
-                specDDUrlCleanUpRequired = true;
             }
 
             // read in the entire specDD as a string, we need this for getDeploymentDescriptor
@@ -383,10 +381,6 @@ public class TomcatModuleBuilder extends
         for (ModuleBuilderExtension mbe : moduleBuilderExtensions) {
             mbe.createModule(module, plan, moduleFile, targetPath, specDDUrl, environment, contextRoot, earName, naming, idBuilder);
         }
-
-        if(specDDUrlCleanUpRequired && specDDUrl != null) {
-            JarUtils.deleteJarFileURL(moduleFile, specDDUrl);
-        }
         return module;
     }