001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.server.cluster;
031:
032: import com.caucho.vfs.Path;
033: import com.caucho.vfs.ReadStream;
034: import com.caucho.vfs.TempStream;
035: import com.caucho.vfs.Vfs;
036:
037: import javax.annotation.PostConstruct;
038: import java.util.logging.Level;
039:
040: /**
041: * Class storing distributed objects based on the filesystem.
042: */
043: public class FileStore extends StoreManager {
044: private final FileBacking _backing = new FileBacking();
045:
046: /**
047: * Create a new file-based persistent store.
048: */
049: public FileStore() {
050: }
051:
052: /**
053: * Sets the file store's path.
054: */
055: public void setPath(Path path) {
056: _backing.setPath(path);
057: }
058:
059: public void addText(String value) {
060: _backing.setPath(Vfs.lookup(value.trim()));
061: }
062:
063: public Path getPath() {
064: return _backing.getPath();
065: }
066:
067: /**
068: * Initialize.
069: */
070: @PostConstruct
071: @Override
072: public boolean init() throws Exception {
073: if (!super .init())
074: return false;
075:
076: String serverId = Cluster.getServerId();
077:
078: String tableName = _backing.serverNameToTableName(serverId);
079:
080: _backing.setTableName(tableName);
081:
082: _backing.init(1);
083:
084: return true;
085: }
086:
087: /**
088: * Start
089: */
090: @Override
091: public boolean start() throws Exception {
092: if (!super .start())
093: return false;
094:
095: _backing.start();
096:
097: return true;
098: }
099:
100: /**
101: * Clears the files which are too old.
102: */
103: @Override
104: public void clearOldObjects() {
105: try {
106: _backing.clearOldObjects(getMaxIdleTime());
107: } catch (Exception e) {
108: log.log(Level.WARNING, e.toString(), e);
109: }
110: }
111:
112: /**
113: * Returns true if this server is a primary for the given object id.
114: */
115: @Override
116: protected boolean isPrimary(String id) {
117: return true;
118: }
119:
120: /**
121: * Creates the cluster object.
122: */
123: @Override
124: ClusterObject create(Store store, String id) {
125: return new ClusterObject(this , store, id);
126: }
127:
128: /**
129: * Loads the session from the filesystem.
130: *
131: * @param clusterObj the object to fill
132: */
133: public boolean load(ClusterObject clusterObj, Object obj)
134: throws Exception {
135: return _backing.loadSelf(clusterObj, obj);
136: }
137:
138: /**
139: * Saves the session to the filesystem.
140: *
141: * @param obj the object to save
142: * @param tempStream stream to the serialized object
143: * @param crc digest of the serialized stream
144: * @param updateCount how many times the object has been updated
145: */
146: public void store(ClusterObject obj, TempStream tempStream, long crc)
147: throws Exception {
148: if (crc == 0)
149: return;
150:
151: int length = tempStream.getLength();
152: ReadStream is = tempStream.openReadAndSaveBuffer();
153: try {
154: _backing.storeSelf(obj.getUniqueId(), is, length, obj
155: .getExpireInterval(), 0, 0, 0);
156:
157: if (log.isLoggable(Level.FINE))
158: log.fine("file store: " + obj.getUniqueId()
159: + " length=" + length);
160: } finally {
161: is.close();
162: }
163: }
164:
165: /**
166: * Updates the object's access time in the persistent store.
167: *
168: * @param uniqueId the identifier of the object.
169: */
170: public void accessImpl(String uniqueId) throws Exception {
171: _backing.updateAccess(uniqueId);
172: }
173:
174: /**
175: * Sets the timef for the expires interval.
176: *
177: * @param uniqueId the identifier of the object.
178: * @param long the time in ms for the expire
179: */
180: @Override
181: public void setExpireInterval(String uniqueId, long expires)
182: throws Exception {
183: _backing.setExpireInterval(uniqueId, expires);
184: }
185:
186: /**
187: * When the session is no longer valid, remove it from the backing store.
188: */
189: @Override
190: public void remove(ClusterObject obj) throws Exception {
191: removeClusterObject(obj.getStoreId(), obj.getObjectId());
192:
193: _backing.remove(obj.getUniqueId());
194: }
195: }
|