Java Code Examples for javax.enterprise.context.spi.Contextual#create()

The following examples show how to use javax.enterprise.context.spi.Contextual#create() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RedirectScopeManager.java    From krazo with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 2
Source File: RequestContext.java    From quarkus with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public <T> T get(Contextual<T> contextual, CreationalContext<T> creationalContext) {
    if (contextual == null) {
        throw new IllegalArgumentException("Contextual parameter must not be null");
    }
    Map<Contextual<?>, ContextInstanceHandle<?>> ctx = currentContext.get();
    if (ctx == null) {
        // Thread local not set - context is not active!
        throw new ContextNotActiveException();
    }
    ContextInstanceHandle<T> instance = (ContextInstanceHandle<T>) ctx.get(contextual);
    if (instance == null && creationalContext != null) {
        // Bean instance does not exist - create one if we have CreationalContext
        instance = new ContextInstanceHandleImpl<T>((InjectableBean<T>) contextual,
                contextual.create(creationalContext), creationalContext);
        ctx.put(contextual, instance);
    }
    return instance != null ? instance.get() : null;
}
 
Example 3
Source File: ContextImpl.java    From weld-junit with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
public <T> T get(Contextual<T> contextual, CreationalContext<T> creationalContext) {
    Map<Contextual<?>, ContextualInstance<?>> ctx = currentContext.get();

    if (ctx == null) {
        // Thread local not set - context is not active!
        throw new ContextNotActiveException();
    }

    ContextualInstance<T> instance = (ContextualInstance<T>) ctx.get(contextual);

    if (instance == null && creationalContext != null) {
        // Bean instance does not exist - create one if we have CreationalContext
        instance = new ContextualInstance<T>(contextual.create(creationalContext), creationalContext, contextual);
        ctx.put(contextual, instance);
    }
    return instance != null ? instance.get() : null;
}
 
Example 4
Source File: RedirectScopeManager.java    From ozark with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 5
Source File: DeploymentContextImpl.java    From thorntail with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
public <T> T get(Contextual<T> contextual, CreationalContext<T> creationalContext) {
    Map<Contextual<?>, ContextualInstance<?>> ctx = currentContext.get();

    if (ctx == null) {
        // Thread local not set - context is not active!
        throw new ContextNotActiveException();
    }

    ContextualInstance<T> instance = (ContextualInstance<T>) ctx.get(contextual);

    if (instance == null && creationalContext != null) {
        // Bean instance does not exist - create one if we have CreationalContext
        instance = new ContextualInstance<T>(contextual.create(creationalContext), creationalContext, contextual);
        ctx.put(contextual, instance);
    }

    return instance != null ? instance.get() : null;
}
 
Example 6
Source File: PortletSessionScopedContext.java    From portals-pluto with Apache License 2.0 6 votes vote down vote up
@Override
public <T> T get(Contextual<T> bean, CreationalContext<T> crco) {
   PortletSessionBeanHolder holder = PortletSessionBeanHolder.getBeanHolder();
   
   if (holder == null) {
      throw new ContextNotActiveException("The portlet session context is not active.");
   }
   
   T inst = holder.getBean(bean);
   if (inst == null) {
      inst = bean.create(crco);
      holder.putBeanInstance(bean, crco, inst);
   }
   
   return inst;
}
 
Example 7
Source File: PortletRequestScopedBeanHolder.java    From portals-pluto with Apache License 2.0 6 votes vote down vote up
/**
 * Returns an instance for the contextual type. If no existing bean is available,
 * a new instance is created.
 * 
 * @param bean       Contextual type (Bean) for which an instance is desired
 * @return           The instance, or null if none exists
 */
@SuppressWarnings("unchecked")
public <T> T getBean(Contextual<T> bean, CreationalContext<T> crco) {
   BeanInstance<?> bi = beans.get(bean);
   
   if (bi == null) {
      
      // No bean available, so create one.
      
      BeanInstance<T> newbi = new BeanInstance<T>();
      newbi.crco = crco;
      newbi.instance = bean.create(crco);
      bi = newbi;
      beans.put(bean, newbi);

      if (isTrace) {
         StringBuilder txt = new StringBuilder(80);
         txt.append("Created bean: ");
         txt.append(((Bean<?>) bean).getBeanClass().getSimpleName());
         LOG.trace(txt.toString());
      }

   }
  
   return (T) bi.instance;
}
 
Example 8
Source File: RedirectScopedContext.java    From portals-pluto with Apache License 2.0 6 votes vote down vote up
@Override
public <T> T get(Contextual<T> bean, CreationalContext<T> crco) {
   RedirectScopedBeanHolder holder = RedirectScopedBeanHolder.getBeanHolder();
   
   if (holder == null) {
      throw new ContextNotActiveException("The redirect context is not active.");
   }
   
   T inst = holder.getBean(bean);
   if (inst == null) {
      inst = bean.create(crco);
      holder.putBeanInstance(bean, crco, inst);
   }

   return inst;
}
 
