Skip to content

Commit

Permalink
Stop exposing the Scanner API at all, javadoc, add helper classes for…
Browse files Browse the repository at this point in the history
… creating BDAs and Deployments, cleanup now that we don't expose the scanner api
  • Loading branch information
pmuir committed May 25, 2010
1 parent b27a83e commit 7106ecb
Show file tree
Hide file tree
Showing 10 changed files with 283 additions and 186 deletions.
94 changes: 65 additions & 29 deletions src/main/java/org/jboss/weld/environment/se/Weld.java
Expand Up @@ -19,38 +19,42 @@
import java.lang.annotation.Annotation;
import java.util.Arrays;

import javax.annotation.PostConstruct;
import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.UnsatisfiedResolutionException;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.BeanManager;

import org.jboss.weld.bootstrap.api.Bootstrap;
import org.jboss.weld.bootstrap.api.Environments;
import org.jboss.weld.bootstrap.spi.Deployment;
import org.jboss.weld.context.api.BeanStore;
import org.jboss.weld.context.api.helpers.ConcurrentHashMapBeanStore;
import org.jboss.weld.environment.se.discovery.WeldSEDeployment;
import org.jboss.weld.environment.se.discovery.url.URLScanner;
import org.jboss.weld.environment.se.discovery.url.WeldSEResourceLoader;
import org.jboss.weld.environment.se.discovery.url.WeldSEUrlDeployment;
import org.jboss.weld.resources.spi.ResourceLoader;

/**
* An alternative means of booting WeldContainer form an arbitrary main method
* within an SE application, <em>without</em> using the built-in
* ContainerInitialized event. Typical usage of this API looks like this: <code>
* <p>
* The preferred method of booting Weld SE.
* </p>
*
* <p>
* Typical usage of this API looks like this:
* </p>
*
* <pre>
* WeldContainer weld = new Weld().initialize();
* weld.instance().select(Foo.class).get();
* weld.event().select(Bar.class).fire(new Bar());
* weld.shutdown();
* </code>
* </pre>
*
* @author Peter Royle
* @author Pete Muir
*/
public class Weld
{

protected static final String[] RESOURCES = { "META-INF/beans.xml" };

private static final String BOOTSTRAP_IMPL_CLASS_NAME = "org.jboss.weld.bootstrap.WeldBootstrap";

private ShutdownManager shutdownManager;
Expand All @@ -59,12 +63,11 @@ public class Weld
* Boots Weld and creates and returns a WeldContainer instance, through which
* beans and events can be accessed.
*/
@PostConstruct
public WeldContainer initialize()
{

BeanStore applicationBeanStore = new ConcurrentHashMapBeanStore();
WeldSEDeployment deployment = createDeployment();
Deployment deployment = createDeployment();

Bootstrap bootstrap = null;
try
Expand All @@ -80,13 +83,9 @@ public WeldContainer initialize()
throw new IllegalStateException("Error loading Weld bootstrap, check that Weld is on the classpath", ex);
}


// Kick off the scan
deployment.getScanner().scan(deployment.getServices().get(ResourceLoader.class));

// Set up the container
bootstrap.startContainer(Environments.SE, deployment, applicationBeanStore);

// Start the container
bootstrap.startInitialization();
bootstrap.deployBeans();
Expand All @@ -101,23 +100,60 @@ public WeldContainer initialize()
}

/**
* Users can subclass and override this method to customise the deployment
* before weld boots up. For example, to add a custom ResourceLoader, you
* would subclass Weld like so: <code>
* public class MyWeld extends Weld {
* <p>
* Extensions to Weld SE can subclass and override this method to customise
* the deployment before weld boots up. For example, to add a custom
* ResourceLoader, you would subclass Weld like so:
* </p>
*
* <pre>
* public class MyWeld extends Weld
* {
* protected Deployment createDeployment()
* {
* Deployment deployment = super.createDeployment();
* deployment.getServices().add(ResourceLoader.class, new MyResourceLoader());
* return deployment;
* }
* }
*</pre>
*
* <p>
* This could then be used as normal:
* </p>
*
* <pre>
* WeldContainer container = new MyWeld().initialize();
* </pre>
*
* @Override protected WeldSEDeployment createDeployment() { WeldSEDeployment
* myDeployment = super.createDeployment();
* deployment.getServices().add(ResourceLoader.class, new
* OSGIResourceLoader()); } } </code>
*/
protected WeldSEDeployment createDeployment()
protected Deployment createDeployment()
{
WeldSEDeployment deployment = new WeldSEDeployment(new URLScanner(RESOURCES));
deployment.getServices().add(ResourceLoader.class, new WeldSEResourceLoader());
return deployment;
return new WeldSEUrlDeployment(new WeldSEResourceLoader());
}


