001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.cache.bean;
023:
024: import org.jboss.aop.Advised;
025: import org.jboss.cache.aop.TreeCacheAop;
026: import org.jboss.logging.Logger;
027: import org.jboss.test.cache.test.standAloneAop.Address;
028: import org.jboss.test.cache.test.standAloneAop.Person;
029: import org.jboss.util.NestedRuntimeException;
030:
031: import javax.ejb.CreateException;
032: import javax.ejb.EJBException;
033: import javax.ejb.SessionBean;
034: import javax.ejb.SessionContext;
035: import java.lang.reflect.Field;
036: import java.rmi.RemoteException;
037: import java.util.*;
038:
039: /**
040: * Proxy to the TreeCacheAop MBean.
041: * The AOP framework requires that classes are loaded by special classloaders (e.g UCL).
042: * This bean is used to execute tests within the server.
043: *
044: * @author <a href="mailto:harald@gliebe.de">Harald Gliebe</a>
045: * @version $Revision: 57211 $
046: * @ejb.bean type="Stateful"
047: * name="test/TreeCacheAopTester"
048: * jndi-name="test/TreeCacheAopTester"
049: * view-type="remote"
050: * @ejb.transaction type="Supports"
051: */
052:
053: public class TreeCacheAopTesterBean implements SessionBean {
054:
055: SessionContext ctx;
056: TreeCacheAop cache;
057: TreeCacheAop cache2;
058:
059: Logger logger_ = Logger.getLogger(TreeCacheAopTesterBean.class);
060:
061: public void ejbActivate() throws EJBException, RemoteException {
062: }
063:
064: public void ejbPassivate() throws EJBException, RemoteException {
065: }
066:
067: public void ejbRemove() throws EJBException, RemoteException {
068: }
069:
070: public void setSessionContext(SessionContext ctx)
071: throws EJBException {
072: this .ctx = ctx;
073: }
074:
075: /**
076: * @ejb.create-method
077: */
078: public void ejbCreate(String cluster_name, String props,
079: int caching_mode) throws CreateException {
080: try {
081: cache = new TreeCacheAop(cluster_name, props, 10000);
082: cache.startService();
083: cache2 = new TreeCacheAop(cluster_name, props, 10000);
084: cache2.startService();
085: } catch (Exception e) {
086: throw new CreateException(e.toString());
087: }
088: }
089:
090: /**
091: * @ejb.interface-method
092: */
093: public void testSetup() {
094: Person p = new Person();
095: if (!(p instanceof Advised)) {
096: logger_
097: .error("testSetup(): p is not an instance of Advised");
098: throw new RuntimeException("Person must be advised!");
099: }
100: Address a = new Address();
101: if (!(a instanceof Advised)) {
102: logger_
103: .error("testSetup(): a is not an instance of Advised");
104: throw new RuntimeException("Address must be advised!");
105: }
106: }
107:
108: /**
109: * @ejb.interface-method
110: */
111: public void createPerson(String key, String name, int age) {
112: Person p = new Person();
113: p.setName(name);
114: p.setAge(age);
115: p.setAddress(new Address());
116: try {
117: cache.putObject(key, p);
118: } catch (Exception e) {
119: throw new RuntimeException(e);
120: }
121: }
122:
123: /**
124: * @ejb.interface-method
125: */
126: public void removePerson(String key) {
127: try {
128: cache.removeObject(key);
129: } catch (Exception e) {
130: throw new RuntimeException(e);
131: }
132: }
133:
134: Object getPerson(String key) {
135: try {
136: return (Person) cache.getObject(key);
137: } catch (Exception e) {
138: throw new RuntimeException(e);
139: }
140: }
141:
142: /**
143: * @ejb.interface-method
144: */
145: public void setName(String key, String name) {
146: ((Person) getPerson(key)).setName(name);
147: }
148:
149: /**
150: * @ejb.interface-method
151: */
152: public String getName(String key) {
153: return ((Person) getPerson(key)).getName();
154: }
155:
156: /**
157: * @ejb.interface-method
158: */
159: public void setAge(String key, int age) {
160: ((Person) getPerson(key)).setAge(age);
161: }
162:
163: /**
164: * @ejb.interface-method
165: */
166: public int getAge(String key) {
167: return ((Person) getPerson(key)).getAge();
168: }
169:
170: /**
171: * @ejb.interface-method
172: */
173: public void setStreet(String key, String street) {
174: ((Person) getPerson(key)).getAddress().setStreet(street);
175: }
176:
177: /**
178: * @ejb.interface-method
179: */
180: public String getStreet(String key) {
181: return ((Person) getPerson(key)).getAddress().getStreet();
182: }
183:
184: /**
185: * @ejb.interface-method
186: */
187: public void setCity(String key, String city) {
188: ((Person) getPerson(key)).getAddress().setCity(city);
189: }
190:
191: /**
192: * @ejb.interface-method
193: */
194: public String getCity(String key) {
195: return ((Person) getPerson(key)).getAddress().getCity();
196: }
197:
198: /**
199: * @ejb.interface-method
200: */
201: public void setZip(String key, int zip) {
202: ((Person) getPerson(key)).getAddress().setZip(zip);
203: }
204:
205: /**
206: * @ejb.interface-method
207: */
208: public int getZip(String key) {
209: return ((Person) getPerson(key)).getAddress().getZip();
210: }
211:
212: // Map operations
213:
214: /**
215: * @ejb.interface-method
216: */
217: public Object getHobby(String key, Object hobbyKey) {
218: Map hobbies = ((Person) getPerson(key)).getHobbies();
219: return hobbies == null ? null : hobbies.get(hobbyKey);
220: }
221:
222: /**
223: * @ejb.interface-method
224: */
225: public void setHobby(String key, Object hobbyKey, Object value) {
226: Person person = ((Person) getPerson(key));
227: Map hobbies = person.getHobbies();
228: if (hobbies == null) {
229: hobbies = new HashMap();
230: person.setHobbies(hobbies);
231: // NB: it is neccessary to get hobbies again to get advised version
232: hobbies = person.getHobbies();
233: }
234: hobbies.put(hobbyKey, value);
235: }
236:
237: // List operations
238:
239: /**
240: * @ejb.interface-method
241: */
242: public Object getLanguage(String key, int index) {
243: List languages = ((Person) getPerson(key)).getLanguages();
244: return languages == null ? null : languages.get(index);
245: }
246:
247: /**
248: * @ejb.interface-method
249: */
250: public void addLanguage(String key, Object language) {
251: Person person = ((Person) getPerson(key));
252: List languages = person.getLanguages();
253: if (languages == null) {
254: person.setLanguages(new ArrayList());
255: languages = person.getLanguages();
256: }
257: languages.add(language);
258: }
259:
260: /**
261: * @ejb.interface-method
262: */
263: public void removeLanguage(String key, Object language) {
264: List languages = ((Person) getPerson(key)).getLanguages();
265: if (languages == null)
266: return;
267: languages.remove(language);
268: }
269:
270: /**
271: * @ejb.interface-method
272: */
273: public int getLanguagesSize(String key) {
274: List languages = ((Person) getPerson(key)).getLanguages();
275: return languages == null ? 0 : languages.size();
276: }
277:
278: /**
279: * @ejb.interface-method
280: */
281: public Set getSkills(String key) {
282: return new HashSet(((Person) getPerson(key)).getSkills());
283: }
284:
285: /**
286: * @ejb.interface-method
287: */
288: public void addSkill(String key, String skill) {
289: Person person = ((Person) getPerson(key));
290: Set skills = person.getSkills();
291: if (skills == null) {
292: person.setSkills(new HashSet());
293: skills = person.getSkills();
294: }
295: skills.add(skill);
296: }
297:
298: /**
299: * @ejb.interface-method
300: */
301: public void removeSkill(String key, String skill) {
302: Person person = ((Person) getPerson(key));
303: Set skills = person.getSkills();
304: if (skills != null) {
305: skills.remove(skill);
306: }
307: }
308:
309: /**
310: * @ejb.interface-method
311: */
312: public Object testSerialization() {
313: try {
314: Person p = new Person();
315: /*
316: if (!(p instanceof Externalizable)) {
317: throw new RuntimeException("p not Externalizable");
318: }
319: */
320: p.setName("Harald Gliebe");
321: Address address = new Address();
322: address.setCity("Mannheim");
323: p.setAddress(address);
324: cache.putObject("/person/harald", p);
325: return (Person) cache.getObject("/person/harald");
326: } catch (Throwable t) {
327: throw new RuntimeException(t);
328: }
329: }
330:
331: /**
332: * @ejb.interface-method
333: */
334: public void testDeserialization(String key, Object value) {
335: try {
336: cache.putObject(key, value);
337: } catch (Throwable t) {
338: throw new RuntimeException(t);
339: }
340: }
341:
342: /**
343: * @ejb.interface-method
344: */
345: public void printPerson(String key) {
346: System.out.println(getPerson(key));
347: }
348:
349: /**
350: * @ejb.interface-method
351: */
352: public void printCache() {
353: System.out.println(cache);
354: }
355:
356: /**
357: * @ejb.interface-method
358: */
359: public Object getFieldValue(String key, String name) {
360: try {
361: Object object = cache.getObject(key);
362: Field f = object.getClass().getDeclaredField(name);
363: f.setAccessible(true);
364: return f.get(object);
365: } catch (Exception e) {
366: throw new NestedRuntimeException(e);
367: }
368: }
369:
370: }
|