stevel 2003/01/25 11:22:17 Modified: java/src/org/apache/axis/attachments AttachmentPart.java ManagedMemoryDataSource.java MultiPartDimeInputStream.java MultiPartRelatedInputStream.java Log: This is my *simple but workable* attachment cleanup solution: the ManagedMemoryDataSource provides an accessor to the path to the file, AttachmentPart extracts this if it is not null and provides an accessor. on finalization it cleans up, and users can get the filename and detach the AttachmentPart from it. I've been using it 3 days; seems to work, though we need to do echo tests on big files. Revision Changes Path 1.35 +90 -10 xml-axis/java/src/org/apache/axis/attachments/AttachmentPart.java Index: AttachmentPart.java =================================================================== RCS file: /home/cvs/xml-axis/java/src/org/apache/axis/attachments/AttachmentPart.java,v retrieving revision 1.34 retrieving revision 1.35 diff -u -r1.34 -r1.35 --- AttachmentPart.java 5 Jan 2003 15:45:05 -0000 1.34 +++ AttachmentPart.java 25 Jan 2003 19:22:16 -0000 1.35 @@ -65,6 +65,7 @@ import javax.xml.soap.SOAPException; import java.util.Iterator; import java.io.ByteArrayOutputStream; +import java.io.File; /** * Class AttachmentPart @@ -77,7 +78,9 @@ protected static Log log = LogFactory.getLog(AttachmentPart.class.getName()); - /** Field datahandler */ + /** Field datahandler + * TODO: make private? + * */ javax.activation.DataHandler datahandler = null; /** Field mimeHeaders */ @@ -88,6 +91,11 @@ private Object contentObject; /** + * the name of a file used to store the data + */ + private String attachmentFile; + + /** * Constructor AttachmentPart */ public AttachmentPart() { @@ -103,8 +111,17 @@ setMimeHeader(HTTPConstants.HEADER_CONTENT_ID, SessionUtils.generateSessionId()); datahandler = dh; - if(dh != null) + if(dh != null) { setMimeHeader(HTTPConstants.HEADER_CONTENT_TYPE, dh.getContentType()); + } + } + + /** + * on death, we clean up our file + * @throws Throwable + */ + protected void finalize() throws Throwable { + dispose(); } /** @@ -161,14 +178,16 @@ boolean found = false; if (values != null) { for (int j = 0; j < values.length; j++) { - if (!hdr.getValue().equalsIgnoreCase(values[j])) + if (!hdr.getValue().equalsIgnoreCase(values[j])) { continue; + } found = true; break; } } - if (!found) + if (!found) { return false; + } } return true; } @@ -305,11 +324,20 @@ * DataHandler</CODE> object */ public void setDataHandler(DataHandler datahandler) { - if(datahandler == null) + if(datahandler == null) { throw new java.lang.IllegalArgumentException( Messages.getMessage("illegalArgumentException00")); + } this.datahandler = datahandler; setMimeHeader(HTTPConstants.HEADER_CONTENT_TYPE, datahandler.getContentType()); + //now look at the source of the data + javax.activation.DataSource ds = datahandler.getDataSource(); + if (ds instanceof ManagedMemoryDataSource) { + //and get the filename if appropriate + extractFilename((ManagedMemoryDataSource)ds); + } + + } /** @@ -346,8 +374,9 @@ * was a data transformation error */ public Object getContent() throws SOAPException { - if(contentObject != null) + if(contentObject != null) { return contentObject; + } if(datahandler == null) { throw new SOAPException(Messages.getMessage("noContent")); @@ -355,7 +384,6 @@ javax.activation.DataSource ds = datahandler.getDataSource(); if (ds instanceof ManagedMemoryDataSource) { - ManagedMemoryDataSource mds = (ManagedMemoryDataSource) ds; if (ds.getContentType().equals("text/plain")) { try { @@ -373,6 +401,7 @@ return null; } + /** * Sets the content of this attachment part to that of the * given <CODE>Object</CODE> and sets the value of the <CODE> @@ -408,8 +437,9 @@ } } else if (object instanceof java.io.InputStream) { try { - datahandler = new DataHandler(new ManagedMemoryDataSource((java.io.InputStream)object, - ManagedMemoryDataSource.MAX_MEMORY_DISK_CACHED, contentType, true)); + ManagedMemoryDataSource source = new ManagedMemoryDataSource((java.io.InputStream)object, + ManagedMemoryDataSource.MAX_MEMORY_DISK_CACHED, contentType, true); + datahandler = new DataHandler(source); contentObject = object; return; } catch (java.io.IOException io) { @@ -443,8 +473,9 @@ * while trying to determine the size. */ public int getSize() throws SOAPException { - if (datahandler == null) + if (datahandler == null) { return 0; + } ByteArrayOutputStream bout = new ByteArrayOutputStream(); try { datahandler.writeTo(bout); @@ -477,5 +508,54 @@ */ public String getContentIdRef() { return Attachments.CIDprefix + getContentId(); + } + + /** + * maybe add file name to the attachment + * @param source the source of the data + */ + + private void extractFilename(ManagedMemoryDataSource source) { + //check for there being a file + if(source.getDiskCacheFile()!=null) { + String path = source.getDiskCacheFile().getAbsolutePath(); + setAttachmentFile(path); + } + } + + /** + * set the filename of this attachment part + * @param path + */ + protected void setAttachmentFile(String path) { + attachmentFile=path; + } + + /** + * detach the attachment file from this class, so it is not cleaned up + */ + public void detachAttachmentFile() { + attachmentFile=null; + } + /** + * get the filename of this attachment + * @return the filename or null for an uncached file + */ + public String getAttachmentFile() { + return attachmentFile; + } + + /** + * when an attachment part is disposed, any associated files + * are deleted + */ + public synchronized void dispose() { + if(attachmentFile!=null) { + File f=new File(attachmentFile); + //no need to check for existence here. + f.delete(); + //set the filename to null to stop repeated use + setAttachmentFile(null); + } } } 1.27 +11 -1 xml-axis/java/src/org/apache/axis/attachments/ManagedMemoryDataSource.java Index: ManagedMemoryDataSource.java =================================================================== RCS file: /home/cvs/xml-axis/java/src/org/apache/axis/attachments/ManagedMemoryDataSource.java,v retrieving revision 1.26 retrieving revision 1.27 diff -u -r1.26 -r1.27 --- ManagedMemoryDataSource.java 5 Jan 2003 15:45:05 -0000 1.26 +++ ManagedMemoryDataSource.java 25 Jan 2003 19:22:16 -0000 1.27 @@ -426,9 +426,11 @@ : new File( attdir)); - log.debug( + if(log.isDebugEnabled()) { + log.debug( Messages.getMessage( "diskCache", diskCacheFile.getAbsolutePath())); + } cachediskstream = new java.io.BufferedOutputStream( new java.io.FileOutputStream(diskCacheFile)); @@ -947,5 +949,13 @@ } catch (java.lang.Exception e) { log.error(Messages.getMessage("exception00"), e); } + } + + /** + * get the filename of the content if it is cached to disk. + * @return file object pointing to file, or null for memory-stored content + */ + public File getDiskCacheFile() { + return diskCacheFile; } } 1.17 +7 -6 xml-axis/java/src/org/apache/axis/attachments/MultiPartDimeInputStream.java Index: MultiPartDimeInputStream.java =================================================================== RCS file: /home/cvs/xml-axis/java/src/org/apache/axis/attachments/MultiPartDimeInputStream.java,v retrieving revision 1.16 retrieving revision 1.17 diff -u -r1.16 -r1.17 --- MultiPartDimeInputStream.java 5 Jan 2003 15:45:05 -0000 1.16 +++ MultiPartDimeInputStream.java 25 Jan 2003 19:22:16 -0000 1.17 @@ -198,15 +198,16 @@ if (type != null && !dimeDelimitedStream.getDimeTypeNameFormat().equals(DimeTypeNameFormat.MIME)) { type = "application/uri; uri=\"" + type + "\""; } - - DataHandler dh = new DataHandler( - new ManagedMemoryDataSource(dimeDelimitedStream, - ManagedMemoryDataSource.MAX_MEMORY_DISK_CACHED, type, true)); - AttachmentPart ap = new AttachmentPart(dh); - if (contentId != null) + ManagedMemoryDataSource source = new ManagedMemoryDataSource(dimeDelimitedStream, + ManagedMemoryDataSource.MAX_MEMORY_DISK_CACHED, type, true); + DataHandler dh = new DataHandler(source); + + AttachmentPart ap = new AttachmentPart(dh); + if (contentId != null) { ap.setMimeHeader(HTTPConstants.HEADER_CONTENT_ID, contentId); + } addPart(contentId, "", ap); 1.37 +3 -3 xml-axis/java/src/org/apache/axis/attachments/MultiPartRelatedInputStream.java Index: MultiPartRelatedInputStream.java =================================================================== RCS file: /home/cvs/xml-axis/java/src/org/apache/axis/attachments/MultiPartRelatedInputStream.java,v retrieving revision 1.36 retrieving revision 1.37 diff -u -r1.36 -r1.37 --- MultiPartRelatedInputStream.java 12 Jan 2003 01:46:46 -0000 1.36 +++ MultiPartRelatedInputStream.java 25 Jan 2003 19:22:16 -0000 1.37 @@ -600,9 +600,9 @@ contentTransferEncoding); } - DataHandler dh = new DataHandler( - new ManagedMemoryDataSource( - decodedStream, ManagedMemoryDataSource.MAX_MEMORY_DISK_CACHED, contentType, true)); + ManagedMemoryDataSource source = new ManagedMemoryDataSource( + decodedStream, ManagedMemoryDataSource.MAX_MEMORY_DISK_CACHED, contentType, true); + DataHandler dh = new DataHandler(source); AttachmentPart ap = new AttachmentPart(dh); if (contentId != null) {