KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > cluster > web > CacheHelper


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.cluster.web;
23
24 import java.util.Collections JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.Set JavaDoc;
28
29 import javax.management.MBeanServer JavaDoc;
30 import javax.management.ObjectName JavaDoc;
31
32 import org.jboss.cache.Cache;
33 import org.jboss.cache.Fqn;
34 import org.jboss.cache.Node;
35 import org.jboss.cache.buddyreplication.BuddyManager;
36 import org.jboss.cache.pojo.PojoCache;
37 import org.jboss.cache.pojo.jmx.PojoCacheJmxWrapperMBean;
38 import org.jboss.mx.util.MBeanProxyExt;
39 import org.jboss.mx.util.ObjectNameFactory;
40
41 /**
42  * Helper class to locate and invoke methods on the cache mbean used by Tomcat.
43  *
44  * @author Ben Wang
45  * Date: Aug 16, 2005
46  * @version $Id: CacheHelper.java 58606 2006-11-18 11:54:58Z bstansberry@jboss.com $
47  */

48 public class CacheHelper implements CacheHelperMBean
49 {
50    public static final ObjectName JavaDoc OBJECT_NAME =
51       ObjectNameFactory.create("jboss.test:service=WebTestCacheHelper");
52    
53    private static final String JavaDoc DEFAULT_CACHE_NAME =
54       "jboss.cache:service=TomcatClusteringCache";
55    private static final String JavaDoc VERSION_KEY = "VERSION";
56    
57    private MBeanServer JavaDoc server;
58    private PojoCache pojoCache;
59    private boolean leaveInstalledAfterShutdown;
60    
61    public CacheHelper(MBeanServer JavaDoc server)
62    {
63       this.server = server;
64    }
65    
66    public static PojoCache getCacheInstance()
67    {
68       try
69       {
70          ObjectName JavaDoc cacheServiceName_ = new ObjectName JavaDoc(DEFAULT_CACHE_NAME);
71          // Create Proxy-Object for this service
72
PojoCacheJmxWrapperMBean mbean = (PojoCacheJmxWrapperMBean) MBeanProxyExt.create(PojoCacheJmxWrapperMBean.class,
73                cacheServiceName_);
74          if (mbean == null)
75          {
76             throw new RuntimeException JavaDoc("getCacheInstance: locate null " + cacheServiceName_);
77          }
78          return mbean.getPojoCache();
79       }
80       catch (Throwable JavaDoc e)
81       {
82          e.printStackTrace();
83          throw new RuntimeException JavaDoc("getCacheInstance: Exception: " +e);
84       }
85    }
86    
87    public Object JavaDoc getSessionVersion(String JavaDoc sessionFqn)
88    {
89       return getCache().get(Fqn.fromString(sessionFqn), VERSION_KEY);
90    }
91    
92    public Object JavaDoc getBuddySessionVersion(String JavaDoc sessionFqn) throws Exception JavaDoc
93    {
94       Object JavaDoc result = null;
95
96       Fqn fqn = Fqn.fromString(sessionFqn);
97       
98       Set JavaDoc buddies = getBuddyBackupRoots();
99       for (Iterator JavaDoc iter = buddies.iterator(); iter.hasNext();)
100       {
101          Node buddy = (Node) iter.next();
102          Node session = buddy.getChild(fqn);
103          if (session != null)
104          {
105             result = session.get(VERSION_KEY);
106             break;
107          }
108       }
109
110       return result;
111    }
112    
113    public Set JavaDoc getSessionIds(String JavaDoc warFqn) throws Exception JavaDoc
114    {
115       Set JavaDoc result = new HashSet JavaDoc();
116       
117       Fqn fqn = Fqn.fromString(warFqn);
118       Node main = getCache().getChild(fqn);
119       if (main != null)
120       {
121          result.addAll(main.getChildrenNames());
122       }
123       
124 // Check in the buddy backup tree
125

126       Set JavaDoc buddies = getBuddyBackupRoots();
127       for (Iterator JavaDoc iter = buddies.iterator(); iter.hasNext();)
128       {
129          Node buddy = (Node) iter.next();
130          Node warRoot = buddy.getChild(fqn);
131          if (warRoot != null)
132          {
133             result.addAll(warRoot.getChildrenNames());
134          }
135       }
136       
137       return result;
138    }
139    
140    public boolean getLeaveInstalledAfterShutdown()
141    {
142       return leaveInstalledAfterShutdown;
143    }
144
145    public void setLeaveInstalledAfterShutdown()
146    {
147       this.leaveInstalledAfterShutdown = true;
148    }
149    
150    public void uninstall()
151    {
152       Thread JavaDoc t = new Thread JavaDoc() {
153         public void run() {
154            try
155            {
156               server.unregisterMBean(OBJECT_NAME);
157            }
158            catch (Exception JavaDoc e) {}
159         }
160       };
161       
162       t.start();
163    }
164
165    private Set JavaDoc getBuddyBackupRoots()
166    {
167       Set JavaDoc buddies = null;
168       Node buddyRoot = getCache().getChild(BuddyManager.BUDDY_BACKUP_SUBTREE_FQN);
169       if (buddyRoot != null)
170       {
171          buddies = buddyRoot.getChildren();
172       }
173       else
174       {
175          buddies = Collections.EMPTY_SET;
176       }
177       return buddies;
178    }
179    
180    private PojoCache getPojoCache()
181    {
182       if (pojoCache == null)
183          pojoCache = getCacheInstance();
184       return pojoCache;
185    }
186    
187    private Cache getCache()
188    {
189       return getPojoCache().getCache();
190    }
191 }
192
Popular Tags