KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > cache > bean > TreeCacheTesterBean


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.cache.bean;
23
24 import org.jboss.cache.Cache;
25 import org.jboss.cache.CacheException;
26 import org.jboss.cache.Fqn;
27 import org.jboss.cache.Node;
28 import org.jboss.cache.TreeCache;
29 import org.jboss.cache.config.Configuration;
30 import org.jboss.cache.factories.DefaultCacheFactory;
31 import org.jboss.cache.lock.IsolationLevel;
32
33 import javax.ejb.CreateException JavaDoc;
34 import javax.ejb.SessionBean JavaDoc;
35 import javax.ejb.SessionContext JavaDoc;
36
37 import java.util.List JavaDoc;
38 import java.util.Map JavaDoc;
39 import java.util.Set JavaDoc;
40 import java.util.Vector JavaDoc;
41
42
43 /**
44  * Proxy to the TreeCache MBean. Mainly used to be able to transport transactions from a test
45  * client to a TreeCache.
46  *
47  * @version $Revision: 58592 $
48  * @ejb.bean type="Stateful"
49  * name="test/TreeCacheTester"
50  * jndi-name="ejb/test/TreeCacheTester"
51  * view-type="remote"
52  * @ejb.transaction type="Supports"
53  */

54 public class TreeCacheTesterBean implements SessionBean JavaDoc
55 {
56    Cache cache = null;
57
58    /**
59     * @throws CreateException
60     * @ejb.create-method
61     */

62    public void ejbCreate() throws CreateException JavaDoc
63    {
64       log("I'm being created");
65    }
66
67    /**
68     * @param cluster_name
69     * @param props
70     * @param caching_mode
71     * @throws CreateException
72     * @ejb.create-method
73     */

74    public void ejbCreate(String JavaDoc cluster_name,
75                          String JavaDoc props,
76                          int caching_mode) throws CreateException JavaDoc
77    {
78       try {
79 // cache=new TreeCache(cluster_name, props, 10000);
80
Configuration config = new Configuration();
81          config.setClusterName(cluster_name);
82          config.setClusterConfig(props);
83          config.setCacheMode(Configuration.legacyModeToCacheMode(caching_mode));
84          config.setTransactionManagerLookupClass("org.jboss.cache.JBossTransactionManagerLookup");
85          
86          cache = DefaultCacheFactory.createCache(config);
87          
88       } catch (Exception JavaDoc e) {
89          throw new CreateException JavaDoc(e.toString());
90       }
91    }
92
93
94 // /**
95
// *
96
// * @param name
97
// * @ejb.create-method
98
// */
99
// public void ejbCreate(String name) throws CreateException {
100
// MBeanServer server=null;
101
// ObjectName cache_service;
102
//
103
// try {
104
// this.name=name;
105
// cache_service=ObjectName.getInstance(name);
106
//
107
// // is this the right way to get hold of the JBoss MBeanServer ?
108
// List servers=MBeanServerFactory.findMBeanServer(null);
109
// if(servers == null || servers.size() == 0)
110
// throw new CreateException("TreeCacheTesterBean.ejbCreate(): no MBeanServers found");
111
// server=(MBeanServer)servers.get(0);
112
// cache=(TreeCacheMBean)MBeanProxy.create(TreeCacheMBean.class, cache_service, server);
113
// }
114
// catch(Exception ex) {
115
// throw new CreateException(ex.toString());
116
// }
117
// }
118

119    public void ejbActivate()
120    {
121    }
122
123    public void ejbPassivate()
124    {
125    }
126
127    public void ejbRemove()
128    {
129       log("I'm being removed");
130       if (cache != null) {
131          cache.stop();
132          cache.destroy();
133          cache = null;
134       }
135    }
136
137    public void setSessionContext(SessionContext JavaDoc ctx)
138    {
139    }
140
141
142    /**
143     * @return
144     * @ejb.interface-method
145     */

146    public Vector JavaDoc getMembers()
147    {
148       List JavaDoc members = cache.getMembers();
149       return members == null ? null : new Vector JavaDoc(members);
150    }
151
152
153    /**
154     * @param mode
155     * @ejb.interface-method
156     */

157    public void setCacheMode(int mode) throws Exception JavaDoc
158    {
159       cache.getConfiguration().setCacheMode(Configuration.legacyModeToCacheMode(mode));
160    }
161
162
163    /**
164     * @param level
165     * @ejb.interface-method
166     */

167    public void setIsolationLevel(IsolationLevel level)
168    {
169       cache.getConfiguration().setIsolationLevel(level);
170    }
171
172
173    /**
174     * @param fqn
175     * @return
176     * @ejb.interface-method
177     */

178    public Set JavaDoc getKeys(String JavaDoc fqn) throws CacheException
179    {
180       Node node = cache.getChild(Fqn.fromString(fqn));
181       return node == null ? null : node.getKeys();
182    }
183
184    /**
185     * @param fqn
186     * @param key
187     * @return
188     * @ejb.interface-method
189     */

190    public Object JavaDoc get(String JavaDoc fqn, String JavaDoc key) throws CacheException {
191       return cache.get(Fqn.fromString(fqn), key);
192    }
193
194    /**
195     * @param fqn
196     * @return
197     * @ejb.interface-method
198     */

199    public boolean exists(String JavaDoc fqn)
200    {
201       return cache.hasChild(Fqn.fromString(fqn));
202    }
203
204    /**
205     * @param fqn
206     * @param data
207     * @throws Exception
208     * @ejb.interface-method
209     */

210    public void put(String JavaDoc fqn, Map JavaDoc data) throws Exception JavaDoc
211    {
212       cache.put(fqn, data);
213    }
214
215    /**
216     * @param fqn
217     * @param key
218     * @param value
219     * @return
220     * @throws Exception
221     * @ejb.interface-method
222     */

223    public Object JavaDoc put(String JavaDoc fqn, String JavaDoc key, Object JavaDoc value) throws Exception JavaDoc
224    {
225       return cache.put(Fqn.fromString(fqn), key, value);
226    }
227
228    /**
229     * @param fqn
230     * @throws Exception
231     * @ejb.interface-method
232     */

233    public void remove(String JavaDoc fqn) throws Exception JavaDoc
234    {
235       cache.remove(fqn);
236    }
237
238    /**
239     * @param fqn
240     * @param key
241     * @return
242     * @throws Exception
243     * @ejb.interface-method
244     */

245    public Object JavaDoc remove(String JavaDoc fqn, String JavaDoc key) throws Exception JavaDoc
246    {
247       return cache.remove(Fqn.fromString(fqn), key);
248    }
249
250 // /**
251
// * @param fqn
252
// * @ejb.interface-method
253
// */
254
// public void releaseAllLocks(String fqn)
255
// {
256
// cache.releaseAllLocks(fqn);
257
// }
258
//
259
// /**
260
// * @param fqn
261
// * @return
262
// * @ejb.interface-method
263
// */
264
// public String print(String fqn)
265
// {
266
// return cache.print(fqn);
267
// }
268

269    /**
270     * @param fqn
271     * @return
272     * @ejb.interface-method
273     */

274    public Set JavaDoc getChildrenNames(String JavaDoc fqn) throws CacheException
275    {
276       Node node = cache.getChild(Fqn.fromString(fqn));
277       return (node == null ? null : node.getChildrenNames());
278    }
279
280 // /**
281
// * @return
282
// * @ejb.interface-method
283
// */
284
// public String printDetails()
285
// {
286
// return cache.printDetails();
287
// }
288
//
289
// /**
290
// * @return
291
// * @ejb.interface-method
292
// */
293
// public String printLockInfo()
294
// {
295
// return cache.printLockInfo();
296
// }
297

298    /**
299     * @ejb.interface-method
300     * @param members
301     * @param method
302     * @param args
303     * @param synchronous
304     * @param exclude_self
305     * @param timeout
306     * @return
307     * @throws Exception
308     */

309 // public List callRemoteMethods(Vector members, Method method, Object[] args,
310
// boolean synchronous, boolean exclude_self,
311
// long timeout) throws Exception {
312
// return cache.callRemoteMethods(members, method, args, synchronous,
313
// exclude_self, timeout);
314
// }
315

316    /**
317     * @param members
318     * @param method_name
319     * @param types
320     * @param args
321     * @param synchronous
322     * @param exclude_self
323     * @param timeout
324     * @return
325     * @throws Exception
326     * @ejb.interface-method
327     */

328 // public List callRemoteMethods(Vector members, String method_name, Class[] types,
329
// Object[] args, boolean synchronous,
330
// boolean exclude_self, long timeout) throws Exception {
331
// return cache.callRemoteMethods(members, method_name, types, args,
332
// synchronous, exclude_self, timeout);
333
// }
334

335    private void log(String JavaDoc msg)
336    {
337       System.out.println("-- [" + Thread.currentThread().getName() + "]: " + msg);
338    }
339
340 }
341
Popular Tags