KickJava   Java API By Example, From Geeks To Geeks.

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


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.lang.reflect.Field JavaDoc;
25 import java.rmi.RemoteException JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.HashSet JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Map JavaDoc;
31 import java.util.Set JavaDoc;
32
33 import javax.ejb.CreateException JavaDoc;
34 import javax.ejb.EJBException JavaDoc;
35 import javax.ejb.SessionBean JavaDoc;
36 import javax.ejb.SessionContext JavaDoc;
37 import javax.management.MBeanServer JavaDoc;
38 import javax.management.ObjectName JavaDoc;
39
40 import org.jboss.cache.pojo.PojoCache;
41 import org.jboss.logging.Logger;
42 import org.jboss.mx.util.MBeanServerLocator;
43 import org.jboss.test.cache.test.standAloneAop.Address;
44 import org.jboss.test.cache.test.standAloneAop.Person;
45 import org.jboss.util.NestedRuntimeException;
46
47 /**
48  * Proxy to the TreeCacheAop MBean.
49  * The AOP framework requires that classes are loaded by special classloaders (e.g UCL).
50  * This bean is used to execute tests within the server.
51  *
52  * @author Ben Wang
53  * @version $Revision: 58592 $
54  * @ejb.bean type="Stateful"
55  * name="test/TreeCacheAopMBeanTester"
56  * jndi-name="ejb/test/TreeCacheAopMBeanTester"
57  * view-type="remote"
58  * @ejb.transaction type="Supports"
59  */

