KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.Map JavaDoc;
25 import java.util.Set JavaDoc;
26 import java.util.Vector JavaDoc;
27
28 import javax.ejb.CreateException JavaDoc;
29 import javax.ejb.SessionBean JavaDoc;
30 import javax.ejb.SessionContext JavaDoc;
31 import javax.management.Attribute JavaDoc;
32 import javax.management.MBeanServer JavaDoc;
33 import javax.management.ObjectName JavaDoc;
34
35 import org.jboss.cache.pojo.PojoCache;
36 import org.jboss.mx.util.MBeanProxyExt;
37 import org.jboss.mx.util.MBeanServerLocator;
38
39 /**
40  * EJB proxy to the TreeCache MBean service. Used to be able to transport
41  * user transactions from a test client to a TreeCache. Note that TreeCache MBean
42  * is deployed during a test case run and is persistent throughout that run only.
43  *
44  * @author Ben Wang
45  * @version $Revision: 58592 $
46  * @ejb.bean type="Stateful"
47  * name="test/TreeCacheMBeanTester"
48  * jndi-name="ejb/test/TreeCacheMBeanTester"
49  * view-type="remote"
50  * @ejb.transaction type="Supports"
51  */

52 public class TreeCacheMBeanTesterBean implements SessionBean JavaDoc
53 {
54    // Use a different service name so that it won't collide with the regular name.
55
static final String JavaDoc OBJECT_NAME = "jboss.cache:service=testTreeCache";
56    MBeanServer JavaDoc server;
57    ObjectName JavaDoc cacheService;
58    PojoCache cache=null;
59
60    /**
61     * @throws CreateException
62     * @ejb.create-method
63     */

64    public void ejbCreate() throws CreateException JavaDoc
65    {
66       log("Creating TreeCache ejb proxy");
67       init();
68    }
69
70    /**
71     * @param name MBean object name.
72     * @throws CreateException
73     * @ejb.create-method
74     */

75    public void ejbCreate(String JavaDoc name) throws CreateException JavaDoc
76    {
77       log("I'm being created");
78       init(name);
79    }
80
81    private void init() throws CreateException JavaDoc
82    {
83       init(OBJECT_NAME);
84    }
85
86    private void init(String JavaDoc name) throws CreateException JavaDoc
87    {
88       try {
89          cacheService = new ObjectName JavaDoc(name);
90          server = MBeanServerLocator.locate();
91          cache=(PojoCache)server.getAttribute(new ObjectName JavaDoc("jboss.cache:service=testTreeCacheAop"),
92          "PojoCache");
93       } catch (Exception JavaDoc ex) {
94          throw new CreateException JavaDoc(ex.toString());
95       }
96    }
97
98    public void ejbActivate()
99    {
100    }
101
102    public void ejbPassivate()
103    {
104    }
105
106    public void ejbRemove()
107    {
108       log("I'm being removed");
109    }
110
111    public void setSessionContext(SessionContext JavaDoc ctx)
112    {
113    }
114
115    /**
116     * @return
117     * @ejb.interface-method
118     */

119    public Vector JavaDoc getMembers() throws Exception JavaDoc
120    {
121       // FIXME restore after Cache exposes getMembers
122
// return cache.getMembers();
123
throw new UnsupportedOperationException JavaDoc("See FIXME in bean code");
124    }
125
126    /**
127     * @return
128     * @ejb.interface-method
129     */

130    public int getCacheMode() throws Exception JavaDoc
131    {
132       return ((Integer JavaDoc) server.getAttribute(cacheService, "CacheMode")).intValue();
133    }
134
135    /**
136     * @param mode
137     * @ejb.interface-method
138     */

139    public void setCacheMode(int mode) throws Exception JavaDoc
140    {
141       server.setAttribute(cacheService, new Attribute JavaDoc("CacheMode",
142             new Integer JavaDoc(mode)));
143    }
144
145    /**
146     * @return
147     * @ejb.interface-method
148     */

149    public boolean getLocking() throws Exception JavaDoc
150    {
151       return ((Boolean JavaDoc) server.getAttribute(cacheService, "Locking")).booleanValue();
152    }
153
154    /**
155     * @param flag
156     * @ejb.interface-method
157     */

158    public void setLocking(boolean flag) throws Exception JavaDoc
159    {
160       server.setAttribute(cacheService, new Attribute JavaDoc("Locking",
161             new Boolean JavaDoc(flag)));
162    }
163
164    /**
165     * @return
166     * @ejb.interface-method
167     */

168    public int getLockingLevel() throws Exception JavaDoc
169    {
170       return ((Integer JavaDoc) server.getAttribute(cacheService, "LockingLevel")).intValue();
171    }
172
173    /**
174     * @param level
175     * @ejb.interface-method
176     */

177    public void setLocking(int level) throws Exception JavaDoc
178    {
179       server.setAttribute(cacheService, new Attribute JavaDoc("LockingLevel",
180             new Integer JavaDoc(level)));
181    }
182
183    /**
184     * @param fqn
185     * @return
186     * @ejb.interface-method
187     */

188    public Set JavaDoc getKeys(String JavaDoc fqn) throws Exception JavaDoc {
189       return (Set JavaDoc) server.invoke(cacheService, "getKeys",
190             new Object JavaDoc[]{fqn},
191             new String JavaDoc[]{String JavaDoc.class.getName()});
192    }
193
194    /**
195     * @param fqn
196     * @param key
197     * @return
198     * @ejb.interface-method
199     */

200    public Object JavaDoc get(String JavaDoc fqn, String JavaDoc key) throws Exception JavaDoc
201    {
202       return server.invoke(cacheService, "get",
203             new Object JavaDoc[]{fqn, key},
204             new String JavaDoc[]{String JavaDoc.class.getName(),
205                          Object JavaDoc.class.getName()});
206    }
207
208    /**
209     * @param fqn
210     * @return
211     * @ejb.interface-method
212     */

213    public boolean exists(String JavaDoc fqn) throws Exception JavaDoc
214    {
215       return ((Boolean JavaDoc) server.invoke(cacheService, "exists",
216             new Object JavaDoc[]{fqn},
217             new String JavaDoc[]{String JavaDoc.class.getName()})).booleanValue();
218    }
219
220    /**
221     * @param fqn
222     * @param data
223     * @throws Exception
224     * @ejb.interface-method
225     */

226    public void put(String JavaDoc fqn, Map JavaDoc data) throws Exception JavaDoc
227    {
228       server.invoke(cacheService, "put",
229             new Object JavaDoc[]{fqn, data},
230             new String JavaDoc[]{String JavaDoc.class.getName(),
231                          Map JavaDoc.class.getName()});
232    }
233
234    /**
235     * @param fqn
236     * @param key
237     * @param value
238     * @throws Exception
239     * @ejb.interface-method
240     */

241    public void put(String JavaDoc fqn, String JavaDoc key, Object JavaDoc value) throws Exception JavaDoc
242    {
243       Object JavaDoc[] args = {fqn, key, value};
244       String JavaDoc[] sig = {String JavaDoc.class.getName(),
245                       Object JavaDoc.class.getName(),
246                       Object JavaDoc.class.getName()};
247
248       server.invoke(cacheService, "put", args, sig);
249    }
250
251    /**
252     * @param fqn
253     * @throws Exception
254     * @ejb.interface-method
255     */

256    public void remove(String JavaDoc fqn) throws Exception JavaDoc
257    {
258       Object JavaDoc[] args = {fqn};
259       String JavaDoc[] sig = {String JavaDoc.class.getName()};
260
261       server.invoke(cacheService, "remove", args, sig);
262    }
263
264
265    /**
266     * @param fqn
267     * @param key
268     * @return
269     * @throws Exception
270     * @ejb.interface-method
271     */

272    public Object JavaDoc remove(String JavaDoc fqn, String JavaDoc key) throws Exception JavaDoc
273    {
274       return server.invoke(cacheService, "remove",
275             new Object JavaDoc[]{fqn, key},
276             new String JavaDoc[]{String JavaDoc.class.getName(),
277                          String JavaDoc.class.getName()});
278    }
279
280    /**
281     * @param fqn
282     * @ejb.interface-method
283     */

284    public void releaseAllLocks(String JavaDoc fqn) throws Exception JavaDoc
285    {
286       server.invoke(cacheService, "releaseAllLocks",
287             new Object JavaDoc[]{fqn},
288             new String JavaDoc[]{String JavaDoc.class.getName()});
289    }
290
291    /**
292     * @param fqn
293     * @return
294     * @ejb.interface-method
295     */

296    public String JavaDoc print(String JavaDoc fqn) throws Exception JavaDoc
297    {
298       return (String JavaDoc) server.invoke(cacheService, "print",
299             new Object JavaDoc[]{fqn},
300             new String JavaDoc[]{String JavaDoc.class.getName()});
301    }
302
303    /**
304     * @param fqn
305     * @return
306     * @ejb.interface-method
307     */

308    public Set JavaDoc getChildrenNames(String JavaDoc fqn) throws Exception JavaDoc
309    {
310       return (Set JavaDoc) server.invoke(cacheService, "getChildrenNames",
311             new Object JavaDoc[]{fqn},
312             new String JavaDoc[]{String JavaDoc.class.getName()});
313    }
314
315    /**
316     * @return
317     * @ejb.interface-method
318     */

319    public String JavaDoc printDetails() throws Exception JavaDoc
320    {
321       return (String JavaDoc) server.invoke(cacheService, "printDetails",
322             null,
323             null);
324    }
325
326    /**
327     * @return
328     * @ejb.interface-method
329     */

330    public String JavaDoc printLockInfo() throws Exception JavaDoc
331    {
332       return (String JavaDoc) server.invoke(cacheService, "printLockInfo",
333             null,
334             null);
335    }
336
337    private void log(String JavaDoc msg)
338    {
339       System.out.println("-- [" + Thread.currentThread().getName() + "]: " + msg);
340    }
341
342 }
343
Popular Tags