It shouldn't be too hard - take a look at org.jboss.seam.servlet.SeamListener. Basically it makes the following call to initialise Seam:
new Initialization( event.getServletContext() ).create().init();
You would need to pass in a mock ServletContext (I guess we should look at making this more abstract, as I believe it's only used for loading resources). Let us know how you go.
Thanks for that. Unfortunately the team decided to use another design approach to the problem. Was really looking forward to dig into Seam code. Hopefully will have some time soon to look into it.
I have the same problem... is there an example on doing this?
I have a seam application that now need a console interface.
Shouldn't it be as easy as creating an ApplicationContext instance? That's all it takes in Spring.
Now if you rely on JNDI, transactions, etc. these will take extra work to configure (ideally just adding config info & the proper JARs) since they are normally handled by the app server.
If it's not that easy, there's no reason why it shouldn't be. At its heart, Seam is a DI container, and if it can't be separated from the web tier then it is a design oversight.
Shane's right...all it took was the following code...
MockServletContext servletContext = new MockServletContext(); Lifecycle.setServletContext(servletContext); new Initialization(servletContext) .create() .init();
I did NOT test a full blown application. Just verified that i could see the typical Seam init o/p. I had my resources directory on the classpath. The resources directory had a META-INF directory which contained the persistence.xml and jboss-bean.xml. The resources directory had all other config. file (including seam.properties)
Of course you need the jBoss microcontainer, if you need JTA.
Well i might have jumped the gun here. Once you init, you need to do this, so that Seam creates the correct context ...
HttpSession session = new MockHttpSession(servletContext); ServletRequest request = new MockHttpServletRequest(session); Lifecycle.beginRequest(servletContext, session, request);
Also when you exit the app you may want to call the Lifecycle destroy/end methods.
One more note, i added components.xml and Seam related files in the dir. META-INF under the src folder.
Well i finally had sometime to try out an example. Just used bits of the registration example. Changed it to a POJO seam component. Using the above code i ran into 2 problems. The entity manager was not getting injected and once that was resolved the entity manager would fail as there was no active transaction. Here's how i fixed that...
// init seam MockServletContext servletContext = new MockServletContext(); Lifecycle.setServletContext(servletContext); new Initialization(servletContext) .create() .init(); // begin a request HttpSession session = new MockHttpSession(servletContext); ServletRequest request = new MockHttpServletRequest(session); Lifecycle.beginRequest(servletContext, session, request); // ensure the MapContext is setup (else em won't be injected) Lifecycle.beginCall(); // Get seam component Mycomponent myComp = (Mycomponent) Component.getInstance("myComp");
No transactionproblem was solved by not using JTA. Need to make this change in persistence.xml (From JTA to RESOURCE_LOCAL)...
<persistence-unit name="rms" transaction-type="RESOURCE_LOCAL">
Largely used the SeamTest as basis to go with RESOURCE_LOCAL. If anyone knows how to use JTA, please let me know. Thanks.