diff --git a/src/org/exist/util/io/Resource.java b/src/org/exist/util/io/Resource.java index 561e4a34aa2..8e29a83e373 100644 --- a/src/org/exist/util/io/Resource.java +++ b/src/org/exist/util/io/Resource.java @@ -108,6 +108,8 @@ public static File createTempFile(String prefix, String suffix, File directory) private Collection collection = null; private DocumentImpl resource = null; + + File file = null; public Resource(XmldbURI uri) { super(uri.toString()); @@ -374,7 +376,7 @@ public boolean _renameTo(File dest) { public boolean renameTo(File dest) { - System.out.println("rename from "+uri+" to "+dest.getPath()); +// System.out.println("rename from "+uri+" to "+dest.getPath()); XmldbURI destinationPath = ((Resource)dest).uri; @@ -437,13 +439,16 @@ public boolean renameTo(File dest) { } } - private File serialize(final DBBroker broker, final DocumentImpl doc) throws IOException { + private synchronized File serialize(final DBBroker broker, final DocumentImpl doc) throws IOException { + if (file != null) + throw new IOException(doc.getFileURI().toString()+" locked."); + try { Serializer serializer = broker.getSerializer(); serializer.setUser(broker.getSubject()); serializer.setProperties(XML_OUTPUT_PROPERTIES); - File file = File.createTempFile("eXist", ".xml"); + file = File.createTempFile("eXist-resource-", ".xml"); file.deleteOnExit(); Writer w = new OutputStreamWriter(new FileOutputStream(file), "UTF-8"); @@ -458,6 +463,57 @@ private File serialize(final DBBroker broker, final DocumentImpl doc) throws IOE throw new IOException(e); } } + + protected void freeFile() throws IOException { + + if (isXML()) { + if (file == null) + throw new IOException(); + + file.delete(); + + file = null; + } + } + + protected synchronized void uploadTmpFile() throws IOException { + if (file == null) + throw new IOException(); + + DBBroker broker = null; + BrokerPool db = null; + TransactionManager tm = null; + Txn txn = null; + + try { + try { + db = BrokerPool.getInstance(); + broker = db.get(null); + } catch (EXistException e) { + throw new IOException(e); + } + + tm = db.getTransactionManager(); + txn = tm.beginTransaction(); + + FileInputSource is = new FileInputSource(file); + + IndexInfo info = collection.validateXMLResource(txn, broker, uri.lastSegment(), is); +// info.getDocument().getMetadata().setMimeType(mimeType.getName()); + + is = new FileInputSource(file); + collection.store(txn, broker, info, is, false); + + tm.commit(txn); + + } catch ( Exception e ) { + e.printStackTrace(); + if (txn != null) tm.abort(txn); + } finally { + if (db != null) + db.release( broker ); + } + } private void moveResource(DBBroker broker, Txn txn, DocumentImpl doc, Collection source, Collection destination, XmldbURI newName) throws PermissionDeniedException, LockException, IOException, SAXException, EXistException { @@ -512,7 +568,7 @@ private void moveResource(DBBroker broker, Txn txn, DocumentImpl doc, Collection File tempFile = null; FileInputStream is = null; try { - tempFile = File.createTempFile("eXist", ".xml"); + tempFile = File.createTempFile("eXist-resource-", ".xml"); tempFile.deleteOnExit(); Writer w = new OutputStreamWriter(new FileOutputStream(tempFile), "UTF-8"); diff --git a/src/org/exist/util/io/ResourceInputStream.java b/src/org/exist/util/io/ResourceInputStream.java index 6a3a9d94c81..31a913f2dda 100644 --- a/src/org/exist/util/io/ResourceInputStream.java +++ b/src/org/exist/util/io/ResourceInputStream.java @@ -42,6 +42,8 @@ public ResourceInputStream(Resource file) throws FileNotFoundException { public void close() throws IOException { super.close(); + resource.freeFile(); + if (resource.isXML()) { //XXX: cleanup tmp file } diff --git a/src/org/exist/util/io/ResourceOutputStream.java b/src/org/exist/util/io/ResourceOutputStream.java index bd46ddc73b8..362c952b30d 100644 --- a/src/org/exist/util/io/ResourceOutputStream.java +++ b/src/org/exist/util/io/ResourceOutputStream.java @@ -31,17 +31,25 @@ */ public class ResourceOutputStream extends FileOutputStream { + private Resource resource; + public ResourceOutputStream(Resource file) throws FileNotFoundException { super(file.getFile()); + + resource = file; } public ResourceOutputStream(Resource file, boolean append) throws FileNotFoundException { super(file.getFile(), append); + + resource = file; } public void close() throws IOException { super.close(); - + + resource.freeFile(); + //XXX: xml upload back to db //XXX: locking? diff --git a/src/org/exist/util/io/ResourceRandomAccess.java b/src/org/exist/util/io/ResourceRandomAccess.java index eee4a1cf8d7..2225b0882b4 100644 --- a/src/org/exist/util/io/ResourceRandomAccess.java +++ b/src/org/exist/util/io/ResourceRandomAccess.java @@ -31,13 +31,19 @@ */ public class ResourceRandomAccess extends RandomAccessFile { + private Resource resource; + public ResourceRandomAccess(Resource resource, String mode) throws FileNotFoundException { super(resource.getFile(), mode); + + this.resource = resource; } public void close() throws IOException { super.close(); + resource.freeFile(); + //XXX: xml upload back to db //XXX: locking?