1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.fjank.jcache;
20
21 import java.io.IOException;
22 import java.io.Serializable;
23 import javax.util.jcache.CacheException;
24 import javax.util.jcache.CacheNotAvailableException;
25 import javax.util.jcache.DiskCacheException;
26
27
28
29
30
31
32
33 public class DiskCacheObject implements Serializable {
34 private Object key;
35 private String group = "";
36 private String region = "";
37 private AttributesImpl attr;
38 private Object 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 out) throws IOException {
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 in) throws IOException, ClassNotFoundException {
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 group) {
90 if(group!=null) {
91 this.group = group;
92 }
93 }
94
95 private String getGroup() {
96 return group;
97 }
98
99 private void setRegion(String region) {
100 if(region!=null) {
101 this.region = region;
102 }
103 }
104
105 private String getRegion() {
106 return region;
107 }
108 }