7 Replies Latest reply on May 13, 2008 10:30 AM by s soza

    Can you yse Seam for non-web apps

    s soza Newbie

      Hi everyone


      We need to create a standalone non-web based Java app. However, we want to leverage some code that exists in a web based Seam app. Is this possible? I know this is done in SeamTest and one could use the jBoss micro-container. However I'm not sure how we bootstrap the container and seam itself in a standalone java app.


      Thanks.

        • 1. Re: Can you yse Seam for non-web apps
          Shane Bryzak Master

          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.

          • 2. Re: Can you yse Seam for non-web apps
            s soza Newbie

            Shane


            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.


            soza.

            • 3. Re: Can you yse Seam for non-web apps
              kelly goedert Apprentice

              Hi,


              I have the same problem... is there an example on doing this?
              I have a seam application that now need a console interface.


              Thanks


              Kelly

              • 4. Re: Can you yse Seam for non-web apps
                Tom Nichols Newbie

                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.

                • 5. Re: Can you yse Seam for non-web apps
                  s soza Newbie

                  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.

                  • 6. Re: Can you yse Seam for non-web apps
                    s soza Newbie

                    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.

                    • 7. Re: Can you yse Seam for non-web apps
                      s soza Newbie

                      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");
                      
                      



                      The No transaction problem 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.