Skip to content

Commit

Permalink
various tidying up, and switch to using ContextualIdStore for context…
Browse files Browse the repository at this point in the history
…ual identification purposes

git-svn-id: http://anonsvn.jboss.org/repos/weld/ri/trunk@3018 1c488680-804c-0410-94cd-c6b725194a0e
  • Loading branch information
pmuir committed Jul 7, 2009
1 parent e3925cf commit 46f9a14
Show file tree
Hide file tree
Showing 46 changed files with 369 additions and 907 deletions.
119 changes: 0 additions & 119 deletions impl/src/main/java/org/jboss/webbeans/BeanIdStore.java

This file was deleted.

45 changes: 18 additions & 27 deletions impl/src/main/java/org/jboss/webbeans/BeanManagerImpl.java
Expand Up @@ -50,7 +50,6 @@
import javax.enterprise.context.spi.Context;
import javax.enterprise.context.spi.Contextual;
import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.event.Observer;
import javax.enterprise.inject.AmbiguousResolutionException;
import javax.enterprise.inject.BindingType;
import javax.enterprise.inject.InjectionException;
Expand All @@ -75,7 +74,6 @@
import org.jboss.webbeans.bootstrap.api.ServiceRegistry;
import org.jboss.webbeans.context.ApplicationContext;
import org.jboss.webbeans.context.CreationalContextImpl;
import org.jboss.webbeans.context.DependentContext;
import org.jboss.webbeans.el.Namespace;
import org.jboss.webbeans.el.WebBeansELResolverImpl;
import org.jboss.webbeans.introspector.WBAnnotated;
Expand Down Expand Up @@ -543,7 +541,6 @@ else if (bean instanceof DecoratorBean)
RIBean<?> riBean = (RIBean<?>) bean;
riBeans.put(riBean.getId(), riBean);
}
getServices().get(BeanIdStore.class).put(bean, this);
registerBeanNamespace(bean);
for (BeanManagerImpl childActivity : childActivities)
{
Expand Down Expand Up @@ -812,21 +809,12 @@ public void fireEvent(Object event, Annotation... bindings)
notifyObservers(event, resolveObserverMethods(event, bindings));
}

private <T> void notifyObservers(T event, Set<ObserverMethod<?, T>> observers)
private <T> void notifyObservers(final T event, final Set<ObserverMethod<?, T>> observers)
{
try
{
DependentContext.instance().setActive(true);
for (ObserverMethod<?, T> observer : observers)
{
observer.notify(event);
}
}
finally
for (ObserverMethod<?, T> observer : observers)
{
// TODO This breaks SE shutdown, also we need to tidy up how dependent context is activated....
DependentContext.instance().setActive(false);
}
observer.notify(event);
}
}

/**
Expand Down Expand Up @@ -938,19 +926,21 @@ public Object getInjectableReference(InjectionPoint injectionPoint, CreationalCo
/**
* Returns an instance by API type and binding types
*
* @param type The API type to match
* @param beanType The API type to match
* @param bindings The binding types to match
* @return An instance of the bean
*
* @deprecated replace with non-contextual injection
*
*/
@Deprecated
public <T> T getInstanceByType(Class<T> type, Annotation... bindings)
public <T> T getInstanceByType(Class<T> beanType, Annotation... bindings)
{
WBAnnotated<T, ?> element = ResolvableWBClass.of(type, bindings, this);
Bean<T> bean = getBean(element, bindings);
return (T) getReference(bean, type, createCreationalContext(bean));
Set<Bean<?>> beans = getBeans(beanType, bindings);
Bean<?> bean = resolve(beans);
Object reference = getReference(bean, beanType, createCreationalContext(bean));

@SuppressWarnings("unchecked")
T instance = (T) reference;

return instance;
}

public <T> Bean<T> getBean(WBAnnotated<T, ?> element, Annotation... bindings)
Expand Down Expand Up @@ -1136,9 +1126,10 @@ public void shutdown()
{
log.trace("Ending application");
shutdownExecutors();
ApplicationContext.instance().destroy();
ApplicationContext.instance().setActive(false);
ApplicationContext.instance().setBeanStore(null);
ApplicationContext applicationContext = getServices().get(ApplicationContext.class);
applicationContext.destroy();
applicationContext.setActive(false);
applicationContext.setBeanStore(null);
CurrentManager.cleanup();
}

Expand Down
72 changes: 72 additions & 0 deletions impl/src/main/java/org/jboss/webbeans/ContextualIdStore.java
@@ -0,0 +1,72 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2008, Red Hat Middleware LLC, and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.webbeans;

import java.util.concurrent.atomic.AtomicInteger;

import javax.enterprise.context.spi.Contextual;

import org.jboss.webbeans.bootstrap.api.Service;