/**
* Utility method allowing managed instances of beans to provide entry points
* for non-managed beans (such as {@link WeldContainer}). Should only called
* once Weld has finished booting.
*
* @param manager the BeanManager to use to access the managed instance
* @param type the type of the Bean
* @param bindings the bean's qualifiers
* @return a managed instance of the bean
* @throws IllegalArgumentException if the given type represents a type
* variable
* @throws IllegalArgumentException if two instances of the same qualifier
* type are given
* @throws IllegalArgumentException if an instance of an annotation that is
* not a qualifier type is given
* @throws UnsatisfiedResolutionException if no beans can be resolved * @throws
* AmbiguousResolutionException if the ambiguous dependency
* resolution rules fail
* @throws IllegalArgumentException if the given type is not a bean type of
* the given bean
*
*/
protected <T> T getInstanceByType(BeanManager manager, Class<T> type, Annotation... bindings)
{
final Bean<?> bean = manager.resolve(manager.getBeans(type));
Expand Down
@@ -0,0 +1,51 @@
package org.jboss.weld.environment.se.discovery;

import java.util.Collection;
import java.util.Collections;

import org.jboss.weld.bootstrap.api.ServiceRegistry;
import org.jboss.weld.bootstrap.api.helpers.SimpleServiceRegistry;
import org.jboss.weld.bootstrap.spi.BeanDeploymentArchive;
import org.jboss.weld.ejb.spi.EjbDescriptor;

/**
* Implements the basic requirements of a {@link BeanDeploymentArchive} (bean
* archive id and service registry).
*
* Suitable for extension by those who need to build custom
* {@link BeanDeploymentArchive} implementations.
*
* @see MutableBeanDeploymentArchive
* @see ImmutableBeanDeploymentArchive
*
* @author Pete Muir
*
*/
public abstract class AbstractWeldSEBeanDeploymentArchive implements BeanDeploymentArchive
{

private final ServiceRegistry serviceRegistry;
private final String id;

public AbstractWeldSEBeanDeploymentArchive(String id)
{
this.id = id;
this.serviceRegistry = new SimpleServiceRegistry();
}

public Collection<EjbDescriptor<?>> getEjbs()
{
return Collections.emptyList();
}

public String getId()
{
return id;
}

public ServiceRegistry getServices()
{
return serviceRegistry;
}

}
@@ -0,0 +1,34 @@
package org.jboss.weld.environment.se.discovery;

import org.jboss.weld.bootstrap.api.ServiceRegistry;
import org.jboss.weld.bootstrap.api.helpers.SimpleServiceRegistry;
import org.jboss.weld.bootstrap.spi.Deployment;

/**
* Implements the basic requirements of a {@link Deployment}. Provides a service
* registry.
*
* Suitable for extension by those who need to build custom {@link Deployment}
* implementations.
*
* @author Pete Muir
*
*/
public abstract class AbstractWeldSEDeployment implements Deployment
{

public static final String[] RESOURCES = { "META-INF/beans.xml" };

private final ServiceRegistry serviceRegistry;

public AbstractWeldSEDeployment()
{
this.serviceRegistry = new SimpleServiceRegistry();
}

public ServiceRegistry getServices()
{
return serviceRegistry;
}

}
@@ -0,0 +1,75 @@
/**
* JBoss, Home of Professional Open Source
* Copyright 2009, Red Hat, Inc. and/or its affiliates, 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.weld.environment.se.discovery;

import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import org.jboss.weld.bootstrap.spi.BeanDeploymentArchive;
import org.jboss.weld.bootstrap.spi.Deployment;

/**
* An immutable implementation of {@link BeanDeploymentArchive} which must have
* classes and beans.xml resources added to it via
* {@link ImmutableBeanDeploymentArchive#ImmutableBeanDeploymentArchive(String, Collection, Collection, List)}
* or
* {@link ImmutableBeanDeploymentArchive#ImmutableBeanDeploymentArchive(String, Collection, Collection)}
*
* See {@link Deployment} for more detailed information on creating deployment
* structures.
*
* @author Pete Muir
*
*/
public class ImmutableBeanDeploymentArchive extends AbstractWeldSEBeanDeploymentArchive
{

private final Collection<Class<?>> beanClasses;
private final Collection<URL> beansXml;
private final Collection<BeanDeploymentArchive> beanDeploymentArchives;

public ImmutableBeanDeploymentArchive(String id, Collection<Class<?>> beanClasses, Collection<URL> beansXml, Collection<BeanDeploymentArchive> beanDeploymentArchives)
{
super(id);
this.beanClasses = beanClasses;
this.beansXml = beansXml;
this.beanDeploymentArchives = beanDeploymentArchives;
}

public ImmutableBeanDeploymentArchive(String id, Collection<Class<?>> beanClasses, Collection<URL> beansXml)
{
this(id, beanClasses, beansXml, new ArrayList<BeanDeploymentArchive>());
}

public Collection<Class<?>> getBeanClasses()
{
return Collections.unmodifiableCollection(beanClasses);
}

public Collection<BeanDeploymentArchive> getBeanDeploymentArchives()
{
return Collections.unmodifiableCollection(beanDeploymentArchives);
}

public Collection<URL> getBeansXml()
{
return Collections.unmodifiableCollection(beansXml);
}
}

0 comments on commit 7106ecb

Please sign in to comment.