@Override public Object getActualBeanReference(final Object ref) { return Factory.maybeUnwrapProxy(ref); }
@Component(immediate=true) @Instantiate public class Controller { @Requires(filter="(factory.name=A)") Factory factoryOfA; @Requires(filter="(factory.name=B)") Factory factoryOfB; ComponentInstance instance; @Validate public void createA() throws Exception { instance = factoryOfA.createInstance(null); } public void switchToB() throws Exception { instance.dispose(); instance = factoryOfB.createInstance(null); } }
/** * @return Returns null if a proxy cannot be created. */ protected <T> Proxy<T> getOrCreateProxy(final String factoryName) { // TODO this will not work for @Dependent proxied beans. @SuppressWarnings("unchecked") Proxy<T> proxy = (Proxy<T>) proxies.get(factoryName); if (proxy == null) { final Factory<T> factory = getFactory(factoryName); proxy = factory.createProxy(this); if (proxy != null) { proxies.put(factoryName, proxy); } } return proxy; }
@SuppressWarnings("unchecked") public <P> P getReferenceAs(final T instance, final String referenceName, final Class<P> type) { return (P) getInstanceRefMap(maybeUnwrapProxy(instance)).get(referenceName); }
/** * This method performs any cleanup required for destroying a type. It will * invoke generated statements from decorators, invoke disposers or predestroy * methods, and destroy and {@link Dependent} scoped dependencies. * * @param instance The instance being destroyed. * @param contextManager For destroying dependencies. */ @SuppressWarnings("unchecked") public void destroyInstance(final Object instance, final ContextManager contextManager) { final Object unwrapped = maybeUnwrapProxy(instance); generatedDestroyInstance(unwrapped, contextManager); referenceMaps.remove(unwrapped); for (final Object depRef : dependentScopedDependencies.get((T) unwrapped)) { contextManager.destroy(depRef); } dependentScopedDependencies.removeAll(instance); }
@Override public <T> T getContextualInstance(final String factoryName, final Class<?>[] typeArgs, final Annotation[] qualifiers) { final Factory<T> factory = this.<T>getFactory(factoryName); final T instance = factory.createContextualInstance(getContextManager(), typeArgs, qualifiers); registerInstance(maybeUnwrapProxy(instance), factory); return instance; }
@Override public <T> T getNewInstance(final String factoryName) { final Factory<T> factory = getFactory(factoryName); final Proxy<T> proxy = factory.createProxy(this); final T instance = factory.createInstance(getContextManager()); if (proxy != null) { proxy.setInstance(instance); } factory.invokePostConstructs(instance); registerInstance(instance, factory); return (proxy != null) ? proxy.asBeanType() : instance; }
protected <T> T createNewUnproxiedInstance(final String factoryName) { final Factory<T> factory = this.<T>getFactory(factoryName); registerIncompleteInstance(factoryName); final T instance = factory.createInstance(getContextManager()); unregisterIncompleteInstance(factoryName, instance); registerInstance(instance, factory); factory.invokePostConstructs(instance); return instance; }
@SuppressWarnings({ "rawtypes", "unchecked" }) @Override public void destroyInstance(final Object instance) { if (isManaged(instance)) { beforeDestroyInstance(instance); final Object unwrapped = maybeUnwrap(instance); final Deque<Factory<?>> factories = factoriesByCreatedInstances.remove(unwrapped); while (factories != null && !factories.isEmpty()) { final Factory<?> factory = factories.pop(); for (final DestructionCallback callback : destructionCallbacksByInstance.removeAll(unwrapped)) { callback.destroy(unwrapped); } factory.destroyInstance(unwrapped, contextManager); } afterDestroyInstance(instance); } }
private Object maybeUnwrap(final Object instance) { return Factory.maybeUnwrapProxy(instance); }
@Override public <T> boolean endInstanceLifecycle(final T instance) { return activeListenerMap.remove(Factory.maybeUnwrapProxy(instance)) != null; }
protected <D> D registerDependentScopedReference(final T instance, final D dependentScopedBeanRef) { dependentScopedDependencies.put(maybeUnwrapProxy(instance), dependentScopedBeanRef); return dependentScopedBeanRef; }
public static boolean containsKey(Object bean) { bean = Factory.maybeUnwrapProxy(bean); return widgetBeanMap.containsKey(bean); }
public static boolean containsKey(Object bean) { bean = Factory.maybeUnwrapProxy(bean); return widgetBeanMap.containsKey(bean); }
private Map<String, Object> getInstanceRefMap(final T instance) { Map<String, Object> map = referenceMaps.get(maybeUnwrapProxy(instance)); if (map == null) { map = new HashMap<>(); referenceMaps.put(instance, map); } return map; }
public String getRealBeanClass(LayoutDragComponent instance) { return Factory.maybeUnwrapProxy(instance).getClass().getName(); }
public static void remove(Object obj) { obj = Factory.maybeUnwrapProxy(obj); final Pair pair = widgetBeanMap.get(obj); if (pair != null) { widgetBeanMap.remove(pair.bean); widgetBeanMap.remove(pair.widget); } }
public String getRealBeanClass(LayoutDragComponent instance) { return Factory.maybeUnwrapProxy(instance).getClass().getName(); }
public static void remove(Object obj) { obj = Factory.maybeUnwrapProxy(obj); final Pair pair = widgetBeanMap.get(obj); if (pair != null) { widgetBeanMap.remove(pair.bean); widgetBeanMap.remove(pair.widget); } }
public static TemplateWidget get(Object bean) { bean = Factory.maybeUnwrapProxy(bean); final Pair pair = widgetBeanMap.get(bean); if (pair == null) { throw new RuntimeException("There is no widget mapped to the " + bean.getClass().getName() + " bean: " + bean.toString()); } else { return pair.widget; } }