Project

General

Profile

« Previous | Next » 

Revision 4334

Added by daigle about 16 years ago

Add functionality to write and read metadata documents to and from the file system.

View differences:

lib/metacat.properties.metadata.xml
242 242
	</config>
243 243
	
244 244
	<config>
245
		<key>application.documentfilepath</key>
246
		<label>Document File Path</label>
247
		<group>3</group>
248
		<index>8</index>
249
		<description>
250
			Where to store metadata documents. This should be some location
251
			outside the knb install direcories.
252
		</description>
253
		<helpFile>properties.html#application.documentfilepath</helpFile>
254
	</config>
255
	
256
	<config>
245 257
		<key>replication.logdir</key>
246 258
		<label>Replication Log Directory</label>
247 259
		<group>3</group>
248
		<index>8</index>
260
		<index>9</index>
249 261
		<description>
250 262
			The directory where replication log should be located.
251 263
		</description>
lib/metacat.properties
32 32
application.knbSiteURL=http://knb.ecoinformatics.org
33 33
application.datafilepath=/var/metacat/data
34 34
application.inlinedatafilepath=/var/metacat/inline-data
35
application.documentfilepath=/var/metacat/documents
35 36
application.tempDir=/var/metacat/temporary
36 37
# the location of cgi scripts relative to the metacat context directory
37 38
application.cgiDir=/cgi-bin
src/edu/ucsb/nceas/metacat/MetaCatServlet.java
27 27
package edu.ucsb.nceas.metacat;
28 28

  
29 29
import java.io.BufferedInputStream;
30
import java.io.BufferedReader;
30 31
import java.io.File;
31 32
import java.io.FileInputStream;
32 33
import java.io.FileReader;
34
import java.io.FileWriter;
33 35
import java.io.IOException;
34 36
import java.io.OutputStreamWriter;
35 37
import java.io.PrintWriter;
......
72 74
import com.oreilly.servlet.multipart.Part;
73 75

  
74 76
import edu.ucsb.nceas.metacat.dataquery.DataQuery;
77
import edu.ucsb.nceas.metacat.service.DatabaseService;
75 78
import edu.ucsb.nceas.metacat.service.PropertyService;
76 79
import edu.ucsb.nceas.metacat.service.ServiceException;
77 80
import edu.ucsb.nceas.metacat.service.SessionService;
......
214 217
		Logger logMetacat = Logger.getLogger(MetaCatServlet.class);
215 218

  
216 219
		try {
220
            DatabaseService databaseService = DatabaseService.getInstance();
221
            logMetacat.debug("DatabaseService singleton created " + databaseService);
222
			
217 223
			// initialize DBConnection pool
218 224
			DBConnectionPool connPool = DBConnectionPool.getInstance();
219 225
			logMetacat.debug("DBConnection pool initialized: " + connPool.toString());
......
1664 1670
            ZipOutputStream zout, boolean withInlineData, Hashtable params)
1665 1671
            throws ClassNotFoundException, IOException, SQLException,
