KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > fjank > jcache > DiskCacheObject


1 /* Open Source Java Caching Service
2  * Copyright (C) 2002 Frank Karlstrøm
3  * This library is free software; you can redistribute it and/or
4  * modify it under the terms of the GNU Lesser General Public
5  * License as published by the Free Software Foundation; either
6  * version 2.1 of the License, or (at your option) any later version.
7  *
8  * This library is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  * Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public
14  * License along with this library; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * The author can be contacted by email: fjankk@users.sourceforge.net
18  */

19 package org.fjank.jcache;
20
21 import java.io.IOException JavaDoc;
22 import java.io.Serializable JavaDoc;
23 import javax.util.jcache.CacheException;
24 import javax.util.jcache.CacheNotAvailableException;
25 import javax.util.jcache.DiskCacheException;
26
27 /**
28  * A special version of an CacheObject which is used to send and retrieve
29  * data to and from the diskcache.
30  * @author frank karlstrøm
31  *
32  */

33 public class DiskCacheObject implements Serializable JavaDoc {
34     private Object JavaDoc key;
35     private String JavaDoc group = "";
36     private String JavaDoc region = "";
37     private AttributesImpl attr;
38     private Object JavaDoc referent;
39
40     public DiskCacheObject(final CacheObject object) {
41         this.key = object.getKey();
42         this.setGroup(object.getGroup().getName());
43         this.setRegion(object.getRegion().getName());
44         this.attr = object.getAttributes();
45         this.referent = object.get();
46     }
47
48     private void writeObject(java.io.ObjectOutputStream JavaDoc out) throws IOException JavaDoc {
49         out.writeObject(key);
50         out.writeUTF(getGroup());
51         out.writeUTF(getRegion());
52         out.writeObject(attr);
53         out.writeObject(attr);
54         out.writeObject(referent);
55     }
56
57     private void readObject(java.io.ObjectInputStream JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
58         this.key=in.readObject();
59         this.setGroup(in.readUTF());
60         this.setRegion(in.readUTF());
61         this.attr=(AttributesImpl) in.readObject();
62         this.referent=in.readObject();
63     }
64
65     public CacheObject getCacheObject() throws DiskCacheException {
66         try {
67             CacheImpl cache = (CacheImpl) CacheAccessFactoryImpl.getInstance().getCache();
68             CacheRegion reg;
69             CacheGroup gr = null;
70             if (getRegion().equals("")) {
71                 reg = cache.getRegion();
72             } else {
73                 reg = cache.getRegion(getRegion());
74             }
75             if (!getGroup().equals("")) {
76                 gr = reg.getGroup(getGroup());
77                 
78             }
79             CacheObject cacheObject = new CacheObject(key, referent, gr, reg, cache.getReferenceQueue());
80             cacheObject.setAttributes(attr);
81             return cacheObject;
82         } catch (CacheNotAvailableException e) {
83             throw new DiskCacheException(e);
84         } catch (CacheException e) {
85             throw new DiskCacheException(e);
86         }
87     }
88
89     private void setGroup(String JavaDoc group) {
90         if(group!=null) {
91             this.group = group;
92         }
93     }
94
95     private String JavaDoc getGroup() {
96         return group;
97     }
98
99     private void setRegion(String JavaDoc region) {
100         if(region!=null) {
101             this.region = region;
102         }
103     }
104
105     private String JavaDoc getRegion() {
106         return region;
107     }
108 }
Popular Tags