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.cache.aop.TreeCacheAop;
025: import org.jboss.logging.Logger;
026: import org.jboss.mx.util.MBeanServerLocator;
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 javax.management.MBeanServer;
036: import javax.management.ObjectName;
037: import java.lang.reflect.Field;
038: import java.rmi.RemoteException;
039: import java.util.*;
040:
041: /**
042: * Proxy to the TreeCacheAop MBean.
043: * The AOP framework requires that classes are loaded by special classloaders (e.g UCL).
044: * This bean is used to execute tests within the server.
045: *
046: * @author Ben Wang
047: * @version $Revision: 57211 $
048: * @ejb.bean type="Stateful"
049: * name="test/TreeCacheAopMBeanTester"
050: * jndi-name="ejb/test/TreeCacheAopMBeanTester"
051: * view-type="remote"
052: * @ejb.transaction type="Supports"
053: */
054:
055: public class TreeCacheAopMBeanTesterBean implements SessionBean {
056: static final String OBJECT_NAME = "jboss.cache:service=testTreeCacheAop";
057: MBeanServer server;
058: ObjectName cacheService;
059:
060: SessionContext ctx;
061: TreeCacheAop cache;
062:
063: Logger logger_ = Logger
064: .getLogger(TreeCacheAopMBeanTesterBean.class);
065:
066: public void ejbActivate() throws EJBException, RemoteException {
067: }
068:
069: public void ejbPassivate() throws EJBException, RemoteException {
070: }
071:
072: public void ejbRemove() throws EJBException, RemoteException {
073: }
074:
075: public void setSessionContext(SessionContext ctx)
076: throws EJBException {
077: this .ctx = ctx;
078: }
079:
080: /**
081: * @ejb.create-method
082: */
083: public void ejbCreate() throws CreateException {
084: init();
085: }
086:
087: private void init() throws CreateException {
088: init(OBJECT_NAME);
089: }
090:
091: private void init(String name) throws CreateException {
092: try {
093: cacheService = new ObjectName(name);
094: server = MBeanServerLocator.locate();
095: } catch (Exception ex) {
096: throw new CreateException(ex.toString());
097: }
098: }
099:
100: /**
101: * @ejb.interface-method
102: */
103: public void createPerson(String key, String name, int age)
104: throws Exception {
105: Person p = new Person();
106: p.setName(name);
107: p.setAge(age);
108: p.setAddress(new Address());
109: server.invoke(cacheService, "putObject",
110: new Object[] { key, p },
111: new String[] { String.class.getName(),
112: Object.class.getName() });
113: }
114:
115: /**
116: * @ejb.interface-method
117: */
118: public void removePerson(String key) throws Exception {
119: server.invoke(cacheService, "removeObject",
120: new Object[] { key }, new String[] { String.class
121: .getName() });
122: }
123:
124: Object getPerson(String key) throws Exception {
125: return server.invoke(cacheService, "getObject",
126: new Object[] { key }, new String[] { String.class
127: .getName() });
128: }
129:
130: /**
131: * @ejb.interface-method
132: */
133: public void setName(String key, String name) throws Exception {
134: ((Person) getPerson(key)).setName(name);
135: }
136:
137: /**
138: * @ejb.interface-method
139: */
140: public String getName(String key) throws Exception {
141: return ((Person) getPerson(key)).getName();
142: }
143:
144: /**
145: * @ejb.interface-method
146: */
147: public void setAge(String key, int age) throws Exception {
148: ((Person) getPerson(key)).setAge(age);
149: }
150:
151: /**
152: * @ejb.interface-method
153: */
154: public int getAge(String key) throws Exception {
155: return ((Person) getPerson(key)).getAge();
156: }
157:
158: /**
159: * @ejb.interface-method
160: */
161: public void setStreet(String key, String street) throws Exception {
162: ((Person) getPerson(key)).getAddress().setStreet(street);
163: }
164:
165: /**
166: * @ejb.interface-method
167: */
168: public String getStreet(String key) throws Exception {
169: return ((Person) getPerson(key)).getAddress().getStreet();
170: }
171:
172: /**
173: * @ejb.interface-method
174: */
175: public void setCity(String key, String city) throws Exception {
176: ((Person) getPerson(key)).getAddress().setCity(city);
177: }
178:
179: /**
180: * @ejb.interface-method
181: */
182: public String getCity(String key) throws Exception {
183: return ((Person) getPerson(key)).getAddress().getCity();
184: }
185:
186: /**
187: * @ejb.interface-method
188: */
189: public void setZip(String key, int zip) throws Exception {
190: ((Person) getPerson(key)).getAddress().setZip(zip);
191: }
192:
193: /**
194: * @ejb.interface-method
195: */
196: public int getZip(String key) throws Exception {
197: return ((Person) getPerson(key)).getAddress().getZip();
198: }
199:
200: // Map operations
201:
202: /**
203: * @ejb.interface-method
204: */
205: public Object getHobby(String key, Object hobbyKey)
206: throws Exception {
207: Map hobbies = ((Person) getPerson(key)).getHobbies();
208: return hobbies == null ? null : hobbies.get(hobbyKey);
209: }
210:
211: /**
212: * @ejb.interface-method
213: */
214: public void setHobby(String key, Object hobbyKey, Object value)
215: throws Exception {
216: Person person = ((Person) getPerson(key));
217: Map hobbies = person.getHobbies();
218: if (hobbies == null) {
219: hobbies = new HashMap();
220: person.setHobbies(hobbies);
221: // NB: it is neccessary to get hobbies again to get advised version
222: hobbies = person.getHobbies();
223: }
224: hobbies.put(hobbyKey, value);
225: }
226:
227: // List operations
228:
229: /**
230: * @ejb.interface-method
231: */
232: public Object getLanguage(String key, int index) throws Exception {
233: List languages = ((Person) getPerson(key)).getLanguages();
234: return languages == null ? null : languages.get(index);
235: }
236:
237: /**
238: * @ejb.interface-method
239: */
240: public void addLanguage(String key, Object language)
241: throws Exception {
242: Person person = ((Person) getPerson(key));
243: List languages = person.getLanguages();
244: if (languages == null) {
245: person.setLanguages(new ArrayList());
246: languages = person.getLanguages();
247: }
248: languages.add(language);
249: }
250:
251: /**
252: * @ejb.interface-method
253: */
254: public void removeLanguage(String key, Object language)
255: throws Exception {
256: List languages = ((Person) getPerson(key)).getLanguages();
257: if (languages == null)
258: return;
259: languages.remove(language);
260: }
261:
262: /**
263: * @ejb.interface-method
264: */
265: public int getLanguagesSize(String key) throws Exception {
266: List languages = ((Person) getPerson(key)).getLanguages();
267: return languages == null ? 0 : languages.size();
268: }
269:
270: /**
271: * @ejb.interface-method
272: */
273: public Set getSkills(String key) throws Exception {
274: return new HashSet(((Person) getPerson(key)).getSkills());
275: }
276:
277: /**
278: * @ejb.interface-method
279: */
280: public void addSkill(String key, String skill) throws Exception {
281: Person person = ((Person) getPerson(key));
282: Set skills = person.getSkills();
283: if (skills == null) {
284: person.setSkills(new HashSet());
285: skills = person.getSkills();
286: }
287: skills.add(skill);
288: }
289:
290: /**
291: * @ejb.interface-method
292: */
293: public void removeSkill(String key, String skill) throws Exception {
294: Person person = ((Person) getPerson(key));
295: Set skills = person.getSkills();
296: if (skills != null) {
297: skills.remove(skill);
298: }
299: }
300:
301: /**
302: * @ejb.interface-method
303: */
304: public void printPerson(String key) throws Exception {
305: System.out.println(getPerson(key));
306: }
307:
308: /**
309: * @ejb.interface-method
310: */
311: public void printCache() {
312: System.out.println(cache);
313: }
314:
315: /**
316: * @ejb.interface-method
317: */
318: public Object getFieldValue(String key, String name) {
319: try {
320: Object object = cache.getObject(key);
321: Field f = object.getClass().getDeclaredField(name);
322: f.setAccessible(true);
323: return f.get(object);
324: } catch (Exception e) {
325: throw new NestedRuntimeException(e);
326: }
327: }
328:
329: }
|