Commit 23f1f701 authored by ncampbell's avatar ncampbell

removing current version of meter...I'll add the new version later

git-svn-id: http://svn.igniterealtime.org/svn/repos/messenger/branches@2940 b35dd754-fafc-0310-a699-88a17e54d16e
parent cf54452d
/**
*
*/
package org.jivesoftware.messenger.plugin.meter.accumulator;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.management.ObjectName;
import org.jivesoftware.messenger.plugin.meter.UnableToAllocateAccumulator;
import org.jrobin.annotations.Arc;
import org.jrobin.annotations.Ds;
import org.jrobin.annotations.Rrd;
import org.jrobin.core.RrdDb;
import org.jrobin.core.RrdDbPool;
import org.jrobin.core.RrdDef;
import org.jrobin.core.RrdException;
import org.jrobin.core.Sample;
/**
* @author Noah Campbell
* @version 1.0
*/
public class Accumulator implements AccumulatorMBean, Runnable {
/** The logger. */
final static private Logger logger = Logger.getLogger("ACCUMULATOR", "resources");
/** The dbPath. */
private final String dbPath;
/** The objectName. */
private final ObjectName objectName;
/** The pool. */
private final RrdDbPool pool;
/**
* Construct a new <code>Accumulator</code>.
*
* @param objectName
* @param db
*/
public Accumulator(ObjectName objectName, String db) {
this.dbPath = db;
this.objectName = objectName;
this.pool = RrdDbPool.getInstance();
}
/** The lastTimestamp. */
private long lastTimestamp = Long.MIN_VALUE;
/** The count. */
private long count = 0;
/**
* @see java.lang.Runnable#run()
*/
public void run() {
RrdDb db = null;
try {
db = pool.requestRrdDb(dbPath);
} catch (IOException e1) {
logger.log(Level.SEVERE, "accumulator.unabletoopenrrd", new Object[]{dbPath,
e1});
return;
} catch (RrdException e1) {
logger.log(Level.SEVERE, "accumulator.unabletoprocessrrd", new Object[]{dbPath,
e1});
return;
}
try {
long timestamp = 0;
Sample s = db.createSample();
timestamp = s.getTime();
AccumulatorHelper helper = (AccumulatorHelper) AccumulatorManager.getAccumulator(this.objectName);
Map<String, Number> results = helper.getResults();
for(String key : results.keySet()) {
s.setValue(key, results.get(key).doubleValue());
}
if( timestamp == lastTimestamp) {
logger.log(Level.INFO,"accumulator.rush", new Object[]{new Date(timestamp * 1000),
this.objectName, this.dbPath,
Thread.currentThread().getName(),
Thread.currentThread().getId()});
Thread.sleep(1000);
}
s.update();
lastTimestamp = timestamp;
count++;
} catch (IOException e) {
logger.log(Level.WARNING, "accumulator.ioexception", e);
} catch (RrdException e) {
logger.log(Level.WARNING, "accumulator.rrdexception", e);
} catch (IllegalArgumentException e) {
logger.log(Level.WARNING, "accumulator.illegalargumentexceptin", e);
} catch (SecurityException e) {
logger.log(Level.WARNING, "accumulator.securityexception", e);
} catch (InterruptedException e) {
logger.log(Level.FINE, "accumulator.interrupted",e.getLocalizedMessage());
} catch (UnableToAllocateAccumulator e) {
logger.log(Level.WARNING, "accumulator.allocateaccumulatorfailed", e.getLocalizedMessage());
} catch (AccumulatorDefinitionNotFoundException e) {
logger.log(Level.WARNING, "accumulator.allocatornotfound", e.getLocalizedMessage());
} finally {
try {
pool.release(db);
} catch (Exception e) {
logger.log(Level.WARNING, "accumulator.releasefailed", e);
}
}
}
/**
* @see org.jivesoftware.messenger.plugin.meter.accumulator.AccumulatorMBean#getPath()
*/
public String getPath() {
return this.getPath();
}
/**
* @see org.jivesoftware.messenger.plugin.meter.accumulator.AccumulatorMBean#getSourceMBean()
*/
public ObjectName getSourceMBean() {
return this.objectName;
}
/**
* @see org.jivesoftware.messenger.plugin.meter.accumulator.AccumulatorMBean#getTotalReads()
*/
public long getTotalReads() {
return count;
}
}
/**
*
*/
package org.jivesoftware.messenger.plugin.meter.accumulator;
/**
* @author Noah Campbell
* @version 1.0
*/
public class AccumulatorDefinitionNotFoundException extends Exception {
/** The serialVersionUID. */
private static final long serialVersionUID = 1L;
/**
* Construct a new <code>AccululationDefinitionNotFoundException</code>.
*
*/
public AccumulatorDefinitionNotFoundException() {
super();
// TODO Auto-generated constructor stub
}
/**
* Construct a new <code>AccululationDefinitionNotFoundException</code>.
*
* @param message
*/
public AccumulatorDefinitionNotFoundException(String message) {
super(message);
// TODO Auto-generated constructor stub
}
/**
* Construct a new <code>AccululationDefinitionNotFoundException</code>.
*
* @param message
* @param cause
*/
public AccumulatorDefinitionNotFoundException(String message,
Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}
/**
* Construct a new <code>AccululationDefinitionNotFoundException</code>.
*
* @param cause
*/
public AccumulatorDefinitionNotFoundException(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}
}
/**
*
*/
package org.jivesoftware.messenger.plugin.meter.accumulator;
import java.util.Map;
/**
* @author Noah Campbell
* @version 1.0
*/
interface AccumulatorHelper {
/**
* <code>getResults</code> returns a map of datasource name and the corresponding
* value for the paticular Accumulator. This is a help method to make the
* extract of an arbitrary accumulator more easily assasible.
*
* @return map A map of the results.
*/
Map<String, Number> getResults();
}
/**
*
*/
package org.jivesoftware.messenger.plugin.meter.accumulator;
import javax.management.ObjectName;
/**
* @author Noah Campbell
* @version 1.0
*/
public interface AccumulatorMBean {
String getPath();
ObjectName getSourceMBean();
long getTotalReads();
}
/**
*
*/
package org.jivesoftware.messenger.plugin.meter.accumulator;
import java.lang.management.ManagementFactory;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import org.apache.commons.jxpath.CompiledExpression;
import org.apache.commons.jxpath.JXPathContext;
import org.apache.commons.jxpath.JXPathException;
import org.jivesoftware.messenger.plugin.meter.UnableToAllocateAccumulator;
import org.jrobin.annotations.Ds;
import org.jrobin.annotations.Rrd;
/**
* @author Noah Campbell
* @version 1.0
*/
public class AccumulatorManager {
/** The overrides. */
private static Map<ObjectName, Class> overrides = new HashMap<ObjectName, Class>();
/**
* @param name
* @return cls The class that overrides the default MBeanInfo.getClassName
*/
public static Class getOverride(ObjectName name) {
return overrides.get(name);
}
/**
* @param objectName
* @param cls
*/
public static void registerOverride(ObjectName objectName, Class cls) {
overrides.put(objectName, cls);
}
/**
* @param objectName
*/
public static void deregisterOverride(ObjectName objectName) {
overrides.remove(objectName);
}
/**
* @author Noah Campbell
* @version 1.0
*/
static final class AccumulatorProxy implements InvocationHandler {
/** The mbean server. */
private final static MBeanServer server = ManagementFactory.getPlatformMBeanServer();
/**
* Construct a new <code>AccumulatorProxy</code>.
*
* @param objectName
* @param override
* @throws AccumulatorDefinitionNotFoundException
*/
@SuppressWarnings("unchecked")
public AccumulatorProxy(ObjectName objectName, Class override) throws AccumulatorDefinitionNotFoundException {
this.objectName = objectName;
Class cls = null;
if(override == null) {
try {
Class overrideClass = overrides.get(objectName);
if(overrideClass != null) {
cls = overrideClass;
} else {
// last ditch effort.
cls = Class.forName(server.getMBeanInfo(objectName).getClassName());
}
} catch (Exception e) {
throw new AccumulatorDefinitionNotFoundException(e);
}
} else {
cls = override;
}
if(cls == null) {
throw new AccumulatorDefinitionNotFoundException("Unable to locate class");
}
if(cls.getAnnotation(Rrd.class) == null) {
throw new IllegalArgumentException("No @Rrd specified");
}
accumulator = java.lang.reflect.Proxy.newProxyInstance(
Thread.currentThread().getContextClassLoader(),
new Class[]{cls, AccumulatorHelper.class},
this);
this.mbeanClass = cls;
context = JXPathContext.newContext(this.objectName);
}
/**
* Construct a new <code>AccumulatorProxy</code>.
*
* @param name
* @throws Exception
*/
public AccumulatorProxy(ObjectName name) throws Exception {
this(name, null);
}
/** The accumulator. */
private final Object accumulator;
/** The objectName. */
private final ObjectName objectName;
/** The mbeanClass. */
private final Class mbeanClass;
/** The context. */
private JXPathContext context = null;
// private Map<Method, CompiledExpression> methodCache = new HashMap<Method, CompiledExpression>();
/**
* @return accumulator The accumulator.
*/
public Object getAccumulator() {
return this.accumulator;
}
/**
* @see java.lang.reflect.InvocationHandler#invoke(java.lang.Object, java.lang.reflect.Method, java.lang.Object[])
*/
public Object invoke(@SuppressWarnings("unused") Object proxy, Method method, @SuppressWarnings("unused") Object[] args)
throws Throwable {
if(method.getName().equals("getResults")) {
return getResults();
}
try {
Ds ds = method.getAnnotation(Ds.class);
return extract(ds);
} catch (Exception e) {
return 0;
}
}
/** ZERO. Declared once, used many. */
private static final Number ZERO = new Double(0.0d);
/**
* @param ds The \@Ds to extract.
* @return number The value or 0 if the result is not of type Number.
*/
private Number extract(Ds ds) {
try {
CompiledExpression e = JXPathContext.compile(ds.expr());
Object result = e.getValue(context);
if(result != null) {
logger.log(Level.INFO, "accumulator.exprresults", new Object[]{ds.name(),
ds.expr(), result});
return (Number) result;
} else {
logger.log(Level.WARNING, "accumulatorproxy.nullresult",
new Object[] { ds.name(), ds.expr() });
}
} catch (JXPathException e) {
logger.log(Level.WARNING, "accumulator.getvaluefailed",
new Object[]{ds.name(),
ds.expr(),
e.getLocalizedMessage()});
} catch (Exception e) {
logger.log(Level.WARNING, "accumulatorproxy.unabletoextract",
new Object[]{ds.name(), ds.expr(), e.getLocalizedMessage()});
}
return ZERO;
}
/**
* @return map A map of results.
*/
private Map<String, Number> getResults() {
Map<String, Number> results = new HashMap<String, Number>();
List<Ds> dss = findDs(this.mbeanClass);
for(Ds ds : dss) {
results.put(ds.name(), extract(ds));
}
return results;
}
/**
* @param forName
* @return dss List of Ds.
*/
private List<Ds> findDs(Class<?> forName) {
List<Ds> anonDs = new ArrayList<Ds>();
Method[] methods = forName.getMethods();
for(Method m : methods) {
Ds ds = m.getAnnotation(Ds.class);
if(ds != null) {
if(m.getReturnType().isPrimitive() || m.getReturnType().isAssignableFrom(Number.class)) {
anonDs.add(ds);
} else {
logger.log(Level.WARNING, "accumulatorproxy.invalidreturntype", ds.name());
}
}
}
return anonDs;
}
}
/** The logger. */
final static private Logger logger = Logger.getLogger("accumulatormanager",
"resources");
/** The accumulators. */
private static Map<ObjectName, AccumulatorProxy> accumulators = new ConcurrentHashMap<ObjectName, AccumulatorProxy>();
/**
* @param objectName
* @param override
* @return accumulator
* @throws UnableToAllocateAccumulator
* @throws AccumulatorDefinitionNotFoundException
*/
@SuppressWarnings("unused")
public static Object getAccumulator(ObjectName objectName, Class override) throws UnableToAllocateAccumulator, AccumulatorDefinitionNotFoundException {
if(!accumulators.containsKey(objectName)) {
accumulators.put(objectName, new AccumulatorProxy(objectName, override));
}
return accumulators.get(objectName).getAccumulator();
}
/**
* @param objectName
* @return accumulator Return the accumulator.
* @throws AccumulatorDefinitionNotFoundException
* @throws UnableToAllocateAccumulator
*/
public static Object getAccumulator(ObjectName objectName) throws UnableToAllocateAccumulator, AccumulatorDefinitionNotFoundException {
return getAccumulator(objectName, null);
}
}
Markdown is supported
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