Commit fad7c63e authored by Tigran Mkrtchyan's avatar Tigran Mkrtchyan
Browse files

nfs: add JMX suport for generic cache

parent cf899455
......@@ -129,6 +129,11 @@ public class Cache<K, V> extends TimerTask {
*/
private final CacheEventListener<K, V> _eventListener;
/**
* The JMX interfate to this cache
*/
private final CacheMXBean<V> _mxBean;
/**
* Create new cache instance with default {@link CacheEventListener} and
* default cleanup period.
......@@ -164,6 +169,7 @@ public class Cache<K, V> extends TimerTask {
_defaultEntryIdleTime = entryIdleTime;
_storage = new HashMap<K, CacheElement<V>>(_size);
_eventListener = eventListener;
_mxBean = new CacheMXBeanImpl<K, V>(this);
_cleaner.schedule(this, 0, timeUnit.toMillis(timeValue));
}
......@@ -341,14 +347,14 @@ public class Cache<K, V> extends TimerTask {
* Get {@link List<V>} of entries.
* @return list of entries.
*/
public List<V> entries() {
List<V> entries;
public List<CacheElement<V>> entries() {
List<CacheElement<V>> entries;
_accessLock.lock();
try {
entries = new ArrayList<V>(_storage.size());
entries = new ArrayList<CacheElement<V>>(_storage.size());
for(CacheElement<V> e: _storage.values()) {
entries.add(e.getObject());
entries.add(e);
}
} finally {
_accessLock.unlock();
......
/*
* This library is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This library 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 Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this program (see the file COPYING.LIB for more
* details); if not, write to the Free Software Foundation, Inc.,
* 675 Mass Ave, Cambridge, MA 02139, USA.
*/
package org.dcache.utils;
/**
* Interface CacheMXBeanImplMXBean
*
*/
public interface CacheMXBean<V> {
void clear();
long getEntryIdleTime();
long getEntryLiveTime();
int getSize();
String[] getEntries();
}
/*
* This library is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This library 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 Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this program (see the file COPYING.LIB for more
* details); if not, write to the Free Software Foundation, Inc.,
* 675 Mass Ave, Cambridge, MA 02139, USA.
*/
package org.dcache.utils;
import java.lang.management.ManagementFactory;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.management.*;
/**
* Class CacheMXBeanImpl
*
*/
public class CacheMXBeanImpl<K,V> implements CacheMXBean<V> {
private static final Logger _log = Logger.getLogger(CacheMXBeanImpl.class.getName());
private final Cache<K,V> _cache;
public CacheMXBeanImpl(Cache<K,V> cache){
_cache = cache;
MBeanServer server = ManagementFactory.getPlatformMBeanServer();
try {
String name = String.format("%s:type=Cache,name=%s",_cache.getClass().getPackage(), _cache.getName());
ObjectName mxBeanName = new ObjectName(name);
if( !server.isRegistered(mxBeanName)) {
server.registerMBean(this, new ObjectName(name));
}
} catch (MalformedObjectNameException ex) {
_log.log(Level.SEVERE, ex.getMessage(), ex);
} catch (InstanceAlreadyExistsException ex) {
_log.log(Level.SEVERE, ex.getMessage(), ex);
} catch (MBeanRegistrationException ex) {
_log.log(Level.SEVERE, ex.getMessage(), ex);
} catch (NotCompliantMBeanException ex) {
_log.log(Level.SEVERE, ex.getMessage(), ex);
}
}
@Override
public void clear() {
_cache.clear();
}
@Override
public String[] getEntries() {
List<CacheElement<V>> entries = _cache.entries();
String[] asArray = new String[entries.size()];
int i = 0;
for(CacheElement<V> v: entries) {
asArray[i] = v.toString();
i++;
}
return asArray;
}
@Override
public int getSize() {
return _cache.size();
}
@Override
public long getEntryIdleTime() {
return _cache.getEntryIdleTime();
}
@Override
public long getEntryLiveTime() {
return _cache.getEntryLiveTime();
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment