Skip to content

Commit

Permalink
split out ejb discovery from bean discovery, restructure bootstrap to…
Browse files Browse the repository at this point in the history
… be a bit nicer

git-svn-id: http://anonsvn.jboss.org/repos/weld/ri/trunk@984 1c488680-804c-0410-94cd-c6b725194a0e
  • Loading branch information
pmuir committed Jan 15, 2009
1 parent d2837e1 commit a7cb6ea
Show file tree
Hide file tree
Showing 14 changed files with 249 additions and 94 deletions.
7 changes: 4 additions & 3 deletions jboss-as/build.xml
Expand Up @@ -60,9 +60,10 @@
<copy todir="${jboss.home}/server/default/deployers/webbeans.deployer/lib-int">
<fileset dir="target/webbeans.deployer">
<include name="webbeans-ri.jar" />
<include name="webbeans-ri-int-jbossas.jar" />
<include name="webbeans-api.jar" />
<include name="webbeans-ri-spi.jar" />
<include name="webbeans-ri-int-jbossas.jar" />
<include name="webbeans-ri-int-jboss-ejb.jar" />
<include name="webbeans-api.jar" />
<include name="webbeans-ri-spi.jar" />
</fileset>
</copy>

Expand Down
@@ -0,0 +1,42 @@
/*
* 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.bootstrap.spi;

import org.jboss.webbeans.ejb.spi.EjbDescriptor;



/**
* A container should implement this interface to allow the Web Beans RI to
* discover the EJBs the application contains
*
* @author Pete Muir
*
*/
public interface EjbDiscovery
{
public static final String PROPERTY_NAME = EjbDiscovery.class.getName();

/**
* Gets a descriptor for each EJB in the application
*
* @return The bean class to descriptor map
*/
public Iterable<EjbDescriptor<?>> discoverEjbs();

}
@@ -0,0 +1,15 @@
package org.jboss.webbeans.bootstrap.spi;

import org.jboss.webbeans.ejb.spi.EjbDescriptor;

public abstract class ForwardingEjbDiscovery implements EjbDiscovery
{

protected abstract EjbDiscovery delegate();

public Iterable<EjbDescriptor<?>> discoverEjbs()
{
return delegate().discoverEjbs();
}

}
Expand Up @@ -19,8 +19,6 @@

import java.net.URL;

import org.jboss.webbeans.ejb.spi.EjbDescriptor;

/**
* A container should implement this interface to allow the Web Beans RI to
* discover the Web Beans to deploy
Expand All @@ -46,11 +44,4 @@ public interface WebBeanDiscovery
*/
public Iterable<URL> discoverWebBeansXml();

/**
* Gets a descriptor for each EJB in the application
*
* @return The bean class to descriptor map
*/
public Iterable<EjbDescriptor<?>> discoverEjbs();

}
@@ -0,0 +1,44 @@
package org.jboss.webbeans.ejb.spi;

import java.lang.annotation.Annotation;

import javax.webbeans.InjectionPoint;

import org.jboss.webbeans.resources.spi.Naming;

public abstract class ForwardingEjbResolver implements EjbResolver
{

public abstract EjbResolver delegate();

public Class<? extends Annotation> getEJBAnnotation()
{
return delegate().getEJBAnnotation();
}

public Class<? extends Annotation> getPersistenceContextAnnotation()
{
return delegate().getPersistenceContextAnnotation();
}

public Class<? extends Annotation> getResourceAnnotation()
{
return delegate().getResourceAnnotation();
}

public Object resolveEjb(InjectionPoint injectionPoint, Naming naming)
{
return delegate().resolveEjb(injectionPoint, naming);
}

public Object resolvePersistenceContext(InjectionPoint injectionPoint, Naming naming)
{
return delegate().resolvePersistenceContext(injectionPoint, naming);
}

public Object resolveResource(InjectionPoint injectionPoint, Naming naming)
{
return delegate().resolveResource(injectionPoint, naming);
}

}
Expand Up @@ -48,8 +48,9 @@
import org.jboss.webbeans.bean.ProducerFieldBean;
import org.jboss.webbeans.bean.ProducerMethodBean;
import org.jboss.webbeans.bean.SimpleBean;
import org.jboss.webbeans.binding.InitializedBinding;
import org.jboss.webbeans.binding.DeployedBinding;
import org.jboss.webbeans.binding.InitializedBinding;
import org.jboss.webbeans.bootstrap.spi.EjbDiscovery;
import org.jboss.webbeans.bootstrap.spi.WebBeanDiscovery;
import org.jboss.webbeans.ejb.EJBApiAbstraction;
import org.jboss.webbeans.ejb.spi.EjbResolver;
Expand Down Expand Up @@ -130,6 +131,8 @@ public ManagerImpl getManager()
}

protected abstract WebBeanDiscovery getWebBeanDiscovery();

protected abstract EjbDiscovery getEjbDiscovery();

public abstract ResourceLoader getResourceLoader();

