KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > core > impl > tree > loader > DelegatingCacheLoader


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  *****************************************/

9 package org.jboss.portal.core.impl.tree.loader;
10
11 import org.jboss.cache.loader.CacheLoader;
12 import org.jboss.cache.TreeCache;
13 import org.jboss.cache.Fqn;
14 import org.jboss.cache.Modification;
15 import org.apache.log4j.Logger;
16
17 import java.util.Properties JavaDoc;
18 import java.util.Set JavaDoc;
19 import java.util.Map JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.io.ByteArrayOutputStream JavaDoc;
25 import java.io.ObjectOutputStream JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.ByteArrayInputStream JavaDoc;
28 import java.io.ObjectInputStream JavaDoc;
29 import java.io.ObjectStreamException JavaDoc;
30
31 /**
32  * Delegate to another cache loader in order to change how serialization is done on the persistent storage.
33  *
34  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
35  * @version $Revision: 1.1 $
36  */

37 public class DelegatingCacheLoader implements CacheLoader
38 {
39
40    /** Logger. */
41    private Logger log = Logger.getLogger(DelegatingCacheLoader.class);
42
43    /** The cache loader it delegates to. */
44    private CacheLoader delegate;
45
46    public void setConfig(Properties JavaDoc props)
47    {
48       try
49       {
50          String JavaDoc className = props.getProperty("delegate.classname");
51
52          log.debug("Loading delegate class " + className);
53          Class JavaDoc clazz = Thread.currentThread().getContextClassLoader().loadClass(className);
54
55          log.debug("Instantiating delegate");
56          delegate = (CacheLoader)clazz.newInstance();
57
58          log.debug("Adapting delegate properties");
59          Properties JavaDoc delegateProps = new Properties JavaDoc();
60          for (Iterator JavaDoc i = props.entrySet().iterator(); i.hasNext();)
61          {
62             Map.Entry JavaDoc entry = (Map.Entry JavaDoc)i.next();
63             String JavaDoc propName = (String JavaDoc)entry.getKey();
64             if (propName.startsWith("delegate.prop."))
65             {
66                propName = propName.substring("delegate.prop.".length());
67                String JavaDoc propValue = (String JavaDoc)entry.getValue();
68                log.debug("Adding delegate property " + propName + " = " + propValue);
69                delegateProps.put(propName, propValue);
70             }
71          }
72          delegate.setConfig(delegateProps);
73       }
74       catch (Exception JavaDoc e)
75       {
76          throw new Error JavaDoc("Cannot create delegate cache loader", e);
77       }
78    }
79
80    public void setCache(TreeCache cache)
81    {
82       delegate.setCache(cache);
83    }
84
85    public Set JavaDoc getChildrenNames(Fqn fqn) throws Exception JavaDoc
86    {
87       return delegate.getChildrenNames(fqn);
88    }
89
90    public Object JavaDoc get(Fqn fqn, Object JavaDoc key) throws Exception JavaDoc
91    {
92       byte[] bytes = (byte[])delegate.get(fqn, key);
93       if (bytes == null)
94       {
95          return null;
96       }
97       Object JavaDoc value = unmarshall(bytes);
98       return value;
99    }
100
101    public Map JavaDoc get(Fqn fqn) throws Exception JavaDoc
102    {
103       Map JavaDoc data = delegate.get(fqn);
104       if (data == null)
105       {
106          return null;
107       }
108       data = unmarshallMap(data);
109       return data;
110    }
111
112    public boolean exists(Fqn fqn) throws Exception JavaDoc
113    {
114       return delegate.exists(fqn);
115    }
116
117    public Object JavaDoc put(Fqn fqn, Object JavaDoc key, Object JavaDoc value) throws Exception JavaDoc
118    {
119       byte[] bytes = marshall(value);
120       bytes = (byte[])delegate.put(fqn, key, bytes);
121       if (bytes == null)
122       {
123          return null;
124       }
125       return unmarshall(bytes);
126    }
127
128    public void put(Fqn fqn, Map JavaDoc data) throws Exception JavaDoc
129    {
130       data = marshallMap(data);
131       delegate.put(fqn, data);
132    }
133
134    public void put(List JavaDoc modifications) throws Exception JavaDoc
135    {
136       modifications = marshallModifications(modifications);
137       delegate.put(modifications);
138    }
139
140    public Object JavaDoc remove(Fqn fqn, Object JavaDoc key) throws Exception JavaDoc
141    {
142       byte[] bytes = (byte[])delegate.remove(fqn, key);
143       if (bytes == null)
144       {
145          return null;
146       }
147       Object JavaDoc value = unmarshall(bytes);
148       return value;
149    }
150
151    public void remove(Fqn fqn) throws Exception JavaDoc
152    {
153       delegate.remove(fqn);
154    }
155
156    public void removeData(Fqn fqn) throws Exception JavaDoc
157    {
158       delegate.removeData(fqn);
159    }
160
161    public void prepare(Object JavaDoc tx, List JavaDoc modifications, boolean onePhase) throws Exception JavaDoc
162    {
163       modifications = marshallModifications(modifications);
164       delegate.prepare(tx, modifications, onePhase);
165    }
166
167    public void commit(Object JavaDoc tx) throws Exception JavaDoc
168    {
169       delegate.commit(tx);
170    }
171
172    public void rollback(Object JavaDoc tx)
173    {
174       delegate.rollback(tx);
175    }
176
177    public byte[] loadEntireState() throws Exception JavaDoc
178    {
179       return delegate.loadEntireState();
180    }
181
182    public void storeEntireState(byte[] bytes) throws Exception JavaDoc
183    {
184       delegate.storeEntireState(bytes);
185    }
186
187    public void create() throws Exception JavaDoc
188    {
189       delegate.create();
190    }
191
192    public void start() throws Exception JavaDoc
193    {
194       delegate.start();
195    }
196
197    public void stop()
198    {
199       delegate.stop();
200    }
201
202    public void destroy()
203    {
204       delegate.destroy();
205    }
206
207    protected List JavaDoc marshallModifications(List JavaDoc modifications) throws ObjectStreamException JavaDoc
208    {
209       List JavaDoc copy = new ArrayList JavaDoc();
210       for (Iterator JavaDoc i = modifications.iterator(); i.hasNext();)
211       {
212          Modification a = (Modification)i.next();
213          Object JavaDoc value = a.getValue();
214          Object JavaDoc oldValue = a.getOldValue();
215          Map JavaDoc data = a.getData();
216          Map JavaDoc oldData = a.getOldData();
217          if (value != null)
218          {
219             value = marshall(value);
220          }
221          if (oldValue != null)
222          {
223             oldValue = marshall(oldValue);
224          }
225          if (data != null)
226          {
227             data = marshallMap(data);
228          }
229          if (oldData != null)
230          {
231             oldData = marshallMap(oldData);
232          }
233          Modification b = new Modification(
234             a.getType(),
235             a.getFqn(),
236             a.getKey(),
237             value,
238             oldValue,
239             data,
240             oldData);
241          copy.add(b);
242       }
243       return copy;
244    }
245
246    protected Map JavaDoc marshallMap(Map JavaDoc data) throws ObjectStreamException JavaDoc
247    {
248       data = new HashMap JavaDoc(data);
249       for (Iterator JavaDoc i = data.entrySet().iterator(); i.hasNext();)
250       {
251          Map.Entry JavaDoc entry = (Map.Entry JavaDoc)i.next();
252          Object JavaDoc value = entry.getValue();
253          byte[] bytes = (byte[])marshall(value);
254          entry.setValue(bytes);
255       }
256       return data;
257    }
258
259    protected Map JavaDoc unmarshallMap(Map JavaDoc data) throws ClassNotFoundException JavaDoc, ObjectStreamException JavaDoc
260    {
261       data = new HashMap JavaDoc(data);
262       for (Iterator JavaDoc i = data.entrySet().iterator(); i.hasNext();)
263       {
264          Map.Entry JavaDoc entry = (Map.Entry JavaDoc)i.next();
265          byte[] bytes = (byte[])entry.getValue();
266          Object JavaDoc value = unmarshall(bytes);
267          entry.setValue(value);
268       }
269       return data;
270    }
271
272    protected byte[] marshall(Object JavaDoc o) throws ObjectStreamException JavaDoc
273    {
274       ByteArrayOutputStream JavaDoc baos = null;
275       try
276       {
277          baos = new ByteArrayOutputStream JavaDoc();
278          ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(baos);
279          oos.writeObject(o);
280          oos.close();
281          return baos.toByteArray();
282       }
283       catch (IOException JavaDoc e)
284       {
285          if (e instanceof ObjectStreamException JavaDoc)
286          {
287             throw (ObjectStreamException JavaDoc)e;
288          }
289          throw new Error JavaDoc("Impossible", e);
290       }
291    }
292
293    protected Object JavaDoc unmarshall(byte[] bytes) throws ClassNotFoundException JavaDoc, ObjectStreamException JavaDoc
294    {
295       try
296       {
297          ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(bytes);
298          ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc(bais);
299          return ois.readObject();
300       }
301       catch (IOException JavaDoc e)
302       {
303          if (e instanceof ObjectStreamException JavaDoc)
304          {
305             throw (ObjectStreamException JavaDoc)e;
306          }
307          throw new Error JavaDoc("Impossible", e);
308       }
309    }
310 }
311
Popular Tags