Index: lib/metacat.properties.metadata.xml
===================================================================
--- lib/metacat.properties.metadata.xml	(revision 4333)
+++ lib/metacat.properties.metadata.xml	(revision 4334)
@@ -242,10 +242,22 @@
 	</config>
 	
 	<config>
+		<key>application.documentfilepath</key>
+		<label>Document File Path</label>
+		<group>3</group>
+		<index>8</index>
+		<description>
+			Where to store metadata documents. This should be some location
+			outside the knb install direcories.
+		</description>
+		<helpFile>properties.html#application.documentfilepath</helpFile>
+	</config>
+	
+	<config>
 		<key>replication.logdir</key>
 		<label>Replication Log Directory</label>
 		<group>3</group>
-		<index>8</index>
+		<index>9</index>
 		<description>
 			The directory where replication log should be located.
 		</description>
Index: lib/metacat.properties
===================================================================
--- lib/metacat.properties	(revision 4333)
+++ lib/metacat.properties	(revision 4334)
@@ -32,6 +32,7 @@
 application.knbSiteURL=http://knb.ecoinformatics.org
 application.datafilepath=/var/metacat/data
 application.inlinedatafilepath=/var/metacat/inline-data
+application.documentfilepath=/var/metacat/documents
 application.tempDir=/var/metacat/temporary
 # the location of cgi scripts relative to the metacat context directory
 application.cgiDir=/cgi-bin
Index: src/edu/ucsb/nceas/metacat/MetaCatServlet.java
===================================================================
--- src/edu/ucsb/nceas/metacat/MetaCatServlet.java	(revision 4333)
+++ src/edu/ucsb/nceas/metacat/MetaCatServlet.java	(revision 4334)
@@ -27,9 +27,11 @@
 package edu.ucsb.nceas.metacat;
 
 import java.io.BufferedInputStream;
+import java.io.BufferedReader;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileReader;
+import java.io.FileWriter;
 import java.io.IOException;
 import java.io.OutputStreamWriter;
 import java.io.PrintWriter;
@@ -72,6 +74,7 @@
 import com.oreilly.servlet.multipart.Part;
 
 import edu.ucsb.nceas.metacat.dataquery.DataQuery;
+import edu.ucsb.nceas.metacat.service.DatabaseService;
 import edu.ucsb.nceas.metacat.service.PropertyService;
 import edu.ucsb.nceas.metacat.service.ServiceException;
 import edu.ucsb.nceas.metacat.service.SessionService;
