[jboss-cvs] JBossCache/src-50/org/jboss/cache/pojo/jmx ...
Brian Stansberry
brian.stansberry at jboss.com
Tue Nov 14 16:54:00 EST 2006
User: bstansberry
Date: 06/11/14 16:54:00
Modified: src-50/org/jboss/cache/pojo/jmx PojoCacheJmxWrapper.java
Log:
Ensure proper function whether or not wrapped cache is available during mbean registration.
Revision Changes Path
1.5 +126 -48 JBossCache/src-50/org/jboss/cache/pojo/jmx/PojoCacheJmxWrapper.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: PojoCacheJmxWrapper.java
===================================================================
RCS file: /cvsroot/jboss/JBossCache/src-50/org/jboss/cache/pojo/jmx/PojoCacheJmxWrapper.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -b -r1.4 -r1.5
--- PojoCacheJmxWrapper.java 13 Nov 2006 04:41:47 -0000 1.4
+++ PojoCacheJmxWrapper.java 14 Nov 2006 21:54:00 -0000 1.5
@@ -1,3 +1,24 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2006, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
package org.jboss.cache.pojo.jmx;
import javax.management.MBeanRegistration;
@@ -9,8 +30,6 @@
import org.jboss.cache.config.Configuration;
import org.jboss.cache.config.ConfigurationException;
import org.jboss.cache.jmx.CacheJmxWrapper;
-import org.jboss.cache.jmx.CacheJmxWrapperMBean;
-import org.jboss.cache.pojo.jmx.JmxUtil;
import org.jboss.cache.pojo.PojoCache;
import org.jboss.cache.pojo.PojoCacheAlreadyDetachedException;
import org.jboss.cache.pojo.PojoCacheFactory;
@@ -26,9 +45,11 @@
private MBeanServer server;
private String cacheObjectName;
private boolean selfConstructed;
- private PojoCache cache_;
- private CacheJmxWrapper plainCacheWrapper_;
+ private PojoCache pojoCache;
+ private CacheJmxWrapper plainCacheWrapper;
private boolean registerPlainCache;
+ private boolean registeredPlainCacheInCreate;
+ private boolean plainCacheRegistered;
private boolean created;
/**
@@ -37,19 +58,23 @@
*/
public PojoCacheJmxWrapper()
{
-
}
+ /**
+ * Creates a PojoCacheJmxWrapper that wraps the given PojoCache.
+ *
+ * @param toWrap the cache
+ */
public PojoCacheJmxWrapper(PojoCache toWrap)
{
- cache_ = toWrap;
+ setPojoCache(toWrap);
}
// PojoCacheMBean
public PojoCache getPojoCache()
{
- return cache_;
+ return pojoCache;
}
public Configuration getConfiguration()
@@ -64,12 +89,12 @@
public String getUnderlyingCacheObjectName()
{
- return plainCacheWrapper_ == null ? null : plainCacheWrapper_.getCacheObjectName();
+ return plainCacheWrapper == null ? null : plainCacheWrapper.getCacheObjectName();
}
public void create() throws Exception
{
- if (cache_ == null)
+ if (pojoCache == null)
{
if (config == null)
{
@@ -81,7 +106,11 @@
}
if (selfConstructed)
- cache_.create();
+ {
+ pojoCache.create();
+ }
+
+ registeredPlainCacheInCreate = registerPlainCache();
created = true;
}
@@ -89,19 +118,30 @@
public void start() throws Exception
{
if (selfConstructed)
- cache_.start();
+ {
+ pojoCache.start();
+ }
}
public void stop()
{
if (selfConstructed)
- cache_.stop();
+ {
+ pojoCache.stop();
+ }
}
public void destroy()
{
if (selfConstructed)
- cache_.destroy();
+ {
+ pojoCache.destroy();
+ }
+
+ if (registeredPlainCacheInCreate)
+ {
+ unregisterPlainCache();
+ }
created = false;
}
@@ -126,7 +166,7 @@
this.registerInterceptors = register;
}
- // ----------------------------------------------------------- MBeanRegistration
+ // ------------------------------------------------------ MBeanRegistration
/**
* Caches the provided <code>server</code> and <code>objName</code>.
@@ -148,22 +188,20 @@
}
/**
- * {@link CacheJmxWrapperMBean#registerWithMBeanServer Registers the CacheJmxWrapper},
+ * Registers the CacheJmxWrapperMBean,
* if {@link #getRegisterPlainCache()} is <code>true</code>.
*/
public void postRegister(Boolean registrationDone)
{
if (Boolean.TRUE.equals(registrationDone) && registerPlainCache)
{
- try
+ log.debug("Registered in JMX under " + cacheObjectName);
+
+ if (plainCacheWrapper != null)
{
- if (config.getServiceName() == null || config.getServiceName().length() == 0)
+ try
{
- // We need to inject the object name into the plain cache
- String plainName = JmxUtil.getPlainCacheObjectName(cacheObjectName);
- plainCacheWrapper_.setCacheObjectName(plainName);
- }
- org.jboss.cache.jmx.JmxUtil.registerCacheMBean(server, plainCacheWrapper_, plainCacheWrapper_.getCacheObjectName());
+ registerPlainCache();
}
catch (Exception e)
{
@@ -171,6 +209,7 @@
}
}
}
+ }
/**
* No-op.
@@ -185,16 +224,9 @@
*/
public void postDeregister()
{
- if (registerPlainCache)
- {
- try
- {
- org.jboss.cache.jmx.JmxUtil.unregisterCacheMBean(server, plainCacheWrapper_.getCacheObjectName());
- }
- catch (Exception e)
+ if (plainCacheWrapper != null)
{
- log.error("Could not unregister plain cache", e);
- }
+ unregisterPlainCache();
}
}
@@ -215,27 +247,73 @@
if (created)
throw new IllegalStateException("Cannot set underlying cache after call to create()");
-
- this.cache_ = cache;
- this.config = (cache == null ? null : cache.getCache().getConfiguration());
+ this.pojoCache = cache;
+ if (pojoCache == null)
+ {
+ this.config = null;
+ this.plainCacheWrapper = null;
+ }
+ else
+ {
+ this.config = cache.getCache().getConfiguration();
+ this.plainCacheWrapper = buildPlainCacheWrapper(pojoCache);
+ }
}
// --------------------------------------------------------------- Private methods
private void constructCache() throws ConfigurationException
{
- cache_ = (PojoCacheImpl) PojoCacheFactory.createCache(config, false);
+ pojoCache = (PojoCacheImpl) PojoCacheFactory.createCache(config, false);
- if (plainCacheWrapper_ == null)
+ plainCacheWrapper= buildPlainCacheWrapper(pojoCache);
+
+ selfConstructed = true;
+ }
+
+ private CacheJmxWrapper buildPlainCacheWrapper(PojoCache pojoCache)
{
- plainCacheWrapper_ = new CacheJmxWrapper();
- plainCacheWrapper_.setConfiguration(config);
+ CacheJmxWrapper plainCache = new CacheJmxWrapper();
+ plainCache.setRegisterInterceptors(getRegisterInterceptors());
+ plainCache.setCache(pojoCache.getCache());
+
+ return plainCache;
}
- plainCacheWrapper_.setRegisterInterceptors(getRegisterInterceptors());
- plainCacheWrapper_.setCache(cache_.getCache());
+ private boolean registerPlainCache() throws Exception
+ {
+ if (registerPlainCache && !plainCacheRegistered && server != null)
+ {
+ if (config.getServiceName() == null || config.getServiceName().length() == 0)
+ {
+ // We need to inject the object name into the plain cache
+ String plainName = JmxUtil.getPlainCacheObjectName(cacheObjectName);
+ plainCacheWrapper.setCacheObjectName(plainName);
+ }
- selfConstructed = true;
+ log.debug("Registering plain cache under name " + plainCacheWrapper.getCacheObjectName());
+ org.jboss.cache.jmx.JmxUtil.registerCacheMBean(server, plainCacheWrapper, plainCacheWrapper.getCacheObjectName());
+ plainCacheRegistered = true;
+ return true;
+ }
+
+ return false;
}
+ private void unregisterPlainCache()
+ {
+ if (registerPlainCache && plainCacheRegistered && server != null)
+ {
+ log.debug("Unregistering plain cache");
+ try
+ {
+ org.jboss.cache.jmx.JmxUtil.unregisterCacheMBean(server, plainCacheWrapper.getCacheObjectName());
+ }
+ catch (Exception e)
+ {
+ log.error("Could not unregister plain cache", e);
+ }
+ plainCacheRegistered = false;
+ }
+ }
}
More information about the jboss-cvs-commits
mailing list