Example 9
Source File: RenderingContext.java    From trimou with Apache License 2.0 6 votes vote down vote up
@Override
public <T> T get(Contextual<T> contextual, CreationalContext<T> creationalContext) {

    checkArgumentNotNull(contextual);
    Map<Contextual<?>, ContextualInstance<?>> ctx = currentContext.get();

    if (ctx == null) {
        throw new ContextNotActiveException();
    }

    @SuppressWarnings("unchecked")
    ContextualInstance<T> instance = (ContextualInstance<T>) ctx.get(contextual);

    if (instance == null && creationalContext != null) {
        instance = new ContextualInstance<T>(contextual.create(creationalContext), creationalContext, contextual);
        ctx.put(contextual, instance);
    }

    return instance != null ? instance.get() : null;
}
 
Example 10
Source File: BaseContext.java    From incubator-batchee with Apache License 2.0 6 votes vote down vote up
@Override
public <T> T get(final Contextual<T> component, final CreationalContext<T> creationalContext) {
    checkActive();

    final ConcurrentMap<Contextual<?>, Instance<?>> storage = getOrCreateCurrentStorage();
    Instance<T> instance = (Instance<T>) storage.get(component);
    if (instance == null) {
        synchronized (this) {
            instance = (Instance<T>)  storage.get(component);
            if (instance == null) {
                final T value = component.create(creationalContext);
                instance = new Instance<T>(value, creationalContext);
                storage.putIfAbsent(component, instance);
            }
        }
    }

    return instance.value;
}
 
Example 11
Source File: TransactionContext.java    From deltaspike with Apache License 2.0 6 votes vote down vote up
public <T> T get(Contextual<T> component, CreationalContext<T> creationalContext)
{
    Map<Contextual, TransactionBeanEntry> transactionBeanEntryMap =
        TransactionBeanStorage.getInstance().getActiveTransactionContext();

    if (transactionBeanEntryMap == null)
    {
        TransactionBeanStorage.close();

        throw new ContextNotActiveException("Not accessed within a transactional method - use @" +
                Transactional.class.getName());
    }

    TransactionBeanEntry transactionBeanEntry = transactionBeanEntryMap.get(component);
    if (transactionBeanEntry != null)
    {
        return (T) transactionBeanEntry.getContextualInstance();
    }

    // if it doesn't yet exist, we need to create it now!
    T instance = component.create(creationalContext);
    transactionBeanEntry = new TransactionBeanEntry(component, instance, creationalContext);
    transactionBeanEntryMap.put(component, transactionBeanEntry);

    return instance;
}
 
Example 12
Source File: TransactionContext.java    From quarkus with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public <T> T get(Contextual<T> contextual, CreationalContext<T> creationalContext) {
    if (!isActive()) {
        throw new ContextNotActiveException();
    }
    if (contextual == null) {
        throw new IllegalArgumentException("Contextual parameter must not be null");
    }

    TransactionContextState contextState;
    contextState = (TransactionContextState) transactionSynchronizationRegistry
            .getResource(TRANSACTION_CONTEXT_MARKER);

    if (contextState == null) {
        contextState = new TransactionContextState();
        transactionSynchronizationRegistry.putResource(TRANSACTION_CONTEXT_MARKER, contextState);
    }

    ContextInstanceHandle<T> instanceHandle = contextState.get(contextual);
    if (instanceHandle != null) {
        return instanceHandle.get();
    } else if (creationalContext != null) {
        T createdInstance = contextual.create(creationalContext);
        instanceHandle = new ContextInstanceHandleImpl<>((InjectableBean<T>) contextual, createdInstance,
                creationalContext);

        contextState.put(contextual, instanceHandle);

        return createdInstance;
    } else {
        return null;
    }
}
 
Example 13
Source File: EntityManagerContext.java    From openwebbeans-meecrowave with Apache License 2.0 5 votes vote down vote up
@Override
public <T> T get(final Contextual<T> component, final CreationalContext<T> creationalContext) {
    BeanInstanceBag<T> bag = (BeanInstanceBag<T>) components.get(component);
    if (bag == null) {
        bag = new BeanInstanceBag<>(creationalContext);
        components.put(component, bag);
    }
    if (bag.beanInstance == null) {
        bag.beanInstance = component.create(creationalContext);
    }
    return bag.beanInstance;
}
 
Example 14
Source File: JoynrJeeMessageContext.java    From joynr with Apache License 2.0 5 votes vote down vote up
@Override
public <T> T get(Contextual<T> contextual, CreationalContext<T> creationalContext) {
    T instance = get(contextual);
    if (instance == null) {
        instance = contextual.create(creationalContext);
        if (instance != null) {
            contextualStore.get().put(contextual, instance);
        }
    }
    return instance;
}
 
Example 15
Source File: TransactionContext.java    From openwebbeans-meecrowave with Apache License 2.0 4 votes vote down vote up
public T create(Contextual<T> contextual) {
    if (beanInstance == null) {
        beanInstance = contextual.create(beanCreationalContext);
    }
    return beanInstance;
}