@@ -214,6 +217,9 @@
 		Logger logMetacat = Logger.getLogger(MetaCatServlet.class);
 
 		try {
+            DatabaseService databaseService = DatabaseService.getInstance();
+            logMetacat.debug("DatabaseService singleton created " + databaseService);
+			
 			// initialize DBConnection pool
 			DBConnectionPool connPool = DBConnectionPool.getInstance();
 			logMetacat.debug("DBConnection pool initialized: " + connPool.toString());
@@ -1664,6 +1670,8 @@
             ZipOutputStream zout, boolean withInlineData, Hashtable params)
             throws ClassNotFoundException, IOException, SQLException,
             McdbException, Exception {
+    	String documentDir = PropertyService.getProperty("application.documentfilepath");
+    	
         Logger logMetacat = Logger.getLogger(MetaCatServlet.class);
         try {
             
@@ -1744,6 +1752,33 @@
                     response.setContentType("text/xml"); //MIME type
                     response.setHeader("Content-Disposition",
                             "attachment; filename=" + docid + ".xml");
+                    
+                    // Try to get the metadata file from disk. If it isn't
+					// found, create it from the
+					// db and write it to disk then.
+					String documentPath = documentDir + FileUtil.getFS() + docid;
+					try {
+						if (FileUtil.getFileStatus(documentPath) == FileUtil.DOES_NOT_EXIST) {
+							FileWriter fileWriter = new FileWriter(documentPath);
+							doc.toXml(fileWriter, user, groups, withInlineData);
+						}
+						FileReader fileReader = new FileReader(documentPath);
+						BufferedReader br = new BufferedReader(fileReader);
+						PrintWriter pw = new PrintWriter(streamOut);
+						String nextLine;
+						while ((nextLine = br.readLine()) != null) {
+							pw.println(nextLine);
+						}
+						pw.flush();					
+					} catch (Exception e) {
+						// any exceptions in reading the xml from disc, and we go back to the
+						// old way of creating the xml directly.
+						logMetacat.error("could not read from document file " + documentPath 
+								+ ": " + e.getMessage());
+						PrintWriter out = new PrintWriter(streamOut);
+						doc.toXml(out, user, groups, withInlineData);
+					}
+                    	
                     PrintWriter out = new PrintWriter(streamOut);
                     doc.toXml(out, user, groups, withInlineData);
                 } else {
@@ -2097,7 +2132,7 @@
                             .getDBConnection("MetaCatServlet.handleInsertOrUpdateAction");
                     serialNumber = dbConn.getCheckOutSerialNumber();
                     
-                    // write the document to the database
+                    // write the document to the database and disk
                     try {
                         String accNumber = docid[0];
                         logMetacat.debug("" + doAction + " "
@@ -2108,9 +2143,32 @@
 
                         newdocid = documentWrapper.write(dbConn, xml, pub, dtd,
                                 doAction, accNumber, user, groups);
+                        
+                        // write the document to disk
+						FileWriter fileWriter = null;
+						try {
+							String documentDir = 
+								PropertyService.getProperty("application.documentfilepath");
+							String documentPath = documentDir + FileUtil.getFS() + accNumber;
+
+							if (accNumber != null
+									&& FileUtil.getFileStatus(documentPath) == FileUtil.DOES_NOT_EXIST) {
+								fileWriter = new FileWriter(documentPath);
+								fileWriter.write(doctext[0]);
+							}
+						} catch (IOException ioe) {
+							logMetacat.error("Did not write " + accNumber
+									+ " to file system: " + ioe.getMessage());
+						} finally {
+							if (fileWriter != null) {
+								fileWriter.close();
+							}
+						}
+                                
                         EventLog.getInstance().log(request.getRemoteAddr(),
                                 user, accNumber, action[0]);
                     } catch (NullPointerException npe) {
+                      // MCD this seems a bit dubious, since many things can throw npe
                       System.out.println("writing with null acnumber");
                         newdocid = documentWrapper.write(dbConn, xml, pub, dtd,
                                 doAction, null, user, groups);
Index: docs/user/properties.html
===================================================================
--- docs/user/properties.html	(revision 4333)
+++ docs/user/properties.html	(revision 4334)
@@ -283,7 +283,30 @@
   <div class="property-description-line">
     <h2 class="property-inline"> Example: </h2> 
     <p class="property-text">/var/metacat/inline-data</p>
-  </div>     
+  </div>  
+  
+  <a name="application.documentfilepath"></a><h1 class="property-main"> application.documentfilepath </h1>
+  <div class="property-description-line">
+    <h2 class="property-inline"> Default Value: </h2> 
+    <p class="property-text">/var/metacat/documents</p>
+  </div>
+  <div class="property-description-line">
+    <h2 class="property-inline"> Set Method: </h2> 
+    <p class="property-text">Configuration Utility</p>
+  </div>
+  <div class="property-description-line">
+    <h2 class="property-inline"> Description: </h2> 
+    <p class="property-text">This is the directory where metadata document files will 
+      be stored.  This should be some directory outside the Metacat installation 
+      directories so document files will not get lost when Metacat is upgraded.  For 
+      clarity of organization, this should probably not be the same as application.datafilepath
+      or application.inlinedatafilepath. The data file directory must be writable by the 
+      user that starts Tomcat (and thus Metacat).</p>
+  </div>
+  <div class="property-description-line">
+    <h2 class="property-inline"> Example: </h2> 
+    <p class="property-text">/var/metacat/documents</p>
+  </div>        
    
   <a name="application.cgiDir"></a><h1 class="property-main"> application.cgiDir </h1>
   <div class="property-description-line">
