You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commons-dev@ws.apache.org by nt...@apache.org on 2008/03/20 19:56:39 UTC

svn commit: r639406 - in /webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/lifecycle: LifecycleManager.java impl/FileAccessor.java impl/LifecycleManagerImpl.java impl/VMShutdownHook.java

Author: nthaker
Date: Thu Mar 20 11:56:37 2008
New Revision: 639406

URL: http://svn.apache.org/viewvc?rev=639406&view=rev
Log:
WSCOMMONS-314

Added:
    webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/lifecycle/impl/VMShutdownHook.java
Modified:
    webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/lifecycle/LifecycleManager.java
    webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/lifecycle/impl/FileAccessor.java
    webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/lifecycle/impl/LifecycleManagerImpl.java

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/lifecycle/LifecycleManager.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/lifecycle/LifecycleManager.java?rev=639406&r1=639405&r2=639406&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/lifecycle/LifecycleManager.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/lifecycle/LifecycleManager.java Thu Mar 20 11:56:37 2008
@@ -19,6 +19,7 @@
 
 package org.apache.axiom.attachments.lifecycle;
 
+import java.io.File;
 import java.io.IOException;
 
 import org.apache.axiom.attachments.lifecycle.impl.FileAccessor;
@@ -48,14 +49,14 @@
      * @param File
      * @throws IOException
      */
-    public void delete(String id) throws IOException;
+    public void delete(File file) throws IOException;
     
     /**
      * Mark the file for deletion on application/VM exit 
      * @param File
      * @throws IOException
      */
-    public void deleteOnExit(String id) throws IOException;
+    public void deleteOnExit(File file) throws IOException;
     
     /**
      * Mark attachment file for deletion when designated time interval in seconds 
@@ -64,5 +65,5 @@
      * @param File
      * @throws IOException
      */