Expand Down Expand Up @@ -298,15 +301,14 @@ public void boot()
validateBootstrap();
// Must populate EJB cache first, as we need it to detect whether a
// bean is an EJB!
getManager().getEjbDescriptorCache().addAll(getWebBeanDiscovery().discoverEjbs());
getManager().getEjbDescriptorCache().addAll(getEjbDiscovery().discoverEjbs());
registerBeans(getWebBeanDiscovery().discoverWebBeanClasses());
getManager().fireEvent(getManager(), new InitializedBinding());
List<Bean<?>> beans = getManager().getBeans();
log.info("Initialization completed. Validing " + beans.size() + " Web Beans");
log.info("Web Beans initialized. Validating beans.");
getManager().getResolver().resolveInjectionPoints();
BeanValidation.validate(getManager().getBeans());
getManager().fireEvent(getManager(), new DeployedBinding());
log.info("Deploy complete");
}
}

Expand Down
Expand Up @@ -23,6 +23,7 @@

import org.jboss.webbeans.bootstrap.PropertiesBasedBootstrap;
import org.jboss.webbeans.bootstrap.SimpleResourceLoader;
import org.jboss.webbeans.bootstrap.spi.EjbDiscovery;
import org.jboss.webbeans.bootstrap.spi.WebBeanDiscovery;
import org.jboss.webbeans.context.ApplicationContext;
import org.jboss.webbeans.context.DependentContext;
Expand All @@ -43,39 +44,29 @@ public class ServletBootstrap extends PropertiesBasedBootstrap
{

// The resource loader
private ResourceLoader resourceLoader;
private final ResourceLoader resourceLoader;
// The discover implementation
private WebBeanDiscovery webBeanDiscovery;
private final WebBeanDiscovery webBeanDiscovery;

private final EjbDiscovery ejbDiscovery;

// The deployment properties
private DeploymentProperties deploymentProperties;
private final DeploymentProperties deploymentProperties;

public ServletBootstrap(ServletContext servletContext)
{

// Create a simpple resource loader based for initial loading
this.resourceLoader = new SimpleResourceLoader();
this.deploymentProperties = new DeploymentProperties(resourceLoader);
ResourceLoader temporaryResourceLoader = new SimpleResourceLoader();
this.deploymentProperties = new DeploymentProperties(temporaryResourceLoader);

// Attempt to create a plugin resource loader
Constructor<? extends ResourceLoader> resourceLoaderConstructor = getClassConstructor(deploymentProperties, resourceLoader, ResourceLoader.PROPERTY_NAME, ResourceLoader.class, ServletContext.class);
if (resourceLoaderConstructor != null)
{
this.resourceLoader = newInstance(resourceLoaderConstructor, servletContext);
}
this.resourceLoader = createResourceLoader(servletContext, temporaryResourceLoader);

// Now safe to initialize the manager
initManager(servletContext);

// Attempt to create a plugin web beans discovery
Constructor<? extends WebBeanDiscovery> webBeanDiscoveryConstructor = getClassConstructor(deploymentProperties, resourceLoader, WebBeanDiscovery.PROPERTY_NAME, WebBeanDiscovery.class, ServletContext.class);
if (webBeanDiscoveryConstructor == null)
{
throw new IllegalStateException("Cannot load Web Bean discovery plugin! Check if Web Beans is properly installed into your container");
}
else
{
this.webBeanDiscovery = newInstance(webBeanDiscoveryConstructor, servletContext);
}
this.webBeanDiscovery = createWebBeanDiscovery(servletContext);
this.ejbDiscovery = createEjbDiscovery(servletContext);

// Register the contexts for the Servlet environment
getManager().addContext(DependentContext.INSTANCE);
Expand All @@ -87,10 +78,10 @@ public ServletBootstrap(ServletContext servletContext)

private void initManager(ServletContext servletContext)
{
initManager(getNaming(servletContext), getEjbResolver(servletContext), getResourceLoader());
initManager(createNaming(servletContext), createEjbResolver(servletContext), getResourceLoader());
}

public Naming getNaming(ServletContext servletContext)
protected Naming createNaming(ServletContext servletContext)
{
Constructor<? extends Naming> namingConstructor = getClassConstructor(getDeploymentProperties(), getResourceLoader(), Naming.PROPERTY_NAME, Naming.class, ServletContext.class);
if (namingConstructor != null)
Expand All @@ -103,7 +94,7 @@ public Naming getNaming(ServletContext servletContext)
}
}

public EjbResolver getEjbResolver(ServletContext servletContext)
protected EjbResolver createEjbResolver(ServletContext servletContext)
{
Constructor<? extends EjbResolver> constructor = getClassConstructor(getDeploymentProperties(), getResourceLoader(), EjbResolver.PROPERTY_NAME, EjbResolver.class, ServletContext.class);
if (constructor != null)
Expand All @@ -115,6 +106,47 @@ public EjbResolver getEjbResolver(ServletContext servletContext)
throw new IllegalStateException("Unable to find a EjbResolver, check Web Beans is correctly installed in your container");
}
}

