Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial addition of new API for booting Weld from SE
- Loading branch information
Showing
4 changed files
with
275 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/** | ||
* 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; | ||
|
||
import javax.enterprise.event.Event; | ||
import javax.enterprise.inject.Instance; | ||
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.BeanDeploymentArchive; | ||
import org.jboss.weld.context.api.BeanStore; | ||
import org.jboss.weld.context.api.helpers.ConcurrentHashMapBeanStore; | ||
import org.jboss.weld.environment.se.beans.InstanceManager; | ||
import org.jboss.weld.environment.se.discovery.SEWeldDeployment; | ||
import org.jboss.weld.environment.se.util.Reflections; | ||
import org.jboss.weld.environment.se.util.WeldManagerUtils; | ||
import org.jboss.weld.manager.api.WeldManager; | ||
|
||
/** | ||
* An alternative means of booting Weld form an arbitrary main method within an | ||
* SE application, <em>without</em> using the built-in ContainerInitialized event. | ||
* Typical usage of the API looks like this: | ||
* <code> | ||
* Weld weld = new Weld().initialize(); | ||
* weld.instance().select(Foo.class).get(); | ||
* weld.event().select(Bar.class).fire( new Bar() ); | ||
* weld.shutdown(); | ||
* </code> | ||
* | ||
* @author Peter Royle | ||
*/ | ||
public class Weld | ||
{ | ||
|
||
private static final String BOOTSTRAP_IMPL_CLASS_NAME = "org.jboss.weld.bootstrap.WeldBootstrap"; | ||
private final Bootstrap bootstrap; | ||
private final BeanStore applicationBeanStore; | ||
private WeldManager manager; | ||
private InstanceManager instanceManager; | ||
|
||
public Weld() | ||
{ | ||
try | ||
{ | ||
bootstrap = Reflections.newInstance(BOOTSTRAP_IMPL_CLASS_NAME, Bootstrap.class); | ||
} catch (Exception e) | ||
{ | ||
throw new IllegalStateException("Error loading Weld bootstrap, check that Weld is on the classpath", e); | ||
} | ||
this.applicationBeanStore = new ConcurrentHashMapBeanStore(); | ||
} | ||
|
||
public Weld initialize() | ||
{ | ||
|
||
SEWeldDeployment deployment = new SEWeldDeployment() | ||
{ | ||
}; | ||
bootstrap.startContainer(Environments.SE, deployment, this.applicationBeanStore); | ||
final BeanDeploymentArchive mainBeanDepArch = deployment.getBeanDeploymentArchives().get(0); | ||
this.manager = bootstrap.getManager(mainBeanDepArch); | ||
bootstrap.startInitialization(); | ||
bootstrap.deployBeans(); | ||
WeldManagerUtils.getInstanceByType(manager, ShutdownManager.class).setBootstrap(bootstrap); | ||
bootstrap.validateBeans(); | ||
bootstrap.endInitialization(); | ||
|
||
instanceManager = WeldManagerUtils.getInstanceByType(manager, InstanceManager.class); | ||
|
||
return this; | ||
} | ||
|
||
public Instance<Object> instance() | ||
{ | ||
return instanceManager.getInstances(); | ||
} | ||
|
||
public Instance<Event> event() | ||
{ | ||
return instanceManager.getEvents(); | ||
} | ||
|
||
public BeanManager getBeanManager() | ||
{ | ||
return manager; | ||
} | ||
|
||
/** | ||
* Convenience method for shutting down the container. | ||
*/ | ||
public void shutdown() { | ||
WeldManagerUtils.getInstanceByType(manager, ShutdownManager.class).shutdown(); | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
src/main/java/org/jboss/weld/environment/se/beans/InstanceManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/** | ||
* 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.beans; | ||
|
||
import javax.enterprise.event.Event; | ||
import javax.enterprise.inject.Instance; | ||
import javax.inject.Inject; | ||
import org.jboss.weld.environment.se.Weld; | ||
|
||
/** | ||
* A managed bean which holds all of the injected instances of managed beans and | ||
* events. It is primarily used as a delegate for the Weld class's instance() | ||
* and event() methods. | ||
* | ||
* @see Weld | ||
* @author Peter Royle | ||
*/ | ||
public class InstanceManager | ||
{ | ||
|
||
@Inject Instance<Object> instances; | ||
@Inject Instance<Event> events; | ||
|
||
public InstanceManager() | ||
{ | ||
} | ||
|
||
public Instance<Object> getInstances() | ||
{ | ||
return instances; | ||
} | ||
|
||
public Instance<Event> getEvents() | ||
{ | ||
return events; | ||
} | ||
} |
91 changes: 91 additions & 0 deletions
91
src/test/java/org/jboss/weld/environment/se/test/WeldMainTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/** | ||
* 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.test; | ||
|
||
import javax.enterprise.inject.spi.BeanManager; | ||
import javax.enterprise.util.AnnotationLiteral; | ||
|
||
import org.jboss.weld.environment.se.Weld; | ||
import org.jboss.weld.environment.se.events.Shutdown; | ||
import org.jboss.weld.environment.se.test.beans.CustomEvent; | ||
import org.jboss.weld.environment.se.test.beans.InitObserverTestBean; | ||
import org.jboss.weld.environment.se.test.beans.MainTestBean; | ||
import org.jboss.weld.environment.se.test.beans.ObserverTestBean; | ||
import org.jboss.weld.environment.se.test.beans.ParametersTestBean; | ||
import org.testng.Assert; | ||
import org.testng.annotations.Test; | ||
|
||
/** | ||
* | ||
* @author Peter Royle | ||
*/ | ||
public class WeldMainTest | ||
{ | ||
|
||
/** | ||
* Test the alternate API for boting Weld from an SE app. | ||
*/ | ||
@Test | ||
public void testInitialize() | ||
{ | ||
|
||
Weld weld = new Weld().initialize(); | ||
|
||
MainTestBean mainTestBean = weld.instance().select(MainTestBean.class).get(); | ||
Assert.assertNotNull(mainTestBean); | ||
|
||
ParametersTestBean paramsBean = mainTestBean.getParametersTestBean(); | ||
Assert.assertNotNull(paramsBean); | ||
Assert.assertNotNull(paramsBean.getParameters()); | ||
|
||
shutdownManager(weld.getBeanManager()); | ||
} | ||
|
||
/** | ||
* Test the firing of observers using the alternate API for boting Weld from an SE app. | ||
*/ | ||
@Test | ||
public void testObservers() | ||
{ | ||
InitObserverTestBean.reset(); | ||
ObserverTestBean.reset(); | ||
|
||
Weld weld = new Weld().initialize(); | ||
weld.getBeanManager().fireEvent(new CustomEvent()); | ||
// TODO(PR): this should work. What's with the compile error? | ||
// weld.event().select(CustomEvent.class).fire(new CustomEvent()); | ||
|
||
Assert.assertTrue(ObserverTestBean.isBuiltInObserved()); | ||
Assert.assertTrue(ObserverTestBean.isCustomObserved()); | ||
Assert.assertFalse(ObserverTestBean.isInitObserved()); | ||
|
||
Assert.assertFalse(InitObserverTestBean.isInitObserved()); | ||
} | ||
|
||
private void shutdownManager(BeanManager manager) | ||
{ | ||
manager.fireEvent(manager, new ShutdownAnnotation()); | ||
} | ||
|
||
private static class ShutdownAnnotation extends AnnotationLiteral<Shutdown> | ||
{ | ||
|
||
public ShutdownAnnotation() | ||
{ | ||
} | ||
} | ||
} |