aslak

Arquillian - Beta1 - Core

Oct 27th, 2010
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.41 KB | None | 0 0
  1.    public void coreLifecycleExample()
  2.    {
  3.       Manager manager = ManagerBuilder.from()
  4.          .context(SuiteContext.class)
  5.          .extensions(
  6.             ContainerStarter.class,
  7.             ContainerCreator.class).create();
  8.  
  9.       manager.getContext(SuiteContext.class).activate();
  10.      
  11.       manager.fire(new BeforeSuite());
  12.       ...
  13.      
  14.       manager.getContext(SuiteContext.class).detivate();
  15.    }
  16.    
  17.    // A Extension for the Arquillian beta-1 core
  18.    public class ContainerCreator
  19.    {
  20.       // inject a Instance/Provider style reference(similar to CDI/ATInject) to a Type in one of the active contexts
  21.       @Inject
  22.       private Instance<ServiceLoader> loaderInst;
  23.  
  24.       // get a reference to a Injector so we can inject Instance refs to non managed objects
  25.       @Inject
  26.       private Instance<Injector> injectorInst;
  27.      
  28.       // since we intend to set this value, we have to specify to which scope to export it to and use the InstanceProvider
  29.       @Inject @SuiteScoped
  30.       private InstanceProducer<DeployableContainer> deployInst;
  31.      
  32.       // A external source has fired a BeforeSuite event
  33.       public void loadAndBindDeployableContainer(@Observes BeforeSuite event)
  34.       {
  35.          // load the instance like we normally would do in the SPI land.
  36.          DeployableContainer container = loaderInst.get().onlyOne(DeployableContainer.class);
  37.  
  38.          // inject into the non managed instance
  39.          injector.get().inject(container);
  40.          
  41.          // setting a value on a Instance will auto trigger a event of that type.
  42.          // Some extension are not interested in a specific life cycle event, but rather when something becomes available in the Context.
  43.          deployInst.set(container);
  44.       }
  45.    }
  46.    
  47.    public class ContainerStarter
  48.    {
  49.       // Like Instance, we can get a reference to fire a Event
  50.       @Inject
  51.       private Event<BeforeContainerStart> before;
  52.  
  53.       @Inject
  54.       private Event<AfterContainerStart> after;
  55.  
  56.       // We are registered to listen to the availability of a DeployalbeContainer
  57.       public void startContainer(@Observes DeployableContainer container)
  58.       {
  59.          // fire a event
  60.          before.fire(new BeforeContainerStart(container));
  61.          
  62.          // do the actual work
  63.          container.start();
  64.  
  65.          // fire a event
  66.          after.fire(new AfterContainerStart(container));
  67.       }
  68.    }
Add Comment
Please, Sign In to add comment