7 Replies Latest reply on Jun 5, 2010 10:20 PM by Aaron Kirley

    Injecting components from Application context

    Aaron Kirley Newbie

      I have been struggling with how to reliably inject components from the application context into my webservice component.  I have tried both @Factory and @Startup/@Create to load/outject the application scoped component but each method returns Null Pointer errors.  Is there a known issue with injecting Appliction Scoped components into a webservice class?


      Any help would be greatly appreciated and i would be glad to provide more details if that would be helpful.


      Best,


      Aaron

        • 1. Re: Injecting components from Application context
          Arbi Sookazian Master

          How are you outjecting the application-scoped component?  show your code in both classes...

          • 2. Re: Injecting components from Application context
            Aaron Kirley Newbie

            Here is my code for outjecting with the @Startup/@Create, this correctly creates the component but for some reason my webservice get's an null pointer exception when it tries to inject with @In:


            @Startup
            @Name("resources")
            @Scope(ScopeType.APPLICATION)
            public class Resources {
                 @Logger Log log;
                 
                 @Out(scope=ScopeType.APPLICATION)
                 private HashMap<String,String> environmentProps;
            
                 @Create
                 public void load(){
                      try{
                           Properties properties = new Properties();
                           FileInputStream fis = (FileInputStream)ResourceLoader.instance().getResourceAsStream("/WEB-INF/classes/environment.properties"); 
                           properties.load(fis);
                           environmentProps = new HashMap<String, String>((Map) properties); 
                      } catch (Exception e){
                           log.warn("Error loading environment resources",e);
                      }
                 }
            }
            



            and this is the code for using the @Factory which doesn't seem to populate the component at all:


            @Name("resources")
            @Scope(ScopeType.APPLICATION)
            public class Resources {
                 @Logger Log log;
            
                 @Factory("environmentProps")
                 public HashMap<String,String> getEnvironmentProps(){
                      Map<String,String> ep = new HashMap<String, String>(); 
                      try{
                           Properties properties = new Properties();
                           FileInputStream fis = (FileInputStream)ResourceLoader.instance().getResourceAsStream("/WEB-INF/classes/environment.properties"); 
                           properties.load(fis);
                           ep = new HashMap<String, String>((Map) properties); 
                      } catch (Exception e){
                           log.warn("Error loading environment resources",e);
                      }
                      return (HashMap<String, String>) ep;
                 }
            }
            




            in my webservice i'm simply injecting like so:


            @In(scope=ScopeType.APPLICATION)
            HashMap<String,String> environmentProps;
            



            • 3. Re: Injecting components from Application context
              Tim Evers Master

              Can you post you full stack trace and possibly a bit more of your webservice if the exception is from within that class please?

              • 4. Re: Injecting components from Application context
                Aaron Kirley Newbie
                Sorry, here is the stack trace and some code as requested. i really appreciate your help with this.

                Sorry, here is the data requested.  The webservice is actually a webservice "client" which relies on JBossWS stubs generated by wsconsume. 
                `
                @Name("shipClient")
                @Scope(ScopeType.CONVERSATION)
                public class ShipClient implements java.io.Serializable {
                @In(scope=ScopeType.APPLICATION)
                HashMap<String,String> environmentProps;

                public Shipment ship(Shipment shipment){
                // if i try to access environmentProps component here i get an error
                // even though the debug.seam page show the component exists in the application context and contains data
                }}`

                Here is the stack trace
                `
                00:32:06,818 ERROR [SOAPFaultHelperJAXWS] SOAP request exception
                java.lang.NullPointerException
                        at com.luggageforward.client.ShipClient.ship(ShipClient.java:116)
                        at com.luggageforward.webservice.Shipping.createShipment(Shipping.java:20)
                        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
                        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
                        at java.lang.reflect.Method.invoke(Method.java:592)
                        at org.jboss.wsf.container.jboss50.invocation.InvocationHandlerJSE.invoke(InvocationHandlerJSE.java:108)
                        at org.jboss.ws.core.server.ServiceEndpointInvoker.invoke(ServiceEndpointInvoker.java:222)
                        at org.jboss.wsf.stack.jbws.RequestHandlerImpl.processRequest(RequestHandlerImpl.java:474)
                        at org.jboss.wsf.stack.jbws.RequestHandlerImpl.handleRequest(RequestHandlerImpl.java:295)
                        at org.jboss.wsf.stack.jbws.RequestHandlerImpl.doPost(RequestHandlerImpl.java:205)
                        at org.jboss.wsf.stack.jbws.RequestHandlerImpl.handleHttpRequest(RequestHandlerImpl.java:131)
                        at org.jboss.wsf.common.servlet.AbstractEndpointServlet.service(AbstractEndpointServlet.java:85)
                        at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
                        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
                        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
                        at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:83)
                        at org.jboss.seam.web.IdentityFilter.doFilter(IdentityFilter.java:40)
                        at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
                        at org.jboss.seam.web.MultipartFilter.doFilter(MultipartFilter.java:90)
                        at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
                        at org.jboss.seam.web.ExceptionFilter.doFilter(ExceptionFilter.java:64)
                        at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
                        at org.jboss.seam.web.RedirectFilter.doFilter(RedirectFilter.java:45)
                        at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
                        at org.ajax4jsf.webapp.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:178)
                        at org.ajax4jsf.webapp.BaseFilter.handleRequest(BaseFilter.java:290)
                        at org.ajax4jsf.webapp.BaseFilter.processUploadsAndHandleRequest(BaseFilter.java:368)
                        at org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:495)
                        at org.jboss.seam.web.Ajax4jsfFilter.doFilter(Ajax4jsfFilter.java:56)
                        at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
                        at org.jboss.seam.web.LoggingFilter.doFilter(LoggingFilter.java:60)
                        at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
                        at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:73)
                        at org.jboss.seam.servlet.SeamFilter.doFilter(SeamFilter.java:158)
                        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
                        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
                        at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
                        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
                        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
                        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:235)
                        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
                        at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:190)
                        at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:433)
                        at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:92)
                        at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.process(SecurityContextEstablishmentValve.java:126)
                        at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:70)
                        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
                        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
                        at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:158)
                        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
                        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:330)
                        at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:829)
                        at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:598)
                        at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
                        at java.lang.Thread.run(Thread.java:595)
                00:32:06,866 ERROR [CommonClient] Exception caught while (preparing for) performing the invocation:
                javax.xml.ws.soap.SOAPFaultException: java.lang.NullPointerException
                        at org.jboss.ws.core.jaxws.SOAPFaultHelperJAXWS.getSOAPFaultException(SOAPFaultHelperJAXWS.java:84)
                        at org.jboss.ws.core.jaxws.binding.SOAP11BindingJAXWS.throwFaultException(SOAP11BindingJAXWS.java:107)
                        at org.jboss.ws.core.CommonSOAPBinding.unbindResponseMessage(CommonSOAPBinding.java:579)
                        at org.jboss.ws.core.CommonClient.invoke(CommonClient.java:381)
                        at org.jboss.ws.core.jaxws.client.ClientImpl.invoke(ClientImpl.java:290)
                        at org.jboss.ws.core.jaxws.client.ClientProxy.invoke(ClientProxy.java:170)
                        at org.jboss.ws.core.jaxws.client.ClientProxy.invoke(ClientProxy.java:150)
                        at $Proxy345.createShipment(Unknown Source)
                        at com.luggageforward.client.ShipClientTest.shipTest(ShipClientTest.java:133)
                        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
                        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
                        at java.lang.reflect.Method.invoke(Method.java:592)
                        at org.jboss.seam.util.Reflections.invoke(Reflections.java:22)
                        at org.jboss.seam.intercept.RootInvocationContext.proceed(RootInvocationContext.java:32)
                        at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:56)
                        at org.jboss.seam.transaction.RollbackInterceptor.aroundInvoke(RollbackInterceptor.java:28)
                        at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
                        at org.jboss.seam.core.BijectionInterceptor.aroundInvoke(BijectionInterceptor.java:77)
                        at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
                        at org.jboss.seam.core.MethodContextInterceptor.aroundInvoke(MethodContextInterceptor.java:44)
                        at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
                        at org.jboss.seam.intercept.RootInterceptor.invoke(RootInterceptor.java:107)
                        at org.jboss.seam.intercept.JavaBeanInterceptor.interceptInvocation(JavaBeanInterceptor.java:185)
                        at org.jboss.seam.intercept.JavaBeanInterceptor.invoke(JavaBeanInterceptor.java:103)
                        at com.luggageforward.client.ShipClientTest_$$_javassist_seam_3.shipTest(ShipClientTest_$$_javassist_seam_3.java)
                        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
                        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
                        at java.lang.reflect.Method.invoke(Method.java:592)
                        at org.jboss.el.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:335)
                        at org.jboss.el.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:348)
                        at org.jboss.el.parser.AstPropertySuffix.invoke(AstPropertySuffix.java:58)
                        at org.jboss.el.parser.AstValue.invoke(AstValue.java:96)
                        at org.jboss.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:276)
                        at org.jboss.seam.core.Expressions$2.invoke(Expressions.java:175)
                        at org.jboss.seam.navigation.Pages.callAction(Pages.java:692)
                        at org.jboss.seam.navigation.Pages.preRender(Pages.java:330)
                        at org.jboss.seam.jsf.SeamPhaseListener.preRenderPage(SeamPhaseListener.java:561)
                        at org.jboss.seam.jsf.SeamPhaseListener.beforeRenderResponse(SeamPhaseListener.java:472)
                        at org.jboss.seam.jsf.SeamPhaseListener.beforeServletPhase(SeamPhaseListener.java:148)
                        at org.jboss.seam.jsf.SeamPhaseListener.beforePhase(SeamPhaseListener.java:118)
                        at com.sun.faces.lifecycle.Phase.handleBeforePhase(Phase.java:214)
                        at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:96)
                        at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
                        at javax.faces.webapp.FacesServlet.service(FacesServlet.java:266)
                        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
                        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
                        at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:83)
                        at org.jboss.seam.web.IdentityFilter.doFilter(IdentityFilter.java:40)
                        at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
                        at org.jboss.seam.web.MultipartFilter.doFilter(MultipartFilter.java:90)
                        at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
                        at org.jboss.seam.web.ExceptionFilter.doFilter(ExceptionFilter.java:64)
                        at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
                        at org.jboss.seam.web.RedirectFilter.doFilter(RedirectFilter.java:45)
                        at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
                        at org.ajax4jsf.webapp.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:178)
                        at org.ajax4jsf.webapp.BaseFilter.handleRequest(BaseFilter.java:290)
                        at org.ajax4jsf.webapp.BaseFilter.processUploadsAndHandleRequest(BaseFilter.java:368)
                        at org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:495)
                        at org.jboss.seam.web.Ajax4jsfFilter.doFilter(Ajax4jsfFilter.java:56)
                        at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
                        at org.jboss.seam.web.LoggingFilter.doFilter(LoggingFilter.java:60)
                        at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
                        at org.jboss.seam.web.HotDeployFilter.doFilter(HotDeployFilter.java:53)
                        at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
                        at org.jboss.seam.servlet.SeamFilter.doFilter(SeamFilter.java:158)
                        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
                        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
                        at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
                        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
                        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
                        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:235)
                        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
                        at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:190)
                        at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:433)
                        at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:92)
                        at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.process(SecurityContextEstablishmentValve.java:126)
                        at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:70)
                        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
                        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
                        at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:158)
                        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
                        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:330)
                        at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:829)
                        at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:598)
                        at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
                        at java.lang.Thread.run(Thread.java:595)`
                • 5. Re: Injecting components from Application context
                  Aaron Kirley Newbie

                  Was the stack trace helpful?  Any help here would be much appreciated... even examples of how people have successfully done similar things would be helpful.  I am trying to load environment specific properties which change between development, test/QA, and production and i thought loading java.util.Properties from a .properties file and saving it into an Application scoped component would be a elegant way to do this but so far is has only caused issues for me.

                  • 6. Re: Injecting components from Application context
                    Tim Evers Master

                    I could be really off track here but could you try something and let me know the results.


                    Instead of using @In to get your Seam component use


                    public Shipment ship(Shipment shipment){
                        HashMap<String,String> environmentProps = Component.getInstance("environmentProps", ScopeType.APPLICATION);
                        if (null == environmentProps) {
                            throw new IllegalStateException("The stupid Seam var is null!");
                        }
                    }
                    



                    For some reason I remember when I was playing around with webservices that sometimes I was trying to do things before the Seam lifecycle had got to the point where Bijection was occuring. Like I said I could be way off but, at least we can rule this out if that's the case.

                    • 7. Re: Injecting components from Application context
                      Aaron Kirley Newbie
                      Tim,

                      I tried what you suggested and this is the error i got.

                      22:15:46,566 ERROR [SOAPFaultHelperJAXWS] SOAP request exception
                      java.lang.IllegalStateException: No application context active

                      Any thoughts?