[webbeans-commits] Webbeans SVN: r2408 - extensions/trunk/tomcat/int/src/main/java/org/jboss/webbeans/environment/servlet and 10 other directories.
webbeans-commits at lists.jboss.org
webbeans-commits at lists.jboss.org
Tue Apr 14 19:26:56 EDT 2009
Author: pete.muir at jboss.org
Date: 2009-04-14 19:26:56 -0400 (Tue, 14 Apr 2009)
New Revision: 2408
Removed:
extensions/trunk/se/src/main/java/org/jboss/webbeans/environment/se/resources/
extensions/trunk/tomcat/int/src/main/java/org/jboss/webbeans/environment/servlet/resources/ReadOnlyNamingContext.java
ri/trunk/impl/src/main/java/org/jboss/webbeans/resources/DefaultNamingContext.java
ri/trunk/spi/src/main/java/org/jboss/webbeans/resources/spi/NamingContext.java
ri/trunk/spi/src/main/java/org/jboss/webbeans/resources/spi/helpers/AbstractNamingContext.java
ri/trunk/spi/src/main/java/org/jboss/webbeans/resources/spi/helpers/ForwardingNamingContext.java
ri/trunk/spi/src/test/java/org/jboss/webbeans/bootstrap/api/test/MockNamingContext.java
ri/trunk/tests/src/main/java/org/jboss/webbeans/mock/MockNamingContext.java
Modified:
extensions/trunk/se/src/main/java/org/jboss/webbeans/environment/se/StartMain.java
extensions/trunk/tomcat/int/src/main/java/org/jboss/webbeans/environment/servlet/Listener.java
ri/trunk/impl/src/main/java/org/jboss/webbeans/ManagerImpl.java
ri/trunk/impl/src/main/java/org/jboss/webbeans/bootstrap/WebBeansBootstrap.java
ri/trunk/spi/src/main/java/org/jboss/webbeans/bootstrap/api/Environments.java
ri/trunk/spi/src/main/java/org/jboss/webbeans/bootstrap/api/helpers/AbstractBootstrap.java
ri/trunk/spi/src/main/java/org/jboss/webbeans/bootstrap/api/helpers/BootstrapBean.java
ri/trunk/spi/src/test/java/org/jboss/webbeans/bootstrap/api/test/BootstrapTest.java
ri/trunk/tests/src/main/java/org/jboss/webbeans/mock/MockServletLifecycle.java
Log:
WBINT-12
Modified: extensions/trunk/se/src/main/java/org/jboss/webbeans/environment/se/StartMain.java
===================================================================
--- extensions/trunk/se/src/main/java/org/jboss/webbeans/environment/se/StartMain.java 2009-04-14 23:07:58 UTC (rev 2407)
+++ extensions/trunk/se/src/main/java/org/jboss/webbeans/environment/se/StartMain.java 2009-04-14 23:26:56 UTC (rev 2408)
@@ -25,10 +25,8 @@
import org.jboss.webbeans.context.api.BeanStore;
import org.jboss.webbeans.context.api.helpers.ConcurrentHashMapBeanStore;
import org.jboss.webbeans.environment.se.discovery.SEWebBeanDiscovery;
-import org.jboss.webbeans.environment.se.resources.NoNamingContext;
import org.jboss.webbeans.environment.se.util.Reflections;
import org.jboss.webbeans.manager.api.WebBeansManager;
-import org.jboss.webbeans.resources.spi.NamingContext;
/**
* This is the main class that should always be called from the command line for
@@ -69,7 +67,6 @@
{
bootstrap.setEnvironment(Environments.SE);
bootstrap.getServices().add(WebBeanDiscovery.class, new SEWebBeanDiscovery() {});
- bootstrap.getServices().add(NamingContext.class, new NoNamingContext() {});
bootstrap.setApplicationContext(applicationBeanStore);
bootstrap.initialize();
this.manager = bootstrap.getManager();
Modified: extensions/trunk/tomcat/int/src/main/java/org/jboss/webbeans/environment/servlet/Listener.java
===================================================================
--- extensions/trunk/tomcat/int/src/main/java/org/jboss/webbeans/environment/servlet/Listener.java 2009-04-14 23:07:58 UTC (rev 2407)
+++ extensions/trunk/tomcat/int/src/main/java/org/jboss/webbeans/environment/servlet/Listener.java 2009-04-14 23:26:56 UTC (rev 2408)
@@ -24,14 +24,12 @@
import org.jboss.webbeans.context.api.BeanStore;
import org.jboss.webbeans.context.api.helpers.ConcurrentHashMapBeanStore;
import org.jboss.webbeans.environment.servlet.discovery.TomcatWebBeanDiscovery;
-import org.jboss.webbeans.environment.servlet.resources.ReadOnlyNamingContext;
import org.jboss.webbeans.environment.servlet.resources.TomcatResourceServices;
import org.jboss.webbeans.environment.servlet.util.Reflections;
import org.jboss.webbeans.environment.tomcat.WebBeansAnnotationProcessor;
import org.jboss.webbeans.log.Log;
import org.jboss.webbeans.log.Logging;
import org.jboss.webbeans.manager.api.WebBeansManager;
-import org.jboss.webbeans.resources.spi.NamingContext;
import org.jboss.webbeans.resources.spi.ResourceServices;
import org.jboss.webbeans.servlet.api.ServletListener;
import org.jboss.webbeans.servlet.api.helpers.ForwardingServletListener;
@@ -86,7 +84,6 @@
sce.getServletContext().setAttribute(APPLICATION_BEAN_STORE_ATTRIBUTE_NAME, applicationBeanStore);
bootstrap.setEnvironment(Environments.SERVLET);
bootstrap.getServices().add(WebBeanDiscovery.class, new TomcatWebBeanDiscovery(sce.getServletContext()) {});
- bootstrap.getServices().add(NamingContext.class, new ReadOnlyNamingContext() {});
bootstrap.getServices().add(ResourceServices.class, new TomcatResourceServices() {});
bootstrap.setApplicationContext(applicationBeanStore);
bootstrap.initialize();
Deleted: extensions/trunk/tomcat/int/src/main/java/org/jboss/webbeans/environment/servlet/resources/ReadOnlyNamingContext.java
===================================================================
--- extensions/trunk/tomcat/int/src/main/java/org/jboss/webbeans/environment/servlet/resources/ReadOnlyNamingContext.java 2009-04-14 23:07:58 UTC (rev 2407)
+++ extensions/trunk/tomcat/int/src/main/java/org/jboss/webbeans/environment/servlet/resources/ReadOnlyNamingContext.java 2009-04-14 23:26:56 UTC (rev 2408)
@@ -1,64 +0,0 @@
-/*
- * 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.environment.servlet.resources;
-
-import javax.inject.DeploymentException;
-import javax.naming.Context;
-import javax.naming.InitialContext;
-import javax.naming.NamingException;
-
-import org.jboss.webbeans.resources.spi.helpers.AbstractNamingContext;
-
-/**
- *
- * @author Pete Muir
- */
-public class ReadOnlyNamingContext extends AbstractNamingContext
-{
-
- private final Context context;
-
- public ReadOnlyNamingContext()
- {
- try
- {
- context = new InitialContext();
- }
- catch (NamingException e)
- {
- throw new DeploymentException("Error creating initial context");
- }
- }
-
- public void bind(String name, Object value)
- {
- // No-op
- }
-
- @Override
- public Context getContext()
- {
- return context;
- }
-
- @Override
- public void unbind(String key)
- {
- // No-op
- }
-
-}
Modified: ri/trunk/impl/src/main/java/org/jboss/webbeans/ManagerImpl.java
===================================================================
--- ri/trunk/impl/src/main/java/org/jboss/webbeans/ManagerImpl.java 2009-04-14 23:07:58 UTC (rev 2407)
+++ ri/trunk/impl/src/main/java/org/jboss/webbeans/ManagerImpl.java 2009-04-14 23:26:56 UTC (rev 2408)
@@ -1,1220 +1,1218 @@
-/*
- * 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.io.InputStream;
-import java.io.Serializable;
-import java.lang.annotation.Annotation;
-import java.lang.reflect.ParameterizedType;
-import java.lang.reflect.Type;
-import java.lang.reflect.TypeVariable;
-import java.lang.reflect.WildcardType;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-import java.util.Stack;
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.CopyOnWriteArrayList;
-import java.util.concurrent.CopyOnWriteArraySet;
-import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Executors;
-import java.util.concurrent.TimeUnit;
-import java.util.concurrent.atomic.AtomicInteger;
-
-import javax.context.Context;
-import javax.context.ContextNotActiveException;
-import javax.context.CreationalContext;
-import javax.event.Observer;
-import javax.inject.AmbiguousDependencyException;
-import javax.inject.BindingType;
-import javax.inject.DeploymentException;
-import javax.inject.DuplicateBindingTypeException;
-import javax.inject.Production;
-import javax.inject.Standard;
-import javax.inject.TypeLiteral;
-import javax.inject.UnproxyableDependencyException;
-import javax.inject.UnsatisfiedDependencyException;
-import javax.inject.manager.Bean;
-import javax.inject.manager.Decorator;
-import javax.inject.manager.InjectionPoint;
-import javax.inject.manager.InterceptionType;
-import javax.inject.manager.Interceptor;
-import javax.inject.manager.Manager;
-
-import org.jboss.webbeans.bean.DisposalMethodBean;
-import org.jboss.webbeans.bean.EnterpriseBean;
-import org.jboss.webbeans.bean.NewEnterpriseBean;
-import org.jboss.webbeans.bean.RIBean;
-import org.jboss.webbeans.bean.proxy.ClientProxyProvider;
-import org.jboss.webbeans.bootstrap.api.ServiceRegistry;
-import org.jboss.webbeans.context.ApplicationContext;
-import org.jboss.webbeans.context.CreationalContextImpl;
-import org.jboss.webbeans.el.Namespace;
-import org.jboss.webbeans.event.EventManager;
-import org.jboss.webbeans.event.EventObserver;
-import org.jboss.webbeans.event.ObserverImpl;
-import org.jboss.webbeans.injection.NonContextualInjector;
-import org.jboss.webbeans.injection.resolution.ResolvableAnnotatedClass;
-import org.jboss.webbeans.injection.resolution.Resolver;
-import org.jboss.webbeans.introspector.AnnotatedItem;
-import org.jboss.webbeans.log.Log;
-import org.jboss.webbeans.log.Logging;
-import org.jboss.webbeans.manager.api.WebBeansManager;
-import org.jboss.webbeans.metadata.MetaDataCache;
-import org.jboss.webbeans.resources.spi.NamingContext;
-import org.jboss.webbeans.util.Beans;
-import org.jboss.webbeans.util.Proxies;
-import org.jboss.webbeans.util.Reflections;
-import org.jboss.webbeans.util.collections.multi.ConcurrentListHashMultiMap;
-import org.jboss.webbeans.util.collections.multi.ConcurrentListMultiMap;
-import org.jboss.webbeans.util.collections.multi.ConcurrentSetHashMultiMap;
-import org.jboss.webbeans.util.collections.multi.ConcurrentSetMultiMap;
-
-/**
- * Implementation of the Web Beans Manager.
- *
- * Essentially a singleton for registering Beans, Contexts, Observers,
- * Interceptors etc. as well as providing resolution
- *
- * @author Pete Muir
- *
- */
-public class ManagerImpl implements WebBeansManager, Serializable
-{
-
- private static class CurrentActivity
- {
-
- private final Context context;
- private final ManagerImpl manager;
-
- public CurrentActivity(Context context, ManagerImpl manager)
- {
- this.context = context;
- this.manager = manager;
- }
-
- public Context getContext()
- {
- return context;
- }
-
- public ManagerImpl getManager()
- {
- return manager;
- }
-
- @Override
- public boolean equals(Object obj)
- {
- if (obj instanceof CurrentActivity)
- {
- return this.getContext().equals(((CurrentActivity) obj).getContext());
- }
- else
- {
- return false;
- }
- }
-
- @Override
- public int hashCode()
- {
- return getContext().hashCode();
- }
-
- @Override
- public String toString()
- {
- return getContext() + " -> " + getManager();
- }
- }
-
- private static final Log log = Logging.getLog(ManagerImpl.class);
-
- private static final long serialVersionUID = 3021562879133838561L;
-
- // The JNDI key to place the manager under
- public static final String JNDI_KEY = "java:app/Manager";
-
- /*
- * Application scoped services
- * ****************************
- */
- private transient final ExecutorService taskExecutor = Executors.newSingleThreadExecutor();
- private transient final ServiceRegistry services;
-
- /*
- * Application scoped data structures
- * ***********************************
- */
- private transient List<Class<? extends Annotation>> enabledDeploymentTypes;
- private transient final ConcurrentListMultiMap<Class<? extends Annotation>, Context> contexts;
- private final transient Set<CurrentActivity> currentActivities;
- private transient final ClientProxyProvider clientProxyProvider;
- private transient final Map<Class<?>, EnterpriseBean<?>> newEnterpriseBeans;
- private transient final Map<String, RIBean<?>> riBeans;
- private final transient Map<Bean<?>, Bean<?>> specializedBeans;
- private final transient AtomicInteger ids;
-
- /*
- * Activity scoped services
- * *************************
- */
- private transient final EventManager eventManager;
- private transient final Resolver resolver;
- private final transient NonContextualInjector nonContextualInjector;
-
- /*
- * Activity scoped data structures
- * ********************************
- */
- private transient final ThreadLocal<Stack<InjectionPoint>> currentInjectionPoint;
- private transient List<Bean<?>> beans;
- private final transient Namespace rootNamespace;
- private final transient ConcurrentSetMultiMap<Type, EventObserver<?>> registeredObservers;
- private final transient Set<ManagerImpl> childActivities;
- private final Integer id;
-
-
- /**
- * Create a new, root, manager
- *
- * @param serviceRegistry
- * @return
- */
- public static ManagerImpl newRootManager(ServiceRegistry serviceRegistry)
- {
- List<Class<? extends Annotation>> defaultEnabledDeploymentTypes = new ArrayList<Class<? extends Annotation>>();
- defaultEnabledDeploymentTypes.add(0, Standard.class);
- defaultEnabledDeploymentTypes.add(1, Production.class);
-
- return new ManagerImpl(
- serviceRegistry,
- new CopyOnWriteArrayList<Bean<?>>(),
- new ConcurrentSetHashMultiMap<Type, EventObserver<?>>(),
- new Namespace(),
- new ConcurrentHashMap<Class<?>, EnterpriseBean<?>>(),
- new ConcurrentHashMap<String, RIBean<?>>(),
- new ClientProxyProvider(),
- new ConcurrentListHashMultiMap<Class<? extends Annotation>, Context>(),
- new CopyOnWriteArraySet<CurrentActivity>(),
- new HashMap<Bean<?>, Bean<?>>(),
- defaultEnabledDeploymentTypes,
- new AtomicInteger()
- );
- }
-
- /**
- * Create a new child manager
- *
- * @param parentManager
- * @return
- */
- public static ManagerImpl newChildManager(ManagerImpl parentManager)
- {
- List<Bean<?>> beans = new CopyOnWriteArrayList<Bean<?>>();
- beans.addAll(parentManager.getBeans());
-
- ConcurrentSetMultiMap<Type, EventObserver<?>> registeredObservers = new ConcurrentSetHashMultiMap<Type, EventObserver<?>>();
- registeredObservers.deepPutAll(parentManager.getRegisteredObservers());
- Namespace rootNamespace = new Namespace(parentManager.getRootNamespace());
-
- return new ManagerImpl(
- parentManager.getServices(),
- beans,
- registeredObservers,
- rootNamespace,
- parentManager.getNewEnterpriseBeanMap(),
- parentManager.getRiBeans(),
- parentManager.getClientProxyProvider(),
- parentManager.getContexts(),
- parentManager.getCurrentActivities(),
- parentManager.getSpecializedBeans(),
- parentManager.getEnabledDeploymentTypes(),
- parentManager.getIds()
- );
- }
-
- /**
- * Create a new manager
- *
- * @param ejbServices the ejbResolver to use
- */
- private ManagerImpl(
- ServiceRegistry serviceRegistry,
- List<Bean<?>> beans,
- ConcurrentSetMultiMap<Type, EventObserver<?>> registeredObservers,
- Namespace rootNamespace,
- Map<Class<?>, EnterpriseBean<?>> newEnterpriseBeans,
- Map<String, RIBean<?>> riBeans,
- ClientProxyProvider clientProxyProvider,
- ConcurrentListMultiMap<Class<? extends Annotation>, Context> contexts,
- Set<CurrentActivity> currentActivities,
- Map<Bean<?>, Bean<?>> specializedBeans,
- List<Class<? extends Annotation>> enabledDeploymentTypes,
- AtomicInteger ids
- )
- {
- this.services = serviceRegistry;
- this.beans = beans;
- this.newEnterpriseBeans = newEnterpriseBeans;
- this.riBeans = riBeans;
- this.clientProxyProvider = clientProxyProvider;
- this.contexts = contexts;
- this.currentActivities = currentActivities;
- this.specializedBeans = specializedBeans;
- this.registeredObservers = registeredObservers;
- setEnabledDeploymentTypes(enabledDeploymentTypes);
- this.rootNamespace = rootNamespace;
- this.ids = ids;
- this.id = ids.incrementAndGet();
-
- this.resolver = new Resolver(this);
- this.eventManager = new EventManager(this);
- this.nonContextualInjector = new NonContextualInjector(this);
- this.childActivities = new CopyOnWriteArraySet<ManagerImpl>();
- this.currentInjectionPoint = new ThreadLocal<Stack<InjectionPoint>>()
- {
- @Override
- protected Stack<InjectionPoint> initialValue()
- {
- return new Stack<InjectionPoint>();
- }
- };
- }
-
- /**
- * Set up the enabled deployment types, if none are specified by the user,
- * the default @Production and @Standard are used. For internal use.
- *
- * @param enabledDeploymentTypes The enabled deployment types from
- * web-beans.xml
- */
- protected void checkEnabledDeploymentTypes()
- {
- if (!this.enabledDeploymentTypes.get(0).equals(Standard.class))
- {
- throw new DeploymentException("@Standard must be the lowest precedence deployment type");
- }
- }
-
- protected void addWebBeansDeploymentTypes()
- {
- if (!this.enabledDeploymentTypes.contains(WebBean.class))
- {
- this.enabledDeploymentTypes.add(1, WebBean.class);
- }
- }
-
- /**
- * Registers a bean with the manager
- *
- * @param bean The bean to register
- * @return A reference to manager
- *
- * @see javax.inject.manager.Manager#addBean(javax.inject.manager.Bean)
- */
- public Manager addBean(Bean<?> bean)
- {
- if (beans.contains(bean))
- {
- return this;
- }
- resolver.clear();
- beans.add(bean);
- registerBeanNamespace(bean);
- for (ManagerImpl childActivity : childActivities)
- {
- childActivity.addBean(bean);
- }
- return this;
- }
-
- /**
- * Resolve the disposal method for the given producer method. For internal
- * use.
- *
- * @param apiType The API type to match
- * @param bindings The binding types to match
- * @return The set of matching disposal methods
- */
- public <T> Set<DisposalMethodBean<T>> resolveDisposalBeans(Class<T> apiType, Annotation... bindings)
- {
- // Correct?
- Set<Bean<T>> beans = resolveByType(apiType, bindings);
- Set<DisposalMethodBean<T>> disposalBeans = new HashSet<DisposalMethodBean<T>>();
- for (Bean<T> bean : beans)
- if (bean instanceof DisposalMethodBean)
- disposalBeans.add((DisposalMethodBean<T>) bean);
- return disposalBeans;
- }
-
- /**
- * Resolves observers for given event and bindings
- *
- * @param event The event to match
- * @param bindings The binding types to match
- * @return The set of matching observers
- *
- * @see javax.inject.manager.Manager#resolveObservers(java.lang.Object,
- * java.lang.annotation.Annotation[])
- */
- public <T> Set<Observer<T>> resolveObservers(T event, Annotation... bindings)
- {
- Class<?> clazz = event.getClass();
- for (Annotation annotation : bindings)
- {
- if (!getServices().get(MetaDataCache.class).getBindingTypeModel(annotation.annotationType()).isValid())
- {
- throw new IllegalArgumentException("Not a binding type " + annotation);
- }
- }
- HashSet<Annotation> bindingAnnotations = new HashSet<Annotation>(Arrays.asList(bindings));
- if (bindingAnnotations.size() < bindings.length)
- {
- throw new DuplicateBindingTypeException("Duplicate binding types: " + bindings);
- }
- checkEventType(clazz);
- return eventManager.getObservers(event, bindings);
- }
-
- private void checkEventType(Type eventType)
- {
- Type[] types;
- if (eventType instanceof Class)
- {
- types = Reflections.getActualTypeArguments((Class<?>) eventType);
- }
- else if (eventType instanceof ParameterizedType)
- {
- types = ((ParameterizedType) eventType).getActualTypeArguments();
- }
- else
- {
- throw new IllegalArgumentException("Event type " + eventType + " isn't a concrete type");
- }
- for (Type type : types)
- {
- if (type instanceof WildcardType)
- {
- throw new IllegalArgumentException("Cannot provide an event type parameterized with a wildcard " + eventType);
- }
- if (type instanceof TypeVariable)
- {
- throw new IllegalArgumentException("Cannot provide an event type parameterized with a type parameter " + eventType);
- }
- }
- }
-
- /**
- * A strongly ordered, unmodifiable list of enabled deployment types
- *
- * @return The ordered enabled deployment types known to the manager
- */
- public List<Class<? extends Annotation>> getEnabledDeploymentTypes()
- {
- return Collections.unmodifiableList(enabledDeploymentTypes);
- }
-
- /**
- * Set the enabled deployment types
- *
- * @param enabledDeploymentTypes
- */
- public void setEnabledDeploymentTypes(List<Class<? extends Annotation>> enabledDeploymentTypes)
- {
- this.enabledDeploymentTypes = new ArrayList<Class<? extends Annotation>>(enabledDeploymentTypes);
- checkEnabledDeploymentTypes();
- addWebBeansDeploymentTypes();
- }
-
- /**
- * Resolves beans by API type and binding types
- *
- * @param type The API type to match
- * @param bindings The binding types to match
- * @return The set of matching beans
- *
- * @see javax.inject.manager.Manager#resolveByType(java.lang.Class,
- * java.lang.annotation.Annotation[])
- */
- public <T> Set<Bean<T>> resolveByType(Class<T> type, Annotation... bindings)
- {
- return resolveByType(ResolvableAnnotatedClass.of(type, bindings), bindings);
- }
-
- /**
- * Resolves beans by API type literal and binding types
- *
- * @param type The API type literal to match
- * @param bindings The binding types to match
- * @return The set of matching beans
- *
- * @see javax.inject.manager.Manager#resolveByType(javax.inject.TypeLiteral,
- * java.lang.annotation.Annotation[])
- */
- public <T> Set<Bean<T>> resolveByType(TypeLiteral<T> type, Annotation... bindings)
- {
- return resolveByType(ResolvableAnnotatedClass.of(type, bindings), bindings);
- }
-
- public <T> Set<Bean<T>> resolveByType(AnnotatedItem<T, ?> element, InjectionPoint injectionPoint, Annotation... bindings)
- {
- boolean registerInjectionPoint = !injectionPoint.getType().equals(InjectionPoint.class);
- try
- {
- if (registerInjectionPoint)
- {
- currentInjectionPoint.get().push(injectionPoint);
- }
- return resolveByType(element, bindings);
- }
- finally
- {
- if (registerInjectionPoint)
- {
- currentInjectionPoint.get().pop();
- }
- }
- }
-
- /**
- * Check the resolution request is valid, and then ask the resolver to
- * perform the resolution. For internal use.
- *
- * @param element The item to resolve
- * @param bindings The binding types to match
- * @return The set of matching beans
- */
- public <T> Set<Bean<T>> resolveByType(AnnotatedItem<T, ?> element, Annotation... bindings)
- {
- for (Annotation annotation : element.getAnnotationsAsSet())
- {
- if (!getServices().get(MetaDataCache.class).getBindingTypeModel(annotation.annotationType()).isValid())
- {
- throw new IllegalArgumentException("Not a binding type " + annotation);
- }
- }
- for (Type type : element.getActualTypeArguments())
- {
- if (type instanceof WildcardType)
- {
- throw new IllegalArgumentException("Cannot resolve a type parameterized with a wildcard " + element);
- }
- if (type instanceof TypeVariable)
- {
- throw new IllegalArgumentException("Cannot resolve a type parameterized with a type parameter " + element);
- }
- }
- if (bindings.length > element.getMetaAnnotations(BindingType.class).size())
- {
- throw new DuplicateBindingTypeException("Duplicate bindings (" + Arrays.asList(bindings) + ") type passed " + element.toString());
- }
- return resolver.get(element);
- }
-
- /**
- * Wraps a collection of beans into a thread safe list. Since this overwrites
- * any existing list of beans in the manager, this should only be done on
- * startup and other controlled situations. Also maps the beans by
- * implementation class. For internal use.
- *
- * @param beans The set of beans to add
- * @return A reference to the manager
- */
- // TODO Build maps in the deployer :-)
- public void setBeans(Set<RIBean<?>> beans)
- {
- synchronized (beans)
- {
- this.beans = new CopyOnWriteArrayList<Bean<?>>(beans);
- for (RIBean<?> bean : beans)
- {
- if (bean instanceof NewEnterpriseBean)
- {
- newEnterpriseBeans.put(bean.getType(), (EnterpriseBean<?>) bean);
- }
- riBeans.put(bean.getId(), bean);
- registerBeanNamespace(bean);
- }
- resolver.clear();
- }
- }
-
- protected void registerBeanNamespace(Bean<?> bean)
- {
- if (bean.getName() != null && bean.getName().indexOf('.') > 0)
- {
- String name = bean.getName().substring(0, bean.getName().lastIndexOf('.'));
- String[] hierarchy = name.split("\\.");
- Namespace namespace = getRootNamespace();
- for (String s : hierarchy)
- {
- namespace = namespace.putIfAbsent(s);
- }
- }
- }
-
- /**
- * Gets the class-mapped beans. For internal use.
- *
- * @return The bean map
- */
- public Map<Class<?>, EnterpriseBean<?>> getNewEnterpriseBeanMap()
- {
- return newEnterpriseBeans;
- }
-
- /**
- * The beans registered with the Web Bean manager. For internal use
- *
- * @return The list of known beans
- */
- public List<Bean<?>> getBeans()
- {
- return Collections.unmodifiableList(beans);
- }
-
- public Map<String, RIBean<?>> getRiBeans()
- {
- return Collections.unmodifiableMap(riBeans);
- }
-
- /**
- * Registers a context with the manager
- *
- * @param context The context to add
- * @return A reference to the manager
- *
- * @see javax.inject.manager.Manager#addContext(javax.context.Context)
- */
- public Manager addContext(Context context)
- {
- contexts.put(context.getScopeType(), context);
- return this;
- }
-
- /**
- * Registers a decorator with the manager
- *
- * @param decorator The decorator to register
- * @return A reference to the manager
- *
- * @see javax.inject.manager.Manager#addDecorator(javax.inject.manager.Decorator)
- */
- public Manager addDecorator(Decorator decorator)
- {
- throw new UnsupportedOperationException("Not yet implemented");
- }
-
- /**
- * Registers an interceptor with the manager
- *
- * @param interceptor The interceptor to register
- * @return A reference to the manager
- *
- * @see javax.inject.manager.Manager#addInterceptor(javax.inject.manager.Interceptor)
- */
- public Manager addInterceptor(Interceptor interceptor)
- {
- throw new UnsupportedOperationException("Not yet implemented");
- }
-
- public <T> Manager addObserver(Observer<T> observer, Class<T> eventType, Annotation... bindings)
- {
- return addObserverByType(observer, eventType, bindings);
- }
-
- /**
- * Shortcut to register an ObserverImpl
- *
- * @param <T>
- * @param observer
- * @return
- */
- public <T> Manager addObserver(ObserverImpl<T> observer)
- {
- return addObserverByType(observer, observer.getEventType(), observer.getBindingsAsArray());
- }
-
- public <T> Manager addObserver(Observer<T> observer, TypeLiteral<T> eventType, Annotation... bindings)
- {
- return addObserverByType(observer, eventType.getType(), bindings);
- }
-
- /**
- * Does the actual observer registration
- *
- * @param <T>
- * @param observer
- * @param eventType
- * @param bindings
- * @return
- */
- public <T> Manager addObserverByType(Observer<T> observer, Type eventType, Annotation... bindings)
- {
- checkEventType(eventType);
- this.eventManager.addObserver(observer, eventType, bindings);
- for (ManagerImpl childActivity : childActivities)
- {
- childActivity.addObserverByType(observer, eventType, bindings);
- }
- return this;
- }
-
- /**
- * Fires an event object with given event object for given bindings
- *
- * @param event The event object to pass along
- * @param bindings The binding types to match
- *
- * @see javax.inject.manager.Manager#fireEvent(java.lang.Object,
- * java.lang.annotation.Annotation[])
- */
- public void fireEvent(Object event, Annotation... bindings)
- {
- // Check the event object for template parameters which are not allowed by
- // the spec.
- if (Reflections.isParameterizedType(event.getClass()))
- {
- throw new IllegalArgumentException("Event type " + event.getClass().getName() + " is not allowed because it is a generic");
- }
- // Also check that the binding types are truly binding types
- for (Annotation binding : bindings)
- {
- if (!Reflections.isBindings(binding))
- {
- throw new IllegalArgumentException("Event type " + event.getClass().getName() + " cannot be fired with non-binding type " + binding.getClass().getName() + " specified");
- }
- }
-
- // Get the observers for this event. Although resolveObservers is
- // parameterized, this method is not, so we have to use
- // Observer<Object> for observers.
- Set<Observer<Object>> observers = resolveObservers(event, bindings);
- eventManager.notifyObservers(observers, event);
- }
-
- /**
- * Gets an active context of the given scope. Throws an exception if there
- * are no active contexts found or if there are too many matches
- *
- * @param scopeType The scope to match
- * @return A single active context of the given scope
- *
- * @see javax.inject.manager.Manager#getContext(java.lang.Class)
- */
- public Context getContext(Class<? extends Annotation> scopeType)
- {
- List<Context> activeContexts = new ArrayList<Context>();
- for (Context context : contexts.get(scopeType))
- {
- if (context.isActive())
- {
- activeContexts.add(context);
- }
- }
- if (activeContexts.isEmpty())
- {
- throw new ContextNotActiveException("No active contexts for scope type " + scopeType.getName());
- }
- if (activeContexts.size() > 1)
- {
- throw new IllegalStateException("More than one context active for scope type " + scopeType.getName());
- }
- return activeContexts.iterator().next();
- }
-
- /**
- * Returns an instance of a bean
- *
- * @param bean The bean to instantiate
- * @return An instance of the bean
- *
- * @see javax.inject.manager.Manager#getInstance(javax.inject.manager.Bean)
- */
- public <T> T getInstance(Bean<T> bean)
- {
- return getInstance(bean, true);
- }
-
- public <T> T getInstance(Bean<T> bean, boolean create)
- {
- if (create)
- {
- return getInstance(bean, new CreationalContextImpl<T>(bean));
- }
- else
- {
- return getInstance(bean, null);
- }
- }
-
- /**
- * Returns an instance of a bean
- *
- * @param bean The bean to instantiate
- * @return An instance of the bean
- *
- * @see javax.inject.manager.Manager#getInstance(javax.inject.manager.Bean)
- */
- @SuppressWarnings("unchecked")
- private <T> T getInstance(Bean<T> bean, CreationalContextImpl<T> creationalContext)
- {
- if (specializedBeans.containsKey(bean))
- {
- return getInstance((Bean<T>) specializedBeans.get(bean), creationalContext);
- }
- else if (getServices().get(MetaDataCache.class).getScopeModel(bean.getScopeType()).isNormal())
- {
- if (creationalContext != null || (creationalContext == null && getContext(bean.getScopeType()).get(bean) != null))
- {
- return clientProxyProvider.getClientProxy(this, bean);
- }
- else
- {
- return null;
- }
- }
- else
- {
- return getContext(bean.getScopeType()).get(bean, creationalContext);
- }
- }
-
- public <T> T getInstanceToInject(InjectionPoint injectionPoint)
- {
- return this.<T> getInstanceToInject(injectionPoint, null);
- }
-
- public void injectNonContextualInstance(Object instance)
- {
- nonContextualInjector.inject(instance);
- }
-
- @SuppressWarnings("unchecked")
- public <T> T getInstanceToInject(InjectionPoint injectionPoint, CreationalContext<?> creationalContext)
- {
- boolean registerInjectionPoint = !injectionPoint.getType().equals(InjectionPoint.class);
- try
- {
- if (registerInjectionPoint)
- {
- currentInjectionPoint.get().push(injectionPoint);
- }
- AnnotatedItem<T, ?> element = ResolvableAnnotatedClass.of(injectionPoint.getType(), injectionPoint.getBindings().toArray(new Annotation[0]));
- Bean<T> resolvedBean = getBeanByType(element, element.getBindingsAsArray());
- if (getServices().get(MetaDataCache.class).getScopeModel(resolvedBean.getScopeType()).isNormal() && !Proxies.isTypeProxyable(injectionPoint.getType()))
- {
- throw new UnproxyableDependencyException("Attempting to inject an unproxyable normal scoped bean " + resolvedBean + " into " + injectionPoint);
- }
- if (creationalContext instanceof CreationalContextImpl)
- {
- CreationalContextImpl<?> ctx = (CreationalContextImpl<?>) creationalContext;
- if (ctx.containsIncompleteInstance(resolvedBean))
- {
- return ctx.getIncompleteInstance(resolvedBean);
- }
- else
- {
- return getInstance(resolvedBean, ctx.getCreationalContext(resolvedBean));
- }
- }
- else
- {
- return getInstance(resolvedBean);
- }
- }
- finally
- {
- if (registerInjectionPoint)
- {
- currentInjectionPoint.get().pop();
- }
- }
- }
-
- /**
- * Gets an instance by name, returning null if none is found and throwing an
- * exception if too many beans match
- *
- * @param name The name to match
- * @return An instance of the bean
- *
- * @see javax.inject.manager.Manager#getInstanceByName(java.lang.String)
- */
- public Object getInstanceByName(String name)
- {
- Set<Bean<?>> beans = resolveByName(name);
- if (beans.size() == 0)
- {
- return null;
- }
- else if (beans.size() > 1)
- {
- throw new AmbiguousDependencyException("Resolved multiple Web Beans with " + name);
- }
- else
- {
- return getInstance(beans.iterator().next());
- }
- }
-
- /**
- * Returns an instance by API type and binding types
- *
- * @param type The API type to match
- * @param bindings The binding types to match
- * @return An instance of the bean
- *
- * @see javax.inject.manager.Manager#getInstanceByType(java.lang.Class,
- * java.lang.annotation.Annotation[])
- */
- public <T> T getInstanceByType(Class<T> type, Annotation... bindings)
- {
- return getInstanceByType(ResolvableAnnotatedClass.of(type, bindings), bindings);
- }
-
- /**
- * Returns an instance by type literal and binding types
- *
- * @param type The type to match
- * @param bindings The binding types to match
- * @return An instance of the bean
- *
- * @see javax.inject.manager.Manager#getInstanceByType(javax.inject.TypeLiteral,
- * java.lang.annotation.Annotation[])
- */
- public <T> T getInstanceByType(TypeLiteral<T> type, Annotation... bindings)
- {
- return getInstanceByType(ResolvableAnnotatedClass.of(type, bindings), bindings);
- }
-
- /**
- * Resolve an instance, verify that the resolved bean can be instantiated,
- * and return
- *
- * @param element The annotated item to match
- * @param bindings The binding types to match
- * @return An instance of the bean
- */
- public <T> T getInstanceByType(AnnotatedItem<T, ?> element, Annotation... bindings)
- {
- return getInstance(getBeanByType(element, bindings));
- }
-
- public <T> Bean<T> getBeanByType(AnnotatedItem<T, ?> element, Annotation... bindings)
- {
- Set<Bean<T>> beans = resolveByType(element, bindings);
- if (beans.size() == 0)
- {
- throw new UnsatisfiedDependencyException(element + "Unable to resolve any Web Beans");
- }
- else if (beans.size() > 1)
- {
- throw new AmbiguousDependencyException(element + "Resolved multiple Web Beans");
- }
- Bean<T> bean = beans.iterator().next();
- boolean normalScoped = getServices().get(MetaDataCache.class).getScopeModel(bean.getScopeType()).isNormal();
- if (normalScoped && !Beans.isBeanProxyable(bean))
- {
- throw new UnproxyableDependencyException("Normal scoped bean " + bean + " is not proxyable");
- }
- return bean;
- }
-
- /**
- * Removes an observer
- *
- * @param observer The observer to remove
- * @param eventType The event type to match
- * @param bindings the binding types to match
- * @return A reference to the manager
- *
- * @see javax.inject.manager.Manager#removeObserver(javax.event.Observer,
- * java.lang.Class, java.lang.annotation.Annotation[])
- */
- public <T> Manager removeObserver(Observer<T> observer, Class<T> eventType, Annotation... bindings)
- {
- this.eventManager.removeObserver(observer, eventType, bindings);
- return this;
- }
-
- /**
- * Removes an observer
- *
- * @param observer The observer to remove
- * @param eventType The event type to match
- * @param bindings the binding types to match
- * @return A reference to the manager
- *
- * @see javax.inject.manager.Manager#removeObserver(javax.event.Observer,
- * javax.inject.TypeLiteral, java.lang.annotation.Annotation[])
- */
- public <T> Manager removeObserver(Observer<T> observer, TypeLiteral<T> eventType, Annotation... bindings)
- {
- this.eventManager.removeObserver(observer, eventType.getRawType(), bindings);
- return this;
- }
-
- /**
- * Resolves a set of beans based on their name
- *
- * @param The name to match
- * @return The set of matching beans
- *
- * @see javax.inject.manager.Manager#resolveByName(java.lang.String)
- */
- public Set<Bean<?>> resolveByName(String name)
- {
- return resolver.get(name);
- }
-
- /**
- * Resolves a list of decorators based on API types and binding types Os
- *
- * @param types The set of API types to match
- * @param bindings The binding types to match
- * @return A list of matching decorators
- *
- * @see javax.inject.manager.Manager#resolveDecorators(java.util.Set,
- * java.lang.annotation.Annotation[])
- */
- public List<Decorator> resolveDecorators(Set<Type> types, Annotation... bindings)
- {
- throw new UnsupportedOperationException();
- }
-
- /**
- * Resolves a list of interceptors based on interception type and interceptor
- * bindings
- *
- * @param type The interception type to resolve
- * @param interceptorBindings The binding types to match
- * @return A list of matching interceptors
- *
- * @see javax.inject.manager.Manager#resolveInterceptors(javax.inject.manager.InterceptionType,
- * java.lang.annotation.Annotation[])
- */
- public List<Interceptor> resolveInterceptors(InterceptionType type, Annotation... interceptorBindings)
- {
- throw new UnsupportedOperationException();
- }
-
- /**
- * Get the web bean resolver. For internal use
- *
- * @return The resolver
- */
- public Resolver getResolver()
- {
- return resolver;
- }
-
- /**
- * Gets a string representation
- *
- * @return A string representation
- */
- @Override
- public String toString()
- {
- StringBuilder buffer = new StringBuilder();
- buffer.append("Manager\n");
- buffer.append("Enabled deployment types: " + getEnabledDeploymentTypes() + "\n");
- buffer.append("Registered contexts: " + contexts.keySet() + "\n");
- buffer.append("Registered beans: " + getBeans().size() + "\n");
- buffer.append("Specialized beans: " + specializedBeans.size() + "\n");
- return buffer.toString();
- }
-
- public Manager parse(InputStream xmlStream)
- {
- throw new UnsupportedOperationException();
- }
-
- public ManagerImpl createActivity()
- {
- ManagerImpl childActivity = newChildManager(this);
- childActivities.add(childActivity);
- CurrentManager.add(childActivity);
- return childActivity;
- }
-
- public ManagerImpl setCurrent(Class<? extends Annotation> scopeType)
- {
- if (!getServices().get(MetaDataCache.class).getScopeModel(scopeType).isNormal())
- {
- throw new IllegalArgumentException("Scope must be a normal scope type " + scopeType);
- }
- currentActivities.add(new CurrentActivity(getContext(scopeType), this));
- return this;
- }
-
- public ManagerImpl getCurrent()
- {
- List<CurrentActivity> activeCurrentActivities = new ArrayList<CurrentActivity>();
- for (CurrentActivity currentActivity : currentActivities)
- {
- if (currentActivity.getContext().isActive())
- {
- activeCurrentActivities.add(currentActivity);
- }
- }
- if (activeCurrentActivities.size() == 0)
- {
- return CurrentManager.rootManager();
- }
- else if (activeCurrentActivities.size() == 1)
- {
- return activeCurrentActivities.get(0).getManager();
- }
- throw new IllegalStateException("More than one current activity for an active context " + currentActivities);
- }
-
- public ServiceRegistry getServices()
- {
- return services;
- }
-
- /**
- * Accesses the factory used to create each instance of InjectionPoint that
- * is injected into web beans.
- *
- * @return the factory
- */
- public InjectionPoint getInjectionPoint()
- {
- if (!currentInjectionPoint.get().empty())
- {
- return currentInjectionPoint.get().peek();
- }
- else
- {
- return null;
- }
- }
-
- /**
- *
- * @return
- */
- public Map<Bean<?>, Bean<?>> getSpecializedBeans()
- {
- // TODO make this unmodifiable after deploy!
- return specializedBeans;
- }
-
- // Serialization
-
- protected Object readResolve()
- {
- return CurrentManager.get(id);
- }
-
- /**
- * Provides access to the executor service used for asynchronous tasks.
- *
- * @return the ExecutorService for this manager
- */
- public ExecutorService getTaskExecutor()
- {
- return taskExecutor;
- }
-
- public void shutdown()
- {
- log.trace("Ending application");
- shutdownExecutors();
- CurrentManager.cleanup();
- ApplicationContext.instance().destroy();
- ApplicationContext.instance().setActive(false);
- ApplicationContext.instance().setBeanStore(null);
- getServices().get(NamingContext.class).unbind(ManagerImpl.JNDI_KEY);
- }
-
- /**
- * Shuts down any executor services in the manager.
- */
- protected void shutdownExecutors()
- {
- taskExecutor.shutdown();
- try
- {
- // Wait a while for existing tasks to terminate
- if (!taskExecutor.awaitTermination(60, TimeUnit.SECONDS))
- {
- taskExecutor.shutdownNow(); // Cancel currently executing tasks
- // Wait a while for tasks to respond to being cancelled
- if (!taskExecutor.awaitTermination(60, TimeUnit.SECONDS))
- {
- // Log the error here
- }
- }
- }
- catch (InterruptedException ie)
- {
- // (Re-)Cancel if current thread also interrupted
- taskExecutor.shutdownNow();
- // Preserve interrupt status
- Thread.currentThread().interrupt();
- }
- }
-
- protected ClientProxyProvider getClientProxyProvider()
- {
- return clientProxyProvider;
- }
-
- protected ConcurrentListMultiMap<Class<? extends Annotation>, Context> getContexts()
- {
- return contexts;
- }
-
- protected AtomicInteger getIds()
- {
- return ids;
- }
-
- protected Set<CurrentActivity> getCurrentActivities()
- {
- return currentActivities;
- }
-
- public Integer getId()
- {
- return id;
- }
-
- public ConcurrentSetMultiMap<Type, EventObserver<?>> getRegisteredObservers()
- {
- return registeredObservers;
- }
-
- public Namespace getRootNamespace()
- {
- return rootNamespace;
- }
-
-}
+/*
+ * 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.io.InputStream;
+import java.io.Serializable;
+import java.lang.annotation.Annotation;
+import java.lang.reflect.ParameterizedType;
+import java.lang.reflect.Type;
+import java.lang.reflect.TypeVariable;
+import java.lang.reflect.WildcardType;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.Stack;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.concurrent.CopyOnWriteArraySet;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import javax.context.Context;
+import javax.context.ContextNotActiveException;
+import javax.context.CreationalContext;
+import javax.event.Observer;
+import javax.inject.AmbiguousDependencyException;
+import javax.inject.BindingType;
+import javax.inject.DeploymentException;
+import javax.inject.DuplicateBindingTypeException;
+import javax.inject.Production;
+import javax.inject.Standard;
+import javax.inject.TypeLiteral;
+import javax.inject.UnproxyableDependencyException;
+import javax.inject.UnsatisfiedDependencyException;
+import javax.inject.manager.Bean;
+import javax.inject.manager.Decorator;
+import javax.inject.manager.InjectionPoint;
+import javax.inject.manager.InterceptionType;
+import javax.inject.manager.Interceptor;
+import javax.inject.manager.Manager;
+
+import org.jboss.webbeans.bean.DisposalMethodBean;
+import org.jboss.webbeans.bean.EnterpriseBean;
+import org.jboss.webbeans.bean.NewEnterpriseBean;
+import org.jboss.webbeans.bean.RIBean;
+import org.jboss.webbeans.bean.proxy.ClientProxyProvider;
+import org.jboss.webbeans.bootstrap.api.ServiceRegistry;
+import org.jboss.webbeans.context.ApplicationContext;
+import org.jboss.webbeans.context.CreationalContextImpl;
+import org.jboss.webbeans.el.Namespace;
+import org.jboss.webbeans.event.EventManager;
+import org.jboss.webbeans.event.EventObserver;
+import org.jboss.webbeans.event.ObserverImpl;
+import org.jboss.webbeans.injection.NonContextualInjector;
+import org.jboss.webbeans.injection.resolution.ResolvableAnnotatedClass;
+import org.jboss.webbeans.injection.resolution.Resolver;
+import org.jboss.webbeans.introspector.AnnotatedItem;
+import org.jboss.webbeans.log.Log;
+import org.jboss.webbeans.log.Logging;
+import org.jboss.webbeans.manager.api.WebBeansManager;
+import org.jboss.webbeans.metadata.MetaDataCache;
+import org.jboss.webbeans.util.Beans;
+import org.jboss.webbeans.util.Proxies;
+import org.jboss.webbeans.util.Reflections;
+import org.jboss.webbeans.util.collections.multi.ConcurrentListHashMultiMap;
+import org.jboss.webbeans.util.collections.multi.ConcurrentListMultiMap;
+import org.jboss.webbeans.util.collections.multi.ConcurrentSetHashMultiMap;
+import org.jboss.webbeans.util.collections.multi.ConcurrentSetMultiMap;
+
+/**
+ * Implementation of the Web Beans Manager.
+ *
+ * Essentially a singleton for registering Beans, Contexts, Observers,
+ * Interceptors etc. as well as providing resolution
+ *
+ * @author Pete Muir
+ *
+ */
+public class ManagerImpl implements WebBeansManager, Serializable
+{
+
+ private static class CurrentActivity
+ {
+
+ private final Context context;
+ private final ManagerImpl manager;
+
+ public CurrentActivity(Context context, ManagerImpl manager)
+ {
+ this.context = context;
+ this.manager = manager;
+ }
+
+ public Context getContext()
+ {
+ return context;
+ }
+
+ public ManagerImpl getManager()
+ {
+ return manager;
+ }
+
+ @Override
+ public boolean equals(Object obj)
+ {
+ if (obj instanceof CurrentActivity)
+ {
+ return this.getContext().equals(((CurrentActivity) obj).getContext());
+ }
+ else
+ {
+ return false;
+ }
+ }
+
+ @Override
+ public int hashCode()
+ {
+ return getContext().hashCode();
+ }
+
+ @Override
+ public String toString()
+ {
+ return getContext() + " -> " + getManager();
+ }
+ }
+
+ private static final Log log = Logging.getLog(ManagerImpl.class);
+
+ private static final long serialVersionUID = 3021562879133838561L;
+
+ // The JNDI key to place the manager under
+ public static final String JNDI_KEY = "java:app/Manager";
+
+ /*
+ * Application scoped services
+ * ****************************
+ */
+ private transient final ExecutorService taskExecutor = Executors.newSingleThreadExecutor();
+ private transient final ServiceRegistry services;
+
+ /*
+ * Application scoped data structures
+ * ***********************************
+ */
+ private transient List<Class<? extends Annotation>> enabledDeploymentTypes;
+ private transient final ConcurrentListMultiMap<Class<? extends Annotation>, Context> contexts;
+ private final transient Set<CurrentActivity> currentActivities;
+ private transient final ClientProxyProvider clientProxyProvider;
+ private transient final Map<Class<?>, EnterpriseBean<?>> newEnterpriseBeans;
+ private transient final Map<String, RIBean<?>> riBeans;
+ private final transient Map<Bean<?>, Bean<?>> specializedBeans;
+ private final transient AtomicInteger ids;
+
+ /*
+ * Activity scoped services
+ * *************************
+ */
+ private transient final EventManager eventManager;
+ private transient final Resolver resolver;
+ private final transient NonContextualInjector nonContextualInjector;
+
+ /*
+ * Activity scoped data structures
+ * ********************************
+ */
+ private transient final ThreadLocal<Stack<InjectionPoint>> currentInjectionPoint;
+ private transient List<Bean<?>> beans;
+ private final transient Namespace rootNamespace;
+ private final transient ConcurrentSetMultiMap<Type, EventObserver<?>> registeredObservers;
+ private final transient Set<ManagerImpl> childActivities;
+ private final Integer id;
+
+
+ /**
+ * Create a new, root, manager
+ *
+ * @param serviceRegistry
+ * @return
+ */
+ public static ManagerImpl newRootManager(ServiceRegistry serviceRegistry)
+ {
+ List<Class<? extends Annotation>> defaultEnabledDeploymentTypes = new ArrayList<Class<? extends Annotation>>();
+ defaultEnabledDeploymentTypes.add(0, Standard.class);
+ defaultEnabledDeploymentTypes.add(1, Production.class);
+
+ return new ManagerImpl(
+ serviceRegistry,
+ new CopyOnWriteArrayList<Bean<?>>(),
+ new ConcurrentSetHashMultiMap<Type, EventObserver<?>>(),
+ new Namespace(),
+ new ConcurrentHashMap<Class<?>, EnterpriseBean<?>>(),
+ new ConcurrentHashMap<String, RIBean<?>>(),
+ new ClientProxyProvider(),
+ new ConcurrentListHashMultiMap<Class<? extends Annotation>, Context>(),
+ new CopyOnWriteArraySet<CurrentActivity>(),
+ new HashMap<Bean<?>, Bean<?>>(),
+ defaultEnabledDeploymentTypes,
+ new AtomicInteger()
+ );
+ }
+
+ /**
+ * Create a new child manager
+ *
+ * @param parentManager
+ * @return
+ */
+ public static ManagerImpl newChildManager(ManagerImpl parentManager)
+ {
+ List<Bean<?>> beans = new CopyOnWriteArrayList<Bean<?>>();
+ beans.addAll(parentManager.getBeans());
+
+ ConcurrentSetMultiMap<Type, EventObserver<?>> registeredObservers = new ConcurrentSetHashMultiMap<Type, EventObserver<?>>();
+ registeredObservers.deepPutAll(parentManager.getRegisteredObservers());
+ Namespace rootNamespace = new Namespace(parentManager.getRootNamespace());
+
+ return new ManagerImpl(
+ parentManager.getServices(),
+ beans,
+ registeredObservers,
+ rootNamespace,
+ parentManager.getNewEnterpriseBeanMap(),
+ parentManager.getRiBeans(),
+ parentManager.getClientProxyProvider(),
+ parentManager.getContexts(),
+ parentManager.getCurrentActivities(),
+ parentManager.getSpecializedBeans(),
+ parentManager.getEnabledDeploymentTypes(),
+ parentManager.getIds()
+ );
+ }
+
+ /**
+ * Create a new manager
+ *
+ * @param ejbServices the ejbResolver to use
+ */
+ private ManagerImpl(
+ ServiceRegistry serviceRegistry,
+ List<Bean<?>> beans,
+ ConcurrentSetMultiMap<Type, EventObserver<?>> registeredObservers,
+ Namespace rootNamespace,
+ Map<Class<?>, EnterpriseBean<?>> newEnterpriseBeans,
+ Map<String, RIBean<?>> riBeans,
+ ClientProxyProvider clientProxyProvider,
+ ConcurrentListMultiMap<Class<? extends Annotation>, Context> contexts,
+ Set<CurrentActivity> currentActivities,
+ Map<Bean<?>, Bean<?>> specializedBeans,
+ List<Class<? extends Annotation>> enabledDeploymentTypes,
+ AtomicInteger ids
+ )
+ {
+ this.services = serviceRegistry;
+ this.beans = beans;
+ this.newEnterpriseBeans = newEnterpriseBeans;
+ this.riBeans = riBeans;
+ this.clientProxyProvider = clientProxyProvider;
+ this.contexts = contexts;
+ this.currentActivities = currentActivities;
+ this.specializedBeans = specializedBeans;
+ this.registeredObservers = registeredObservers;
+ setEnabledDeploymentTypes(enabledDeploymentTypes);
+ this.rootNamespace = rootNamespace;
+ this.ids = ids;
+ this.id = ids.incrementAndGet();
+
+ this.resolver = new Resolver(this);
+ this.eventManager = new EventManager(this);
+ this.nonContextualInjector = new NonContextualInjector(this);
+ this.childActivities = new CopyOnWriteArraySet<ManagerImpl>();
+ this.currentInjectionPoint = new ThreadLocal<Stack<InjectionPoint>>()
+ {
+ @Override
+ protected Stack<InjectionPoint> initialValue()
+ {
+ return new Stack<InjectionPoint>();
+ }
+ };
+ }
+
+ /**
+ * Set up the enabled deployment types, if none are specified by the user,
+ * the default @Production and @Standard are used. For internal use.
+ *
+ * @param enabledDeploymentTypes The enabled deployment types from
+ * web-beans.xml
+ */
+ protected void checkEnabledDeploymentTypes()
+ {
+ if (!this.enabledDeploymentTypes.get(0).equals(Standard.class))
+ {
+ throw new DeploymentException("@Standard must be the lowest precedence deployment type");
+ }
+ }
+
+ protected void addWebBeansDeploymentTypes()
+ {
+ if (!this.enabledDeploymentTypes.contains(WebBean.class))
+ {
+ this.enabledDeploymentTypes.add(1, WebBean.class);
+ }
+ }
+
+ /**
+ * Registers a bean with the manager
+ *
+ * @param bean The bean to register
+ * @return A reference to manager
+ *
+ * @see javax.inject.manager.Manager#addBean(javax.inject.manager.Bean)
+ */
+ public Manager addBean(Bean<?> bean)
+ {
+ if (beans.contains(bean))
+ {
+ return this;
+ }
+ resolver.clear();
+ beans.add(bean);
+ registerBeanNamespace(bean);
+ for (ManagerImpl childActivity : childActivities)
+ {
+ childActivity.addBean(bean);
+ }
+ return this;
+ }
+
+ /**
+ * Resolve the disposal method for the given producer method. For internal
+ * use.
+ *
+ * @param apiType The API type to match
+ * @param bindings The binding types to match
+ * @return The set of matching disposal methods
+ */
+ public <T> Set<DisposalMethodBean<T>> resolveDisposalBeans(Class<T> apiType, Annotation... bindings)
+ {
+ // Correct?
+ Set<Bean<T>> beans = resolveByType(apiType, bindings);
+ Set<DisposalMethodBean<T>> disposalBeans = new HashSet<DisposalMethodBean<T>>();
+ for (Bean<T> bean : beans)
+ if (bean instanceof DisposalMethodBean)
+ disposalBeans.add((DisposalMethodBean<T>) bean);
+ return disposalBeans;
+ }
+
+ /**
+ * Resolves observers for given event and bindings
+ *
+ * @param event The event to match
+ * @param bindings The binding types to match
+ * @return The set of matching observers
+ *
+ * @see javax.inject.manager.Manager#resolveObservers(java.lang.Object,
+ * java.lang.annotation.Annotation[])
+ */
+ public <T> Set<Observer<T>> resolveObservers(T event, Annotation... bindings)
+ {
+ Class<?> clazz = event.getClass();
+ for (Annotation annotation : bindings)
+ {
+ if (!getServices().get(MetaDataCache.class).getBindingTypeModel(annotation.annotationType()).isValid())
+ {
+ throw new IllegalArgumentException("Not a binding type " + annotation);
+ }
+ }
+ HashSet<Annotation> bindingAnnotations = new HashSet<Annotation>(Arrays.asList(bindings));
+ if (bindingAnnotations.size() < bindings.length)
+ {
+ throw new DuplicateBindingTypeException("Duplicate binding types: " + bindings);
+ }
+ checkEventType(clazz);
+ return eventManager.getObservers(event, bindings);
+ }
+
+ private void checkEventType(Type eventType)
+ {
+ Type[] types;
+ if (eventType instanceof Class)
+ {
+ types = Reflections.getActualTypeArguments((Class<?>) eventType);
+ }
+ else if (eventType instanceof ParameterizedType)
+ {
+ types = ((ParameterizedType) eventType).getActualTypeArguments();
+ }
+ else
+ {
+ throw new IllegalArgumentException("Event type " + eventType + " isn't a concrete type");
+ }
+ for (Type type : types)
+ {
+ if (type instanceof WildcardType)
+ {
+ throw new IllegalArgumentException("Cannot provide an event type parameterized with a wildcard " + eventType);
+ }
+ if (type instanceof TypeVariable)
+ {
+ throw new IllegalArgumentException("Cannot provide an event type parameterized with a type parameter " + eventType);
+ }
+ }
+ }
+
+ /**
+ * A strongly ordered, unmodifiable list of enabled deployment types
+ *
+ * @return The ordered enabled deployment types known to the manager
+ */
+ public List<Class<? extends Annotation>> getEnabledDeploymentTypes()
+ {
+ return Collections.unmodifiableList(enabledDeploymentTypes);
+ }
+
+ /**
+ * Set the enabled deployment types
+ *
+ * @param enabledDeploymentTypes
+ */
+ public void setEnabledDeploymentTypes(List<Class<? extends Annotation>> enabledDeploymentTypes)
+ {
+ this.enabledDeploymentTypes = new ArrayList<Class<? extends Annotation>>(enabledDeploymentTypes);
+ checkEnabledDeploymentTypes();
+ addWebBeansDeploymentTypes();
+ }
+
+ /**
+ * Resolves beans by API type and binding types
+ *
+ * @param type The API type to match
+ * @param bindings The binding types to match
+ * @return The set of matching beans
+ *
+ * @see javax.inject.manager.Manager#resolveByType(java.lang.Class,
+ * java.lang.annotation.Annotation[])
+ */
+ public <T> Set<Bean<T>> resolveByType(Class<T> type, Annotation... bindings)
+ {
+ return resolveByType(ResolvableAnnotatedClass.of(type, bindings), bindings);
+ }
+
+ /**
+ * Resolves beans by API type literal and binding types
+ *
+ * @param type The API type literal to match
+ * @param bindings The binding types to match
+ * @return The set of matching beans
+ *
+ * @see javax.inject.manager.Manager#resolveByType(javax.inject.TypeLiteral,
+ * java.lang.annotation.Annotation[])
+ */
+ public <T> Set<Bean<T>> resolveByType(TypeLiteral<T> type, Annotation... bindings)
+ {
+ return resolveByType(ResolvableAnnotatedClass.of(type, bindings), bindings);
+ }
+
+ public <T> Set<Bean<T>> resolveByType(AnnotatedItem<T, ?> element, InjectionPoint injectionPoint, Annotation... bindings)
+ {
+ boolean registerInjectionPoint = !injectionPoint.getType().equals(InjectionPoint.class);
+ try
+ {
+ if (registerInjectionPoint)
+ {
+ currentInjectionPoint.get().push(injectionPoint);
+ }
+ return resolveByType(element, bindings);
+ }
+ finally
+ {
+ if (registerInjectionPoint)
+ {
+ currentInjectionPoint.get().pop();
+ }
+ }
+ }
+
+ /**
+ * Check the resolution request is valid, and then ask the resolver to
+ * perform the resolution. For internal use.
+ *
+ * @param element The item to resolve
+ * @param bindings The binding types to match
+ * @return The set of matching beans
+ */
+ public <T> Set<Bean<T>> resolveByType(AnnotatedItem<T, ?> element, Annotation... bindings)
+ {
+ for (Annotation annotation : element.getAnnotationsAsSet())
+ {
+ if (!getServices().get(MetaDataCache.class).getBindingTypeModel(annotation.annotationType()).isValid())
+ {
+ throw new IllegalArgumentException("Not a binding type " + annotation);
+ }
+ }
+ for (Type type : element.getActualTypeArguments())
+ {
+ if (type instanceof WildcardType)
+ {
+ throw new IllegalArgumentException("Cannot resolve a type parameterized with a wildcard " + element);
+ }
+ if (type instanceof TypeVariable)
+ {
+ throw new IllegalArgumentException("Cannot resolve a type parameterized with a type parameter " + element);
+ }
+ }
+ if (bindings.length > element.getMetaAnnotations(BindingType.class).size())
+ {
+ throw new DuplicateBindingTypeException("Duplicate bindings (" + Arrays.asList(bindings) + ") type passed " + element.toString());
+ }
+ return resolver.get(element);
+ }
+
+ /**
+ * Wraps a collection of beans into a thread safe list. Since this overwrites
+ * any existing list of beans in the manager, this should only be done on
+ * startup and other controlled situations. Also maps the beans by
+ * implementation class. For internal use.
+ *
+ * @param beans The set of beans to add
+ * @return A reference to the manager
+ */
+ // TODO Build maps in the deployer :-)
+ public void setBeans(Set<RIBean<?>> beans)
+ {
+ synchronized (beans)
+ {
+ this.beans = new CopyOnWriteArrayList<Bean<?>>(beans);
+ for (RIBean<?> bean : beans)
+ {
+ if (bean instanceof NewEnterpriseBean)
+ {
+ newEnterpriseBeans.put(bean.getType(), (EnterpriseBean<?>) bean);
+ }
+ riBeans.put(bean.getId(), bean);
+ registerBeanNamespace(bean);
+ }
+ resolver.clear();
+ }
+ }
+
+ protected void registerBeanNamespace(Bean<?> bean)
+ {
+ if (bean.getName() != null && bean.getName().indexOf('.') > 0)
+ {
+ String name = bean.getName().substring(0, bean.getName().lastIndexOf('.'));
+ String[] hierarchy = name.split("\\.");
+ Namespace namespace = getRootNamespace();
+ for (String s : hierarchy)
+ {
+ namespace = namespace.putIfAbsent(s);
+ }
+ }
+ }
+
+ /**
+ * Gets the class-mapped beans. For internal use.
+ *
+ * @return The bean map
+ */
+ public Map<Class<?>, EnterpriseBean<?>> getNewEnterpriseBeanMap()
+ {
+ return newEnterpriseBeans;
+ }
+
+ /**
+ * The beans registered with the Web Bean manager. For internal use
+ *
+ * @return The list of known beans
+ */
+ public List<Bean<?>> getBeans()
+ {
+ return Collections.unmodifiableList(beans);
+ }
+
+ public Map<String, RIBean<?>> getRiBeans()
+ {
+ return Collections.unmodifiableMap(riBeans);
+ }
+
+ /**
+ * Registers a context with the manager
+ *
+ * @param context The context to add
+ * @return A reference to the manager
+ *
+ * @see javax.inject.manager.Manager#addContext(javax.context.Context)
+ */
+ public Manager addContext(Context context)
+ {
+ contexts.put(context.getScopeType(), context);
+ return this;
+ }
+
+ /**
+ * Registers a decorator with the manager
+ *
+ * @param decorator The decorator to register
+ * @return A reference to the manager
+ *
+ * @see javax.inject.manager.Manager#addDecorator(javax.inject.manager.Decorator)
+ */
+ public Manager addDecorator(Decorator decorator)
+ {
+ throw new UnsupportedOperationException("Not yet implemented");
+ }
+
+ /**
+ * Registers an interceptor with the manager
+ *
+ * @param interceptor The interceptor to register
+ * @return A reference to the manager
+ *
+ * @see javax.inject.manager.Manager#addInterceptor(javax.inject.manager.Interceptor)
+ */
+ public Manager addInterceptor(Interceptor interceptor)
+ {
+ throw new UnsupportedOperationException("Not yet implemented");
+ }
+
+ public <T> Manager addObserver(Observer<T> observer, Class<T> eventType, Annotation... bindings)
+ {
+ return addObserverByType(observer, eventType, bindings);
+ }
+
+ /**
+ * Shortcut to register an ObserverImpl
+ *
+ * @param <T>
+ * @param observer
+ * @return
+ */
+ public <T> Manager addObserver(ObserverImpl<T> observer)
+ {
+ return addObserverByType(observer, observer.getEventType(), observer.getBindingsAsArray());
+ }
+
+ public <T> Manager addObserver(Observer<T> observer, TypeLiteral<T> eventType, Annotation... bindings)
+ {
+ return addObserverByType(observer, eventType.getType(), bindings);
+ }
+
+ /**
+ * Does the actual observer registration
+ *
+ * @param <T>
+ * @param observer
+ * @param eventType
+ * @param bindings
+ * @return
+ */
+ public <T> Manager addObserverByType(Observer<T> observer, Type eventType, Annotation... bindings)
+ {
+ checkEventType(eventType);
+ this.eventManager.addObserver(observer, eventType, bindings);
+ for (ManagerImpl childActivity : childActivities)
+ {
+ childActivity.addObserverByType(observer, eventType, bindings);
+ }
+ return this;
+ }
+
+ /**
+ * Fires an event object with given event object for given bindings
+ *
+ * @param event The event object to pass along
+ * @param bindings The binding types to match
+ *
+ * @see javax.inject.manager.Manager#fireEvent(java.lang.Object,
+ * java.lang.annotation.Annotation[])
+ */
+ public void fireEvent(Object event, Annotation... bindings)
+ {
+ // Check the event object for template parameters which are not allowed by
+ // the spec.
+ if (Reflections.isParameterizedType(event.getClass()))
+ {
+ throw new IllegalArgumentException("Event type " + event.getClass().getName() + " is not allowed because it is a generic");
+ }
+ // Also check that the binding types are truly binding types
+ for (Annotation binding : bindings)
+ {
+ if (!Reflections.isBindings(binding))
+ {
+ throw new IllegalArgumentException("Event type " + event.getClass().getName() + " cannot be fired with non-binding type " + binding.getClass().getName() + " specified");
+ }
+ }
+
+ // Get the observers for this event. Although resolveObservers is
+ // parameterized, this method is not, so we have to use
+ // Observer<Object> for observers.
+ Set<Observer<Object>> observers = resolveObservers(event, bindings);
+ eventManager.notifyObservers(observers, event);
+ }
+
+ /**
+ * Gets an active context of the given scope. Throws an exception if there
+ * are no active contexts found or if there are too many matches
+ *
+ * @param scopeType The scope to match
+ * @return A single active context of the given scope
+ *
+ * @see javax.inject.manager.Manager#getContext(java.lang.Class)
+ */
+ public Context getContext(Class<? extends Annotation> scopeType)
+ {
+ List<Context> activeContexts = new ArrayList<Context>();
+ for (Context context : contexts.get(scopeType))
+ {
+ if (context.isActive())
+ {
+ activeContexts.add(context);
+ }
+ }
+ if (activeContexts.isEmpty())
+ {
+ throw new ContextNotActiveException("No active contexts for scope type " + scopeType.getName());
+ }
+ if (activeContexts.size() > 1)
+ {
+ throw new IllegalStateException("More than one context active for scope type " + scopeType.getName());
+ }
+ return activeContexts.iterator().next();
+ }
+
+ /**
+ * Returns an instance of a bean
+ *
+ * @param bean The bean to instantiate
+ * @return An instance of the bean
+ *
+ * @see javax.inject.manager.Manager#getInstance(javax.inject.manager.Bean)
+ */
+ public <T> T getInstance(Bean<T> bean)
+ {
+ return getInstance(bean, true);
+ }
+
+ public <T> T getInstance(Bean<T> bean, boolean create)
+ {
+ if (create)
+ {
+ return getInstance(bean, new CreationalContextImpl<T>(bean));
+ }
+ else
+ {
+ return getInstance(bean, null);
+ }
+ }
+
+ /**
+ * Returns an instance of a bean
+ *
+ * @param bean The bean to instantiate
+ * @return An instance of the bean
+ *
+ * @see javax.inject.manager.Manager#getInstance(javax.inject.manager.Bean)
+ */
+ @SuppressWarnings("unchecked")
+ private <T> T getInstance(Bean<T> bean, CreationalContextImpl<T> creationalContext)
+ {
+ if (specializedBeans.containsKey(bean))
+ {
+ return getInstance((Bean<T>) specializedBeans.get(bean), creationalContext);
+ }
+ else if (getServices().get(MetaDataCache.class).getScopeModel(bean.getScopeType()).isNormal())
+ {
+ if (creationalContext != null || (creationalContext == null && getContext(bean.getScopeType()).get(bean) != null))
+ {
+ return clientProxyProvider.getClientProxy(this, bean);
+ }
+ else
+ {
+ return null;
+ }
+ }
+ else
+ {
+ return getContext(bean.getScopeType()).get(bean, creationalContext);
+ }
+ }
+
+ public <T> T getInstanceToInject(InjectionPoint injectionPoint)
+ {
+ return this.<T> getInstanceToInject(injectionPoint, null);
+ }
+
+ public void injectNonContextualInstance(Object instance)
+ {
+ nonContextualInjector.inject(instance);
+ }
+
+ @SuppressWarnings("unchecked")
+ public <T> T getInstanceToInject(InjectionPoint injectionPoint, CreationalContext<?> creationalContext)
+ {
+ boolean registerInjectionPoint = !injectionPoint.getType().equals(InjectionPoint.class);
+ try
+ {
+ if (registerInjectionPoint)
+ {
+ currentInjectionPoint.get().push(injectionPoint);
+ }
+ AnnotatedItem<T, ?> element = ResolvableAnnotatedClass.of(injectionPoint.getType(), injectionPoint.getBindings().toArray(new Annotation[0]));
+ Bean<T> resolvedBean = getBeanByType(element, element.getBindingsAsArray());
+ if (getServices().get(MetaDataCache.class).getScopeModel(resolvedBean.getScopeType()).isNormal() && !Proxies.isTypeProxyable(injectionPoint.getType()))
+ {
+ throw new UnproxyableDependencyException("Attempting to inject an unproxyable normal scoped bean " + resolvedBean + " into " + injectionPoint);
+ }
+ if (creationalContext instanceof CreationalContextImpl)
+ {
+ CreationalContextImpl<?> ctx = (CreationalContextImpl<?>) creationalContext;
+ if (ctx.containsIncompleteInstance(resolvedBean))
+ {
+ return ctx.getIncompleteInstance(resolvedBean);
+ }
+ else
+ {
+ return getInstance(resolvedBean, ctx.getCreationalContext(resolvedBean));
+ }
+ }
+ else
+ {
+ return getInstance(resolvedBean);
+ }
+ }
+ finally
+ {
+ if (registerInjectionPoint)
+ {
+ currentInjectionPoint.get().pop();
+ }
+ }
+ }
+
+ /**
+ * Gets an instance by name, returning null if none is found and throwing an
+ * exception if too many beans match
+ *
+ * @param name The name to match
+ * @return An instance of the bean
+ *
+ * @see javax.inject.manager.Manager#getInstanceByName(java.lang.String)
+ */
+ public Object getInstanceByName(String name)
+ {
+ Set<Bean<?>> beans = resolveByName(name);
+ if (beans.size() == 0)
+ {
+ return null;
+ }
+ else if (beans.size() > 1)
+ {
+ throw new AmbiguousDependencyException("Resolved multiple Web Beans with " + name);
+ }
+ else
+ {
+ return getInstance(beans.iterator().next());
+ }
+ }
+
+ /**
+ * Returns an instance by API type and binding types
+ *
+ * @param type The API type to match
+ * @param bindings The binding types to match
+ * @return An instance of the bean
+ *
+ * @see javax.inject.manager.Manager#getInstanceByType(java.lang.Class,
+ * java.lang.annotation.Annotation[])
+ */
+ public <T> T getInstanceByType(Class<T> type, Annotation... bindings)
+ {
+ return getInstanceByType(ResolvableAnnotatedClass.of(type, bindings), bindings);
+ }
+
+ /**
+ * Returns an instance by type literal and binding types
+ *
+ * @param type The type to match
+ * @param bindings The binding types to match
+ * @return An instance of the bean
+ *
+ * @see javax.inject.manager.Manager#getInstanceByType(javax.inject.TypeLiteral,
+ * java.lang.annotation.Annotation[])
+ */
+ public <T> T getInstanceByType(TypeLiteral<T> type, Annotation... bindings)
+ {
+ return getInstanceByType(ResolvableAnnotatedClass.of(type, bindings), bindings);
+ }
+
+ /**
+ * Resolve an instance, verify that the resolved bean can be instantiated,
+ * and return
+ *
+ * @param element The annotated item to match
+ * @param bindings The binding types to match
+ * @return An instance of the bean
+ */
+ public <T> T getInstanceByType(AnnotatedItem<T, ?> element, Annotation... bindings)
+ {
+ return getInstance(getBeanByType(element, bindings));
+ }
+
+ public <T> Bean<T> getBeanByType(AnnotatedItem<T, ?> element, Annotation... bindings)
+ {
+ Set<Bean<T>> beans = resolveByType(element, bindings);
+ if (beans.size() == 0)
+ {
+ throw new UnsatisfiedDependencyException(element + "Unable to resolve any Web Beans");
+ }
+ else if (beans.size() > 1)
+ {
+ throw new AmbiguousDependencyException(element + "Resolved multiple Web Beans");
+ }
+ Bean<T> bean = beans.iterator().next();
+ boolean normalScoped = getServices().get(MetaDataCache.class).getScopeModel(bean.getScopeType()).isNormal();
+ if (normalScoped && !Beans.isBeanProxyable(bean))
+ {
+ throw new UnproxyableDependencyException("Normal scoped bean " + bean + " is not proxyable");
+ }
+ return bean;
+ }
+
+ /**
+ * Removes an observer
+ *
+ * @param observer The observer to remove
+ * @param eventType The event type to match
+ * @param bindings the binding types to match
+ * @return A reference to the manager
+ *
+ * @see javax.inject.manager.Manager#removeObserver(javax.event.Observer,
+ * java.lang.Class, java.lang.annotation.Annotation[])
+ */
+ public <T> Manager removeObserver(Observer<T> observer, Class<T> eventType, Annotation... bindings)
+ {
+ this.eventManager.removeObserver(observer, eventType, bindings);
+ return this;
+ }
+
+ /**
+ * Removes an observer
+ *
+ * @param observer The observer to remove
+ * @param eventType The event type to match
+ * @param bindings the binding types to match
+ * @return A reference to the manager
+ *
+ * @see javax.inject.manager.Manager#removeObserver(javax.event.Observer,
+ * javax.inject.TypeLiteral, java.lang.annotation.Annotation[])
+ */
+ public <T> Manager removeObserver(Observer<T> observer, TypeLiteral<T> eventType, Annotation... bindings)
+ {
+ this.eventManager.removeObserver(observer, eventType.getRawType(), bindings);
+ return this;
+ }
+
+ /**
+ * Resolves a set of beans based on their name
+ *
+ * @param The name to match
+ * @return The set of matching beans
+ *
+ * @see javax.inject.manager.Manager#resolveByName(java.lang.String)
+ */
+ public Set<Bean<?>> resolveByName(String name)
+ {
+ return resolver.get(name);
+ }
+
+ /**
+ * Resolves a list of decorators based on API types and binding types Os
+ *
+ * @param types The set of API types to match
+ * @param bindings The binding types to match
+ * @return A list of matching decorators
+ *
+ * @see javax.inject.manager.Manager#resolveDecorators(java.util.Set,
+ * java.lang.annotation.Annotation[])
+ */
+ public List<Decorator> resolveDecorators(Set<Type> types, Annotation... bindings)
+ {
+ throw new UnsupportedOperationException();
+ }
+
+ /**
+ * Resolves a list of interceptors based on interception type and interceptor
+ * bindings
+ *
+ * @param type The interception type to resolve
+ * @param interceptorBindings The binding types to match
+ * @return A list of matching interceptors
+ *
+ * @see javax.inject.manager.Manager#resolveInterceptors(javax.inject.manager.InterceptionType,
+ * java.lang.annotation.Annotation[])
+ */
+ public List<Interceptor> resolveInterceptors(InterceptionType type, Annotation... interceptorBindings)
+ {
+ throw new UnsupportedOperationException();
+ }
+
+ /**
+ * Get the web bean resolver. For internal use
+ *
+ * @return The resolver
+ */
+ public Resolver getResolver()
+ {
+ return resolver;
+ }
+
+ /**
+ * Gets a string representation
+ *
+ * @return A string representation
+ */
+ @Override
+ public String toString()
+ {
+ StringBuilder buffer = new StringBuilder();
+ buffer.append("Manager\n");
+ buffer.append("Enabled deployment types: " + getEnabledDeploymentTypes() + "\n");
+ buffer.append("Registered contexts: " + contexts.keySet() + "\n");
+ buffer.append("Registered beans: " + getBeans().size() + "\n");
+ buffer.append("Specialized beans: " + specializedBeans.size() + "\n");
+ return buffer.toString();
+ }
+
+ public Manager parse(InputStream xmlStream)
+ {
+ throw new UnsupportedOperationException();
+ }
+
+ public ManagerImpl createActivity()
+ {
+ ManagerImpl childActivity = newChildManager(this);
+ childActivities.add(childActivity);
+ CurrentManager.add(childActivity);
+ return childActivity;
+ }
+
+ public ManagerImpl setCurrent(Class<? extends Annotation> scopeType)
+ {
+ if (!getServices().get(MetaDataCache.class).getScopeModel(scopeType).isNormal())
+ {
+ throw new IllegalArgumentException("Scope must be a normal scope type " + scopeType);
+ }
+ currentActivities.add(new CurrentActivity(getContext(scopeType), this));
+ return this;
+ }
+
+ public ManagerImpl getCurrent()
+ {
+ List<CurrentActivity> activeCurrentActivities = new ArrayList<CurrentActivity>();
+ for (CurrentActivity currentActivity : currentActivities)
+ {
+ if (currentActivity.getContext().isActive())
+ {
+ activeCurrentActivities.add(currentActivity);
+ }
+ }
+ if (activeCurrentActivities.size() == 0)
+ {
+ return CurrentManager.rootManager();
+ }
+ else if (activeCurrentActivities.size() == 1)
+ {
+ return activeCurrentActivities.get(0).getManager();
+ }
+ throw new IllegalStateException("More than one current activity for an active context " + currentActivities);
+ }
+
+ public ServiceRegistry getServices()
+ {
+ return services;
+ }
+
+ /**
+ * Accesses the factory used to create each instance of InjectionPoint that
+ * is injected into web beans.
+ *
+ * @return the factory
+ */
+ public InjectionPoint getInjectionPoint()
+ {
+ if (!currentInjectionPoint.get().empty())
+ {
+ return currentInjectionPoint.get().peek();
+ }
+ else
+ {
+ return null;
+ }
+ }
+
+ /**
+ *
+ * @return
+ */
+ public Map<Bean<?>, Bean<?>> getSpecializedBeans()
+ {
+ // TODO make this unmodifiable after deploy!
+ return specializedBeans;
+ }
+
+ // Serialization
+
+ protected Object readResolve()
+ {
+ return CurrentManager.get(id);
+ }
+
+ /**
+ * Provides access to the executor service used for asynchronous tasks.
+ *
+ * @return the ExecutorService for this manager
+ */
+ public ExecutorService getTaskExecutor()
+ {
+ return taskExecutor;
+ }
+
+ public void shutdown()
+ {
+ log.trace("Ending application");
+ shutdownExecutors();
+ CurrentManager.cleanup();
+ ApplicationContext.instance().destroy();
+ ApplicationContext.instance().setActive(false);
+ ApplicationContext.instance().setBeanStore(null);
+ }
+
+ /**
+ * Shuts down any executor services in the manager.
+ */
+ protected void shutdownExecutors()
+ {
+ taskExecutor.shutdown();
+ try
+ {
+ // Wait a while for existing tasks to terminate
+ if (!taskExecutor.awaitTermination(60, TimeUnit.SECONDS))
+ {
+ taskExecutor.shutdownNow(); // Cancel currently executing tasks
+ // Wait a while for tasks to respond to being cancelled
+ if (!taskExecutor.awaitTermination(60, TimeUnit.SECONDS))
+ {
+ // Log the error here
+ }
+ }
+ }
+ catch (InterruptedException ie)
+ {
+ // (Re-)Cancel if current thread also interrupted
+ taskExecutor.shutdownNow();
+ // Preserve interrupt status
+ Thread.currentThread().interrupt();
+ }
+ }
+
+ protected ClientProxyProvider getClientProxyProvider()
+ {
+ return clientProxyProvider;
+ }
+
+ protected ConcurrentListMultiMap<Class<? extends Annotation>, Context> getContexts()
+ {
+ return contexts;
+ }
+
+ protected AtomicInteger getIds()
+ {
+ return ids;
+ }
+
+ protected Set<CurrentActivity> getCurrentActivities()
+ {
+ return currentActivities;
+ }
+
+ public Integer getId()
+ {
+ return id;
+ }
+
+ public ConcurrentSetMultiMap<Type, EventObserver<?>> getRegisteredObservers()
+ {
+ return registeredObservers;
+ }
+
+ public Namespace getRootNamespace()
+ {
+ return rootNamespace;
+ }
+
+}
Modified: ri/trunk/impl/src/main/java/org/jboss/webbeans/bootstrap/WebBeansBootstrap.java
===================================================================
--- ri/trunk/impl/src/main/java/org/jboss/webbeans/bootstrap/WebBeansBootstrap.java 2009-04-14 23:07:58 UTC (rev 2407)
+++ ri/trunk/impl/src/main/java/org/jboss/webbeans/bootstrap/WebBeansBootstrap.java 2009-04-14 23:26:56 UTC (rev 2408)
@@ -21,10 +21,6 @@
import java.util.Collection;
import java.util.List;
-import javax.inject.ExecutionException;
-import javax.inject.manager.Manager;
-import javax.naming.Reference;
-
import org.jboss.webbeans.BeanValidator;
import org.jboss.webbeans.CurrentManager;
import org.jboss.webbeans.ManagerImpl;
@@ -64,10 +60,7 @@
import org.jboss.webbeans.persistence.spi.EntityDiscovery;
import org.jboss.webbeans.persistence.spi.JpaServices;
import org.jboss.webbeans.resources.ClassTransformer;
-import org.jboss.webbeans.resources.DefaultNamingContext;
import org.jboss.webbeans.resources.DefaultResourceLoader;
-import org.jboss.webbeans.resources.ManagerObjectFactory;
-import org.jboss.webbeans.resources.spi.NamingContext;
import org.jboss.webbeans.resources.spi.ResourceLoader;
import org.jboss.webbeans.resources.spi.ResourceServices;
import org.jboss.webbeans.servlet.HttpSessionManager;
@@ -94,7 +87,6 @@
{
// initialize default services
getServices().add(ResourceLoader.class, new DefaultResourceLoader());
- getServices().add(NamingContext.class, new DefaultNamingContext());
getServices().add(EntityDiscovery.class, new DefaultEntityDiscovery(getServices()));
}
@@ -119,7 +111,6 @@
}
addImplementationServices();
this.manager = ManagerImpl.newRootManager(ServiceRegistries.unmodifiableServiceRegistry(getServices()));
- bindManagerIntoJndi();
CurrentManager.setRootManager(manager);
initializeContexts();
}
@@ -137,20 +128,6 @@
getServices().add(MetaDataCache.class, new MetaDataCache(getServices().get(ClassTransformer.class)));
}
- private void bindManagerIntoJndi()
- {
- try
- {
- getServices().get(NamingContext.class).unbind(ManagerImpl.JNDI_KEY);
- }
- catch (ExecutionException e) {}
- finally
- {
- Reference managerReference = new Reference(Manager.class.getName(), ManagerObjectFactory.class.getName(), null);
- getServices().get(NamingContext.class).bind(ManagerImpl.JNDI_KEY, managerReference);
- }
- }
-
public ManagerImpl getManager()
{
return manager;
Deleted: ri/trunk/impl/src/main/java/org/jboss/webbeans/resources/DefaultNamingContext.java
===================================================================
--- ri/trunk/impl/src/main/java/org/jboss/webbeans/resources/DefaultNamingContext.java 2009-04-14 23:07:58 UTC (rev 2407)
+++ ri/trunk/impl/src/main/java/org/jboss/webbeans/resources/DefaultNamingContext.java 2009-04-14 23:26:56 UTC (rev 2408)
@@ -1,62 +0,0 @@
-/*
- * 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.resources;
-
-import javax.inject.ExecutionException;
-import javax.naming.InitialContext;
-import javax.naming.NamingException;
-
-import org.jboss.webbeans.resources.spi.helpers.AbstractNamingContext;
-
-/**
- * The default naming provider
- *
- * @author Pete Muir
- */
-public class DefaultNamingContext extends AbstractNamingContext
-{
- private static final long serialVersionUID = 1L;
- // The initial lookup context
- private transient InitialContext initialContext;
-
- /**
- * Constructor
- */
- public DefaultNamingContext()
- {
- try
- {
- this.initialContext = new InitialContext();
- }
- catch (NamingException e)
- {
- throw new ExecutionException("Could not obtain InitialContext", e);
- }
- }
-
- /**
- * Gets the initial context
- *
- * @return The initial context
- */
- public InitialContext getContext()
- {
- return initialContext;
- }
-
-}
Modified: ri/trunk/spi/src/main/java/org/jboss/webbeans/bootstrap/api/Environments.java
===================================================================
--- ri/trunk/spi/src/main/java/org/jboss/webbeans/bootstrap/api/Environments.java 2009-04-14 23:07:58 UTC (rev 2407)
+++ ri/trunk/spi/src/main/java/org/jboss/webbeans/bootstrap/api/Environments.java 2009-04-14 23:26:56 UTC (rev 2408)
@@ -25,7 +25,6 @@
import org.jboss.webbeans.messaging.spi.JmsServices;
import org.jboss.webbeans.persistence.spi.EntityDiscovery;
import org.jboss.webbeans.persistence.spi.JpaServices;
-import org.jboss.webbeans.resources.spi.NamingContext;
import org.jboss.webbeans.resources.spi.ResourceLoader;
import org.jboss.webbeans.resources.spi.ResourceServices;
import org.jboss.webbeans.transaction.spi.TransactionServices;
@@ -43,22 +42,22 @@
/**
* Java EE5 or Java EE6
*/
- EE(WebBeanDiscovery.class, EjbServices.class, JpaServices.class, WebServices.class, JmsServices.class, EntityDiscovery.class, ResourceServices.class, TransactionServices.class, NamingContext.class, ResourceLoader.class),
+ EE(WebBeanDiscovery.class, EjbServices.class, JpaServices.class, WebServices.class, JmsServices.class, EntityDiscovery.class, ResourceServices.class, TransactionServices.class, ResourceLoader.class),
/**
* Java EE6 Web Profile
*/
- EE_WEB_PROFILE(WebBeanDiscovery.class, EjbServices.class, EntityDiscovery.class, JpaServices.class, ResourceServices.class, TransactionServices.class, NamingContext.class, ResourceLoader.class),
+ EE_WEB_PROFILE(WebBeanDiscovery.class, EjbServices.class, EntityDiscovery.class, JpaServices.class, ResourceServices.class, TransactionServices.class, ResourceLoader.class),
/**
* Servlet container such as Tomcat
*/
- SERVLET(WebBeanDiscovery.class, NamingContext.class, ResourceLoader.class),
+ SERVLET(WebBeanDiscovery.class, ResourceLoader.class),
/**
* Java SE
*/
- SE(WebBeanDiscovery.class, NamingContext.class, ResourceLoader.class);
+ SE(WebBeanDiscovery.class, ResourceLoader.class);
private Set<Class<? extends Service>> requiredServices;
Modified: ri/trunk/spi/src/main/java/org/jboss/webbeans/bootstrap/api/helpers/AbstractBootstrap.java
===================================================================
--- ri/trunk/spi/src/main/java/org/jboss/webbeans/bootstrap/api/helpers/AbstractBootstrap.java 2009-04-14 23:07:58 UTC (rev 2407)
+++ ri/trunk/spi/src/main/java/org/jboss/webbeans/bootstrap/api/helpers/AbstractBootstrap.java 2009-04-14 23:26:56 UTC (rev 2408)
@@ -25,7 +25,6 @@
import org.jboss.webbeans.bootstrap.spi.WebBeanDiscovery;
import org.jboss.webbeans.context.api.BeanStore;
import org.jboss.webbeans.ejb.spi.EjbServices;
-import org.jboss.webbeans.resources.spi.NamingContext;
import org.jboss.webbeans.resources.spi.ResourceLoader;
import org.jboss.webbeans.transaction.spi.TransactionServices;
@@ -56,12 +55,6 @@
}
@Deprecated
- public void setNamingContext(NamingContext namingContext)
- {
- getServices().add(NamingContext.class, namingContext);
- }
-
- @Deprecated
public void setResourceLoader(ResourceLoader resourceLoader)
{
getServices().add(ResourceLoader.class, resourceLoader);
@@ -92,12 +85,6 @@
}
@Deprecated
- public NamingContext getNamingContext()
- {
- return getServices().get(NamingContext.class);
- }
-
- @Deprecated
public EjbServices getEjbServices()
{
return getServices().get(EjbServices.class);
Modified: ri/trunk/spi/src/main/java/org/jboss/webbeans/bootstrap/api/helpers/BootstrapBean.java
===================================================================
--- ri/trunk/spi/src/main/java/org/jboss/webbeans/bootstrap/api/helpers/BootstrapBean.java 2009-04-14 23:07:58 UTC (rev 2407)
+++ ri/trunk/spi/src/main/java/org/jboss/webbeans/bootstrap/api/helpers/BootstrapBean.java 2009-04-14 23:26:56 UTC (rev 2408)
@@ -9,7 +9,6 @@
import org.jboss.webbeans.manager.api.WebBeansManager;
import org.jboss.webbeans.messaging.spi.JmsServices;
import org.jboss.webbeans.persistence.spi.JpaServices;
-import org.jboss.webbeans.resources.spi.NamingContext;
import org.jboss.webbeans.resources.spi.ResourceLoader;
import org.jboss.webbeans.resources.spi.ResourceServices;
import org.jboss.webbeans.transaction.spi.TransactionServices;
@@ -86,16 +85,6 @@
bootstrap.setApplicationContext(applicationContext);
}
- public void setNamingContext(NamingContext namingContext)
- {
- bootstrap.getServices().add(NamingContext.class, namingContext);
- }
-
- public NamingContext getNamingContext()
- {
- return bootstrap.getServices().get(NamingContext.class);
- }
-
public void setResourceLoader(ResourceLoader resourceLoader)
{
bootstrap.getServices().add(ResourceLoader.class, resourceLoader);
Deleted: ri/trunk/spi/src/main/java/org/jboss/webbeans/resources/spi/NamingContext.java
===================================================================
--- ri/trunk/spi/src/main/java/org/jboss/webbeans/resources/spi/NamingContext.java 2009-04-14 23:07:58 UTC (rev 2407)
+++ ri/trunk/spi/src/main/java/org/jboss/webbeans/resources/spi/NamingContext.java 2009-04-14 23:26:56 UTC (rev 2408)
@@ -1,59 +0,0 @@
-/*
- * 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.resources.spi;
-
-import org.jboss.webbeans.bootstrap.api.Service;
-
-/**
- * JNDI operations for Web Beans, by default a read-write spec compliant
- * implementation will be used. If you wish to substitute, for example, a read-
- * only implementation, you may
- *
- * @author Pete Muir
- *
- */
-public interface NamingContext extends Service
-{
-
- public static final String PROPERTY_NAME = NamingContext.class.getName();
-
- /**
- * Typed JNDI lookup
- *
- * @param <T> The type
- * @param name The JNDI name
- * @param expectedType The expected type
- * @return The object
- */
- public <T> T lookup(String name, Class<? extends T> expectedType);
-
- /**
- * Binds an item to JNDI
- *
- * @param key The key to bind under
- * @param value The item to bind
- */
- public void bind(String name, Object value);
-
- /**
- * Unbinds an entry from JNDI.
- *
- * @param key The key of the object in JNDI to unbind
- */
- public void unbind(String key);
-}
Deleted: ri/trunk/spi/src/main/java/org/jboss/webbeans/resources/spi/helpers/AbstractNamingContext.java
===================================================================
--- ri/trunk/spi/src/main/java/org/jboss/webbeans/resources/spi/helpers/AbstractNamingContext.java 2009-04-14 23:07:58 UTC (rev 2407)
+++ ri/trunk/spi/src/main/java/org/jboss/webbeans/resources/spi/helpers/AbstractNamingContext.java 2009-04-14 23:26:56 UTC (rev 2408)
@@ -1,128 +0,0 @@
-/*
- * 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.resources.spi.helpers;
-
-import java.util.ArrayList;
-import java.util.List;
-
-import javax.inject.ExecutionException;
-import javax.naming.Context;
-import javax.naming.NamingException;
-
-import org.jboss.webbeans.resources.spi.NamingContext;
-
-/**
- * Provides common functionality required by a NamingContext
- *
- * @author Pete Muir
- *
- */
-public abstract class AbstractNamingContext implements NamingContext
-{
-
- public abstract Context getContext();
-
- /**
- * Binds in item to JNDI
- *
- * @param key The key to bind under
- * @param value The value to bind
- */
- public void bind(String key, Object value)
- {
- try
- {
- List<String> parts = splitIntoContexts(key);
- Context context = getContext();
- Context nextContext = null;
- for (int i = 0; i < parts.size() - 1; i++)
- {
- try
- {
- nextContext = (Context) context.lookup(parts.get(i));
- }
- catch (NamingException e)
- {
- nextContext = context.createSubcontext(parts.get(i));
- }
- context = nextContext;
- }
- context.bind(parts.get(parts.size() - 1), value);
- }
- catch (NamingException e)
- {
- throw new ExecutionException("Cannot bind " + value + " to " + key, e);
- }
- }
-
- /**
- * Unbinds an entry from JNDI.
- *
- * @param key The key of the object in JNDI to unbind
- */
- public void unbind(String key)
- {
- try
- {
- getContext().unbind(key);
- }
- catch (NamingException e)
- {
- throw new ExecutionException("Cannot unbind " + key, e);
- }
- }
-
- /**
- * Lookup an item from JNDI
- *
- * @param name The key
- * @param expectedType The expected return type
- * @return The found item
- */
- public <T> T lookup(String name, Class<? extends T> expectedType)
- {
- Object instance;
- try
- {
- instance = getContext().lookup(name);
- }
- catch (NamingException e)
- {
- throw new ExecutionException("Cannot lookup " + name, e);
- }
- try
- {
- return expectedType.cast(instance);
- }
- catch (ClassCastException e)
- {
- throw new ExecutionException(instance + " not of expected type " + expectedType, e);
- }
- }
-
- private static List<String> splitIntoContexts(String key)
- {
- List<String> parts = new ArrayList<String>();
- for (String part : key.split("/"))
- {
- parts.add(part);
- }
- return parts;
- }
-
-}
\ No newline at end of file
Deleted: ri/trunk/spi/src/main/java/org/jboss/webbeans/resources/spi/helpers/ForwardingNamingContext.java
===================================================================
--- ri/trunk/spi/src/main/java/org/jboss/webbeans/resources/spi/helpers/ForwardingNamingContext.java 2009-04-14 23:07:58 UTC (rev 2407)
+++ ri/trunk/spi/src/main/java/org/jboss/webbeans/resources/spi/helpers/ForwardingNamingContext.java 2009-04-14 23:26:56 UTC (rev 2408)
@@ -1,49 +0,0 @@
-package org.jboss.webbeans.resources.spi.helpers;
-
-import org.jboss.webbeans.resources.spi.NamingContext;
-
-
-/**
- * An implementation of {@link NamingContext} which forwards all its method calls
- * to another {@link NamingContext}}. Subclasses should override one or more
- * methods to modify the behavior of the backing {@link NamingContext} as desired
- * per the <a
- * href="http://en.wikipedia.org/wiki/Decorator_pattern">decorator pattern</a>.
- *
- * @author Pete Muir
- *
- */
-public abstract class ForwardingNamingContext implements NamingContext
-{
-
- protected abstract NamingContext delegate();
-
- public void bind(String key, Object value)
- {
- delegate().bind(key, value);
- }
-
- public <T> T lookup(String name, Class<? extends T> expectedType)
- {
- return delegate().lookup(name, expectedType);
- }
-
- @Override
- public boolean equals(Object obj)
- {
- return delegate().equals(obj);
- }
-
- @Override
- public String toString()
- {
- return delegate().toString();
- }
-
- @Override
- public int hashCode()
- {
- return delegate().hashCode();
- }
-
-}
Modified: ri/trunk/spi/src/test/java/org/jboss/webbeans/bootstrap/api/test/BootstrapTest.java
===================================================================
--- ri/trunk/spi/src/test/java/org/jboss/webbeans/bootstrap/api/test/BootstrapTest.java 2009-04-14 23:07:58 UTC (rev 2407)
+++ ri/trunk/spi/src/test/java/org/jboss/webbeans/bootstrap/api/test/BootstrapTest.java 2009-04-14 23:26:56 UTC (rev 2408)
@@ -8,7 +8,6 @@
import org.jboss.webbeans.messaging.spi.JmsServices;
import org.jboss.webbeans.persistence.spi.EntityDiscovery;
import org.jboss.webbeans.persistence.spi.JpaServices;
-import org.jboss.webbeans.resources.spi.NamingContext;
import org.jboss.webbeans.resources.spi.ResourceLoader;
import org.jboss.webbeans.resources.spi.ResourceServices;
import org.jboss.webbeans.transaction.spi.TransactionServices;
@@ -23,7 +22,6 @@
{
AbstractBootstrap bootstrap = new MockBootstrap();
bootstrap.setEnvironment(Environments.SE);
- bootstrap.getServices().add(NamingContext.class, new MockNamingContext());
bootstrap.getServices().add(ResourceLoader.class, new MockResourceLoader());
bootstrap.setApplicationContext(new ConcurrentHashMapBeanStore());
bootstrap.initialize();
@@ -35,7 +33,6 @@
AbstractBootstrap bootstrap = new MockBootstrap();
bootstrap.setEnvironment(Environments.EE);
bootstrap.setApplicationContext(new ConcurrentHashMapBeanStore());
- bootstrap.getServices().add(NamingContext.class, new MockNamingContext());
bootstrap.getServices().add(ResourceLoader.class, new MockResourceLoader());
bootstrap.getServices().add(TransactionServices.class, new MockTransactionServices());
bootstrap.getServices().add(WebBeanDiscovery.class, new MockWebBeanDiscovery());
@@ -53,7 +50,6 @@
AbstractBootstrap bootstrap = new MockBootstrap();
bootstrap.setEnvironment(Environments.EE);
bootstrap.setApplicationContext(new ConcurrentHashMapBeanStore());
- bootstrap.getServices().add(NamingContext.class, new MockNamingContext());
bootstrap.getServices().add(ResourceLoader.class, new MockResourceLoader());
bootstrap.getServices().add(TransactionServices.class, new MockTransactionServices());
bootstrap.getServices().add(WebBeanDiscovery.class, new MockWebBeanDiscovery());
@@ -71,7 +67,6 @@
AbstractBootstrap bootstrap = new MockBootstrap();
bootstrap.setEnvironment(Environments.EE);
bootstrap.setApplicationContext(new ConcurrentHashMapBeanStore());
- bootstrap.getServices().add(NamingContext.class, new MockNamingContext());
bootstrap.getServices().add(ResourceLoader.class, new MockResourceLoader());
bootstrap.getServices().add(TransactionServices.class, new MockTransactionServices());
bootstrap.getServices().add(WebBeanDiscovery.class, new MockWebBeanDiscovery());
@@ -89,7 +84,6 @@
AbstractBootstrap bootstrap = new MockBootstrap();
bootstrap.setEnvironment(Environments.EE);
bootstrap.setApplicationContext(new ConcurrentHashMapBeanStore());
- bootstrap.getServices().add(NamingContext.class, new MockNamingContext());
bootstrap.getServices().add(ResourceLoader.class, new MockResourceLoader());
bootstrap.getServices().add(TransactionServices.class, new MockTransactionServices());
bootstrap.getServices().add(WebBeanDiscovery.class, new MockWebBeanDiscovery());
@@ -107,7 +101,6 @@
AbstractBootstrap bootstrap = new MockBootstrap();
bootstrap.setEnvironment(Environments.EE);
bootstrap.setApplicationContext(new ConcurrentHashMapBeanStore());
- bootstrap.getServices().add(NamingContext.class, new MockNamingContext());
bootstrap.getServices().add(ResourceLoader.class, new MockResourceLoader());
bootstrap.getServices().add(TransactionServices.class, new MockTransactionServices());
bootstrap.getServices().add(WebBeanDiscovery.class, new MockWebBeanDiscovery());
@@ -126,7 +119,6 @@
AbstractBootstrap bootstrap = new MockBootstrap();
bootstrap.setEnvironment(Environments.EE);
bootstrap.setApplicationContext(new ConcurrentHashMapBeanStore());
- bootstrap.getServices().add(NamingContext.class, new MockNamingContext());
bootstrap.getServices().add(ResourceLoader.class, new MockResourceLoader());
bootstrap.getServices().add(TransactionServices.class, new MockTransactionServices());
bootstrap.getServices().add(WebBeanDiscovery.class, new MockWebBeanDiscovery());
@@ -145,7 +137,6 @@
AbstractBootstrap bootstrap = new MockBootstrap();
bootstrap.setEnvironment(Environments.EE_WEB_PROFILE);
bootstrap.setApplicationContext(new ConcurrentHashMapBeanStore());
- bootstrap.getServices().add(NamingContext.class, new MockNamingContext());
bootstrap.getServices().add(ResourceLoader.class, new MockResourceLoader());
bootstrap.getServices().add(TransactionServices.class, new MockTransactionServices());
bootstrap.getServices().add(WebBeanDiscovery.class, new MockWebBeanDiscovery());
@@ -161,7 +152,6 @@
{
AbstractBootstrap bootstrap = new MockBootstrap();
bootstrap.setEnvironment(Environments.EE);
- bootstrap.getServices().add(NamingContext.class, new MockNamingContext());
bootstrap.getServices().add(ResourceLoader.class, new MockResourceLoader());
bootstrap.setApplicationContext(new ConcurrentHashMapBeanStore());
bootstrap.getServices().add(EjbServices.class, new MockEjbServices());
@@ -177,7 +167,6 @@
{
AbstractBootstrap bootstrap = new MockBootstrap();
bootstrap.setEnvironment(Environments.EE);
- bootstrap.getServices().add(NamingContext.class, new MockNamingContext());
bootstrap.getServices().add(ResourceLoader.class, new MockResourceLoader());
bootstrap.setApplicationContext(new ConcurrentHashMapBeanStore());
bootstrap.getServices().add(EjbServices.class, new MockEjbServices());
@@ -194,7 +183,6 @@
AbstractBootstrap bootstrap = new MockBootstrap();
bootstrap.setEnvironment(Environments.SE);
bootstrap.setApplicationContext(new ConcurrentHashMapBeanStore());
- bootstrap.getServices().add(NamingContext.class, new MockNamingContext());
bootstrap.getServices().add(ResourceLoader.class, new MockResourceLoader());
bootstrap.getServices().add(WebBeanDiscovery.class, new MockWebBeanDiscovery());
bootstrap.initialize();
@@ -205,7 +193,6 @@
{
AbstractBootstrap bootstrap = new MockBootstrap();
bootstrap.setEnvironment(Environments.SERVLET);
- bootstrap.getServices().add(NamingContext.class, new MockNamingContext());
bootstrap.getServices().add(ResourceLoader.class, new MockResourceLoader());
bootstrap.setApplicationContext(new ConcurrentHashMapBeanStore());
bootstrap.getServices().add(WebBeanDiscovery.class, new MockWebBeanDiscovery());
Deleted: ri/trunk/spi/src/test/java/org/jboss/webbeans/bootstrap/api/test/MockNamingContext.java
===================================================================
--- ri/trunk/spi/src/test/java/org/jboss/webbeans/bootstrap/api/test/MockNamingContext.java 2009-04-14 23:07:58 UTC (rev 2407)
+++ ri/trunk/spi/src/test/java/org/jboss/webbeans/bootstrap/api/test/MockNamingContext.java 2009-04-14 23:26:56 UTC (rev 2408)
@@ -1,22 +0,0 @@
-package org.jboss.webbeans.bootstrap.api.test;
-
-import org.jboss.webbeans.resources.spi.NamingContext;
-
-public class MockNamingContext implements NamingContext
-{
-
- public void bind(String name, Object value)
- {
- }
-
- public <T> T lookup(String name, Class<? extends T> expectedType)
- {
- return null;
- }
-
- public void unbind(String key)
- {
-
- }
-
-}
Deleted: ri/trunk/tests/src/main/java/org/jboss/webbeans/mock/MockNamingContext.java
===================================================================
--- ri/trunk/tests/src/main/java/org/jboss/webbeans/mock/MockNamingContext.java 2009-04-14 23:07:58 UTC (rev 2407)
+++ ri/trunk/tests/src/main/java/org/jboss/webbeans/mock/MockNamingContext.java 2009-04-14 23:26:56 UTC (rev 2408)
@@ -1,114 +0,0 @@
-/**
- *
- */
-package org.jboss.webbeans.mock;
-
-import javax.naming.Context;
-import javax.transaction.HeuristicMixedException;
-import javax.transaction.HeuristicRollbackException;
-import javax.transaction.NotSupportedException;
-import javax.transaction.RollbackException;
-import javax.transaction.SystemException;
-import javax.transaction.TransactionManager;
-import javax.transaction.UserTransaction;
-
-import org.jboss.webbeans.resources.spi.helpers.AbstractNamingContext;
-
-public class MockNamingContext extends AbstractNamingContext
-{
-
- private final Context context;
-
- public MockNamingContext(Context context)
- {
- this.context = context;
- }
-
- public void bind(String key, Object value)
- {
- if (context != null)
- {
- super.bind(key, value);
- }
- }
-
- public void unbind(String key)
- {
- if (context != null)
- {
- super.unbind(key);
- }
- }
-
- public <T> T lookup(String name, Class<? extends T> expectedType)
- {
- if (context != null)
- {
- T instance = overrideLookup(name, expectedType);
- if (instance == null)
- {
- instance = super.lookup(name, expectedType);
- }
- return instance;
- }
- else
- {
- return null;
- }
- }
-
- @SuppressWarnings("unchecked")
- private <T> T overrideLookup(String name, Class<? extends T> expectedType)
- {
- // JBoss Embedded EJB 3.1 doesn't seem to bind this!
- if (name.equals("java:comp/UserTransaction"))
- {
- final TransactionManager tm = super.lookup("java:/TransactionManager", TransactionManager.class);
- return (T) new UserTransaction()
- {
-
- public void begin() throws NotSupportedException, SystemException
- {
- tm.begin();
- }
-
- public void commit() throws RollbackException, HeuristicMixedException, HeuristicRollbackException, SecurityException, IllegalStateException, SystemException
- {
- tm.commit();
- }
-
- public int getStatus() throws SystemException
- {
- return tm.getStatus();
- }
-
- public void rollback() throws IllegalStateException, SecurityException, SystemException
- {
- tm.rollback();
- }
-
- public void setRollbackOnly() throws IllegalStateException, SystemException
- {
- tm.setRollbackOnly();
- }
-
- public void setTransactionTimeout(int seconds) throws SystemException
- {
- tm.setTransactionTimeout(seconds);
- }
-
- };
- }
- else
- {
- return null;
- }
- }
-
- @Override
- public Context getContext()
- {
- return context;
- }
-
-}
\ No newline at end of file
Modified: ri/trunk/tests/src/main/java/org/jboss/webbeans/mock/MockServletLifecycle.java
===================================================================
--- ri/trunk/tests/src/main/java/org/jboss/webbeans/mock/MockServletLifecycle.java 2009-04-14 23:07:58 UTC (rev 2407)
+++ ri/trunk/tests/src/main/java/org/jboss/webbeans/mock/MockServletLifecycle.java 2009-04-14 23:26:56 UTC (rev 2408)
@@ -6,7 +6,6 @@
import org.jboss.webbeans.context.ContextLifecycle;
import org.jboss.webbeans.context.api.BeanStore;
import org.jboss.webbeans.context.api.helpers.ConcurrentHashMapBeanStore;
-import org.jboss.webbeans.resources.spi.NamingContext;
import org.jboss.webbeans.resources.spi.ResourceLoader;
public class MockServletLifecycle extends ContextLifecycle
@@ -28,7 +27,6 @@
}
bootstrap = new WebBeansBootstrap();
bootstrap.setEnvironment(Environments.SERVLET);
- bootstrap.getServices().add(NamingContext.class, new MockNamingContext(null));
bootstrap.getServices().add(ResourceLoader.class, MOCK_RESOURCE_LOADER);
bootstrap.getServices().add(WebBeanDiscovery.class, webBeanDiscovery);
bootstrap.setApplicationContext(applicationBeanStore);
More information about the weld-commits
mailing list