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.log.Log;
033: import com.caucho.util.L10N;
034:
035: import java.util.logging.Logger;
036:
037: /**
038: * Application view of the store.
039: */
040: public class Store {
041: static protected final Logger log = Log.open(Store.class);
042: static final L10N L = new L10N(Store.class);
043:
044: private StoreManager _storeManager;
045: private ObjectManager _objectManager;
046: private String _storeId;
047: private long _maxIdleTime;
048:
049: private boolean _isAlwaysLoad;
050: private boolean _isAlwaysSave;
051:
052: /**
053: * Creates the new application view of the store.
054: *
055: * @param storeId the application identifiers
056: * @param objectManager the application's object manager, e.g. the SessionManager
057: * @param storeManager the persistent store manater
058: */
059: Store(String storeId, StoreManager storeManager) {
060: _storeId = mangleId(storeId);
061: _storeManager = storeManager;
062:
063: _maxIdleTime = storeManager.getMaxIdleTime();
064:
065: _isAlwaysLoad = _storeManager.isAlwaysLoad();
066: _isAlwaysSave = _storeManager.isAlwaysSave();
067: }
068:
069: /**
070: * Gets the store identifier.
071: */
072: public String getId() {
073: return _storeId;
074: }
075:
076: /**
077: * Returns the max idle time.
078: */
079: public long getMaxIdleTime() {
080: return _maxIdleTime;
081: }
082:
083: /**
084: * Sets the max idle time.
085: */
086: public void setMaxIdleTime(long maxIdleTime) {
087: _maxIdleTime = maxIdleTime;
088:
089: _storeManager.updateIdleCheckInterval(maxIdleTime);
090: }
091:
092: /**
093: * Returns the length of time an idle object can remain in the store before
094: * being cleaned.
095: */
096: public long getAccessWindowTime() {
097: long window = _maxIdleTime / 4;
098:
099: if (window < 60000L)
100: return 60000L;
101: else
102: return window;
103: }
104:
105: /**
106: * Returns true if the object should always be loaded.
107: */
108: public boolean isAlwaysLoad() {
109: return _isAlwaysLoad;
110: }
111:
112: /**
113: * Set true if the object should always be loaded.
114: */
115: public void setAlwaysLoad(boolean isAlwaysLoad) {
116: _isAlwaysLoad = isAlwaysLoad;
117: }
118:
119: /**
120: * Set true if the object should always be saved.
121: */
122: public void setAlwaysSave(boolean isAlwaysSave) {
123: _isAlwaysSave = isAlwaysSave;
124: }
125:
126: /**
127: * Returns true if the object should always be saved.
128: */
129: public boolean isAlwaysSave() {
130: return _isAlwaysSave;
131: }
132:
133: /**
134: * Returns the object manager.
135: */
136: public ObjectManager getObjectManager() {
137: return _objectManager;
138: }
139:
140: /**
141: * Sets the object manager.
142: */
143: public void setObjectManager(ObjectManager obj) {
144: _objectManager = obj;
145: }
146:
147: /**
148: * Returns the store manager.
149: */
150: public StoreManager getStoreManager() {
151: return _storeManager;
152: }
153:
154: /**
155: * Returns a ClusterObject.
156: */
157: public ClusterObject createClusterObject(String objectId) {
158: return _storeManager.createClusterObject(this , objectId);
159: }
160:
161: /**
162: * Updates the object's access time.
163: *
164: * @param obj the object to update.
165: */
166: public void access(String objectId) throws Exception {
167: _storeManager.access(this , objectId);
168: }
169:
170: /**
171: * When the object is no longer valid, remove it from the backing store.
172: *
173: * @param key the object's id
174: */
175: public void remove(String objectId) throws Exception {
176: _storeManager.remove(this , objectId);
177: }
178:
179: /**
180: * When the object is no longer valid, remove it from the backing store.
181: *
182: * @param key the object's id
183: */
184: void notifyRemove(String objectId) throws Exception {
185: if (_objectManager != null)
186: _objectManager.notifyRemove(objectId);
187: }
188:
189: /**
190: * Returns the mangled id.
191: */
192: static private String mangleId(String id) {
193: StringBuilder cb = new StringBuilder();
194:
195: for (int i = 0; i < id.length(); i++) {
196: char ch = id.charAt(i);
197:
198: if (ch == '/')
199: cb.append("__");
200: else if (ch == ':')
201: cb.append("_0");
202: else if (ch == '_')
203: cb.append("_1");
204: else
205: cb.append(ch);
206: }
207:
208: return cb.toString();
209: }
210: }
|