/** * Destroy the instance. * * @param contextual the contextual. */ public void destroy(Contextual contextual) { String scopeId = (String) request.getAttribute(SCOPE_ID); if (null != scopeId) { HttpSession session = request.getSession(); if (contextual instanceof PassivationCapable == false) { throw new RuntimeException("Unexpected type for contextual"); } PassivationCapable pc = (PassivationCapable) contextual; final String sessionKey = SCOPE_ID + "-" + scopeId; Map<String, Object> scopeMap = (Map<String, Object>) session.getAttribute(sessionKey); if (null != scopeMap) { Object instance = scopeMap.get(INSTANCE + pc.getId()); CreationalContext<?> creational = (CreationalContext<?>) scopeMap.get(CREATIONAL + pc.getId()); if (null != instance && null != creational) { contextual.destroy(instance, creational); creational.release(); } } } }
/** * Get the instance. * * @param <T> the type. * @param contextual the contextual. * @return the instance, or null. */ public <T> T get(Contextual<T> contextual) { T result = null; String scopeId = (String) request.getAttribute(SCOPE_ID); if (null != scopeId) { HttpSession session = request.getSession(); if (contextual instanceof PassivationCapable == false) { throw new RuntimeException("Unexpected type for contextual"); } PassivationCapable pc = (PassivationCapable) contextual; final String sessionKey = SCOPE_ID + "-" + scopeId; Map<String, Object> scopeMap = (Map<String, Object>) session.getAttribute(sessionKey); if (null != scopeMap) { result = (T) scopeMap.get(INSTANCE + pc.getId()); } else { request.setAttribute(SCOPE_ID, null); // old cookie, force new scope generation } } return result; }
/** * Get the instance (create it if it does not exist). * * @param <T> the type. * @param contextual the contextual. * @param creational the creational. * @return the instance. */ public <T> T get(Contextual<T> contextual, CreationalContext<T> creational) { T result = get(contextual); if (result == null) { String scopeId = (String) request.getAttribute(SCOPE_ID); if (null == scopeId) { scopeId = generateScopeId(); } HttpSession session = request.getSession(); result = contextual.create(creational); if (contextual instanceof PassivationCapable == false) { throw new RuntimeException("Unexpected type for contextual"); } PassivationCapable pc = (PassivationCapable) contextual; final String sessionKey = SCOPE_ID + "-" + scopeId; Map<String, Object> scopeMap = (Map<String, Object>) session.getAttribute(sessionKey); if (null != scopeMap) { session.setAttribute(sessionKey, scopeMap); scopeMap.put(INSTANCE + pc.getId(), result); scopeMap.put(CREATIONAL + pc.getId(), creational); } } return result; }
@Override public boolean accept(final Bean<?> bean) { if (BuiltInOwbBean.class.isInstance(bean) || ExtensionBean.class.isInstance(bean)) { return false; } if (OwbBean.class.isInstance(bean)) { final OwbBean owbBean = OwbBean.class.cast(bean); if (owbBean.isPassivationCapable()) { if (hasBean(owbBean.getId())) { return false; } } } else if (PassivationCapable.class.isInstance(bean)) { if (hasBean(PassivationCapable.class.cast(bean).getId())) { return false; } } final Set<Annotation> qualifiers = bean.getQualifiers(); return beanManager.getBeans( bean.getBeanClass(), qualifiers.isEmpty() ? EMPTY_ANNOTATIONS : qualifiers.toArray(new Annotation[qualifiers.size()])).isEmpty(); }
private void writeObject(ObjectOutputStream out) throws IOException { if (!(bean instanceof PassivationCapable)) { throw new NotSerializableException("Bean is not PassivationCapable: " + bean.toString()); } String passivationId = ((PassivationCapable) bean).getId(); if (passivationId == null) { throw new NotSerializableException(bean.toString()); } out.writeLong(serialVersionUID); out.writeObject(passivationId); out.writeObject(instance); out.writeObject(creationalContext); }
@Override public <T> T get(Contextual<T> bean) { try { return super.get(bean); } finally { if (bean instanceof PassivationCapable) { PassivationCapable pc = (PassivationCapable) bean; viewAccessBeanAccessHistory.getAccessedBeans().add(pc.getId()); } } }
@Override public <T> T get(Contextual<T> bean, CreationalContext<T> creationalContext) { try { return super.get(bean, creationalContext); } finally { if (bean instanceof PassivationCapable) { PassivationCapable pc = (PassivationCapable) bean; viewAccessBeanAccessHistory.getAccessedBeans().add(pc.getId()); } } }
private synchronized void init() { if (this.deltaSpikeProxyInvocationHandler == null) { this.deltaSpikeProxyInvocationHandler = BeanProvider.getContextualReference( beanManager, DeltaSpikeProxyInvocationHandler.class, false); Set<Bean<H>> handlerBeans = BeanProvider.getBeanDefinitions( delegateInvocationHandlerClass, false, true, beanManager); if (handlerBeans.size() != 1) { StringBuilder beanInfo = new StringBuilder(); for (Bean<H> bean : handlerBeans) { if (beanInfo.length() != 0) { beanInfo.append(", "); } beanInfo.append(bean); if (bean instanceof PassivationCapable) { beanInfo.append(" bean-id: ").append(((PassivationCapable) bean).getId()); } } throw new IllegalStateException(handlerBeans.size() + " beans found for " + delegateInvocationHandlerClass + " found beans: " + beanInfo.toString()); } this.handlerBean = handlerBeans.iterator().next(); } }
@Override public <T> T get(Contextual<T> bean, CreationalContext<T> creationalContext) { if (creationalContext == null) { return get(bean); } checkActive(); if (passivatingScope) { if (!(bean instanceof PassivationCapable)) { throw new IllegalStateException(bean.toString() + " doesn't implement " + PassivationCapable.class.getName()); } } ContextualStorage storage = getContextualStorage(bean, true); Map<Object, ContextualInstanceInfo<?>> contextMap = storage.getStorage(); ContextualInstanceInfo<?> contextualInstanceInfo = contextMap.get(storage.getBeanKey(bean)); if (contextualInstanceInfo != null) { @SuppressWarnings("unchecked") final T instance = (T) contextualInstanceInfo.getContextualInstance(); if (instance != null) { return instance; } } return storage.createContextualInstance(bean, creationalContext); }
/** * If the context is a passivating scope then we return * the passivationId of the Bean. Otherwise we use * the Bean directly. * @return the key to use in the context map */ public <T> Object getBeanKey(Contextual<T> bean) { if (passivationCapable) { // if the return ((PassivationCapable) bean).getId(); } return bean; }
public static ConversationKey convertToConversationKey(Contextual<?> contextual, BeanManager beanManager) { if (!(contextual instanceof Bean)) { if (contextual instanceof PassivationCapable) { contextual = beanManager.getPassivationCapableBean(((PassivationCapable) contextual).getId()); } else { throw new IllegalArgumentException( contextual.getClass().getName() + " is not of type " + Bean.class.getName()); } } Bean<?> bean = (Bean<?>) contextual; //don't cache it (due to the support of different producers) ConversationGroup conversationGroupAnnotation = findConversationGroupAnnotation(bean); Class<?> conversationGroup; if (conversationGroupAnnotation != null) { conversationGroup = conversationGroupAnnotation.value(); } else { conversationGroup = bean.getBeanClass(); } Set<Annotation> qualifiers = bean.getQualifiers(); return new ConversationKey(conversationGroup, qualifiers.toArray(new Annotation[qualifiers.size()])); }