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.hibernate.scrollable.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.apache.commons.logging.Log;
027: import org.apache.commons.logging.LogFactory;
028: import org.compass.gps.device.hibernate.HibernateGpsDeviceException;
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 FSHibernateSnapshotPersister implements
042: HibernateSnapshotPersister {
043:
044: private static final Log log = LogFactory
045: .getLog(FSHibernateSnapshotPersister.class);
046:
047: private String path;
048:
049: public FSHibernateSnapshotPersister() {
050:
051: }
052:
053: public FSHibernateSnapshotPersister(String path) {
054: this .path = path;
055: }
056:
057: public HibernateSnapshot load() throws HibernateGpsDeviceException {
058: File file = new File(path);
059: if (!file.exists()) {
060: if (log.isDebugEnabled()) {
061: log.debug("No snapshot data found at [" + path
062: + "], creating a new one");
063: }
064: return new HibernateSnapshot();
065: }
066: try {
067: if (log.isDebugEnabled()) {
068: log.debug("Snapshot data found at [" + path
069: + "], loading [" + file.length() + "bytes]");
070: }
071: ObjectInputStream objStream = new ObjectInputStream(
072: new FileInputStream(path));
073: return (HibernateSnapshot) objStream.readObject();
074: } catch (Exception e) {
075: throw new HibernateGpsDeviceException(
076: "Failed to load hibernate snapshot", e);
077: }
078: }
079:
080: public void save(HibernateSnapshot snapshot)
081: throws HibernateGpsDeviceException {
082: try {
083: ObjectOutputStream objStream = new ObjectOutputStream(
084: new FileOutputStream(path));
085: objStream.writeObject(snapshot);
086: objStream.flush();
087: objStream.close();
088: if (log.isDebugEnabled()) {
089: File file = new File(path);
090: log.debug("Saved snapshot data to [" + path
091: + "] size [" + file.length() + "bytes]");
092: }
093: } catch (IOException e) {
094: throw new HibernateGpsDeviceException(
095: "Failed to save hibernate snapshot", e);
096: }
097: }
098:
099: public String getPath() {
100: return path;
101: }
102:
103: public void setPath(String path) {
104: this.path = path;
105: }
106: }
|