1666 1672
            McdbException, Exception {
1673
    	String documentDir = PropertyService.getProperty("application.documentfilepath");
1674
    	
1667 1675
        Logger logMetacat = Logger.getLogger(MetaCatServlet.class);
1668 1676
        try {
1669 1677
            
......
1744 1752
                    response.setContentType("text/xml"); //MIME type
1745 1753
                    response.setHeader("Content-Disposition",
1746 1754
                            "attachment; filename=" + docid + ".xml");
1755
                    
1756
                    // Try to get the metadata file from disk. If it isn't
1757
					// found, create it from the
1758
					// db and write it to disk then.
1759
					String documentPath = documentDir + FileUtil.getFS() + docid;
1760
					try {
1761
						if (FileUtil.getFileStatus(documentPath) == FileUtil.DOES_NOT_EXIST) {
1762
							FileWriter fileWriter = new FileWriter(documentPath);
1763
							doc.toXml(fileWriter, user, groups, withInlineData);
1764
						}
1765
						FileReader fileReader = new FileReader(documentPath);
1766
						BufferedReader br = new BufferedReader(fileReader);
1767
						PrintWriter pw = new PrintWriter(streamOut);
1768
						String nextLine;
1769
						while ((nextLine = br.readLine()) != null) {
1770
							pw.println(nextLine);
1771
						}
1772
						pw.flush();					
1773
					} catch (Exception e) {
1774
						// any exceptions in reading the xml from disc, and we go back to the
1775
						// old way of creating the xml directly.
1776
						logMetacat.error("could not read from document file " + documentPath 
1777
								+ ": " + e.getMessage());
1778
						PrintWriter out = new PrintWriter(streamOut);
1779
						doc.toXml(out, user, groups, withInlineData);
1780
					}
1781
                    	
1747 1782
                    PrintWriter out = new PrintWriter(streamOut);
1748 1783
                    doc.toXml(out, user, groups, withInlineData);
1749 1784
                } else {
......
2097 2132
                            .getDBConnection("MetaCatServlet.handleInsertOrUpdateAction");
2098 2133
                    serialNumber = dbConn.getCheckOutSerialNumber();
2099 2134
                    
2100
                    // write the document to the database
2135
                    // write the document to the database and disk
2101 2136
                    try {
2102 2137
                        String accNumber = docid[0];
2103 2138
                        logMetacat.debug("" + doAction + " "
......
2108 2143

  
2109 2144
                        newdocid = documentWrapper.write(dbConn, xml, pub, dtd,
2110 2145
                                doAction, accNumber, user, groups);
2146
                        
2147
                        // write the document to disk
2148
						FileWriter fileWriter = null;
2149
						try {
2150
							String documentDir = 
2151
								PropertyService.getProperty("application.documentfilepath");
2152
							String documentPath = documentDir + FileUtil.getFS() + accNumber;
2153

  
2154
							if (accNumber != null
2155
									&& FileUtil.getFileStatus(documentPath) == FileUtil.DOES_NOT_EXIST) {
2156
								fileWriter = new FileWriter(documentPath);
2157
								fileWriter.write(doctext[0]);
2158
							}
2159
						} catch (IOException ioe) {
2160
							logMetacat.error("Did not write " + accNumber
2161
									+ " to file system: " + ioe.getMessage());
2162
						} finally {
2163
							if (fileWriter != null) {
2164
								fileWriter.close();
2165
							}
2166
						}
2167
                                
2111 2168
                        EventLog.getInstance().log(request.getRemoteAddr(),
2112 2169
                                user, accNumber, action[0]);
2113 2170
                    } catch (NullPointerException npe) {
2171
                      // MCD this seems a bit dubious, since many things can throw npe
2114 2172
                      System.out.println("writing with null acnumber");
2115 2173
                        newdocid = documentWrapper.write(dbConn, xml, pub, dtd,
2116 2174
                                doAction, null, user, groups);
docs/user/properties.html
283 283
  <div class="property-description-line">
284 284
    <h2 class="property-inline"> Example: </h2> 
285 285
    <p class="property-text">/var/metacat/inline-data</p>
286
  </div>     
286
  </div>  
287
  
288
  <a name="application.documentfilepath"></a><h1 class="property-main"> application.documentfilepath </h1>
289
  <div class="property-description-line">
290
    <h2 class="property-inline"> Default Value: </h2> 
291
    <p class="property-text">/var/metacat/documents</p>
292
  </div>
293
  <div class="property-description-line">
294
    <h2 class="property-inline"> Set Method: </h2> 
295
    <p class="property-text">Configuration Utility</p>
296
  </div>
297
  <div class="property-description-line">
298
    <h2 class="property-inline"> Description: </h2> 
299
    <p class="property-text">This is the directory where metadata document files will 
300
      be stored.  This should be some directory outside the Metacat installation 
301
      directories so document files will not get lost when Metacat is upgraded.  For 
302
      clarity of organization, this should probably not be the same as application.datafilepath
303
      or application.inlinedatafilepath. The data file directory must be writable by the 
304
      user that starts Tomcat (and thus Metacat).</p>
305
  </div>
306
  <div class="property-description-line">
307
    <h2 class="property-inline"> Example: </h2> 
308
    <p class="property-text">/var/metacat/documents</p>
309
  </div>        
287 310
   
288 311
  <a name="application.cgiDir"></a><h1 class="property-main"> application.cgiDir </h1>
289 312
  <div class="property-description-line">

Also available in: Unified diff