[jboss-cvs] JBossCache/src/org/jboss/cache/pojo/jmx ...
Ben Wang
bwang at jboss.com
Sat Jan 13 10:55:11 EST 2007
User: bwang
Date: 07/01/13 10:55:11
Added: src/org/jboss/cache/pojo/jmx PojoCacheMBean.java
PojoCache.java PojoCacheJmxWrapperMBean.java
PojoCacheJmxWrapper.java JmxUtil.java
Log:
JBCACHE-922 Merged src-50 and tests-50 into src and tests, respectively.
Revision Changes Path
1.1 date: 2007/01/13 15:55:11; author: bwang; state: Exp;JBossCache/src/org/jboss/cache/pojo/jmx/PojoCacheMBean.java
Index: PojoCacheMBean.java
===================================================================
/*
* JBoss, Home of Professional Open Source
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jboss.cache.pojo.jmx;
import org.jboss.cache.pojo.PojoCacheException;
import org.jboss.cache.pojo.PojoCacheListener;
import org.jboss.cache.pojo.PojoCacheAlreadyDetachedException;
import org.jboss.cache.Cache;
import org.jboss.cache.jmx.LifeCycle;
import java.util.Map;
import java.util.Collection;
/**
* @author Ben Wang
* @since 2.0
*/
public interface PojoCacheMBean extends LifeCycle
{
/**
* Return number of POJO attach operations for this particular id.
* @return Number of attach operation.
*/
// public long getNumberOfAttach();
/**
* Return number of POJO detach operations for this particular id.
* @return Number of detach operation.
*/
// public long getNumberOfDetach();
/**
* Return number of POJO field read operations for this particulxar id.
* @param pojo That is associated with this POJO. If null, it means all POJOs in this cache system.
* @return Number of field read operation.
* @throws PojoCacheAlreadyDetachedException if pojo has been detached already.
*/
// public long getNumberOfFieldRead(Object pojo) throws PojoCacheAlreadyDetachedException;
/**
* Return number of POJO field write operations for this particular id.
* @param pojo That is associated with this POJO. If null, it means all POJOs in this cache system.
* @return Number of field read operation.
* @throws PojoCacheAlreadyDetachedException if pojo has been detached already.
*/
// public long getNumberOfFieldWrite(Object pojo) throws PojoCacheAlreadyDetachedException;
/**
* Reset all stats.
*/
// public void reset();
/**
* Obtain the internal location of this pojo stored under PojoCache.
* @param pojo That is associated with this POJO. If null, it means all POJOs in this cache system.
* @return String that indicates the location.
* @throws PojoCacheAlreadyDetachedException if pojo has been detached already.
*/
public String getInternalLocation(Object pojo) throws PojoCacheAlreadyDetachedException;
/**
* Get the MBean object name that the underlying replicated cache is using.
*/
public String getUnderlyingCacheObjectName();
}
1.1 date: 2007/01/13 15:55:11; author: bwang; state: Exp;JBossCache/src/org/jboss/cache/pojo/jmx/PojoCache.java
Index: PojoCache.java
===================================================================
/*
* JBoss, Home of Professional Open Source
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jboss.cache.pojo.jmx;
import org.jboss.cache.jmx.LifeCycle;
import org.jboss.cache.pojo.jmx.JmxUtil;
import org.jboss.cache.pojo.PojoCacheAlreadyDetachedException;
import org.jboss.cache.pojo.impl.PojoCacheImpl;
import org.jboss.cache.CacheSPI;
/**
* @author Ben Wang
* @since 2.0
*/
public class PojoCache implements PojoCacheMBean
{
PojoCacheImpl cache_;
public PojoCache(PojoCacheImpl cache)
{
cache_ = cache;
}
public String getInternalLocation(Object pojo) throws PojoCacheAlreadyDetachedException
{
return null;
}
public String getUnderlyingCacheObjectName()
{
CacheSPI spi =(CacheSPI)cache_.getCache();
// get the cache's registration name
String tmpName = JmxUtil.getCacheObjectName(spi);
return tmpName;
}
public void create() throws Exception
{
cache_.create();
}
public void start() throws Exception
{
cache_.start();
}
public void stop()
{
cache_.stop();
}
public void destroy()
{
cache_.destroy();
}
}
1.1 date: 2007/01/13 15:55:11; author: bwang; state: Exp;JBossCache/src/org/jboss/cache/pojo/jmx/PojoCacheJmxWrapperMBean.java
Index: PojoCacheJmxWrapperMBean.java
===================================================================
/*
* 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 org.jboss.cache.Cache;
import org.jboss.cache.config.Configuration;
import org.jboss.cache.jmx.LifeCycle;
import org.jboss.cache.pojo.PojoCache;
/**
* StandardMBean interface for {@link PojoCacheJmxWrapperMBean}.
*
* @author <a href="brian.stansberry at jboss.com">Brian Stansberry</a>
* @version $Revision: 1.1 $
*/
public interface PojoCacheJmxWrapperMBean
extends PojoCacheMBean
{
/**
* Get the PojoCache.
*
* @return
*/
public PojoCache getPojoCache();
/**
* Retrieves an immutable configuration.
*/
Configuration getConfiguration();
/**
* Gets whether this object should register the underlying {@link Cache}
* with JMX during {@link LifeCycle#create() create()}.
* <p/>
* Default is <code>false</code>.
*/
boolean getRegisterPlainCache();
/**
* Sets whether this object should register the underlying {@link Cache}
* with JMX during {@link LifeCycle#create() create()}.
* <p/>
* Default is <code>false</code>.
*/
public void setRegisterPlainCache(boolean registerPlainCache);
/**
* Gets whether this object should register the cache's interceptors
* with JMX.
* <p/>
* This property is only relevant if {@link #setRegisterPlainCache(boolean) registerPlainCache}
* is <code>true</code>.
* <p/>
* Default is <code>true</code>.
*/
boolean getRegisterInterceptors();
/**
* Sets whether this object should register the cache's interceptors
* with JMX.
* <p/>
* This property is only relevant if {@link #setRegisterPlainCache(boolean) registerPlainCache}
* is <code>true</code>.
* <p/>
* Default is <code>true</code>.
*/
void setRegisterInterceptors(boolean register);
}
1.1 date: 2007/01/13 15:55:11; author: bwang; state: Exp;JBossCache/src/org/jboss/cache/pojo/jmx/PojoCacheJmxWrapper.java
Index: PojoCacheJmxWrapper.java
===================================================================
/*
* 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;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jboss.cache.config.Configuration;
import org.jboss.cache.config.ConfigurationException;
import org.jboss.cache.jmx.CacheJmxWrapper;
import org.jboss.cache.pojo.PojoCache;
import org.jboss.cache.pojo.PojoCacheAlreadyDetachedException;
import org.jboss.cache.pojo.PojoCacheFactory;
import org.jboss.cache.pojo.impl.PojoCacheImpl;
public class PojoCacheJmxWrapper
implements PojoCacheJmxWrapperMBean, MBeanRegistration
{
private Log log = LogFactory.getLog(getClass().getName());
private boolean registerInterceptors = true;
private Configuration config;
private MBeanServer server;
private String cacheObjectName;
private boolean selfConstructed;
private PojoCache pojoCache;
private CacheJmxWrapper plainCacheWrapper;
private boolean registerPlainCache;
private boolean registeredPlainCacheInCreate;
private boolean plainCacheRegistered;
private boolean created;
/**
* Default constructor.
*
*/
public PojoCacheJmxWrapper()
{
}
/**
* Creates a PojoCacheJmxWrapper that wraps the given PojoCache.
*
* @param toWrap the cache
*/
public PojoCacheJmxWrapper(PojoCache toWrap)
{
setPojoCache(toWrap);
}
// PojoCacheMBean
public PojoCache getPojoCache()
{
return pojoCache;
}
public Configuration getConfiguration()
{
return config;
}
public String getInternalLocation(Object pojo) throws PojoCacheAlreadyDetachedException
{
return null;
}
public String getUnderlyingCacheObjectName()
{
return plainCacheWrapper == null ? null : plainCacheWrapper.getCacheObjectName();
}
public void create() throws Exception
{
if (pojoCache == null)
{
if (config == null)
{
throw new ConfigurationException("Must call setConfiguration() " +
"or setCache() before call to create()");
}
constructCache();
}
if (selfConstructed)
{
pojoCache.create();
}
registeredPlainCacheInCreate = registerPlainCache();
created = true;
}
public void start() throws Exception
{
if (selfConstructed)
{
pojoCache.start();
}
}
public void stop()
{
if (selfConstructed)
{
pojoCache.stop();
}
}
public void destroy()
{
if (selfConstructed)
{
pojoCache.destroy();
}
if (registeredPlainCacheInCreate)
{
unregisterPlainCache();
}
created = false;
}
public boolean getRegisterPlainCache()
{
return registerPlainCache;
}
public void setRegisterPlainCache(boolean registerPlainCache)
{
this.registerPlainCache = registerPlainCache;
}
public boolean getRegisterInterceptors()
{
return registerInterceptors;
}
public void setRegisterInterceptors(boolean register)
{
this.registerInterceptors = register;
}
// ------------------------------------------------------ MBeanRegistration
/**
* Caches the provided <code>server</code> and <code>objName</code>.
*
* @return either <code>objName</code>, or, if a name was previously assigned
* via {@link #setPojoCacheObjectName(String)}, that name.
*/
public ObjectName preRegister(MBeanServer server, ObjectName objName)
throws Exception
{
this.server = server;
if (cacheObjectName == null)
{
cacheObjectName = objName.getCanonicalName();
}
return new ObjectName(cacheObjectName);
}
/**
* Registers the CacheJmxWrapperMBean,
* if {@link #getRegisterPlainCache()} is <code>true</code>.
*/
public void postRegister(Boolean registrationDone)
{
if (Boolean.TRUE.equals(registrationDone) && registerPlainCache)
{
log.debug("Registered in JMX under " + cacheObjectName);
if (plainCacheWrapper != null)
{
try
{
registerPlainCache();
}
catch (Exception e)
{
log.error("Caught exception registering plain cache with JMX", e);
}
}
}
}
/**
* No-op.
*/
public void preDeregister() throws Exception
{
}
/**
* Unregisters the CacheJmxWrapper, if {@link #getRegisterPlainCache()} is
* <code>true</code>.
*/
public void postDeregister()
{
if (plainCacheWrapper != null)
{
unregisterPlainCache();
}
}
// -------------------------------------------------------------- Public methods
/**
* Sets the configuration that the underlying cache should use.
*
* @param config the configuration
*/
public void setConfiguration(Configuration config)
{
this.config = config;
}
public void setPojoCache(PojoCache cache)
{
if (created)
throw new IllegalStateException("Cannot set underlying cache after call to create()");
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
{
pojoCache = (PojoCacheImpl) PojoCacheFactory.createCache(config, false);
plainCacheWrapper= buildPlainCacheWrapper(pojoCache);
selfConstructed = true;
}
private CacheJmxWrapper buildPlainCacheWrapper(PojoCache pojoCache)
{
CacheJmxWrapper plainCache = new CacheJmxWrapper();
plainCache.setRegisterInterceptors(getRegisterInterceptors());
plainCache.setCache(pojoCache.getCache());
return plainCache;
}
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);
}
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;
}
}
}
1.1 date: 2007/01/13 15:55:11; author: bwang; state: Exp;JBossCache/src/org/jboss/cache/pojo/jmx/JmxUtil.java
Index: JmxUtil.java
===================================================================
/*
* JBoss, Home of Professional Open Source
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jboss.cache.pojo.jmx;
import org.jboss.cache.CacheSPI;
import javax.management.MBeanServer;
import javax.management.ObjectName;
/**
* Various JMX related utilities
*
* @author Ben Wang
* @version $Id: JmxUtil.java,v 1.1 2007/01/13 15:55:11 bwang Exp $
*/
public class JmxUtil extends org.jboss.cache.jmx.JmxUtil
{
public static final String POJO_CACHE_DOMAIN = "jboss.pojocache";
public static final String PC_PREFIX = POJO_CACHE_DOMAIN + ":service=PojoCache,clusterName=";
public static final String POJO_CACHE_TYPE = "cacheType=PojoCache";
public static void registerPojoCache(MBeanServer server, PojoCacheJmxWrapper cache, String objectName)
throws Exception
{
if (server == null || cache == null || objectName == null)
return;
ObjectName tmpObj = new ObjectName(objectName);
if (!server.isRegistered(tmpObj))
server.registerMBean(cache, tmpObj);
}
public static String getPojoCacheObjectName(org.jboss.cache.pojo.PojoCache cache)
{
// get the cache's registration name
String tmpName = getCacheObjectName((CacheSPI) cache.getCache());
String newName = null;
if (tmpName.startsWith(PREFIX))
{
newName = tmpName.replaceFirst(PREFIX, PC_PREFIX);
}
else if (tmpName.startsWith(JBOSS_CACHE_DOMAIN))
{
newName = tmpName.replaceFirst(JBOSS_CACHE_DOMAIN, POJO_CACHE_DOMAIN);
}
else if (tmpName.contains(CACHE_TYPE_KEY))
{
if (tmpName.contains(POJO_CACHE_TYPE))
throw new IllegalStateException("Plain Cache ObjectName should not include " + POJO_CACHE_TYPE);
int start = tmpName.indexOf(POJO_CACHE_TYPE);
int end = tmpName.indexOf(",", start);
if (end < 0)
end = tmpName.length();
String oldType = tmpName.substring(start, end);
newName = tmpName.replaceFirst(oldType, POJO_CACHE_TYPE);
}
else
{
newName = tmpName + "," + POJO_CACHE_TYPE;
}
return newName;
}
public static String getPlainCacheObjectName(String pojoCacheObjectName)
{
String plainName = null;
if (pojoCacheObjectName.startsWith(PC_PREFIX))
{
plainName = pojoCacheObjectName.replaceFirst(PC_PREFIX, PREFIX);
}
else if (pojoCacheObjectName.startsWith(POJO_CACHE_DOMAIN))
{
plainName = pojoCacheObjectName.replaceFirst(POJO_CACHE_DOMAIN, JBOSS_CACHE_DOMAIN);
}
else if (pojoCacheObjectName.contains(CACHE_TYPE_KEY))
{
if (pojoCacheObjectName.contains(PLAIN_CACHE_TYPE))
throw new IllegalStateException("PojoCache ObjectName should not include " + PLAIN_CACHE_TYPE);
int start = pojoCacheObjectName.indexOf(PLAIN_CACHE_TYPE);
int end = pojoCacheObjectName.indexOf(",", start);
if (end < 0)
end = pojoCacheObjectName.length();
String oldType = pojoCacheObjectName.substring(start, end);
plainName = pojoCacheObjectName.replaceFirst(oldType, PLAIN_CACHE_TYPE);
}
else
{
plainName = pojoCacheObjectName + "," + PLAIN_CACHE_TYPE;
}
return plainName;
}
public static void unregisterPojoCache(MBeanServer server, String objectName)
throws Exception
{
if (server == null || objectName == null)
return;
ObjectName on = new ObjectName(objectName);
if (server.isRegistered(on))
server.unregisterMBean(on);
}
}
More information about the jboss-cvs-commits
mailing list