/** * Returns class for this context. There is no target for constructors. * * @param context The context. * @param element Method or constructor. * @param <E> Method or constructor type. * @return The class. */ protected <E extends Member & AnnotatedElement> Class<?> getClass(InvocationContext context, E element) { return context.getTarget() != null ? MetricsCdiExtension.getRealClass(context.getTarget()) : element.getDeclaringClass(); }
@PostConstruct private void inject(final InvocationContext context) throws Exception { final Object beanInstance = context.getTarget(); injectionManager.inject(beanInstance, CdiComponentProvider.CDI_CLASS_ANALYZER); // Invoke next interceptor in chain context.proceed(); } }
@Override protected String getRequestName(InvocationContext context) { final Method method = context.getMethod(); final Object target = context.getTarget(); return target.getClass().getSimpleName() + '.' + method.getName(); } }
/** * Returns a key for a command. Keys are specific to the pair of instance (target) * and the method being called. * * @return A command key. */ private String createCommandKey() { return method.getName() + Objects.hash(context.getTarget(), context.getMethod().hashCode()); } }
/** * Intercepts a call to bean method annotated by any of the fault tolerance * annotations. * * @param context Invocation context. * @return Whatever the intercepted method returns. * @throws Throwable If a problem occurs. */ @AroundInvoke public Object interceptCommand(InvocationContext context) throws Throwable { try { LOGGER.fine("Interceptor called for '" + context.getTarget().getClass() + "::" + context.getMethod().getName() + "'"); // Create method introspector and executer retrier final MethodIntrospector introspector = new MethodIntrospector( context.getTarget().getClass(), context.getMethod()); final CommandRetrier retrier = new CommandRetrier(context, introspector); return retrier.execute(); } catch (Throwable t) { LOGGER.fine("Throwable caught by interceptor '" + t.getMessage() + "'"); throw t; } } }
/** * Constructor. * * @param context Invocation context. * @param introspector Method introspector. * @param throwable Throwable that resulted in this fallback being called. */ CommandFallback(InvocationContext context, MethodIntrospector introspector, Throwable throwable) { this.context = context; this.throwable = throwable; // Establish fallback strategy final Fallback fallback = introspector.getFallback(); if (fallback.value() != Fallback.DEFAULT.class) { handlerClass = fallback.value(); } else if (!fallback.fallbackMethod().isEmpty()) { Object instance = context.getTarget(); try { fallbackMethod = instance.getClass().getMethod(introspector.getFallback().fallbackMethod(), context.getMethod().getParameterTypes()); } catch (NoSuchMethodException e) { throw new InternalError(e); // should have been validated } } else { handlerClass = Fallback.DEFAULT.class; } }
result = fallbackMethod.invoke(context.getTarget(), context.getParameters());
@AroundConstruct public Object onConstruct(InvocationContext context) throws Exception { // null before the InvocationContext.proceed() returns Object target = context.getTarget(); isNull(target); // null in case of AroundConstruct Method method = context.getMethod(); isNull(method); // NOT null in case of AroundConstruct Constructor ctor = context.getConstructor(); isNotNull(ctor); // perform the constructor injection Object result = context.proceed(); isNull(result); // NOT null after the InvocationContext.proceed() completes target = context.getTarget(); isNotNull(target); // a constructor should have been called GreetingBean bean = (GreetingBean) target; isBoolean(bean.isConstructed(), true); isBoolean(bean.isInitialized(), false); // constructor injection should have been done isNotNull(bean.getParam()); return null; }
/** * This interceptor will catch all exception and convert them using the exceptionhandler * @param ctx the invocation context * @return the result * @throws JaxRsResponseException an exception that can be handled by a jee container */ @AroundInvoke public Object intercept(final InvocationContext ctx) throws JaxRsResponseException { try { return ctx.proceed(); } catch (final Exception theException) { final AbstractJaxRsProvider theServer = (AbstractJaxRsProvider) ctx.getTarget(); throw convertException(theServer, theException); } }
private Map<String, Object> extractVariables(StartProcess startProcessAnnotation, InvocationContext ctx) throws Exception { VariableMap variables = new VariableMapImpl(); for (Field field : ctx.getMethod().getDeclaringClass().getDeclaredFields()) { if (!field.isAnnotationPresent(ProcessVariable.class) && !field.isAnnotationPresent(ProcessVariableTyped.class)) { continue; } field.setAccessible(true); String fieldName = null; ProcessVariable processStartVariable = field.getAnnotation(ProcessVariable.class); if (processStartVariable != null) { fieldName = processStartVariable.value(); } else { ProcessVariableTyped processStartVariableTyped = field.getAnnotation(ProcessVariableTyped.class); fieldName = processStartVariableTyped.value(); } if (fieldName == null || fieldName.length() == 0) { fieldName = field.getName(); } Object value = field.get(ctx.getTarget()); variables.put(fieldName, value); } return variables; }
@Override public Object getTarget() { return delegate.getTarget(); }
Object createdObject = ctx.getTarget();
@AroundInvoke public Object intercept(InvocationContext ctx) throws Exception { Object target = ctx.getTarget(); if (target instanceof Foo) { ((Foo) target).inspections.add(this.getClass().getName()); } return ctx.proceed(); }
@Override protected String getRequestName(InvocationContext context) { final Method method = context.getMethod(); final Object target = context.getTarget(); return target.getClass().getSimpleName() + '.' + method.getName(); } }
@PostConstruct public void postConstruct(InvocationContext ctx) { if (ctx.getTarget() instanceof Foo) { ActionSequence.addAction("postConstruct", NAME); } try { ctx.proceed(); } catch (Throwable e) { throw new RuntimeException(e); } }
@AroundInvoke public Object aroundInvoke(InvocationContext ctx) throws Exception { SimpleBean target = (SimpleBean) ctx.getTarget(); int id1 = target.getId(); int id2 = (Integer) ctx.proceed(); if (id1 == id2) { getTargetOK = true; } return id1; }
@AroundInvoke public Object logMethod(InvocationContext ic) throws Exception { logger.entering(ic.getTarget().toString(), ic.getMethod().getName()); logger.severe(">>>" + ic.getTarget().toString() + " - " + ic.getMethod().getName()); try { return ic.proceed(); } finally { logger.severe("<<<" + ic.getTarget().toString() + " - " + ic.getMethod().getName()); logger.exiting(ic.getTarget().toString(), ic.getMethod().getName()); } } }
@PostConstruct public void postConstruct(InvocationContext ctx) { Rocket target = (Rocket) ctx.getTarget(); assertNotNull(target.getFoo()); ActionSequence.addAction("postConstruct", DestructionInterceptor.class.getSimpleName()); try { ctx.proceed(); } catch (Throwable e) { throw new RuntimeException(e); } }
private ELProcessor getElProcessor(InvocationContext invocationContext, HttpMessageContext httpMessageContext) { ELProcessor elProcessor = new ELProcessor(); elProcessor.getELManager().addELResolver(beanManager.getELResolver()); elProcessor.defineBean("self", invocationContext.getTarget()); elProcessor.defineBean("httpMessageContext", httpMessageContext); return elProcessor; }