1 22 package org.jboss.test.cluster.web; 23 24 import java.util.Collections ; 25 import java.util.HashSet ; 26 import java.util.Iterator ; 27 import java.util.Set ; 28 29 import javax.management.MBeanServer ; 30 import javax.management.ObjectName ; 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 48 public class CacheHelper implements CacheHelperMBean 49 { 50 public static final ObjectName OBJECT_NAME = 51 ObjectNameFactory.create("jboss.test:service=WebTestCacheHelper"); 52 53 private static final String DEFAULT_CACHE_NAME = 54 "jboss.cache:service=TomcatClusteringCache"; 55 private static final String VERSION_KEY = "VERSION"; 56 57 private MBeanServer server; 58 private PojoCache pojoCache; 59 private boolean leaveInstalledAfterShutdown; 60 61 public CacheHelper(MBeanServer server) 62 { 63 this.server = server; 64 } 65 66 public static PojoCache getCacheInstance() 67 { 68 try 69 { 70 ObjectName cacheServiceName_ = new ObjectName (DEFAULT_CACHE_NAME); 71 PojoCacheJmxWrapperMBean mbean = (PojoCacheJmxWrapperMBean) MBeanProxyExt.create(PojoCacheJmxWrapperMBean.class, 73 cacheServiceName_); 74 if (mbean == null) 75 { 76 throw new RuntimeException ("getCacheInstance: locate null " + cacheServiceName_); 77 } 78 return mbean.getPojoCache(); 79 } 80 catch (Throwable e) 81 { 82 e.printStackTrace(); 83 throw new RuntimeException ("getCacheInstance: Exception: " +e); 84 } 85 } 86 87 public Object getSessionVersion(String sessionFqn) 88 { 89 return getCache().get(Fqn.fromString(sessionFqn), VERSION_KEY); 90 } 91 92 public Object getBuddySessionVersion(String sessionFqn) throws Exception 93 { 94 Object result = null; 95 96 Fqn fqn = Fqn.fromString(sessionFqn); 97 98 Set buddies = getBuddyBackupRoots(); 99 for (Iterator 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 getSessionIds(String warFqn) throws Exception 114 { 115 Set result = new HashSet (); 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 126 Set buddies = getBuddyBackupRoots(); 127 for (Iterator 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 t = new Thread () { 153 public void run() { 154 try 155 { 156 server.unregisterMBean(OBJECT_NAME); 157 } 158 catch (Exception e) {} 159 } 160 }; 161 162 t.start(); 163 } 164 165 private Set getBuddyBackupRoots() 166 { 167 Set 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 |