KickJava   Java API By Example, From Geeks To Geeks.

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


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.vfs.Path;
33 import com.caucho.vfs.ReadStream;
34 import com.caucho.vfs.TempStream;
35 import com.caucho.vfs.Vfs;
36
37 import javax.annotation.PostConstruct;
38 import java.util.logging.Level JavaDoc;
39
40 /**
41  * Class storing distributed objects based on the filesystem.
42  */

43 public class FileStore extends StoreManager {
44   private final FileBacking _backing = new FileBacking();
45
46   /**
47    * Create a new file-based persistent store.
48    */

49   public FileStore()
50   {
51   }
52
53   /**
54    * Sets the file store's path.
55    */

56   public void setPath(Path path)
57   {
58     _backing.setPath(path);
59   }
60
61   public void addText(String JavaDoc value)
62   {
63     _backing.setPath(Vfs.lookup(value.trim()));
64   }
65
66   public Path getPath()
67   {
68     return _backing.getPath();
69   }
70
71   /**
72    * Initialize.
73    */

74   @PostConstruct
75   public boolean init()
76     throws Exception JavaDoc
77   {
78     if (! super.init())
79       return false;
80     
81     String JavaDoc serverId = Cluster.getServerId();
82     
83     String JavaDoc tableName = _backing.serverNameToTableName(serverId);
84     
85     _backing.setTableName(tableName);
86
87     _backing.init(1);
88
89     return true;
90   }
91
92   /**
93    * Start
94    */

95   public boolean start()
96     throws Exception JavaDoc
97   {
98     if (! super.start())
99       return false;
100     
101     _backing.start();
102
103     return true;
104   }
105
106   /**
107    * Clears the files which are too old.
108    */

109   public void clearOldObjects()
110   {
111     try {
112       _backing.clearOldObjects(getMaxIdleTime());
113     } catch (Exception JavaDoc e) {
114       log.log(Level.WARNING, e.toString(), e);
115     }
116   }
117
118   /**
119    * Returns true if this server is a primary for the given object id.
120    */

121   @Override JavaDoc
122   protected boolean isPrimary(String JavaDoc id)
123   {
124     return true;
125   }
126   
127   /**
128    * Creates the cluster object.
129    */

130   ClusterObject create(Store store, String JavaDoc id)
131   {
132     return new ClusterObject(this, store, id);
133   }
134
135   /**
136    * Loads the session from the filesystem.
137    *
138    * @param clusterObj the object to fill
139    */

140   public boolean load(ClusterObject clusterObj, Object JavaDoc obj)
141     throws Exception JavaDoc
142   {
143     return _backing.loadSelf(clusterObj, obj);
144   }
145
146   /**
147    * Saves the session to the filesystem.
148    *
149    * @param obj the object to save
150    * @param tempStream stream to the serialized object
151    * @param crc digest of the serialized stream
152    * @param updateCount how many times the object has been updated
153    */

154   public void store(ClusterObject obj,
155             TempStream tempStream,
156             long crc)
157     throws Exception JavaDoc
158   {
159     if (crc == 0)
160       return;
161
162     int length = tempStream.getLength();
163     ReadStream is = tempStream.openRead(true);
164     try {
165       _backing.storeSelf(obj.getUniqueId(), is, length,
166              obj.getExpireInterval(), 0, 0, 0);
167
168       if (log.isLoggable(Level.FINE))
169         log.fine("file store: " + obj.getUniqueId() + " length=" +
170                  length);
171     } finally {
172       is.close();
173     }
174   }
175   
176   /**
177    * Updates the object's access time in the persistent store.
178    *
179    * @param uniqueId the identifier of the object.
180    */

181   public void accessImpl(String JavaDoc uniqueId)
182     throws Exception JavaDoc
183   {
184     _backing.updateAccess(uniqueId);
185   }
186   
187   /**
188    * Sets the timef for the expires interval.
189    *
190    * @param uniqueId the identifier of the object.
191    * @param long the time in ms for the expire
192    */

193   public void setExpireInterval(String JavaDoc uniqueId, long expires)
194     throws Exception JavaDoc
195   {
196     _backing.setExpireInterval(uniqueId, expires);
197   }
198
199   /**
200    * When the session is no longer valid, remove it from the backing store.
201    */

202   public void remove(ClusterObject obj)
203     throws Exception JavaDoc
204   {
205     removeClusterObject(obj.getStoreId(), obj.getObjectId());
206
207     _backing.remove(obj.getUniqueId());
208   }
209
210   public String JavaDoc toString()
211   {
212     return "FileStore[]";
213   }
214 }
215
Popular Tags