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.aop.bean;
023:
024: import org.jboss.aspects.versioned.DistributedTxCache;
025: import org.jboss.logging.Logger;
026: import org.jboss.system.ServiceMBeanSupport;
027: import org.jboss.util.id.GUID;
028:
029: import javax.management.MBeanRegistration;
030: import javax.management.MBeanServer;
031: import javax.management.ObjectName;
032: import java.util.ArrayList;
033: import java.util.Iterator;
034: import java.util.List;
035:
036: /**
037: *
038: * @see Monitorable
039: * @author <a href="mailto:bill@jboss.org">Bill Burke</a>
040: * @version $Revision: 57211 $
041: */
042: public class CacheTester extends ServiceMBeanSupport implements
043: CacheTesterMBean, MBeanRegistration {
044: // Constants ----------------------------------------------------
045: // Attributes ---------------------------------------------------
046: static Logger log = Logger.getLogger(CacheTester.class);
047: MBeanServer m_mbeanServer;
048: DistributedTxCache cache;
049: GUID vmid = new GUID();
050:
051: // Static -------------------------------------------------------
052:
053: // Constructors -------------------------------------------------
054: public CacheTester() {
055: }
056:
057: // Public -------------------------------------------------------
058:
059: // MBeanRegistration implementation -----------------------------------
060: public ObjectName preRegister(MBeanServer server, ObjectName name)
061: throws Exception {
062: m_mbeanServer = server;
063: return name;
064: }
065:
066: public void postRegister(Boolean registrationDone) {
067: }
068:
069: public void preDeregister() throws Exception {
070: }
071:
072: public void postDeregister() {
073: }
074:
075: protected void createService() throws Exception {
076: cache = new DistributedTxCache(10, 5000, "Test");
077: cache.create();
078: }
079:
080: protected void startService() throws Exception {
081: cache.start();
082: Thread.sleep(5000);
083: Person person = (Person) cache.get("Bill");
084: if (person != null) {
085: log.info("Bill found in cache, no need to create");
086: log.info(person.getName() + " is " + person.getAge()
087: + " years old");
088: log.info("lives at : " + person.getAddress().getStreet());
089: log.info(person.getAddress().getCity() + ", "
090: + person.getAddress().getState());
091: log.info("hobbies: ");
092: Iterator it = person.getHobbies().iterator();
093: while (it.hasNext()) {
094: log.info(it.next());
095: }
096: } else {
097: log.info("inserting stuff");
098: Address address = new Address("Marlborough Street",
099: "Boston", "MA");
100: person = new Person("Bill", 32, address);
101: person.addHobby("Football");
102: person.addHobby("Basketball");
103: cache.insert("Bill", person);
104: }
105: }
106:
107: protected void stopService() {
108: }
109:
110: public String getVMID() {
111: return vmid.toString();
112: }
113:
114: public int getAge(String key) {
115: Person person = (Person) cache.get(key);
116: return person.getAge();
117: }
118:
119: public void setAge(String key, int value) {
120: Person person = (Person) cache.get(key);
121: person.setAge(value);
122: }
123:
124: public List getHobbies(String key) {
125: Person person = (Person) cache.get(key);
126: return new ArrayList(person.getHobbies());
127: }
128:
129: public void addHobby(String key, String hobby) {
130: Person person = (Person) cache.get(key);
131: person.addHobby(hobby);
132: }
133:
134: public String getCity(String key) {
135: Person person = (Person) cache.get(key);
136: return person.getAddress().getCity();
137: }
138:
139: public void setCity(String key, String city) {
140: Person person = (Person) cache.get(key);
141: person.getAddress().setCity(city);
142: }
143:
144: // Inner classes -------------------------------------------------
145: }
|