KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > tests > aop > TreeCacheAopTester


1 /*
2  */

3 package org.jboss.cache.tests.aop;
4
5 import org.apache.commons.logging.Log;
6 import org.apache.commons.logging.LogFactory;
7 import org.jboss.aop.Advised;
8 import org.jboss.cache.PropertyConfigurator;
9 import org.jboss.cache.aop.TreeCacheAop;
10 import org.jboss.util.NestedRuntimeException;
11
12 import java.lang.reflect.Field JavaDoc;
13 import java.util.*;
14
15 /**
16  * Proxy to the TreeCacheAop.
17  * The AOP framework requires that classes are loaded by special classloaders (e.g UCL).
18  */

19
20 public class TreeCacheAopTester
21 {
22
23    TreeCacheAop cache;
24
25    Log logger_=LogFactory.getLog(TreeCacheAopTester.class);
26
27    public TreeCacheAopTester(String JavaDoc configFile)
28    {
29       try {
30          /*
31              cache=new TreeCacheAop(cluster_name, props, 10000);
32              cache.startService();
33              cache2=new TreeCacheAop(cluster_name, props, 10000);
34              cache2.startService();
35              */

36          cache = new TreeCacheAop();
37          PropertyConfigurator config = new PropertyConfigurator();
38          config.configure(cache, configFile); // read in generic replSync xml
39
cache.startService();
40       } catch (Exception JavaDoc e) {
41          e.printStackTrace();
42       }
43    }
44
45    public void stop()
46    {
47       cache.stopService();
48    }
49
50    public void testSetup()
51    {
52       Person p = new Person();
53       if (!(p instanceof Advised)) {
54          logger_.error("testSetup(): p is not an instance of Advised");
55          throw new RuntimeException JavaDoc("Person must be advised!");
56       }
57       Address a = new Address();
58       if (!(a instanceof Advised)) {
59          logger_.error("testSetup(): a is not an instance of Advised");
60          throw new RuntimeException JavaDoc("Address must be advised!");
61       }
62    }
63    
64    public void setSyncCommitPhase(boolean bool) {
65       cache.setSyncCommitPhase(true);
66    }
67
68    public void createPerson(String JavaDoc key, String JavaDoc name, int age)
69    {
70       Person p = new Person();
71       p.setName(name);
72       p.setAge(age);
73       p.setAddress(new Address());
74       try {
75          cache.putObject(key, p);
76       } catch (Exception JavaDoc e) {
77          throw new RuntimeException JavaDoc(e);
78       }
79    }
80
81    public void removePerson(String JavaDoc key)
82    {
83       try {
84          cache.removeObject(key);
85       } catch (Exception JavaDoc e) {
86          e.printStackTrace();
87          throw new RuntimeException JavaDoc(e);
88       }
89    }
90
91    Object JavaDoc getPerson(String JavaDoc key)
92    {
93       try {
94          return (Person) cache.getObject(key);
95       } catch (Exception JavaDoc e) {
96          throw new RuntimeException JavaDoc(e);
97       }
98    }
99
100    public void createStudent(String JavaDoc key, String JavaDoc name, int age, String JavaDoc year)
101    {
102       Student p = new Student();
103       p.setName(name);
104       p.setAge(age);
105       p.setAddress(new Address());
106       p.setYear(year);
107       try {
108          cache.putObject(key, p);
109       } catch (Exception JavaDoc e) {
110          throw new RuntimeException JavaDoc(e);
111       }
112    }
113
114    public void removeStudent(String JavaDoc key)
115    {
116       try {
117          cache.removeObject(key);
118       } catch (Exception JavaDoc e) {
119          e.printStackTrace();
120          throw new RuntimeException JavaDoc(e);
121       }
122    }
123
124    Object JavaDoc getStudent(String JavaDoc key)
125    {
126       try {
127          return (Student) cache.getObject(key);
128       } catch (Exception JavaDoc e) {
129          throw new RuntimeException JavaDoc(e);
130       }
131    }
132
133    public void setYear(String JavaDoc key, String JavaDoc year)
134    {
135       ((Student) getStudent(key)).setYear(year);
136    }
137
138    public String JavaDoc getYear(String JavaDoc key) {
139       return ((Student) getStudent(key)).getYear();
140    }
141
142    public void setAddress(Person person, Address addr)
143    {
144       person.setAddress(addr);
145    }
146
147    public Address createAddress()
148    {
149       return new Address();
150    }
151
152    public void setName(String JavaDoc key, String JavaDoc name)
153    {
154       ((Person) getPerson(key)).setName(name);
155    }
156
157    public String JavaDoc getName(String JavaDoc key)
158    {
159       return ((Person) getPerson(key)).getName();
160    }
161
162    public void setCurrentStatus(String JavaDoc key, String JavaDoc status)
163    {
164       ((Person) getPerson(key)).setCurrentStatus(status);
165    }
166
167    public String JavaDoc getCurrentStatus(String JavaDoc key)
168    {
169       return ((Person) getPerson(key)).getCurrentStatus();
170    }
171
172    public void setAge(String JavaDoc key, int age)
173    {
174       ((Person) getPerson(key)).setAge(age);
175    }
176
177    public int getAge(String JavaDoc key)
178    {
179       return ((Person) getPerson(key)).getAge();
180    }
181
182    public void setStreet(String JavaDoc key, String JavaDoc street)
183    {
184       ((Person) getPerson(key)).getAddress().setStreet(street);
185    }
186
187    public String JavaDoc getStreet(String JavaDoc key)
188    {
189       return ((Person) getPerson(key)).getAddress().getStreet();
190    }
191
192    public void setCity(String JavaDoc key, String JavaDoc city)
193    {
194       ((Person) getPerson(key)).getAddress().setCity(city);
195    }
196
197    public String JavaDoc getCity(String JavaDoc key)
198    {
199       return ((Person) getPerson(key)).getAddress().getCity();
200    }
201
202    public void setZip(String JavaDoc key, int zip)
203    {
204       ((Person) getPerson(key)).getAddress().setZip(zip);
205    }
206
207    public int getZip(String JavaDoc key)
208    {
209       return ((Person) getPerson(key)).getAddress().getZip();
210    }
211
212    // Map operations
213

214    public Object JavaDoc getHobby(String JavaDoc key, Object JavaDoc hobbyKey)
215    {
216       Map hobbies = ((Person) getPerson(key)).getHobbies();
217       return hobbies == null ? null : hobbies.get(hobbyKey);
218    }
219
220    public void setHobby(String JavaDoc key, Object JavaDoc hobbyKey, Object JavaDoc value)
221    {
222       Person person = ((Person) getPerson(key));
223       Map hobbies = person.getHobbies();
224       if (hobbies == null) {
225          hobbies = new HashMap();
226          person.setHobbies(hobbies);
227          // NB: it is neccessary to get hobbies again to get advised version
228
hobbies = person.getHobbies();
229       }
230       hobbies.put(hobbyKey, value);
231    }
232
233    public void setHobbies(String JavaDoc key, Map map)
234    {
235       Person person = ((Person) getPerson(key));
236       person.setHobbies(map);
237    }
238
239    public Map getHobbies(String JavaDoc key) {
240       return ((Person) getPerson(key)).getHobbies();
241    }
242    // List operations
243

244    public Object JavaDoc getLanguage(String JavaDoc key, int index)
245    {
246       List languages = ((Person) getPerson(key)).getLanguages();
247       return languages == null ? null : languages.get(index);
248    }
249
250    
251    public Object JavaDoc getLanguages(String JavaDoc key)
252    {
253       List languages = ((Person) getPerson(key)).getLanguages();
254       return languages;
255    }
256
257    public void addLanguage(String JavaDoc key, Object JavaDoc language)
258    {
259       Person person = ((Person) getPerson(key));
260       List languages = person.getLanguages();
261       if (languages == null) {
262          person.setLanguages(new ArrayList());
263          languages = person.getLanguages();
264       }
265       languages.add(language);
266    }
267
268    public void removeLanguage(String JavaDoc key, Object JavaDoc language)
269    {
270       List languages = ((Person) getPerson(key)).getLanguages();
271       if (languages == null) return;
272       languages.remove(language);
273    }
274
275    public int getLanguagesSize(String JavaDoc key)
276    {
277       List languages = ((Person) getPerson(key)).getLanguages();
278       return languages == null ? 0 : languages.size();
279    }
280
281    public Set getSkills(String JavaDoc key)
282    {
283       return new HashSet(((Person) getPerson(key)).getSkills());
284    }
285
286    public void addSkill(String JavaDoc key, String JavaDoc skill)
287    {
288       Person person = ((Person) getPerson(key));
289       Set skills = person.getSkills();
290       if (skills == null) {
291          person.setSkills(new HashSet());
292          skills = person.getSkills();
293       }
294       skills.add(skill);
295    }
296
297    public void removeSkill(String JavaDoc key, String JavaDoc skill)
298    {
299       Person person = ((Person) getPerson(key));
300       Set skills = person.getSkills();
301       if (skills != null) {
302          skills.remove(skill);
303       }
304    }
305
306    public Object JavaDoc testSerialization()
307    {
308       try {
309          Person p = new Person();
310          /*
311          if (!(p instanceof Externalizable)) {
312         throw new RuntimeException("p not Externalizable");
313          }
314          */

315          p.setName("Joe Black");
316          Address address = new Address();
317          address.setCity("Mannheim");
318          p.setAddress(address);
319          cache.putObject("/person/joe", p);
320          return (Person) cache.getObject("/person/joe");
321       } catch (Throwable JavaDoc t) {
322          throw new RuntimeException JavaDoc(t);
323       }
324    }
325
326    public void testDeserialization(String JavaDoc key, Object JavaDoc value)
327    {
328       try {
329          cache.putObject(key, value);
330       } catch (Throwable JavaDoc t) {
331          throw new RuntimeException JavaDoc(t);
332       }
333    }
334
335    public void printPerson(String JavaDoc key)
336    {
337       System.out.println(getPerson(key));
338    }
339
340    public void printCache()
341    {
342       System.out.println(cache.toString());
343    }
344
345    public String JavaDoc printCacheDetails()
346    {
347       return cache.printDetails();
348    }
349
350    public Object JavaDoc getFieldValue(String JavaDoc key, String JavaDoc name)
351    {
352       try {
353          Object JavaDoc object = cache.getObject(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    public TreeCacheAop getCache()
363    {
364       return cache;
365    }
366
367 }
368
369
Popular Tags