Author: nthaker
Date: Wed Apr 23 16:31:53 2008
New Revision: 651105
URL: http://svn.apache.org/viewvc?rev=651105&view=rev
Log:
WSCOMMONS-331
Added:
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/lifecycle/DataHandlerExt.java
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/lifecycle/impl/DataHandlerExtImpl.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
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/DataHandlerExt.java
URL:
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/lifecycle/DataHandlerExt.java?rev=651105&view=auto
==============================================================================
---
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/lifecycle/DataHandlerExt.java
(added)
+++
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/lifecycle/DataHandlerExt.java
Wed Apr 23 16:31:53 2008
@@ -0,0 +1,31 @@
+package org.apache.axiom.attachments.lifecycle;
+
+import java.io.IOException;
+
+public interface DataHandlerExt {
+
+ /**
+ * This method will give users an option to trigger a purge
+ * on temporary attachment files. Temp files are created for
+ * attachment data that is greater than a threshold limit.
+ * On client side These temp attachment files are not deleted
+ * untilthe virtual machine exits as user can choose to read
+ * this dataHandler. So if user is not going to use the data
+ * handlers provided on this temproray files they can choose
+ * to purge the file.
+ */
+ public void purgeDataSource() throws IOException;
+
+ /**
+ * This method will give users an option to trigger a delete on
+ * temporary attachment file when DataHandler associated with the
+ * attachment is read once. Temp files are created for
+ * attachment data that is greater than a threshold limit.
+ * On client side These temp attachment files are not deleted untill
+ * the virtual machine exits. This method gives options to user to
+ * trigger a delete on attachment files when they read the dataHandler
+ * once.
+ */
+
+ public void deleteWhenReadOnce() throws IOException;
+}
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=651105&r1=651104&r2=651105&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
Wed Apr 23 16:31:53 2008
@@ -66,4 +66,12 @@
* @throws IOException
*/
public void deleteOnTimeInterval(int interval, File file) throws
IOException;
+
+ /**
+ * This method will return the file accessor associated with this file.
+ * @param file
+ * @return
+ * @throws IOException
+ */
+ public FileAccessor getFileAccessor(String file) throws IOException;
}
Added:
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/lifecycle/impl/DataHandlerExtImpl.java
URL:
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/lifecycle/impl/DataHandlerExtImpl.java?rev=651105&view=auto
==============================================================================
---
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/lifecycle/impl/DataHandlerExtImpl.java
(added)
+++
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/lifecycle/impl/DataHandlerExtImpl.java
Wed Apr 23 16:31:53 2008
@@ -0,0 +1,105 @@
+package org.apache.axiom.attachments.lifecycle.impl;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Observable;
+import java.util.Observer;
+
+import javax.activation.DataHandler;
+import javax.activation.DataSource;
+
+import org.apache.axiom.attachments.CachedFileDataSource;
+import org.apache.axiom.attachments.lifecycle.DataHandlerExt;
+import org.apache.axiom.attachments.lifecycle.LifecycleManager;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+public class DataHandlerExtImpl extends DataHandler implements
+ DataHandlerExt, Observer {
+
+ private static final Log log =
LogFactory.getLog(DataHandlerExtImpl.class);
+ private DataHandler dataHandler = null;
+ private LifecycleManager manager = null;
+ private static int READ_COUNT = 1;
+ private boolean deleteOnreadOnce = false;
+ public DataHandlerExtImpl(DataHandler dataHandler, LifecycleManager
manager){
+ super(dataHandler.getDataSource());
+ this.dataHandler = dataHandler;
+ this.manager = manager;
+ }
+
+ public void deleteWhenReadOnce() throws IOException {
+ deleteOnreadOnce = true;
+ FileAccessor fa =manager.getFileAccessor(getName());
+ if(fa==null){
+ log.warn("Could not find FileAccessor, delete on
readOnce Failed");
+ return;
+ }
+ if(fa.getAccessCount() >= READ_COUNT){
+ purgeDataSource();
+ }else{
+ fa.addObserver(this);
+ }
+ }
+
+ public void purgeDataSource() throws IOException {
+ if(log.isDebugEnabled()){
+ log.debug("Start purgeDataSource");
+ }
+ File file = getFile();
+ if(file!=null){
+ //Invoke delete from LifecycleManager
+ manager.delete(file);
+ //If file was registered with VMShutdown hook
+ //lets remove it from the list to be deleted on VMExit.
+ VMShutdownHook hook =VMShutdownHook.hook();
+ if(hook.isRegistered()){
+ hook.remove(file);
+ }
+ if(log.isDebugEnabled()){
+ log.debug("File Purged and removed from
Shutdown Hook Collection");
+ }
+ }else{
+ if(log.isDebugEnabled()){
+ log.debug("DataSource is not a
CachedFileDataSource, Unable to Purge.");
+ }
+ }
+
+ if(log.isDebugEnabled()){
+ log.debug("End purgeDataSource");
+ }
+ }
+
+ public void update(Observable o, Object arg) {
+ try{
+ if(log.isDebugEnabled()){
+ log.debug("Start update in Observer");
+ }
+ if(o instanceof FileAccessor){
+ FileAccessor fa = (FileAccessor)o;
+ if(deleteOnreadOnce &&
fa.getAccessCount()>=READ_COUNT){
+ purgeDataSource();
+ }
+ }
+ }catch(IOException e){
+ if(log.isDebugEnabled()){
+ log.debug("delete on readOnce Failed");
+ }
+ log.warn("delete on readOnce Failed with IOException in
Observer"+e.getMessage());
+ }
+ if(log.isDebugEnabled()){
+ log.debug("End update in Observer");
+ }
+ }
+
+ private File getFile(){
+ //get DataSource from DataHandler
+ DataSource dataSource = dataHandler.getDataSource();
+ if(dataSource instanceof CachedFileDataSource){
+ CachedFileDataSource cds =
(CachedFileDataSource)dataSource;
+ //get the file object from data source.
+ return cds.getFile();
+ }
+ return null;
+ }
+}
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=651105&r1=651104&r2=651105&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
Wed Apr 23 16:31:53 2008
@@ -19,13 +19,6 @@
package org.apache.axiom.attachments.lifecycle.impl;
-import org.apache.axiom.attachments.CachedFileDataSource;
-import org.apache.axiom.attachments.lifecycle.LifecycleManager;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-
-import javax.activation.DataHandler;
-import javax.mail.MessagingException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
@@ -33,6 +26,15 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
+import java.util.Observable;
+
+import javax.activation.DataHandler;
+import javax.mail.MessagingException;
+
+import org.apache.axiom.attachments.CachedFileDataSource;
+import org.apache.axiom.attachments.lifecycle.LifecycleManager;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
/**
* FileAccessor wraps the attachment temp file. It is created from PartOnFile.
@@ -41,13 +43,11 @@
* events to handle the the files lifecycle.
*
*/
-public class FileAccessor implements LifecycleEventHandler{
+public class FileAccessor extends Observable{
private static final Log log = LogFactory.getLog(FileAccessor.class);
File file = null;
LifecycleManager manager;
-
- //TODO remove hard coded time interval, 30 mins/1800 secs
- private final static int DELETE_INTERVAL = 1800;
+ private int accessCount = 0;
public FileAccessor(LifecycleManager manager, File file) {
super();
this.manager = manager;
@@ -57,10 +57,15 @@
public DataHandler getDataHandler(String contentType) throws
MessagingException {
if(log.isDebugEnabled()){
log.debug("getDataHandler()");
+ log.debug("accessCount =" +accessCount);
}
CachedFileDataSource dataSource = new CachedFileDataSource(file);
dataSource.setContentType(contentType);
- return new DataHandler(dataSource);
+ accessCount++;
+ setChanged();
+ notifyObservers();
+ DataHandler dataHandler = new DataHandler(dataSource);
+ return new DataHandlerExtImpl(dataHandler, manager);
}
public String getFileName() throws MessagingException {
@@ -87,24 +92,7 @@
public long getSize() {
return file.length();
}
-
- public void handleEvent(int eventId) throws IOException {
- switch (eventId) {
- case LifecycleEventDefinitions.DELETE_ON_EXIT:
- manager.deleteOnExit(file);
- break;
- case LifecycleEventDefinitions.DELETE_ON_TIME_INTERVAL:
- manager.deleteOnTimeInterval(DELETE_INTERVAL, file);
- break;
- case LifecycleEventDefinitions.READ_ONCE_AND_DELETE:
- manager.delete(file);
- break;
- default:
- manager.delete(file);
- break;
- }
- }
-
+
public File getFile() {
return file;
}
@@ -112,5 +100,9 @@
public void setFile(File file) {
this.file = file;
}
+
+ public int getAccessCount() {
+ return accessCount;
+ }
}
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=651105&r1=651104&r2=651105&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
Wed Apr 23 16:31:53 2008
@@ -71,7 +71,7 @@
file = new File(dir, fileString);
FileAccessor fa = new FileAccessor(this, file);
//add the fileAccesor to table
- table.put(file, fa);
+ table.put(fileString, fa);
//Default behaviour
deleteOnExit(file);
if(log.isDebugEnabled()){
@@ -201,6 +201,10 @@
}
}
}
+
+ public FileAccessor getFileAccessor(String fileName) throws IOException
{
+ return (FileAccessor)table.get(fileName);
+ }
}
Modified:
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=651105&r1=651104&r2=651105&view=diff
==============================================================================
---
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/lifecycle/impl/VMShutdownHook.java
(original)
+++
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/lifecycle/impl/VMShutdownHook.java
Wed Apr 23 16:31:53 2008
@@ -52,7 +52,15 @@
}
private VMShutdownHook(){}
-
+ void remove(File file){
+ if(file == null){
+ return;
+ }
+ if(log.isDebugEnabled()){
+ log.debug("Removing File to Shutdown Hook Collection");
+ }
+ files.remove(file);
+ }
void add(File file) {
if(file == null){
return;