KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.server.cluster;
30
31 import com.caucho.log.Log;
32 import com.caucho.vfs.Path;
33 import com.caucho.vfs.ReadStream;
34 import com.caucho.vfs.WriteStream;
35
36 import java.io.IOException JavaDoc;
37 import java.util.logging.Level JavaDoc;
38 import java.util.logging.Logger JavaDoc;
39
40 /**
41  * The ObjectBacking encapsulates the file persistent store
42  * for a distributed object.
43  */

44 public class ObjectBacking {
45   static protected final Logger JavaDoc log = Log.open(ObjectBacking.class);
46
47   // The backing path
48
private Path _objectPath;
49
50   // The owning persistent store
51
private Store _store;
52   
53   // The object id
54
private String JavaDoc _id;
55   
56   // The corresponding object (when live)
57
private DistributedObject _object;
58
59   /**
60    * Initialize the object backing.
61    *
62    * @param id the object identifier
63    * @param objectPath the backing object path.
64    * @param store the owning distributed store
65    */

66   public ObjectBacking(String JavaDoc id, Path objectPath, Store store)
67   {
68     _id = id;
69     _store = store;
70     _objectPath = objectPath;
71   }
72
73   /**
74    * Returns the mangled id.
75    */

76   public String JavaDoc getId()
77   {
78     return _id;
79   }
80
81   /**
82    * Returns the backing path.
83    */

84   public Path getPath()
85   {
86     return _objectPath;
87   }
88
89   /**
90    * Returns any associated object.
91    */

92   public DistributedObject getObject()
93   {
94     return _object;
95   }
96
97   /**
98    * Sets an associated object.
99    */

100   public void setObject(DistributedObject object)
101   {
102     _object = object;
103   }
104
105   /**
106    * Opens the backing for reading.
107    */

108   public ReadStream openRead()
109     throws IOException JavaDoc
110   {
111     return _objectPath.openRead();
112   }
113
114   /**
115    * Opens the backing for writing.
116    */

117   public WriteStream openWrite()
118     throws IOException JavaDoc
119   {
120     return _objectPath.openWrite();
121   }
122
123   /**
124    * Saves a distributed object in the backing file.
125    */

126   synchronized void save(DistributedObject object)
127   {
128     WriteStream os = null;
129     try {
130       os = _objectPath.openWrite();
131
132       // _store.store(object, os);
133

134       _object = object;
135     } catch (Exception JavaDoc e) {
136       log.log(Level.FINE, e.toString(), e);
137     } finally {
138       try {
139         if (os != null)
140           os.close();
141       } catch (IOException JavaDoc e1) {
142       }
143     }
144   }
145
146   /**
147    * Removes the object from the backing.
148    */

149   synchronized public void remove()
150   {
151     try {
152       _objectPath.remove();
153     } catch (IOException JavaDoc e) {
154       log.log(Level.FINE, e.toString(), e);
155     }
156   }
157
158   /**
159    * Returns true if this backing allows logging.
160    */

161   public boolean canLog()
162   {
163     return log.isLoggable(Level.FINE);
164   }
165
166   /**
167    * Log data to the backing's log file.
168    */

169   public void log(String JavaDoc value)
170   {
171     log.fine(value);
172   }
173
174   public String JavaDoc toString()
175   {
176     return "DistBacking[" + _objectPath + "]";
177   }
178 }
179
Popular Tags