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 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 60 61 public class TreeCacheAopTesterBean implements SessionBean 62 { 63 64 SessionContext ctx; 65 PojoCache cache; 66 PojoCache cache2; 67 68 Logger logger_ = Logger.getLogger(TreeCacheAopTesterBean.class); 69 70 public void ejbActivate() throws EJBException , RemoteException 71 { 72 } 73 74 public void ejbPassivate() throws EJBException , RemoteException 75 { 76 } 77 78 public void ejbRemove() throws EJBException , RemoteException 79 { 80 } 81 82 public void setSessionContext(SessionContext ctx) throws EJBException 83 { 84 this.ctx = ctx; 85 } 86 87 90 public void ejbCreate(String cluster_name, String props, int caching_mode) throws CreateException 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 e) { 102 throw new CreateException (e.toString()); 103 } 104 } 105 106 107 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 ("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 ("Address must be advised!"); 121 } 122 } 123 124 127 public void createPerson(String key, String 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 e) { 136 throw new RuntimeException (e); 137 } 138 } 139 140 143 public void removePerson(String key) 144 { 145 try { 146 cache.detach(key); 147 } catch (Exception e) { 148 throw new RuntimeException (e); 149 } 150 } 151 152 153 Object getPerson(String key) 154 { 155 try { 156 return (Person) cache.find(key); 157 } catch (Exception e) { 158 throw new RuntimeException (e); 159 } 160 } 161 162 165 public void setName(String key, String name) 166 { 167 ((Person) getPerson(key)).setName(name); 168 } 169 170 173 public String getName(String key) 174 { 175 return ((Person) getPerson(key)).getName(); 176 } 177 178 181 public void setAge(String key, int age) 182 { 183 ((Person) getPerson(key)).setAge(age); 184 } 185 186 189 public int getAge(String key) 190 { 191 return ((Person) getPerson(key)).getAge(); 192 } 193 194 197 public void setStreet(String key, String street) 198 { 199 ((Person) getPerson(key)).getAddress().setStreet(street); 200 } 201 202 205 public String getStreet(String key) 206 { 207 return ((Person) getPerson(key)).getAddress().getStreet(); 208 } 209 210 213 public void setCity(String key, String city) 214 { 215 ((Person) getPerson(key)).getAddress().setCity(city); 216 } 217 218 221 public String getCity(String key) 222 { 223 return ((Person) getPerson(key)).getAddress().getCity(); 224 } 225 226 229 public void setZip(String key, int zip) 230 { 231 ((Person) getPerson(key)).getAddress().setZip(zip); 232 } 233 234 237 public int getZip(String key) 238 { 239 return ((Person) getPerson(key)).getAddress().getZip(); 240 } 241 242 244 247 public Object getHobby(String key, Object hobbyKey) 248 { 249 Map hobbies = ((Person) getPerson(key)).getHobbies(); 250 return hobbies == null ? null : hobbies.get(hobbyKey); 251 } 252 253 256 public void setHobby(String key, Object hobbyKey, Object value) 257 { 258 Person person = ((Person) getPerson(key)); 259 Map hobbies = person.getHobbies(); 260 if (hobbies == null) { 261 hobbies = new HashMap (); 262 person.setHobbies(hobbies); 263 hobbies = person.getHobbies(); 265 } 266 hobbies.put(hobbyKey, value); 267 } 268 269 271 274 public Object getLanguage(String key, int index) 275 { 276 List languages = ((Person) getPerson(key)).getLanguages(); 277 return languages == null ? null : languages.get(index); 278 } 279 280 283 public void addLanguage(String key, Object language) 284 { 285 Person person = ((Person) getPerson(key)); 286 List languages = person.getLanguages(); 287 if (languages == null) { 288 person.setLanguages(new ArrayList ()); 289 languages = person.getLanguages(); 290 } 291 languages.add(language); 292 } 293 294 297 public void removeLanguage(String key, Object language) 298 { 299 List languages = ((Person) getPerson(key)).getLanguages(); 300 if (languages == null) return; 301 languages.remove(language); 302 } 303 304 307 public int getLanguagesSize(String key) 308 { 309 List languages = ((Person) getPerson(key)).getLanguages(); 310 return languages == null ? 0 : languages.size(); 311 } 312 313 316 public Set getSkills(String key) 317 { 318 return new HashSet (((Person) getPerson(key)).getSkills()); 319 } 320 321 324 public void addSkill(String key, String skill) 325 { 326 Person person = ((Person) getPerson(key)); 327 Set skills = person.getSkills(); 328 if (skills == null) { 329 person.setSkills(new HashSet ()); 330 skills = person.getSkills(); 331 } 332 skills.add(skill); 333 } 334 335 338 public void removeSkill(String key, String skill) 339 { 340 Person person = ((Person) getPerson(key)); 341 Set skills = person.getSkills(); 342 if (skills != null) { 343 skills.remove(skill); 344 } 345 } 346 347 350 public Object testSerialization() 351 { 352 try { 353 Person p = new Person(); 354 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 t) { 366 throw new RuntimeException (t); 367 } 368 } 369 370 373 public void testDeserialization(String key, Object value) 374 { 375 try { 376 cache.attach(key, value); 377 } catch (Throwable t) { 378 throw new RuntimeException (t); 379 } 380 } 381 382 385 public void printPerson(String key) 386 { 387 System.out.println(getPerson(key)); 388 } 389 390 393 public void printCache() 394 { 395 System.out.println(cache); 396 } 397 398 401 public Object getFieldValue(String key, String name) 402 { 403 try { 404 Object object = cache.find(key); 405 Field f = object.getClass().getDeclaredField(name); 406 f.setAccessible(true); 407 return f.get(object); 408 } catch (Exception e) { 409 throw new NestedRuntimeException(e); 410 } 411 } 412 413 } 414 415 | Popular Tags |