-    public void deleteOnTimeInterval(int interval, String id) throws IOException;
+    public void deleteOnTimeInterval(int interval, File file) throws IOException;
 }

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/lifecycle/impl/FileAccessor.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/lifecycle/impl/FileAccessor.java?rev=639406&r1=639405&r2=639406&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/lifecycle/impl/FileAccessor.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/lifecycle/impl/FileAccessor.java Thu Mar 20 11:56:37 2008
@@ -44,16 +44,14 @@
 public class FileAccessor implements LifecycleEventHandler{
     private static final Log log = LogFactory.getLog(FileAccessor.class);
     File file = null;
-    String id = null;
     LifecycleManager manager;
-    
+
     //TODO remove hard coded time interval, 30 mins/1800 secs
     private final static int DELETE_INTERVAL = 1800;
-    public FileAccessor(LifecycleManager manager, File file, String id) {
+    public FileAccessor(LifecycleManager manager, File file) {
         super();
         this.manager = manager;
-        this.file = file;
-        this.id = id;
+        this.file = file;   
     }
 
     public DataHandler getDataHandler(String contentType) throws MessagingException {
@@ -93,17 +91,17 @@
     public void handleEvent(int eventId) throws IOException {
         switch (eventId) {
         case LifecycleEventDefinitions.DELETE_ON_EXIT:
-            manager.deleteOnExit(id);
+            manager.deleteOnExit(file);
             break;
         case LifecycleEventDefinitions.DELETE_ON_TIME_INTERVAL:
-            manager.deleteOnTimeInterval(DELETE_INTERVAL, id);
+            manager.deleteOnTimeInterval(DELETE_INTERVAL, file);
             break;
         case LifecycleEventDefinitions.READ_ONCE_AND_DELETE:
-            manager.delete(id);
+            manager.delete(file);
             break;
         default:
-            manager.delete(id);
-            break;
+            manager.delete(file);
+        break;
         }
     }
 
@@ -111,7 +109,8 @@
         return file;
     }
 
-    public String getId() {
-        return id;
+    public void setFile(File file) {
+        this.file = file;
     }
+
 }

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/lifecycle/impl/LifecycleManagerImpl.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/lifecycle/impl/LifecycleManagerImpl.java?rev=639406&r1=639405&r2=639406&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/lifecycle/impl/LifecycleManagerImpl.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/lifecycle/impl/LifecycleManagerImpl.java Thu Mar 20 11:56:37 2008
@@ -19,21 +19,24 @@
 
 package org.apache.axiom.attachments.lifecycle.impl;
 
+import java.io.File;
+import java.io.IOException;
+import java.security.AccessController;
+import java.security.PrivilegedActionException;
+import java.security.PrivilegedExceptionAction;
+import java.util.Hashtable;
+
 import org.apache.axiom.attachments.lifecycle.LifecycleManager;
 import org.apache.axiom.om.util.UUIDGenerator;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
-import java.io.File;
-import java.io.IOException;
-import java.util.Hashtable;
-import java.util.Map;
-
 public class LifecycleManagerImpl implements LifecycleManager {
     private static final Log log = LogFactory.getLog(LifecycleManagerImpl.class);
-    private Map table = new Hashtable();
-    public static LifecycleManager manager = new LifecycleManagerImpl();
 
+    //Hashtable to store file accessors.
+    private static Hashtable table = new Hashtable();
+    private VMShutdownHook hook = null;
     public LifecycleManagerImpl() {
         super(); 
     }
@@ -66,13 +69,11 @@
 
         String fileString = "Axis2" + id + ".att";
         file = new File(dir, fileString);
-        //add the file to table
-        table.put(id, file);
-        FileAccessor fa = new FileAccessor(manager, file, id);
-        //TODO: change deleteOnExit call such that it's sent as an event to 
-        //LifecycleEventHandler. example fa.handleEvent(LifeCycleDefinition.DELETE_ON_EXIT)
-        //This is the default behaviour. Delete file on VM Exit.
-        deleteOnExit(id);
+        FileAccessor fa = new FileAccessor(this, file);
+        //add the fileAccesor to table
+        table.put(file, fa);
+        //Default behaviour
+        deleteOnExit(file);
         if(log.isDebugEnabled()){
             log.debug("End Create()");
         }
@@ -82,15 +83,27 @@
     /* (non-Javadoc)
      * @see org.apache.axiom.lifecycle.LifecycleManager#delete(java.io.File)
      */
-    public void delete(String id) throws IOException {
+    public void delete(File file) throws IOException {
         if(log.isDebugEnabled()){
             log.debug("Start delete()");
         }
-        File file = getFile(id); 
 
         if(file!=null && file.exists()){
-            file.delete();
-            table.remove(id);
+            table.remove(file);
+            if(log.isDebugEnabled()){
+                log.debug("invoking file.delete()");
+            }
+
+            if(file.delete()){
+                if(log.isDebugEnabled()){
+                    log.debug("delete() successful");
+                }
+            }else{
+                if(log.isDebugEnabled()){
+                    log.debug("Cannot delete file, set to delete on VM shutdown");
+                }
+                deleteOnExit(file);
+            }
         }
         if(log.isDebugEnabled()){
             log.debug("End delete()");
@@ -100,14 +113,20 @@
     /* (non-Javadoc)
      * @see org.apache.axiom.lifecycle.LifecycleManager#deleteOnExit(java.io.File)
      */
-    public void deleteOnExit(String id) throws IOException {
+    public void deleteOnExit(File file) throws IOException {
         if(log.isDebugEnabled()){
             log.debug("Start deleteOnExit()");
         }
-        File file = getFile(id); 
+        if(hook == null){
+            hook = RegisterVMShutdownHook();
+        }
+
         if(file!=null){
-            file.deleteOnExit();
-            table.remove(id);
+            if(log.isDebugEnabled()){
+                log.debug("Invoking deleteOnExit() for file = "+file.getAbsolutePath());
+            }
+            hook.add(file);
+            table.remove(file);
         }
         if(log.isDebugEnabled()){
             log.debug("End deleteOnExit()");
@@ -117,39 +136,59 @@
     /* (non-Javadoc)
      * @see org.apache.axiom.lifecycle.LifecycleManager#deleteOnTimeInterval(int)
      */
-    public void deleteOnTimeInterval(int interval, String id) throws IOException {
+    public void deleteOnTimeInterval(int interval, File file) throws IOException {
         if(log.isDebugEnabled()){
             log.debug("Start deleteOnTimeInterval()");
         }
-        File file = getFile(id);
-        Thread t = new Thread(new LifecycleManagerImpl.FileDeletor(interval, file, id));
+
+        Thread t = new Thread(new LifecycleManagerImpl.FileDeletor(interval, file));
         t.start();
         if(log.isDebugEnabled()){
             log.debug("End deleteOnTimeInterval()");
         }
     }
 
-    private File getFile(String id){
-        return (File)table.get(id);
+    private VMShutdownHook RegisterVMShutdownHook() throws RuntimeException{
+        if(log.isDebugEnabled()){
+            log.debug("Start RegisterVMShutdownHook()");
+        }
+        try{
+            hook = (VMShutdownHook)AccessController.doPrivileged(new PrivilegedExceptionAction() {
+                public Object run() throws SecurityException, IllegalStateException, IllegalArgumentException {
+                    VMShutdownHook hook = VMShutdownHook.hook();
+                    Runtime.getRuntime().addShutdownHook(hook);
+                    return hook;
+                }
+            });
+        }catch (PrivilegedActionException e) {
+            if (log.isDebugEnabled()) {
+                log.debug("Exception thrown from AccessController: " + e);
+                log.debug("VM Shutdown Hook not registered.");
+            }
+            throw new RuntimeException(e);
+        }
+        if(log.isDebugEnabled()){
+            log.debug("Exit RegisterVMShutdownHook()");
+        }
+        return hook;
     }
 
     public class FileDeletor implements Runnable{
         int interval;
         File _file;
-        String _id;
-        public FileDeletor(int interval, File file, String id) {
+
+        public FileDeletor(int interval, File file) {
             super();
             this.interval = interval;
-            this._file = file;
-            this._id = id;
+            this._file = file;           
         }
 
         public void run() {
             try{
                 Thread.sleep(interval*1000);
                 if(_file.exists()){
+                    table.remove(_file);
                     _file.delete();
-                    table.remove(_id);
                 }
             }catch(InterruptedException e){
                 //Log Exception

Added: webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/lifecycle/impl/VMShutdownHook.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/lifecycle/impl/VMShutdownHook.java?rev=639406&view=auto
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/lifecycle/impl/VMShutdownHook.java (added)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/lifecycle/impl/VMShutdownHook.java Thu Mar 20 11:56:37 2008
@@ -0,0 +1,76 @@
+/*
+ * 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.axiom.attachments.lifecycle.impl;
+
+import java.io.File;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.Set;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+/*
+ * VMShutdown Hook will be registered with Runtime object to be invoked 
+ * when Virutal Machine is shutdown.
+ * This class will be used to delete any cached attachments file that where
+ * added by runtime to be deleted on VM shutdown.
+ */
+public class VMShutdownHook extends Thread {
+    private static final Log log = LogFactory.getLog(VMShutdownHook.class);
+    private static VMShutdownHook instance = null;
+    private static Set files = Collections.synchronizedSet(new HashSet());
+
+    static VMShutdownHook hook() {
+        if (instance == null)
+            instance = new VMShutdownHook();
+
+        return instance;
+    }
+
+    private VMShutdownHook(){}
+
+    void add(File file) {
+        if(file == null){
+            return;
+        }
+        if(log.isDebugEnabled()){
+            log.debug("Adding File to Shutdown Hook Collection");
+        }
+        files.add(file);   
+    }
+
+    public void run() {
+        if(log.isDebugEnabled()){
+            log.debug("JVM running VM Shutdown Hook");
+        }
+        Iterator iter = files.iterator();
+        while(iter.hasNext()){
+            File file = (File)iter.next();
+            if(log.isDebugEnabled()){
+                log.debug("Deleting File from Shutdown Hook Collection"+file.getAbsolutePath());
+            }    		
+            file.delete();
+        }
+
+        if(log.isDebugEnabled()){
+            log.debug("JVM Done running VM Shutdown Hook");
+        }
+    }
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@ws.apache.org
For additional commands, e-mail: commons-dev-help@ws.apache.org