KickJava   Java API By Example, From Geeks To Geeks.

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


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
38 import org.jboss.aop.Advised;
39 import org.jboss.cache.config.Configuration;
40 import org.jboss.cache.pojo.PojoCache;
41 import org.jboss.cache.pojo.PojoCacheFactory;
42 import org.jboss.logging.Logger;
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 <a HREF="mailto:harald@gliebe.de">Harald Gliebe</a>
53  * @version $Revision: 58592 $
54  * @ejb.bean type="Stateful"
55  * name="test/TreeCacheAopTester"
56  * jndi-name="test/TreeCacheAopTester"
57  * view-type="remote"
58  * @ejb.transaction type="Supports"
59  */

60
61 public class TreeCacheAopTesterBean implements SessionBean JavaDoc
62 {
63
64    SessionContext JavaDoc ctx;
65    PojoCache cache;
66    PojoCache cache2;
67
68    Logger logger_ = Logger.getLogger(TreeCacheAopTesterBean.class);
69
70    public void ejbActivate() throws EJBException JavaDoc, RemoteException JavaDoc
71    {
72    }
73
74    public void ejbPassivate() throws EJBException JavaDoc, RemoteException JavaDoc
75    {
76    }
77
78    public void ejbRemove() throws EJBException JavaDoc, RemoteException JavaDoc
79    {
80    }
81
82    public void setSessionContext(SessionContext JavaDoc ctx) throws EJBException JavaDoc
83    {
84       this.ctx = ctx;
85    }
86
87    /**
88     * @ejb.create-method
89     */

90    public void ejbCreate(String JavaDoc cluster_name, String JavaDoc props, int caching_mode) throws CreateException JavaDoc
91    {
92       try {
93          Configuration config = new Configuration();
94          config.setClusterName(cluster_name);
95          config.setClusterConfig(props);
96          config.setCacheMode(Configuration.legacyModeToCacheMode(caching_mode));
97          cache = PojoCacheFactory.createCache(config, false);
98          cache.start();
99          cache2 = PojoCacheFactory.createCache(config, false);
100          cache2.start();
101       } catch (Exception JavaDoc e) {
102          throw new CreateException JavaDoc(e.toString());
103       }
104    }
105
106
107    /**
108     * @ejb.interface-method
109     */

110    public void testSetup()
111    {
112       Person p = new Person();
113       if (!(p instanceof Advised)) {
114          logger_.error("testSetup(): p is not an instance of Advised");
115          throw new RuntimeException JavaDoc("Person must be advised!");
116       }
117       Address a = new Address();
118       if (!(a instanceof Advised)) {
119          logger_.error("testSetup(): a is not an instance of Advised");
120          throw new RuntimeException JavaDoc("Address must be advised!");
121       }
122    }
123
124    /**
125     * @ejb.interface-method
126     */

127    public void createPerson(String JavaDoc key, String JavaDoc name, int age)
128    {
129       Person p = new Person();
130       p.setName(name);
131       p.setAge(age);
132       p.setAddress(new Address());
133       try {
134          cache.attach(key, p);
135       } catch (Exception JavaDoc e) {
136          throw new RuntimeException JavaDoc(e);
137       }
138    }
139
140    /**
141     * @ejb.interface-method
142     */

143    public void removePerson(String JavaDoc key)
144    {
145       try {
146          cache.detach(key);
147       } catch (Exception JavaDoc e) {
148          throw new RuntimeException JavaDoc(e);
149       }
150    }
151
152
153    Object JavaDoc getPerson(String JavaDoc key)
154    {
155       try {
156          return (Person) cache.find(key);
157       } catch (Exception JavaDoc e) {
158          throw new RuntimeException JavaDoc(e);
159       }
160    }
161
162    /**
163     * @ejb.interface-method
164     */

165    public void setName(String JavaDoc key, String JavaDoc name)
166    {
167       ((Person) getPerson(key)).setName(name);
168    }
169
170    /**
171     * @ejb.interface-method
172     */

173    public String JavaDoc getName(String JavaDoc key)
174    {
175       return ((Person) getPerson(key)).getName();
176    }
177
178    /**
179     * @ejb.interface-method
180     */

181    public void setAge(String JavaDoc key, int age)
182    {
183       ((Person) getPerson(key)).setAge(age);
184    }
185
186    /**
187     * @ejb.interface-method
188     */

189    public int getAge(String JavaDoc key)
190    {
191       return ((Person) getPerson(key)).getAge();
192    }
193
194    /**
195     * @ejb.interface-method
196     */

197    public void setStreet(String JavaDoc key, String JavaDoc street)
198    {
199       ((Person) getPerson(key)).getAddress().setStreet(street);
200    }
201
202    /**
203     * @ejb.interface-method
204     */

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

213    public void setCity(String JavaDoc key, String JavaDoc city)
214    {
215       ((Person) getPerson(key)).getAddress().setCity(city);
216    }
217
218    /**
219     * @ejb.interface-method
220     */

221    public String JavaDoc getCity(String JavaDoc key)
222    {
223       return ((Person) getPerson(key)).getAddress().getCity();
224    }
225
226    /**
227     * @ejb.interface-method
228     */

229    public void setZip(String JavaDoc key, int zip)
230    {
231       ((Person) getPerson(key)).getAddress().setZip(zip);
232    }
233
234    /**
235     * @ejb.interface-method
236     */

237    public int getZip(String JavaDoc key)
238    {
239       return ((Person) getPerson(key)).getAddress().getZip();
240    }
241
242    // Map operations
243

244    /**
245     * @ejb.interface-method
246     */

247    public Object JavaDoc getHobby(String JavaDoc key, Object JavaDoc hobbyKey)
248    {
249       Map JavaDoc hobbies = ((Person) getPerson(key)).getHobbies();
250       return hobbies == null ? null : hobbies.get(hobbyKey);
251    }
252
253    /**
254     * @ejb.interface-method
255     */

256    public void setHobby(String JavaDoc key, Object JavaDoc hobbyKey, Object JavaDoc value)
257    {
258       Person person = ((Person) getPerson(key));
259       Map JavaDoc hobbies = person.getHobbies();
260       if (hobbies == null) {
261          hobbies = new HashMap JavaDoc();
262          person.setHobbies(hobbies);
263          // NB: it is neccessary to get hobbies again to get advised version
264
hobbies = person.getHobbies();
265       }
266       hobbies.put(hobbyKey, value);
267    }
268
269    // List operations
270

271    /**
272     * @ejb.interface-method
273     */

274    public Object JavaDoc getLanguage(String JavaDoc key, int index)
275    {
276       List JavaDoc languages = ((Person) getPerson(key)).getLanguages();
277       return languages == null ? null : languages.get(index);
278    }
279
280    /**
281     * @ejb.interface-method
282     */

283    public void addLanguage(String JavaDoc key, Object JavaDoc language)
284    {
285       Person person = ((Person) getPerson(key));
286       List JavaDoc languages = person.getLanguages();
287       if (languages == null) {
288          person.setLanguages(new ArrayList JavaDoc());
289          languages = person.getLanguages();
290       }
291       languages.add(language);
292    }
293
294    /**
295     * @ejb.interface-method
296     */

297    public void removeLanguage(String JavaDoc key, Object JavaDoc language)
298    {
299       List JavaDoc languages = ((Person) getPerson(key)).getLanguages();
300       if (languages == null) return;
301       languages.remove(language);
302    }
303
304    /**
305     * @ejb.interface-method
306     */

307    public int getLanguagesSize(String JavaDoc key)
308    {
309       List JavaDoc languages = ((Person) getPerson(key)).getLanguages();
310       return languages == null ? 0 : languages.size();
311    }
312
313    /**
314     * @ejb.interface-method
315     */

316    public Set JavaDoc getSkills(String JavaDoc key)
317    {
318       return new HashSet JavaDoc(((Person) getPerson(key)).getSkills());
319    }
320
321    /**
322     * @ejb.interface-method
323     */

324    public void addSkill(String JavaDoc key, String JavaDoc skill)
325    {
326       Person person = ((Person) getPerson(key));
327       Set JavaDoc skills = person.getSkills();
328       if (skills == null) {
329          person.setSkills(new HashSet JavaDoc());
330          skills = person.getSkills();
331       }
332       skills.add(skill);
333    }
334
335    /**
336     * @ejb.interface-method
337     */

338    public void removeSkill(String JavaDoc key, String JavaDoc skill)
339    {
340       Person person = ((Person) getPerson(key));
341       Set JavaDoc skills = person.getSkills();
342       if (skills != null) {
343          skills.remove(skill);
344       }
345    }
346
347    /**
348     * @ejb.interface-method
349     */

350    public Object JavaDoc testSerialization()
351    {
352       try {
353          Person p = new Person();
354          /*
355          if (!(p instanceof Externalizable)) {
356         throw new RuntimeException("p not Externalizable");
357          }
358          */

359          p.setName("Harald Gliebe");
360          Address address = new Address();
361          address.setCity("Mannheim");
362          p.setAddress(address);
363          cache.attach("/person/harald", p);
364          return (Person) cache.find("/person/harald");
365       } catch (Throwable JavaDoc t) {
366          throw new RuntimeException JavaDoc(t);
367       }
368    }
369
370    /**
371     * @ejb.interface-method
372     */

373    public void testDeserialization(String JavaDoc key, Object JavaDoc value)
374    {
375       try {
376          cache.attach(key, value);
377       } catch (Throwable JavaDoc t) {
378          throw new RuntimeException JavaDoc(t);
379       }
380    }
381
382    /**
383     * @ejb.interface-method
384     */

385    public void printPerson(String JavaDoc key)
386    {
387       System.out.println(getPerson(key));
388    }
389
390    /**
391     * @ejb.interface-method
392     */

393    public void printCache()
394    {
395       System.out.println(cache);
396    }
397
398    /**
399     * @ejb.interface-method
400     */

401    public Object JavaDoc getFieldValue(String JavaDoc key, String JavaDoc name)
402    {
403       try {
404          Object JavaDoc object = cache.find(key);
405          Field JavaDoc f = object.getClass().getDeclaredField(name);
406          f.setAccessible(true);
407          return f.get(object);
408       } catch (Exception JavaDoc e) {
409          throw new NestedRuntimeException(e);
410       }
411    }
412
413 }
414
415
Popular Tags