/** * {@inheritDoc} */ @Override public Object dispatchSpecialMethodBeforeBody(long id, Object object, Object[] parameters) { try { SpecialSensorConfig ssc = specialMappings.get(id); IMethodSensor methodSensor = ssc.getSensor(); ISpecialHook specialHook = (ISpecialHook) methodSensor.getHook(); Object result = specialHook.beforeBody(id, object, parameters, ssc); if (null != result) { return result; } } catch (Throwable throwable) { // NOPMD log.error("An error happened in the Hook Dispatcher! (before special method)", throwable); } return null; }
/** * {@inheritDoc} */ @Override public Object dispatchSpecialMethodAfterBody(long id, Object object, Object[] parameters, Object returnValue) { try { SpecialSensorConfig ssc = specialMappings.get(id); IMethodSensor methodSensor = ssc.getSensor(); ISpecialHook specialHook = (ISpecialHook) methodSensor.getHook(); Object result = specialHook.afterBody(id, object, parameters, returnValue, ssc); if (null != result) { return result; } } catch (Throwable throwable) { // NOPMD log.error("An error happened in the Hook Dispatcher! (after special method)", throwable); } return null; }
@Test public void dispatchOneSpecialHook() { long sensorTypeId = 7L; IMethodSensor methodSensor = mock(IMethodSensor.class); ISpecialHook specialHook = mock(ISpecialHook.class); MethodSensorTypeConfig methodSensorConfig = mock(MethodSensorTypeConfig.class); when(methodSensor.getHook()).thenReturn(specialHook); when(methodSensor.getSensorTypeConfig()).thenReturn(methodSensorConfig); when(methodSensorConfig.getId()).thenReturn(sensorTypeId); SpecialSensorConfig specialSensorConfig = mock(SpecialSensorConfig.class); when(specialSensorConfig.getSensor()).thenReturn(methodSensor); int methodId = 3; Object object = mock(Object.class); Object[] parameters = new Object[0]; Object returnValue = mock(Object.class); hookDispatcher.addMapping(methodId, specialSensorConfig); Object result = hookDispatcher.dispatchSpecialMethodBeforeBody(methodId, object, parameters); verify(specialSensorConfig, times(1)).getSensor(); verify(specialHook, times(1)).beforeBody(methodId, object, parameters, specialSensorConfig); assertThat(result, is(nullValue())); result = hookDispatcher.dispatchSpecialMethodAfterBody(methodId, object, parameters, returnValue); verify(specialSensorConfig, times(2)).getSensor(); verify(specialHook, times(1)).afterBody(methodId, object, parameters, returnValue, specialSensorConfig); assertThat(result, is(nullValue())); verifyZeroInteractions(object, coreService, returnValue); verifyNoMoreInteractions(specialSensorConfig, specialHook); }
ISpecialHook specialHook = mock(ISpecialHook.class); MethodSensorTypeConfig methodSensorConfig = mock(MethodSensorTypeConfig.class); when(methodSensor.getHook()).thenReturn(specialHook); when(methodSensor.getSensorTypeConfig()).thenReturn(methodSensorConfig); when(methodSensorConfig.getId()).thenReturn(sensorTypeId);
@Test public void dispatchOneConstructorHookWithoutInvocationTrace() { long sensorTypeId = 7L; IConstructorHook constructorHook = mock(IConstructorHook.class); IMethodSensor methodSensor = mock(IMethodSensor.class); MethodSensorTypeConfig methodSensorConfig = mock(MethodSensorTypeConfig.class); when(methodSensor.getHook()).thenReturn(constructorHook); when(methodSensor.getSensorTypeConfig()).thenReturn(methodSensorConfig); when(methodSensorConfig.getId()).thenReturn(sensorTypeId); RegisteredSensorConfig registeredSensorConfig = mock(RegisteredSensorConfig.class); when(registeredSensorConfig.getMethodSensors()).thenReturn(Collections.singletonList(methodSensor)); when(registeredSensorConfig.getMethodSensorsReverse()).thenReturn(Collections.singletonList(methodSensor)); int methodId = 3; Object object = mock(Object.class); Object[] parameters = new Object[0]; hookDispatcher.addMapping(methodId, registeredSensorConfig); hookDispatcher.dispatchConstructorBeforeBody(methodId, parameters); verify(registeredSensorConfig, times(1)).isStartsInvocation(); verify(registeredSensorConfig, times(1)).getMethodSensorsReverse(); verify(constructorHook, times(1)).beforeConstructor(methodId, sensorTypeId, parameters, registeredSensorConfig); hookDispatcher.dispatchConstructorAfterBody(methodId, object, parameters); verify(registeredSensorConfig, times(2)).isStartsInvocation(); verify(registeredSensorConfig, times(1)).getMethodSensors(); verify(constructorHook, times(1)).afterConstructor(coreService, methodId, sensorTypeId, object, parameters, registeredSensorConfig); verifyZeroInteractions(object, coreService); verifyNoMoreInteractions(registeredSensorConfig, constructorHook); }
IConstructorHook constructorHook = (IConstructorHook) methodSensor.getHook(); constructorHook.beforeConstructor(id, methodSensor.getSensorTypeConfig().getId(), parameters, rsc);
IMethodHook methodHook = mock(IMethodHook.class); MethodSensorTypeConfig methodSensorConfig = mock(MethodSensorTypeConfig.class); when(methodSensor.getHook()).thenReturn(methodHook); when(methodSensor.getSensorTypeConfig()).thenReturn(methodSensorConfig); when(methodSensorConfig.getId()).thenReturn(sensorTypeId);
IMethodHook methodHook = (IMethodHook) methodSensor.getHook(); methodHook.beforeBody(id, methodSensor.getSensorTypeConfig().getId(), object, parameters, rsc);
when(methodSensorConfigTwo.getId()).thenReturn(sensorTypeIdTwo); when(methodSensorConfigThree.getId()).thenReturn(sensorTypeIdThree); when(methodSensorOne.getHook()).thenReturn(constructorHookOne); when(methodSensorTwo.getHook()).thenReturn(constructorHookTwo); when(methodSensorThree.getHook()).thenReturn(constructorHookThree); when(methodSensorOne.getSensorTypeConfig()).thenReturn(methodSensorConfigOne); when(methodSensorTwo.getSensorTypeConfig()).thenReturn(methodSensorConfigTwo);
when(methodSensorConfigTwo.getId()).thenReturn(sensorTypeIdTwo); when(methodSensorConfigThree.getId()).thenReturn(sensorTypeIdThree); when(methodSensorOne.getHook()).thenReturn(methodHookOne); when(methodSensorTwo.getHook()).thenReturn(methodHookTwo); when(methodSensorThree.getHook()).thenReturn(methodHookThree); when(methodSensorOne.getSensorTypeConfig()).thenReturn(methodSensorConfigOne); when(methodSensorTwo.getSensorTypeConfig()).thenReturn(methodSensorConfigTwo);
/** * {@inheritDoc} */ @Override public void dispatchFirstMethodAfterBody(long id, Object object, Object[] parameters, Object returnValue, boolean exception) { if (!executionMarker.isActive()) { try { executionMarker.active(); try { RegisteredSensorConfig rsc = mappings.get(id); // Now iterate over all registered sensor types and execute them // normal execution (sensor with highest priority first) for (IMethodSensor methodSensor : rsc.getMethodSensors()) { IMethodHook methodHook = (IMethodHook) methodSensor.getHook(); methodHook.firstAfterBody(id, methodSensor.getSensorTypeConfig().getId(), object, parameters, returnValue, exception, rsc); } } catch (Throwable throwable) { // NOPMD log.error("An error happened in the Hook Dispatcher! (after body)", throwable); } } finally { executionMarker.deactive(); } } }
IMethodHook methodHook = mock(IMethodHook.class); MethodSensorTypeConfig methodSensorConfig = mock(MethodSensorTypeConfig.class); when(methodSensor.getHook()).thenReturn(methodHook); when(methodSensor.getSensorTypeConfig()).thenReturn(methodSensorConfig); when(methodSensorConfig.getId()).thenReturn(methodSensorTypeId); verify(registeredSensorConfig, times(1)).isStartsInvocation(); verify(registeredSensorConfig, times(1)).getMethodSensorsReverse(); verify(methodSensor, times(1)).getHook(); verify(methodHook, times(1)).beforeBody(methodId, methodSensorTypeId, object, parameters, registeredSensorConfig); verify(invocHook, times(1)).beforeBody(methodId, invocSensorTypeId, object, parameters, registeredSensorConfig); verify(registeredSensorConfigTwo, times(1)).isStartsInvocation(); verify(registeredSensorConfigTwo, times(1)).getMethodSensorsReverse(); verify(methodSensor, times(2)).getHook(); verify(methodHook, times(1)).beforeBody(methodIdTwo, methodSensorTypeId, object, parameters, registeredSensorConfigTwo); verify(invocHook, times(1)).beforeBody(eq(methodIdTwo), anyLong(), eq(object), eq(parameters), eq(registeredSensorConfigTwo)); verify(methodSensor, times(3)).getHook(); verify(methodHook, times(1)).firstAfterBody(methodIdTwo, methodSensorTypeId, object, parameters, returnValue, exception, registeredSensorConfigTwo); verify(registeredSensorConfigTwo, times(2)).isStartsInvocation(); verify(registeredSensorConfigTwo, times(2)).getMethodSensors(); verify(methodSensor, times(4)).getHook(); verify(methodHook, times(1)).secondAfterBody(invocHook, methodIdTwo, methodSensorTypeId, object, parameters, returnValue, exception, registeredSensorConfigTwo); verify(invocHook, times(1)).secondAfterBody(eq(coreService), eq(methodIdTwo), anyLong(), eq(object), eq(parameters), eq(returnValue), eq(exception), eq(registeredSensorConfigTwo)); verify(methodSensor, times(5)).getHook(); verify(methodHook, times(1)).firstAfterBody(methodId, methodSensorTypeId, object, parameters, returnValue, false, registeredSensorConfig);
IMethodSensor methodSensor = mock(IMethodSensor.class); MethodSensorTypeConfig methodSensorConfig = mock(MethodSensorTypeConfig.class); when(methodSensor.getHook()).thenReturn(constructorHook); when(methodSensor.getSensorTypeConfig()).thenReturn(methodSensorConfig); when(methodSensorConfig.getId()).thenReturn(methodSensorTypeId); verify(registeredSensorConfigTwo, times(1)).isStartsInvocation(); verify(registeredSensorConfigTwo, times(1)).getMethodSensorsReverse(); verify(methodSensor, times(1)).getHook(); verify(constructorHook, times(1)).beforeConstructor(methodIdTwo, methodSensorTypeId, parameters, registeredSensorConfigTwo); verify((IConstructorHook) invocHook, times(1)).beforeConstructor(eq(methodIdTwo), anyLong(), eq(parameters), eq(registeredSensorConfigTwo)); verify(registeredSensorConfigTwo, times(2)).isStartsInvocation(); verify(registeredSensorConfigTwo, times(1)).getMethodSensors(); verify(methodSensor, times(2)).getHook(); verify(constructorHook, times(1)).afterConstructor(invocHook, methodIdTwo, methodSensorTypeId, object, parameters, registeredSensorConfigTwo); verify((IConstructorHook) invocHook, times(1)).afterConstructor(eq(coreService), eq(methodIdTwo), anyLong(), eq(object), eq(parameters), eq(registeredSensorConfigTwo));
MethodSensorTypeConfig methodSensorConfig = mock(MethodSensorTypeConfig.class); when(methodSensor.getSensorTypeConfig()).thenReturn(methodSensorConfig); when(methodSensor.getHook()).thenReturn(methodHook); when(methodSensorConfig.getId()).thenReturn(sensorTypeId);
when(methodSensorConfigTwo.getId()).thenReturn(sensorTypeIdTwo); when(methodSensorConfigThree.getId()).thenReturn(sensorTypeIdThree); when(methodSensorOne.getHook()).thenReturn(methodHookOne); when(methodSensorTwo.getHook()).thenReturn(methodHookTwo); when(methodSensorThree.getHook()).thenReturn(methodHookThree); when(methodSensorOne.getSensorTypeConfig()).thenReturn(methodSensorConfigOne); when(methodSensorTwo.getSensorTypeConfig()).thenReturn(methodSensorConfigTwo);
MethodSensorTypeConfig methodSensorConfig = mock(MethodSensorTypeConfig.class); when(methodSensor.getSensorTypeConfig()).thenReturn(methodSensorConfig); when(methodSensor.getHook()).thenReturn(methodHook); when(methodSensorConfig.getId()).thenReturn(methodSensorTypeId);