/** * Instantiates <em>unmanaged instances</em> of HealthCheckProcedure and * handle manually their CDI creation lifecycle. * Add them to the {@link Monitor}. */ private void afterDeploymentValidation(@Observes final AfterDeploymentValidation abd, BeanManager beanManager) { try { for (AnnotatedType delegate : delegates) { Unmanaged<HealthCheck> unmanagedHealthCheck = new Unmanaged<HealthCheck>(beanManager, delegate.getJavaClass()); Unmanaged.UnmanagedInstance<HealthCheck> healthCheckInstance = unmanagedHealthCheck.newInstance(); HealthCheck healthCheck = healthCheckInstance.produce().inject().postConstruct().get(); healthChecks.add(healthCheck); healthCheckInstances.add(healthCheckInstance); monitor.registerHealthBean(healthCheck); log.info(">> Added health bean impl " + healthCheck); } // we don't need the references anymore delegates.clear(); } catch (Exception e) { throw new RuntimeException("Failed to register health bean", e); } }
Supplier<Object> getFallback(ExecutionContextWithInvocationContext ctx) { if (!hasFallback()) { return null; } else if (unmanaged != null) { return () -> { Unmanaged.UnmanagedInstance<FallbackHandler<?>> unmanagedInstance = unmanaged.newInstance(); FallbackHandler<?> handler = unmanagedInstance.produce().inject().postConstruct().get(); try { return handler.handle(ctx); } finally { // The instance exists to service a single invocation only unmanagedInstance.preDestroy().dispose(); } }; } else { return () -> { try { return fallbackMethod.invoke(ctx.getTarget(), ctx.getParameters()); } catch (IllegalAccessException | InvocationTargetException e) { throw new FaultToleranceException("Error during fallback method invocation", e); } }; } }
@Override public Producer createProducer() throws IllegalAccessException { // FIXME: to be replaced once event firing with dynamic parameterized type // is properly supported (see https://issues.jboss.org/browse/CDI-516) TypeLiteral<T> literal = new TypeLiteral<T>() {}; for (Field field : TypeLiteral.class.getDeclaredFields()) { if (field.getType().equals(Type.class)) { field.setAccessible(true); field.set(literal, type); break; } } return new CdiEventProducer<>(this, new Unmanaged<>(manager, AnyEvent.class) .newInstance().produce().inject().get() .event.select(literal, qualifiers.stream().toArray(Annotation[]::new))); }
@Test public void testUnmanaged() { Unmanaged<Car> unmanaged = new Unmanaged<Car>(Car.class); UnmanagedInstance<Car> unmanagedCar = unmanaged.newInstance(); assertNotNull("exists", unmanagedCar); UnmanagedInstance<Car> carProduced = unmanagedCar.produce(); Car car = carProduced.inject().get(); Car car2 = carProduced.inject().get(); assertEquals("same instances. Car is a simple managed bean", car, car2); Unmanaged<TaxiManager> unmanaged2 = new Unmanaged<TaxiManager>(TaxiManager.class); UnmanagedInstance<TaxiManager> unmanagedTaxi = unmanaged2.newInstance(); assertNotNull("exists", unmanagedTaxi); UnmanagedInstance<TaxiManager> taxiProduced = unmanagedTaxi.produce(); TaxiManager taxiManager = taxiProduced.inject().get(); TaxiManager taxiManager2 = taxiProduced.inject().get(); assertEquals("same instances. TaxiManager is a simple singleton", taxiManager, taxiManager2); Unmanaged<TaxiManager> unmanaged3 = new Unmanaged<TaxiManager>(TaxiManager.class); UnmanagedInstance<TaxiManager> unmanagedTaxi3 = unmanaged3.newInstance(); UnmanagedInstance<TaxiManager> unmanagedTaxi4 = unmanaged3.newInstance(); assertNotNull("exists", unmanagedTaxi3); assertNotNull("exists", unmanagedTaxi4); TaxiManager taxiManager3 = unmanagedTaxi3.produce().inject().get(); TaxiManager taxiManager4 = unmanagedTaxi4.produce().inject().get(); assertNotEquals("different instances. TaxiManager is a new singleton", unmanagedTaxi3, unmanagedTaxi4); assertNotEquals("different instances. TaxiManager is a new singleton", taxiManager3, taxiManager4); }
/** * Instantiates <em>unmanaged instances</em> of HealthCheckProcedure and * handle manually their CDI creation lifecycle. * Add them to the {@link HealthMonitor}. */ private void afterDeploymentValidation(@Observes final AfterDeploymentValidation avd, BeanManager bm) { for (AnnotatedType delegate : delegates) { try { Unmanaged<HealthCheck> unmanagedHealthCheck = new Unmanaged<HealthCheck>(bm, delegate.getJavaClass()); UnmanagedInstance<HealthCheck> healthCheckInstance = unmanagedHealthCheck.newInstance(); HealthCheck healthCheck = healthCheckInstance.produce().inject().postConstruct().get(); healthChecks.add(healthCheck); healthCheckInstances.add(healthCheckInstance); healthMonitor.addHealthCheck(healthCheck); } catch (Exception e) { throw new RuntimeException("Failed to register health bean", e); } } }
public static Bindings getBindings(Class clazz) throws InstantiationException, IllegalAccessException { Stub stub = (Stub) new Unmanaged(clazz) .newInstance() .produce() .inject() .postConstruct() .get(); return stub.getBindings(); }
@Override public Object newInstance(Class<?> clazz) throws IllegalAccessException, InvocationTargetException, NamingException, InstantiationException { try { Unmanaged.UnmanagedInstance<?> instance = new Unmanaged<>(beanManager, clazz).newInstance(); instance.produce().inject().postConstruct(); Object o = instance.get(); instanceMap.put(o, instance); return o; } catch (Exception e) { return clazz.newInstance(); } }
@Override public void destroyInstance(Object o) throws IllegalAccessException, InvocationTargetException { Unmanaged.UnmanagedInstance remove = instanceMap.remove(o); if (remove != null) { remove.preDestroy(); remove.dispose(); } }
@Override public InstanceHandle<T> createInstance() { try { return new HammockInstanceHandle<>(new Unmanaged<>(beanManager, clazz).newInstance()); } catch (Exception e) { try { return new BasicInstanceFactory<T>(clazz.newInstance()); } catch (Exception ex) { throw new RuntimeException("Unable to instantiate "+clazz, ex); } } }
private Unmanaged<FallbackHandler<?>> initUnmanaged(FaultToleranceOperation operation) { if (operation.hasFallback()) { return new Unmanaged<>(beanManager, operation.getFallback().get(FallbackConfig.VALUE)); } return null; }
HammockInstanceHandle(Unmanaged.UnmanagedInstance<T> instance) { this.instance = instance; instance.produce().inject().postConstruct(); }
public Unmanageable(Class<T> clazz) { this.instance = new Unmanaged<T>(clazz).newInstance(); this.instance.produce().inject().postConstruct(); }