001: /*
002: * <copyright>
003: *
004: * Copyright 1997-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026:
027: package org.cougaar.core.persist;
028:
029: import java.io.File;
030: import java.io.FileInputStream;
031: import java.io.FileNotFoundException;
032: import java.io.FileOutputStream;
033: import java.io.IOException;
034: import java.io.InputStream;
035: import java.io.OutputStream;
036:
037: /**
038: * This {@link PersistencePlugin} saves and restores blackboard
039: * objects in files. There is one optional parameter naming the persistence
040: * root directory. If the parameter is omitted, the persistence root
041: * is specified by system properties.
042: *
043: * @property org.cougaar.install.path
044: * Used by FilePersistence as the
045: * parent directory for persistence snapshots when there is no
046: * directory specified in configuration parameters and
047: * org.cougaar.core.persistence.path is a relative pathname. This
048: * property is not used if the plugin is configured with a specific
049: * parameter specifying the location of the persistence root.
050: *
051: * @property org.cougaar.core.persistence.path
052: * Specifies the directory
053: * in which persistence snapshots should be saved. If this is a
054: * relative path, it the base will be the value or
055: * org.cougaar.install.path. This property is not used if the plugin
056: * is configured with a specific parameter specifying the location of
057: * the persistence root.
058: */
059: public class FilePersistence extends FilePersistenceBase implements
060: PersistencePlugin {
061: /**
062: * Wrap a FileOutputStream to prove safe close semantics. Explicitly
063: * sync the file descriptor on close() to insure the file has been
064: * completely written to the disk.
065: */
066: private static class SafeFileOutputStream extends OutputStream {
067: private FileOutputStream fileOutputStream;
068:
069: public SafeFileOutputStream(File file)
070: throws FileNotFoundException {
071: fileOutputStream = new FileOutputStream(file);
072: }
073:
074: public void write(int b) throws IOException {
075: fileOutputStream.write(b);
076: }
077:
078: public void write(byte[] b) throws IOException {
079: fileOutputStream.write(b, 0, b.length);
080: }
081:
082: public void write(byte[] b, int offset, int nbytes)
083: throws IOException {
084: fileOutputStream.write(b, offset, nbytes);
085: }
086:
087: public void flush() throws IOException {
088: fileOutputStream.flush();
089: }
090:
091: public void close() throws IOException {
092: fileOutputStream.flush();
093: fileOutputStream.getFD().sync();
094: fileOutputStream.close();
095: }
096: }
097:
098: protected OutputStream openFileOutputStream(File file)
099: throws FileNotFoundException {
100: return new SafeFileOutputStream(file);
101: }
102:
103: protected InputStream openFileInputStream(File file)
104: throws FileNotFoundException {
105: return new FileInputStream(file);
106: }
107:
108: protected boolean rename(File from, File to) {
109: return from.renameTo(to);
110: }
111: }
|