60
61 public class TreeCacheAopMBeanTesterBean implements SessionBean JavaDoc
62 {
63    static final String JavaDoc OBJECT_NAME = "jboss.cache:service=testTreeCacheAop";
64    MBeanServer JavaDoc server;
65    ObjectName JavaDoc cacheService;
66
67    SessionContext JavaDoc ctx;
68    PojoCache cache;
69
70    Logger logger_ = Logger.getLogger(TreeCacheAopMBeanTesterBean.class);
71
72    public void ejbActivate() throws EJBException JavaDoc, RemoteException JavaDoc
73    {
74    }
75
76    public void ejbPassivate() throws EJBException JavaDoc, RemoteException JavaDoc
77    {
78    }
79
80    public void ejbRemove() throws EJBException JavaDoc, RemoteException JavaDoc
81    {
82    }
83
84    public void setSessionContext(SessionContext JavaDoc ctx) throws EJBException JavaDoc
85    {
86       this.ctx = ctx;
87    }
88
89    /**
90     * @ejb.create-method
91     */

92    public void ejbCreate() throws CreateException JavaDoc
93    {
94       init();
95    }
96
97    private void init() throws CreateException JavaDoc
98    {
99       init(OBJECT_NAME);
100    }
101
102    private void init(String JavaDoc name) throws CreateException JavaDoc
103    {
104       try {
105          cacheService = new ObjectName JavaDoc(name);
106          server = MBeanServerLocator.locate();
107       } catch (Exception JavaDoc ex) {
108          throw new CreateException JavaDoc(ex.toString());
109       }
110    }
111
112    /**
113     * @ejb.interface-method
114     */

115    public void createPerson(String JavaDoc key, String JavaDoc name, int age) throws Exception JavaDoc
116    {
117       Person p = new Person();
118       p.setName(name);
119       p.setAge(age);
120       p.setAddress(new Address());
121       server.invoke(cacheService, "putObject",
122             new Object JavaDoc[]{key, p},
123             new String JavaDoc[]{String JavaDoc.class.getName(),
124                          Object JavaDoc.class.getName()});
125    }
126
127    /**
128     * @ejb.interface-method
129     */

130    public void removePerson(String JavaDoc key) throws Exception JavaDoc
131    {
132       server.invoke(cacheService, "removeObject",
133             new Object JavaDoc[]{key},
134             new String JavaDoc[]{String JavaDoc.class.getName()});
135    }
136
137
138    Object JavaDoc getPerson(String JavaDoc key) throws Exception JavaDoc
139    {
140       return server.invoke(cacheService, "getObject",
141             new Object JavaDoc[]{key},
142             new String JavaDoc[]{String JavaDoc.class.getName()});
143    }
144
145    /**
146     * @ejb.interface-method
147     */

148    public void setName(String JavaDoc key, String JavaDoc name) throws Exception JavaDoc
149    {
150       ((Person) getPerson(key)).setName(name);
151    }
152
153    /**
154     * @ejb.interface-method
155     */

156    public String JavaDoc getName(String JavaDoc key) throws Exception JavaDoc
157    {
158       return ((Person) getPerson(key)).getName();
159    }
160
161    /**
162     * @ejb.interface-method
163     */

164    public void setAge(String JavaDoc key, int age) throws Exception JavaDoc
165    {
166       ((Person) getPerson(key)).setAge(age);
167    }
168
169    /**
170     * @ejb.interface-method
171     */

172    public int getAge(String JavaDoc key) throws Exception JavaDoc
173    {
174       return ((Person) getPerson(key)).getAge();
175    }
176
177    /**
178     * @ejb.interface-method
179     */

180    public void setStreet(String JavaDoc key, String JavaDoc street) throws Exception JavaDoc
181    {
182       ((Person) getPerson(key)).getAddress().setStreet(street);
183    }
184
185    /**
186     * @ejb.interface-method
187     */

188    public String JavaDoc getStreet(String JavaDoc key) throws Exception JavaDoc
189    {
190       return ((Person) getPerson(key)).getAddress().getStreet();
191    }
192
193    /**
194     * @ejb.interface-method
195     */

196    public void setCity(String JavaDoc key, String JavaDoc city) throws Exception JavaDoc
197    {
198       ((Person) getPerson(key)).getAddress().setCity(city);
199    }
200
201    /**
202     * @ejb.interface-method
203     */

204    public String JavaDoc getCity(String JavaDoc key) throws Exception JavaDoc
205    {
206       return ((Person) getPerson(key)).getAddress().getCity();
207    }
208
209    /**
210     * @ejb.interface-method
211     */

212    public void setZip(String JavaDoc key, int zip) throws Exception JavaDoc
213    {
214       ((Person) getPerson(key)).getAddress().setZip(zip);
215    }
216
217    /**
218     * @ejb.interface-method
219     */

220    public int getZip(String JavaDoc key) throws Exception JavaDoc
221    {
222       return ((Person) getPerson(key)).getAddress().getZip();
223    }
224
225    // Map operations
226

227    /**
228     * @ejb.interface-method
229     */

230    public Object JavaDoc getHobby(String JavaDoc key, Object JavaDoc hobbyKey) throws Exception JavaDoc
231    {
232       Map JavaDoc hobbies = ((Person) getPerson(key)).getHobbies();
233       return hobbies == null ? null : hobbies.get(hobbyKey);
234    }
235
236    /**
237     * @ejb.interface-method
238     */

239    public void setHobby(String JavaDoc key, Object JavaDoc hobbyKey, Object JavaDoc value) throws Exception JavaDoc
240    {
241       Person person = ((Person) getPerson(key));
242       Map JavaDoc hobbies = person.getHobbies();
243       if (hobbies == null) {
244          hobbies = new HashMap JavaDoc();
245          person.setHobbies(hobbies);
246          // NB: it is neccessary to get hobbies again to get advised version
247
hobbies = person.getHobbies();
248       }
249       hobbies.put(hobbyKey, value);
250    }
251
252    // List operations
253

254    /**
255     * @ejb.interface-method
256     */

257    public Object JavaDoc getLanguage(String JavaDoc key, int index) throws Exception JavaDoc
258    {
259       List JavaDoc languages = ((Person) getPerson(key)).getLanguages();
260       return languages == null ? null : languages.get(index);
261    }
262
263    /**
264     * @ejb.interface-method
265     */

266    public void addLanguage(String JavaDoc key, Object JavaDoc language) throws Exception JavaDoc
267    {
268       Person person = ((Person) getPerson(key));
269       List JavaDoc languages = person.getLanguages();
270       if (languages == null) {
271          person.setLanguages(new ArrayList JavaDoc());
272          languages = person.getLanguages();
273       }
274       languages.add(language);
275    }
276
277    /**
278     * @ejb.interface-method
279     */

280    public void removeLanguage(String JavaDoc key, Object JavaDoc language) throws Exception JavaDoc
281    {
282       List JavaDoc languages = ((Person) getPerson(key)).getLanguages();
283       if (languages == null) return;
284       languages.remove(language);
285    }
286
287    /**
288     * @ejb.interface-method
289     */

290    public int getLanguagesSize(String JavaDoc key) throws Exception JavaDoc
291    {
292       List JavaDoc languages = ((Person) getPerson(key)).getLanguages();
293       return languages == null ? 0 : languages.size();
294    }
295
296    /**
297     * @ejb.interface-method
298     */

299    public Set JavaDoc getSkills(String JavaDoc key) throws Exception JavaDoc
300    {
301       return new HashSet JavaDoc(((Person) getPerson(key)).getSkills());
302    }
303
304    /**
305     * @ejb.interface-method
306     */

307    public void addSkill(String JavaDoc key, String JavaDoc skill) throws Exception JavaDoc
308    {
309       Person person = ((Person) getPerson(key));
310       Set JavaDoc skills = person.getSkills();
311       if (skills == null) {
312          person.setSkills(new HashSet JavaDoc());
313          skills = person.getSkills();
314       }
315       skills.add(skill);
316    }
317
318    /**
319     * @ejb.interface-method
320     */

321    public void removeSkill(String JavaDoc key, String JavaDoc skill) throws Exception JavaDoc
322    {
323       Person person = ((Person) getPerson(key));
324       Set JavaDoc skills = person.getSkills();
325       if (skills != null) {
326          skills.remove(skill);
327       }
328    }
329
330
331    /**
332     * @ejb.interface-method
333     */

334    public void printPerson(String JavaDoc key) throws Exception JavaDoc
335    {
336       System.out.println(getPerson(key));
337    }
338
339    /**
340     * @ejb.interface-method
341     */

342    public void printCache()
343    {
344       System.out.println(cache);
345    }
346
347    /**
348     * @ejb.interface-method
349     */

350    public Object JavaDoc getFieldValue(String JavaDoc key, String JavaDoc name)
351    {
352       try {
353          Object JavaDoc object = cache.find(key);
354          Field JavaDoc f = object.getClass().getDeclaredField(name);
355          f.setAccessible(true);
356          return f.get(object);
357       } catch (Exception JavaDoc e) {
358          throw new NestedRuntimeException(e);
359       }
360    }
361
362 }
363
364
Popular Tags