KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > server > cluster > Store


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.server.cluster;
31
32 import com.caucho.log.Log;
33 import com.caucho.util.L10N;
34
35 import java.util.logging.Logger JavaDoc;
36
37 /**
38  * Application view of the store.
39  */

40 public class Store {
41   static protected final Logger JavaDoc log = Log.open(Store.class);
42   static final L10N L = new L10N(Store.class);
43
44   private StoreManager _storeManager;
45   private ObjectManager _objectManager;
46   private String JavaDoc _storeId;
47   private long _maxIdleTime;
48
49   private boolean _isAlwaysLoad;
50   private boolean _isAlwaysSave;
51
52   /**
53    * Creates the new application view of the store.
54    *
55    * @param storeId the application identifiers
56    * @param objectManager the application's object manager, e.g. the SessionManager
57    * @param storeManager the persistent store manater
58    */

59   Store(String JavaDoc storeId, StoreManager storeManager)
60   {
61     _storeId = mangleId(storeId);
62     _storeManager = storeManager;
63
64     _maxIdleTime = storeManager.getMaxIdleTime();
65
66     _isAlwaysLoad = _storeManager.isAlwaysLoad();
67     _isAlwaysSave = _storeManager.isAlwaysSave();
68   }
69
70   /**
71    * Gets the store identifier.
72    */

73   public String JavaDoc getId()
74   {
75     return _storeId;
76   }
77
78   /**
79    * Returns the max idle time.
80    */

81   public long getMaxIdleTime()
82   {
83     return _maxIdleTime;
84   }
85
86   /**
87    * Sets the max idle time.
88    */

89   public void setMaxIdleTime(long maxIdleTime)
90   {
91     _maxIdleTime = maxIdleTime;
92
93     _storeManager.updateIdleCheckInterval(maxIdleTime);
94   }
95
96   /**
97    * Returns the length of time an idle object can remain in the store before
98    * being cleaned.
99    */

100   public long getAccessWindowTime()
101   {
102     long window = _maxIdleTime / 4;
103
104     if (window < 60000L)
105       return 60000L;
106     else
107       return window;
108   }
109
110   /**
111    * Returns true if the object should always be loaded.
112    */

113   public boolean isAlwaysLoad()
114   {
115     return _isAlwaysLoad;
116   }
117
118   /**
119    * Set true if the object should always be loaded.
120    */

121   public void setAlwaysLoad(boolean isAlwaysLoad)
122   {
123     _isAlwaysLoad = isAlwaysLoad;
124   }
125
126   /**
127    * Set true if the object should always be saved.
128    */

129   public void setAlwaysSave(boolean isAlwaysSave)
130   {
131     _isAlwaysSave = isAlwaysSave;
132   }
133
134   /**
135    * Returns true if the object should always be saved.
136    */

137   public boolean isAlwaysSave()
138   {
139     return _isAlwaysSave;
140   }
141
142   /**
143    * Returns the object manager.
144    */

145   public ObjectManager getObjectManager()
146   {
147     return _objectManager;
148   }
149
150   /**
151    * Sets the object manager.
152    */

153   public void setObjectManager(ObjectManager obj)
154   {
155     _objectManager = obj;
156   }
157
158   /**
159    * Returns the store manager.
160    */

161   public StoreManager getStoreManager()
162   {
163     return _storeManager;
164   }
165
166   /**
167    * Returns a ClusterObject.
168    */

169   public ClusterObject createClusterObject(String JavaDoc objectId)
170   {
171     return _storeManager.createClusterObject(this, objectId);
172   }
173   
174   /**
175    * Updates the object's access time.
176    *
177    * @param obj the object to update.
178    */

179   public void access(String JavaDoc objectId)
180     throws Exception JavaDoc
181   {
182     _storeManager.access(this, objectId);
183   }
184
185   /**
186    * When the object is no longer valid, remove it from the backing store.
187    *
188    * @param key the object's id
189    */

190   public void remove(String JavaDoc objectId)
191     throws Exception JavaDoc
192   {
193     _storeManager.remove(this, objectId);
194   }
195
196   /**
197    * When the object is no longer valid, remove it from the backing store.
198    *
199    * @param key the object's id
200    */

201   void notifyRemove(String JavaDoc objectId)
202     throws Exception JavaDoc
203   {
204     if (_objectManager != null)
205       _objectManager.notifyRemove(objectId);
206   }
207
208   /**
209    * Returns the mangled id.
210    */

211   static private String JavaDoc mangleId(String JavaDoc id)
212   {
213     StringBuilder JavaDoc cb = new StringBuilder JavaDoc();
214
215     for (int i = 0; i < id.length(); i++) {
216       char ch = id.charAt(i);
217
218       if (ch == '/')
219     cb.append("__");
220       else if (ch == ':')
221     cb.append("_0");
222       else if (ch == '_')
223     cb.append("_1");
224       else
225     cb.append(ch);
226     }
227
228     return cb.toString();
229   }
230 }
231
Popular Tags