import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
import com.google.common.collect.Maps;

/**
* Application wide contextual identifier service which allows a serializable
* reference to a contextual to be obtained, and the contextual to be returned
* for a given id. Note that this allows a Bean object to be loaded regardless
* of the bean's accessiblity from the current module, and should not be abused
* as a way to ignore accessibility rules enforced during resolution.
*
* @author Pete Muir
*
*/
public class ContextualIdStore implements Service
{

private final BiMap<Contextual<?>, Integer> contextuals;
private final AtomicInteger idGenerator;

public ContextualIdStore()
{
this.idGenerator = new AtomicInteger(0);
BiMap<Contextual<?>, Integer> map = HashBiMap.create();
// TODO Somehow remove this sync if it shows bad in a profiler
this.contextuals = Maps.synchronizedBiMap(map);
}

@SuppressWarnings("unchecked")
public <T> Contextual<T> getContextual(Integer id)
{
return (Contextual<T>) contextuals.inverse().get(id);
}

public Integer getId(Contextual<?> contextual)
{
if (contextuals.containsKey(contextual))
{
return contextuals.get(contextual);
}
else
{
Integer id = idGenerator.incrementAndGet();
contextuals.put(contextual, id);
return id;
}
}
}
Expand Up @@ -41,7 +41,6 @@
import org.jboss.webbeans.DefinitionException;
import org.jboss.webbeans.bootstrap.BeanDeployerEnvironment;
import org.jboss.webbeans.context.CreationalContextImpl;
import org.jboss.webbeans.context.DependentContext;
import org.jboss.webbeans.introspector.WBMember;
import org.jboss.webbeans.log.LogProvider;
import org.jboss.webbeans.log.Logging;
Expand Down Expand Up @@ -333,11 +332,10 @@ protected Object getReceiver(CreationalContext<?> creationalContext)
*
* @returns The instance
*/
public T create(CreationalContext<T> creationalContext)
public T create(final CreationalContext<T> creationalContext)
{
try
{
DependentContext.instance().setActive(true);
T instance = produceInstance(creationalContext);
checkReturnValue(instance);
return instance;
Expand All @@ -348,7 +346,6 @@ public T create(CreationalContext<T> creationalContext)
{
creationalContext.release();
}
DependentContext.instance().setActive(false);
}
}

Expand Down
44 changes: 15 additions & 29 deletions impl/src/main/java/org/jboss/webbeans/bean/EnterpriseBean.java
Expand Up @@ -39,7 +39,6 @@
import org.jboss.webbeans.bean.proxy.EnterpriseBeanInstance;
import org.jboss.webbeans.bean.proxy.EnterpriseBeanProxyMethodHandler;
import org.jboss.webbeans.bootstrap.BeanDeployerEnvironment;
import org.jboss.webbeans.context.DependentContext;
import org.jboss.webbeans.ejb.InternalEjbDescriptor;
import org.jboss.webbeans.ejb.api.SessionObjectReference;
import org.jboss.webbeans.ejb.spi.BusinessInterfaceDescriptor;
Expand Down Expand Up @@ -221,11 +220,10 @@ protected void specialize(BeanDeployerEnvironment environment)
*
* @return The instance
*/
public T create(CreationalContext<T> creationalContext)
public T create(final CreationalContext<T> creationalContext)
{
try
{
DependentContext.instance().setActive(true);
T instance = proxyClass.newInstance();
creationalContext.push(instance);
((ProxyObject) instance).setHandler(new EnterpriseBeanProxyMethodHandler<T>(this, creationalContext));
Expand All @@ -244,10 +242,6 @@ public T create(CreationalContext<T> creationalContext)
{
throw new CreationException("could not find the EJB in JNDI " + proxyClass, e);
}
finally
{
DependentContext.instance().setActive(false);
}
}

public void destroy(T instance, CreationalContext<T> creationalContext)
Expand Down Expand Up @@ -308,28 +302,20 @@ public String toString()

public void postConstruct(T instance)
{
try
{
CreationalContext<T> creationalContext = new CreationalContext<T>()
{

public void push(T incompleteInstance) {};

public void release()
{
// TODO implement this
}

};
DependentContext.instance().setActive(true);
injectBoundFields(instance, creationalContext);
callInitializers(instance, creationalContext);
}
finally
{
DependentContext.instance().setActive(false);
}

// TODO Why do we need a special CC for Enterprise beans?
CreationalContext<T> creationalContext = new CreationalContext<T>()
{

public void push(T incompleteInstance) {};

public void release()
{
// TODO implement this
}

};
injectBoundFields(instance, creationalContext);
callInitializers(instance, creationalContext);
}

public void preDestroy(CreationalContext<T> creationalContext)
Expand Down

0 comments on commit 46f9a14

Please sign in to comment.