protected EjbDiscovery createEjbDiscovery(ServletContext servletContext)
{
Constructor<? extends EjbDiscovery> constructor = getClassConstructor(getDeploymentProperties(), getResourceLoader(), EjbDiscovery.PROPERTY_NAME, EjbDiscovery.class, ServletContext.class);
if (constructor != null)
{
return newInstance(constructor, servletContext);
}
else
{
throw new IllegalStateException("Unable to find a EjbDiscovery, check Web Beans is correctly installed in your container");
}
}

protected WebBeanDiscovery createWebBeanDiscovery(ServletContext servletContext)
{
// Attempt to create a plugin web beans discovery
Constructor<? extends WebBeanDiscovery> webBeanDiscoveryConstructor = getClassConstructor(deploymentProperties, resourceLoader, WebBeanDiscovery.PROPERTY_NAME, WebBeanDiscovery.class, ServletContext.class);
if (webBeanDiscoveryConstructor == null)
{
throw new IllegalStateException("Cannot load Web Bean discovery plugin! Check if Web Beans is properly installed into your container");
}
else
{
return newInstance(webBeanDiscoveryConstructor, servletContext);
}
}

protected ResourceLoader createResourceLoader(ServletContext servletContext, ResourceLoader resourceLoader)
{
// Attempt to create a plugin resource loader
Constructor<? extends ResourceLoader> resourceLoaderConstructor = getClassConstructor(deploymentProperties, resourceLoader, ResourceLoader.PROPERTY_NAME, ResourceLoader.class, ServletContext.class);
if (resourceLoaderConstructor != null)
{
return newInstance(resourceLoaderConstructor, servletContext);
}
else
{
return resourceLoader;
}
}

@Override
protected DeploymentProperties getDeploymentProperties()
Expand All @@ -133,5 +165,11 @@ protected WebBeanDiscovery getWebBeanDiscovery()
{
return webBeanDiscovery;
}

@Override
protected EjbDiscovery getEjbDiscovery()
{
return ejbDiscovery;
}

}
Expand Up @@ -158,7 +158,7 @@ protected void addStandardDeploymentTypesForTests()

protected <T> void addToEjbCache(Class<T> clazz)
{
manager.getEjbDescriptorCache().add(new MockEjbDescriptor<T>(clazz));
manager.getEjbDescriptorCache().add(MockEjbDescriptor.of(clazz));
}

protected byte[] serialize(Object instance) throws IOException
Expand Down
@@ -1,8 +1,6 @@
package org.jboss.webbeans.test;

import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -232,7 +230,7 @@ public void testDiscoverFails()
@Test(groups="bootstrap")
public void testDiscover()
{
webBeansBootstrap.setWebBeanDiscovery(new MockWebBeanDiscovery(new HashSet<Class<?>>(Arrays.asList(Hound.class, Elephant.class, Panther.class, Tiger.class, Tuna.class, Salmon.class, SeaBass.class, Sole.class))));
webBeansBootstrap.setWebBeanDiscovery(new MockWebBeanDiscovery(Hound.class, Elephant.class, Panther.class, Tiger.class, Tuna.class, Salmon.class, SeaBass.class, Sole.class));
webBeansBootstrap.boot();

Map<Class<?>, Bean<?>> classes = new HashMap<Class<?>, Bean<?>>();
Expand Down Expand Up @@ -266,7 +264,7 @@ public void testDiscover()
public void testInitializedEvent()
{
assert !InitializedObserver.observered;
webBeansBootstrap.setWebBeanDiscovery(new MockWebBeanDiscovery(new HashSet<Class<?>>(Arrays.asList(InitializedObserver.class))));
webBeansBootstrap.setWebBeanDiscovery(new MockWebBeanDiscovery(InitializedObserver.class));
webBeansBootstrap.boot();

assert InitializedObserver.observered;
Expand All @@ -275,14 +273,14 @@ public void testInitializedEvent()
@Test(groups="bootstrap")
public void testRequestContextActiveDuringInitializtionEvent()
{
webBeansBootstrap.setWebBeanDiscovery(new MockWebBeanDiscovery(new HashSet<Class<?>>(Arrays.asList(InitializedObserverWhichUsesRequestContext.class, Tuna.class)), null, new HashSet<Class<?>>()));
webBeansBootstrap.setWebBeanDiscovery(new MockWebBeanDiscovery(InitializedObserverWhichUsesRequestContext.class, Tuna.class));
webBeansBootstrap.boot();
}

@Test(groups={"bootstrap", "broken"})
public void testApplicationContextActiveDuringInitializtionEvent()
{
webBeansBootstrap.setWebBeanDiscovery(new MockWebBeanDiscovery(new HashSet<Class<?>>(Arrays.asList(InitializedObserverWhichUsesApplicationContext.class, LadybirdSpider.class)), null, new HashSet<Class<?>>()));
webBeansBootstrap.setWebBeanDiscovery(new MockWebBeanDiscovery(InitializedObserverWhichUsesApplicationContext.class, LadybirdSpider.class));
webBeansBootstrap.boot();
}

Expand Down

0 comments on commit a7cb6ea

Please sign in to comment.