1 22 package org.jboss.test.cache.bean; 23 24 import java.lang.reflect.Field ; 25 import java.rmi.RemoteException ; 26 import java.util.ArrayList ; 27 import java.util.HashMap ; 28 import java.util.HashSet ; 29 import java.util.List ; 30 import java.util.Map ; 31 import java.util.Set ; 32 33 import javax.ejb.CreateException ; 34 import javax.ejb.EJBException ; 35 import javax.ejb.SessionBean ; 36 import javax.ejb.SessionContext ; 37 import javax.management.MBeanServer ; 38 import javax.management.ObjectName ; 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 60 61 public class TreeCacheAopMBeanTesterBean implements SessionBean 62 { 63 static final String OBJECT_NAME = "jboss.cache:service=testTreeCacheAop"; 64 MBeanServer server; 65 ObjectName cacheService; 66 67 SessionContext ctx; 68 PojoCache cache; 69 70 Logger logger_ = Logger.getLogger(TreeCacheAopMBeanTesterBean.class); 71 72 public void ejbActivate() throws EJBException , RemoteException 73 { 74 } 75 76 public void ejbPassivate() throws EJBException , RemoteException 77 { 78 } 79 80 public void ejbRemove() throws EJBException , RemoteException 81 { 82 } 83 84 public void setSessionContext(SessionContext ctx) throws EJBException 85 { 86 this.ctx = ctx; 87 } 88 89 92 public void ejbCreate() throws CreateException 93 { 94 init(); 95 } 96 97 private void init() throws CreateException 98 { 99 init(OBJECT_NAME); 100 } 101 102 private void init(String name) throws CreateException 103 { 104 try { 105 cacheService = new ObjectName (name); 106 server = MBeanServerLocator.locate(); 107 } catch (Exception ex) { 108 throw new CreateException (ex.toString()); 109 } 110 } 111 112 115 public void createPerson(String key, String name, int age) throws Exception 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 []{key, p}, 123 new String []{String .class.getName(), 124 Object .class.getName()}); 125 } 126 127 130 public void removePerson(String key) throws Exception 131 { 132 server.invoke(cacheService, "removeObject", 133 new Object []{key}, 134 new String []{String .class.getName()}); 135 } 136 137 138 Object getPerson(String key) throws Exception 139 { 140 return server.invoke(cacheService, "getObject", 141 new Object []{key}, 142 new String []{String .class.getName()}); 143 } 144 145 148 public void setName(String key, String name) throws Exception 149 { 150 ((Person) getPerson(key)).setName(name); 151 } 152 153 156 public String getName(String key) throws Exception 157 { 158 return ((Person) getPerson(key)).getName(); 159 } 160 161 164 public void setAge(String key, int age) throws Exception 165 { 166 ((Person) getPerson(key)).setAge(age); 167 } 168 169 172 public int getAge(String key) throws Exception 173 { 174 return ((Person) getPerson(key)).getAge(); 175 } 176 177 180 public void setStreet(String key, String street) throws Exception 181 { 182 ((Person) getPerson(key)).getAddress().setStreet(street); 183 } 184 185 188 public String getStreet(String key) throws Exception 189 { 190 return ((Person) getPerson(key)).getAddress().getStreet(); 191 } 192 193 196 public void setCity(String key, String city) throws Exception 197 { 198 ((Person) getPerson(key)).getAddress().setCity(city); 199 } 200 201 204 public String getCity(String key) throws Exception 205 { 206 return ((Person) getPerson(key)).getAddress().getCity(); 207 } 208 209 212 public void setZip(String key, int zip) throws Exception 213 { 214 ((Person) getPerson(key)).getAddress().setZip(zip); 215 } 216 217 220 public int getZip(String key) throws Exception 221 { 222 return ((Person) getPerson(key)).getAddress().getZip(); 223 } 224 225 227 230 public Object getHobby(String key, Object hobbyKey) throws Exception 231 { 232 Map hobbies = ((Person) getPerson(key)).getHobbies(); 233 return hobbies == null ? null : hobbies.get(hobbyKey); 234 } 235 236 239 public void setHobby(String key, Object hobbyKey, Object value) throws Exception 240 { 241 Person person = ((Person) getPerson(key)); 242 Map hobbies = person.getHobbies(); 243 if (hobbies == null) { 244 hobbies = new HashMap (); 245 person.setHobbies(hobbies); 246 hobbies = person.getHobbies(); 248 } 249 hobbies.put(hobbyKey, value); 250 } 251 252 254 257 public Object getLanguage(String key, int index) throws Exception 258 { 259 List languages = ((Person) getPerson(key)).getLanguages(); 260 return languages == null ? null : languages.get(index); 261 } 262 263 266 public void addLanguage(String key, Object language) throws Exception 267 { 268 Person person = ((Person) getPerson(key)); 269 List languages = person.getLanguages(); 270 if (languages == null) { 271 person.setLanguages(new ArrayList ()); 272 languages = person.getLanguages(); 273 } 274 languages.add(language); 275 } 276 277 280 public void removeLanguage(String key, Object language) throws Exception 281 { 282 List languages = ((Person) getPerson(key)).getLanguages(); 283 if (languages == null) return; 284 languages.remove(language); 285 } 286 287 290 public int getLanguagesSize(String key) throws Exception 291 { 292 List languages = ((Person) getPerson(key)).getLanguages(); 293 return languages == null ? 0 : languages.size(); 294 } 295 296 299 public Set getSkills(String key) throws Exception 300 { 301 return new HashSet (((Person) getPerson(key)).getSkills()); 302 } 303 304 307 public void addSkill(String key, String skill) throws Exception 308 { 309 Person person = ((Person) getPerson(key)); 310 Set skills = person.getSkills(); 311 if (skills == null) { 312 person.setSkills(new HashSet ()); 313 skills = person.getSkills(); 314 } 315 skills.add(skill); 316 } 317 318 321 public void removeSkill(String key, String skill) throws Exception 322 { 323 Person person = ((Person) getPerson(key)); 324 Set skills = person.getSkills(); 325 if (skills != null) { 326 skills.remove(skill); 327 } 328 } 329 330 331 334 public void printPerson(String key) throws Exception 335 { 336 System.out.println(getPerson(key)); 337 } 338 339 342 public void printCache() 343 { 344 System.out.println(cache); 345 } 346 347 350 public Object getFieldValue(String key, String name) 351 { 352 try { 353 Object object = cache.find(key); 354 Field f = object.getClass().getDeclaredField(name); 355 f.setAccessible(true); 356 return f.get(object); 357 } catch (Exception e) { 358 throw new NestedRuntimeException(e); 359 } 360 } 361 362 } 363 364 | Popular Tags |