KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > aop > bean > CacheTester


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.aop.bean;
23
24 import org.jboss.aspects.versioned.DistributedTxCache;
25 import org.jboss.logging.Logger;
26 import org.jboss.system.ServiceMBeanSupport;
27 import org.jboss.util.id.GUID;
28
29 import javax.management.MBeanRegistration JavaDoc;
30 import javax.management.MBeanServer JavaDoc;
31 import javax.management.ObjectName JavaDoc;
32 import java.util.ArrayList JavaDoc;
33 import java.util.Iterator JavaDoc;
34 import java.util.List JavaDoc;
35 /**
36  *
37  * @see Monitorable
38  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
39  * @version $Revision: 37406 $
40  */

41 public class CacheTester
42    extends ServiceMBeanSupport
43    implements CacheTesterMBean, MBeanRegistration JavaDoc
44 {
45    // Constants ----------------------------------------------------
46
// Attributes ---------------------------------------------------
47
static Logger log = Logger.getLogger(CacheTester.class);
48    MBeanServer JavaDoc m_mbeanServer;
49    DistributedTxCache cache;
50    GUID vmid = new GUID();
51    // Static -------------------------------------------------------
52

53    // Constructors -------------------------------------------------
54
public CacheTester()
55    {}
56    
57    // Public -------------------------------------------------------
58

59    // MBeanRegistration implementation -----------------------------------
60
public ObjectName JavaDoc preRegister(MBeanServer JavaDoc server, ObjectName JavaDoc name)
61    throws Exception JavaDoc
62    {
63       m_mbeanServer = server;
64       return name;
65    }
66    
67    public void postRegister(Boolean JavaDoc registrationDone)
68    {}
69    public void preDeregister() throws Exception JavaDoc
70    {}
71    public void postDeregister()
72    {}
73
74    protected void createService()
75       throws Exception JavaDoc
76    {
77       cache = new DistributedTxCache(10, 5000, "Test");
78       cache.create();
79    }
80
81    protected void startService()
82       throws Exception JavaDoc
83    {
84       cache.start();
85       Thread.sleep(5000);
86       Person person = (Person)cache.get("Bill");
87       if (person != null)
88       {
89          log.info("Bill found in cache, no need to create");
90          log.info(person.getName() + " is " + person.getAge() + " years old");
91          log.info("lives at : " + person.getAddress().getStreet());
92          log.info(person.getAddress().getCity() + ", " + person.getAddress().getState());
93          log.info("hobbies: ");
94          Iterator JavaDoc it = person.getHobbies().iterator();
95          while (it.hasNext())
96          {
97             log.info(it.next());
98          }
99       }
100       else
101       {
102          log.info("inserting stuff");
103          Address address = new Address("Marlborough Street", "Boston", "MA");
104          person = new Person("Bill", 32, address);
105          person.addHobby("Football");
106          person.addHobby("Basketball");
107          cache.insert("Bill", person);
108       }
109    }
110
111    protected void stopService() { }
112
113    public String JavaDoc getVMID()
114    {
115       return vmid.toString();
116    }
117
118    public int getAge(String JavaDoc key)
119    {
120       Person person = (Person)cache.get(key);
121       return person.getAge();
122    }
123
124    public void setAge(String JavaDoc key, int value)
125    {
126       Person person = (Person)cache.get(key);
127       person.setAge(value);
128    }
129
130    public List JavaDoc getHobbies(String JavaDoc key)
131    {
132       Person person = (Person)cache.get(key);
133       return new ArrayList JavaDoc(person.getHobbies());
134    }
135
136    public void addHobby(String JavaDoc key, String JavaDoc hobby)
137    {
138       Person person = (Person)cache.get(key);
139       person.addHobby(hobby);
140    }
141
142    public String JavaDoc getCity(String JavaDoc key)
143    {
144       Person person = (Person)cache.get(key);
145       return person.getAddress().getCity();
146    }
147
148    public void setCity(String JavaDoc key, String JavaDoc city)
149    {
150       Person person = (Person)cache.get(key);
151       person.getAddress().setCity(city);
152    }
153
154
155    // Inner classes -------------------------------------------------
156
}
157
158
Popular Tags