1 package jfun.yan.monitoring; 2 3 import java.beans.IntrospectionException ; 4 import java.beans.PropertyDescriptor ; 5 import java.lang.reflect.Constructor ; 6 import java.lang.reflect.Method ; 7 8 import jfun.util.beans.Bean; 9 import jfun.yan.Component; 10 import jfun.yan.Components; 11 import jfun.yan.Functions; 12 import jfun.yan.function.Function; 13 import jfun.yan.util.ReflectionUtil; 14 15 22 public class Monitors { 23 29 public static FunctionMonitor monitor(final Constructor ctor, final CtorMonitor mon){ 30 return new FunctionMonitor(){ 31 public void calling(Function f, Object [] args){ 32 mon.constructing(ctor, args); 33 } 34 public void called(Function f, Object [] args, Object result, long duration){ 35 mon.constructed(ctor, args, result, duration); 36 } 37 public void callFailed(Function f, Object [] args, Throwable err, long duration){ 38 mon.constructionFailed(ctor, args, err, duration); 39 } 40 }; 41 } 42 49 public static FunctionMonitor monitor(final Object obj, final Method mtd, final MethodMonitor mon){ 50 return new FunctionMonitor(){ 51 public void calling(Function f, Object [] args){ 52 mon.invoking(obj, mtd, args); 53 } 54 public void called(Function f, Object [] args, Object result, long duration){ 55 mon.invoked(obj, mtd, args, result, duration); 56 } 57 public void callFailed(Function f, Object [] args, Throwable err, long duration){ 58 mon.invocationFailed(obj, mtd, args, err, duration); 59 } 60 }; 61 } 62 69 public static FunctionMonitor monitorGetter(final Object obj, final PropertyDescriptor prop, final GetterMonitor mon){ 70 return new FunctionMonitor(){ 71 public void calling(Function f, Object [] args){ 72 mon.propertyGetting(obj, prop); 73 } 74 public void called(Function f, Object [] args, Object result, long duration){ 75 mon.propertyGot(obj, prop, result, duration); 76 } 77 public void callFailed(Function f, Object [] args, Throwable err, long duration){ 78 mon.propertyGetFailed(obj, prop, err, duration); 79 } 80 }; 81 } 82 90 public static FunctionMonitor monitorGetter(final Object obj, final PropertyDescriptor prop, 91 final int ind, final IndexedGetterMonitor mon){ 92 return new FunctionMonitor(){ 93 public void calling(Function f, Object [] args){ 94 mon.propertyGetting(obj, prop, ind); 95 } 96 public void called(Function f, Object [] args, Object result, long duration){ 97 mon.propertyGot(obj, prop, ind, result, duration); 98 } 99 public void callFailed(Function f, Object [] args, Throwable err, long duration){ 100 mon.propertyGetFailed(obj, prop, ind, err, duration); 101 } 102 }; 103 } 104 private static Object getValue(Object [] args){ 105 return args[0]; 106 } 107 114 public static FunctionMonitor monitorSetter(final Object obj, final PropertyDescriptor prop, final SetterMonitor mon){ 115 return new FunctionMonitor(){ 116 public void calling(Function f, Object [] args){ 117 mon.propertySetting(obj, prop, getValue(args)); 118 } 119 public void called(Function f, Object [] args, Object result, long duration){ 120 mon.propertySet(obj, prop, getValue(args), duration); 121 } 122 public void callFailed(Function f, Object [] args, Throwable err, long duration){ 123 mon.propertySetFailed(obj, prop, getValue(args), err, duration); 124 } 125 }; 126 } 127 135 public static FunctionMonitor monitorSetter(final Object obj, final PropertyDescriptor prop, 136 final int ind, final IndexedSetterMonitor mon){ 137 return new FunctionMonitor(){ 138 public void calling(Function f, Object [] args){ 139 mon.propertySetting(obj, prop, ind, getValue(args)); 140 } 141 public void called(Function f, Object [] args, Object result, long duration){ 142 mon.propertySet(obj, prop, ind, getValue(args), duration); 143 } 144 public void callFailed(Function f, Object [] args, Throwable err, long duration){ 145 mon.propertySetFailed(obj, prop, ind, getValue(args), err, duration); 146 } 147 }; 148 } 149 150 156 public static Function fun(Function f, FunctionMonitor mon){ 157 return new MonitoredFunction(f, mon); 158 } 159 165 public static Function ctor(Constructor ctor, CtorMonitor mon){ 166 return fun(Functions.ctor(ctor), monitor(ctor, mon)); 167 } 168 175 public static Function method(Object self, Method mtd, MethodMonitor mon){ 176 return fun(Functions.method(self, mtd), monitor(self, mtd, mon)); 177 } 178 184 public static Function static_method(Method mtd, MethodMonitor mon){ 185 return fun(Functions.static_method(mtd), monitor(null, mtd, mon)); 186 } 187 194 public static Function getter(Bean bean, String prop_name, GetterMonitor mon) 195 throws IntrospectionException { 196 final PropertyDescriptor prop = bean.getBeanType().getPropertyDescriptor(prop_name); 197 return fun(Functions.getter(bean, prop_name), monitorGetter(bean.getObject(), prop, mon)); 198 } 199 206 public static Function setter(Bean bean, String prop_name, SetterMonitor mon) 207 throws IntrospectionException { 208 final PropertyDescriptor prop = bean.getBeanType().getPropertyDescriptor(prop_name); 209 return fun(Functions.setter(bean, prop_name), monitorSetter(bean.getObject(), prop, mon)); 210 } 211 219 public static Function getter(Bean bean, String prop_name, int ind, IndexedGetterMonitor mon) 220 throws IntrospectionException { 221 final PropertyDescriptor prop = bean.getBeanType().getPropertyDescriptor(prop_name); 222 return fun(Functions.indexed_getter(bean, prop_name, ind), 223 monitorGetter(bean.getObject(), prop, ind, mon)); 224 } 225 233 public static Function setter(Bean bean, String prop_name, int ind, IndexedSetterMonitor mon) 234 throws IntrospectionException { 235 final PropertyDescriptor prop = bean.getBeanType().getPropertyDescriptor(prop_name); 236 return fun(Functions.indexed_setter(bean, prop_name, ind), 237 monitorSetter(bean.getObject(), prop, ind, mon)); 238 } 239 private final ComponentMonitor mon; 240 244 public Monitors(ComponentMonitor mon) { 245 this.mon = mon; 246 } 247 252 public Component ctor(Constructor ctor){ 253 return Components.fun(ctor(ctor, mon)); 254 } 255 263 public Component ctor(Class type, Class [] param_types) 264 throws IllegalArgumentException { 265 return ctor(type, param_types, false); 266 } 267 276 public Component ctor(Class type, Class [] param_types, boolean suppress_security) 277 throws IllegalArgumentException { 278 final Constructor ctor = ReflectionUtil.getConstructor(type, param_types, 279 suppress_security); 280 return ctor(ctor); 281 } 282 289 public Component ctor(Class type) 290 throws IllegalArgumentException { 291 return ctor(type, false); 292 } 293 301 public Component ctor(Class type, boolean suppress_security) 302 throws IllegalArgumentException { 303 final Constructor ctor = ReflectionUtil.getConstructor(type, suppress_security); 304 return ctor(ctor); 305 } 306 312 public Component method(Object obj, Method mtd){ 313 return Components.fun(method(obj, mtd, mon)); 314 } 315 324 public Component method(Class type, Object obj, String name, Class [] param_types) 325 throws IllegalArgumentException { 326 return method(type, obj, name, param_types, false); 327 } 328 338 public Component method(Class type, Object obj, String name, Class [] param_types, 339 boolean suppress_security) 340 throws IllegalArgumentException { 341 return method(obj, ReflectionUtil.getMethod(type, name, param_types, suppress_security)); 342 } 343 351 public Component method(Class type, Object obj, String name) 352 throws IllegalArgumentException { 353 return method(type, obj, name, false); 354 } 355 364 public Component method(Class type, Object obj, String name, boolean suppress_security) 365 throws IllegalArgumentException { 366 return method(obj, ReflectionUtil.getMethod(type, name, suppress_security)); 367 } 368 374 public Component static_method(Method mtd) 375 throws IllegalArgumentException { 376 return Components.fun(static_method(mtd, mon)); 377 } 378 386 public Component static_method(Class type, String name, Class [] param_types) 387 throws IllegalArgumentException { 388 return static_method(type, name, param_types, false); 389 } 390 399 public Component static_method(Class type, String name, Class [] param_types, 400 boolean suppress_security) 401 throws IllegalArgumentException { 402 return static_method(ReflectionUtil.getMethod(type, name, param_types, 403 suppress_security)); 404 } 405 412 public Component static_method(Class type, String name) 413 throws IllegalArgumentException { 414 return static_method(type, name, false); 415 } 416 424 public Component static_method(Class type, String name, boolean suppress_security) 425 throws IllegalArgumentException { 426 return static_method(ReflectionUtil.getStaticMethod(type, name, suppress_security)); 427 } 428 436 public Component method(Object obj, String name, Class [] param_types) 437 throws IllegalArgumentException { 438 return method(obj, name, param_types, false); 439 } 440 449 public Component method(Object obj, String name, Class [] param_types, 450 boolean suppress_security) 451 throws IllegalArgumentException { 452 return method(obj, ReflectionUtil.getMethod(obj.getClass(), name, param_types, 453 suppress_security)); 454 } 455 462 public Component method(Object obj, String name) 463 throws IllegalArgumentException { 464 return method(obj, name, false); 465 } 466 474 public Component method(Object obj, String name, boolean suppress_security) 475 throws IllegalArgumentException { 476 return method(obj, ReflectionUtil.getMethod(obj.getClass(), name, 477 suppress_security)); 478 } 479 486 public Component getter(Bean bean, String prop_name) 487 throws IntrospectionException { 488 return Components.fun(getter(bean, prop_name, mon)); 489 } 490 497 public Component setter(Bean bean, String prop_name) 498 throws IntrospectionException { 499 return Components.fun(setter(bean, prop_name, mon)); 500 } 501 509 public Component getter(Bean bean, String prop_name, int ind) 510 throws IntrospectionException { 511 return Components.fun(getter(bean, prop_name, ind, mon)); 512 } 513 521 public Component setter(Bean bean, String prop_name, int ind) 522 throws IntrospectionException { 523 return Components.fun(setter(bean, prop_name, ind, mon)); 524 } 525 533 public Component getter(Class type, Object obj, String prop_name) 534 throws IntrospectionException { 535 return getter(Bean.instance(type, obj), prop_name); 536 } 537 545 public Component setter(Class type, Object obj, String prop_name) 546 throws IntrospectionException { 547 return setter(Bean.instance(type, obj), prop_name); 548 } 549 558 public Component getter(Class type, Object obj, String prop_name, int ind) 559 throws IntrospectionException { 560 return getter(Bean.instance(type, obj), prop_name, ind); 561 } 562 571 public Component setter(Class type, Object obj, String prop_name, int ind) 572 throws IntrospectionException { 573 return setter(Bean.instance(type, obj), prop_name, ind); 574 } 575 582 public Component getter(Object obj, String prop_name) 583 throws IntrospectionException { 584 return getter(obj.getClass(), obj, prop_name); 585 } 586 593 public Component setter(Object obj, String prop_name) 594 throws IntrospectionException { 595 return setter(obj.getClass(), obj, prop_name); 596 } 597 605 public Component getter(Object obj, String prop_name, int ind) 606 throws IntrospectionException { 607 return getter(obj.getClass(), obj, prop_name, ind); 608 } 609 617 public Component setter(Object obj, String prop_name, int ind) 618 throws IntrospectionException { 619 return setter(obj.getClass(), obj, prop_name, ind); 620 } 621 } 622 | Popular Tags |