001: /*
002: * Copyright 2004-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.compass.gps.device.jdbc.snapshot;
018:
019: import java.io.File;
020: import java.io.FileInputStream;
021: import java.io.FileOutputStream;
022: import java.io.IOException;
023: import java.io.ObjectInputStream;
024: import java.io.ObjectOutputStream;
025:
026: import org.compass.gps.device.jdbc.JdbcGpsDeviceException;
027: import org.apache.commons.logging.Log;
028: import org.apache.commons.logging.LogFactory;
029:
030: /**
031: * A FS (File System) based snapshot persister. The persister will store and
032: * load the snapshot to the file system, using the given file path (using
033: * {@link #setPath(String)}). The data will be saved using Java
034: * <code>ObjectOutputStream</code>, and loaded using
035: * <code>ObjectInputStream</code>.
036: * <p>
037: * Note that the path set is the path to the actual file that will be created.
038: *
039: * @author kimchy
040: */
041: public class FSJdbcSnapshotPersister implements JdbcSnapshotPersister {
042:
043: private static final Log log = LogFactory
044: .getLog(FSJdbcSnapshotPersister.class);
045:
046: private String path;
047:
048: public FSJdbcSnapshotPersister() {
049:
050: }
051:
052: public FSJdbcSnapshotPersister(String path) {
053: this .path = path;
054: }
055:
056: public JdbcSnapshot load() throws JdbcGpsDeviceException {
057: File file = new File(path);
058: if (!file.exists()) {
059: if (log.isDebugEnabled()) {
060: log.debug("No snapshot data found at [" + path
061: + "], creating a new one");
062: }
063: return new JdbcSnapshot();
064: }
065: try {
066: if (log.isDebugEnabled()) {
067: log.debug("Snapshot data found at [" + path
068: + "], loading [" + file.length() + "bytes]");
069: }
070: ObjectInputStream objStream = new ObjectInputStream(
071: new FileInputStream(path));
072: return (JdbcSnapshot) objStream.readObject();
073: } catch (Exception e) {
074: throw new JdbcGpsDeviceException(
075: "Failed to load jdbc snapshot", e);
076: }
077: }
078:
079: public void save(JdbcSnapshot snapshot)
080: throws JdbcGpsDeviceException {
081: try {
082: ObjectOutputStream objStream = new ObjectOutputStream(
083: new FileOutputStream(path));
084: objStream.writeObject(snapshot);
085: objStream.flush();
086: objStream.close();
087: if (log.isDebugEnabled()) {
088: File file = new File(path);
089: log.debug("Saved snapshot data to [" + path
090: + "] size [" + file.length() + "bytes]");
091: }
092: } catch (IOException e) {
093: throw new JdbcGpsDeviceException(
094: "Failed to save jdbc snapshot", e);
095: }
096: }
097:
098: public String getPath() {
099: return path;
100: }
101:
102: public void setPath(String path) {
103: this.path = path;
104: }
105: }
|