/** Call destroy */ @Override public void destroy(T instance, CreationalContext<T> cxt) { if (_producer == _methodProducer) _methodProducer.destroy(instance, (CreationalContextImpl<T>) cxt); else _producer.dispose(instance); if (cxt instanceof CreationalContextImpl<?>) { CreationalContextImpl<?> env = (CreationalContextImpl<?>) cxt; env.clearTarget(); } cxt.release(); }
/** Produces a new bean instance */ private T produce(X factory, CreationalContextImpl<T> env) { try { // InjectManager inject = getBeanManager(); Object[] args; if (_producesArgs.length > 0) { args = new Object[_producesArgs.length]; for (int i = 0; i < args.length; i++) { if (_producesArgs[i] instanceof InjectionPointArg<?>) args[i] = env.findInjectionPoint(); else args[i] = _producesArgs[i].eval((CreationalContext) env); } } else args = NULL_ARGS; Method method = _producesMethod.getJavaMember(); if (factory instanceof ScopeProxy && !Modifier.isPublic(method.getModifiers())) { // ioc/0714 ScopeProxy proxy = (ScopeProxy) factory; factory = (X) proxy.__caucho_getDelegate(); } T value = (T) method.invoke(factory, args); env.push(value); if (value != null) return value; if (Dependent.class.equals(getScope())) return null; throw new IllegalProductException( L.l("producer {0} returned null, which is not allowed by the CDI spec.", this)); } catch (RuntimeException e) { throw e; } catch (InvocationTargetException e) { if (e.getCause() instanceof RuntimeException) throw (RuntimeException) e.getCause(); else throw new CreationException(e.getCause()); } catch (Exception e) { throw new CreationException(e); } }
/** Produces a new bean instance */ @Override public T produce(CreationalContext<T> cxt) { Class<?> type = _producerBean.getBeanClass(); // factory instance owns its own dependency chain; it's not one of the // context bean's dependencies. CreationalContextImpl<T> env = null; if (cxt instanceof CreationalContextImpl<?>) { env = (CreationalContextImpl<T>) cxt; } ProducesCreationalContext<X> factoryEnv = null; X factory = CreationalContextImpl.find(env, _producerBean); if (factory == null) { factoryEnv = new ProducesCreationalContext<X>(_producerBean, env); factory = getBeanManager().getReference(_producerBean, factoryEnv); } if (factory == null) { throw new IllegalStateException( L.l("{0}: unexpected null factory for {1}", this, _producerBean)); } T instance = produce(factory, env); if (env != null && _producerBean.getScope() == Dependent.class) { factoryEnv.release(); // _producerBean.destroy(factory, factoryEnv); } if (_isPassivating && !(instance instanceof Serializable)) throw new IllegalProductException( L.l( "'{0}' is an invalid @{1} instance because it's not serializable for bean {2}", instance, getScope().getSimpleName(), this)); return instance; }