1 15 package org.apache.hivemind.management.impl; 16 17 import java.lang.reflect.InvocationHandler ; 18 import java.lang.reflect.InvocationTargetException ; 19 import java.lang.reflect.Method ; 20 import java.lang.reflect.Proxy ; 21 import java.util.HashSet ; 22 import java.util.Iterator ; 23 import java.util.List ; 24 import java.util.Set ; 25 26 import javax.management.JMException ; 27 import javax.management.ObjectName ; 28 29 import org.apache.hivemind.ApplicationRuntimeException; 30 import org.apache.hivemind.InterceptorStack; 31 import org.apache.hivemind.ServiceInterceptorFactory; 32 import org.apache.hivemind.internal.Module; 33 import org.apache.hivemind.internal.ServicePoint; 34 import org.apache.hivemind.management.MBeanRegistry; 35 import org.apache.hivemind.management.ManagementMessages; 36 import org.apache.hivemind.management.ObjectNameBuilder; 37 import org.apache.hivemind.management.mbeans.PerformanceMonitorMBean; 38 import org.apache.hivemind.methodmatch.MethodMatcher; 39 import org.apache.hivemind.service.MethodContribution; 40 import org.apache.hivemind.service.MethodIterator; 41 import org.apache.hivemind.service.MethodSignature; 42 43 53 public class PerformanceMonitorFactory implements ServiceInterceptorFactory 54 { 55 private static final String SERVICE_DECORATOR_TYPE = "PerformanceCollector"; 56 57 private MBeanRegistry _mbeanRegistry; 58 59 private ObjectNameBuilder _objectNameBuilder; 60 61 private String _serviceId; 62 63 public PerformanceMonitorFactory(MBeanRegistry mbeanRegistry, 64 ObjectNameBuilder objectNameBuilder) 65 { 66 _mbeanRegistry = mbeanRegistry; 67 _objectNameBuilder = objectNameBuilder; 68 } 69 70 public void setServiceId(String string) 71 { 72 _serviceId = string; 73 } 74 75 78 private class PerformanceMonitorHandler implements InvocationHandler 79 { 80 private Object _inner; 81 82 private PerformanceCollector _counter; 83 84 private Set _interceptedMethods; 85 86 PerformanceMonitorHandler(Object inner, PerformanceCollector counter, Set interceptedMethods) 87 { 88 _inner = inner; 89 _counter = counter; 90 _interceptedMethods = interceptedMethods; 91 } 92 93 public Object invoke(Object proxy, Method method, Object [] args) throws Throwable 94 { 95 try 96 { 97 MethodSignature signature = new MethodSignature(method); 99 if (_interceptedMethods.contains(signature)) 100 { 101 long startTime = System.currentTimeMillis(); 103 Object result = method.invoke(_inner, args); 104 long endTime = System.currentTimeMillis(); 105 106 _counter.addMeasurement(signature, endTime - startTime); 107 return result; 108 } 109 110 return method.invoke(_inner, args); 111 } 112 catch (InvocationTargetException ex) 113 { 114 throw ex.getTargetException(); 115 } 116 } 117 118 } 119 120 public void createInterceptor(InterceptorStack stack, Module invokingModule, Object parameters) 121 { 122 ServicePoint servicePoint = invokingModule.getServicePoint(stack 123 .getServiceExtensionPointId()); 124 Set methods = getInterceptedMethods(stack, (List ) parameters); 125 try 126 { 127 PerformanceCollector counter = createMBean(servicePoint, methods); 128 InvocationHandler countHandler = new PerformanceMonitorHandler(stack.peek(), counter, 129 methods); 130 131 Object proxy = Proxy.newProxyInstance(invokingModule.getClassResolver() 132 .getClassLoader(), new Class [] 133 { stack.getServiceInterface() }, countHandler); 134 135 stack.push(proxy); 136 } 137 catch (Exception ex) 138 { 139 throw new ApplicationRuntimeException(ManagementMessages 140 .errorInstantiatingPerformanceInterceptor(_serviceId, stack, ex), ex); 141 } 142 } 143 144 147 public PerformanceCollector createMBean(ServicePoint servicePoint, Set methods) 148 throws JMException 149 { 150 PerformanceCollector counter = new PerformanceMonitorMBean(methods); 151 ObjectName objectName = _objectNameBuilder.createServiceDecoratorName( 152 servicePoint, 153 SERVICE_DECORATOR_TYPE); 154 _mbeanRegistry.registerMBean(counter, null, objectName); 155 156 return counter; 157 } 158 159 162 private MethodMatcher buildMethodMatcher(List parameters) 163 { 164 MethodMatcher result = null; 165 166 Iterator i = parameters.iterator(); 167 while (i.hasNext()) 168 { 169 MethodContribution mc = (MethodContribution) i.next(); 170 171 if (result == null) 172 result = new MethodMatcher(); 173 174 result.put(mc.getMethodPattern(), mc); 175 } 176 177 return result; 178 } 179 180 184 protected Set getInterceptedMethods(InterceptorStack stack, List parameters) 185 { 186 Set methods = new HashSet (); 187 MethodMatcher matcher = buildMethodMatcher(parameters); 188 189 MethodIterator mi = new MethodIterator(stack.getServiceInterface()); 190 191 while (mi.hasNext()) 192 { 193 MethodSignature sig = mi.next(); 194 195 if (includeMethod(matcher, sig)) 196 methods.add(sig); 197 } 198 return methods; 199 } 200 201 private boolean includeMethod(MethodMatcher matcher, MethodSignature sig) 202 { 203 if (matcher == null) 204 return true; 205 206 MethodContribution mc = (MethodContribution) matcher.get(sig); 207 return mc == null || mc.getInclude(); 208 } 209 210 } | Popular Tags |