13 Replies Latest reply on Apr 25, 2013 10:18 PM by deepbluez

    @Reference annotation causing customised servlet stop getting called

    deepbluez

      Hi guys,

       

      I noticed that as soon as I add the @Reference annotation in my servlet, the sevlet will not be called when the form is submitted. As soon as I remove @Reference the servlet will be called. But the email will not function because @Reference is required to make the email to work.

       

      I have the servlet registered and briefly it looks like below:

       

      @Service(value = Servlet.class)

      @SlingServlet(

                          resourceTypes = {"myapp/components/content/faqNewQuestions"},

                          methods = {"POST"},

                          selectors = {"email"},

                          extensions = {"html"}

                          )

       

      @SuppressWarnings("serial")

      public class FAQNewQuestionServlet extends SlingAllMethodsServlet {

       

      @Reference

                private MessageGatewayService messageGatewayService;

       

      @Override

          protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException

          {

                    response.setContentType("text/plain");

                    response.getOutputStream().print("FAQNewQuestionServlet is called");

          }

      }

       

      MessageGatewayService is imported through:

      import com.day.cq.mailer.MessageGatewayService;

       

      This package is added to the .bnd file under:

      Import-Package: ..., com.day.cq.mailer, org.apache.felix.scr,org.apache.felix.scr.annotations,...

       

      the faqNewQuestions.jsp (which lives under apps/myapp/components/content/faqNewQuestions) has a form looks like this:

       

      <form method="POST" action="<c:out value="${resource.path}" />.email.html">

                <input type="text" id="email" name="email"/>

                <textarea name="question" id="question"></textarea>

                <input class="button" id="questionSubmit" type="submit" value="submit" />

      </form>

       

      I just keep getting 'Content modified' page rather seeing the "FAQNewQuestionServlet is called" I was expecting.

       

      Content modified /content/my-trick/jcr:content/par/faqnewquestions

      Status

      200

      Message

      OK

      Location/my-trick/_jcr_content/par/faqnewquestions
      Parent Location/my-trick/_jcr_content/par

       

      Really appreciate any inputs..

        • 1. Re: @Reference annotation causing customised servlet stop getting called
          dcollie Adobe Employee

          Hi deepbluez,

           

          With the @Reference added, what is the status of the bundle containing your Servlet in the Felix console?

           

          Thanks -- David

          • 2. Re: @Reference annotation causing customised servlet stop getting called
            deepbluez Level 1

            Thanks David for your reply.

             

            The status is 'active'. But I'm not sure if I have answered your question?

            • 3. Re: @Reference annotation causing customised servlet stop getting called
              deepbluez Level 1

              Under the 'Component' tab in Felix console, I found my servlet but its status is 'registered', not 'active' as the other components.

               

              Also the @Reference annotated two objects have 'No service bound'.

               

              Reference mailingservice["Satisfied","Service Name: com.day.cq.mailer.MailingService","Multiple: single","Optional: mandatory","Policy: dynamic","No Services bound"]
              Reference messageGatewayService["Satisfied","Service Name: com.day.cq.mailer.MessageGatewayService","Multiple: single","Optional: mandatory","Policy: static","No Services bound"]

               

              The code in my servlet is like this:

               

              @Reference(policy=ReferencePolicy.DYNAMIC)

                        private MailingService mailingservice;

                        @Reference(policy=ReferencePolicy.STATIC)

                        private MessageGatewayService messageGatewayService;

               

              I was trying out 'dynamic' and 'static' just to see which one will trigger the binding. Any advice on the binding of service?

               

              Thanks!!!

              • 4. Re: @Reference annotation causing customised servlet stop getting called
                Sham HC Level 7

                Hi deepbluez,

                 

                Instead of MessageGatewayService can you use mailService [1] ?

                 

                [1]

                 

                @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY, policy = ReferencePolicy.STATIC)

                private MailService mailService;

                 

                Thanks,

                Sham

                • 5. Re: @Reference annotation causing customised servlet stop getting called
                  deepbluez Level 1

                  Thank Sham,

                   

                  I have tried MailService and copy-pasted your code in literally, built the bundle and no error found, but still getting:

                   

                  Reference mailService["Satisfied","Service Name: com.day.cq.mailer.MailService","Multiple: single","Optional: mandatory","Policy: static","No Services bound"]

                   

                   

                   

                   

                   

                  • 6. Re: @Reference annotation causing customised servlet stop getting called
                    Sham HC Level 7

                    Hi deepbluez,

                     

                    Make sure the bundle [1] is active. I could not reproduce the issue with [2]

                     

                    [1]  http://<host>:<port>/system/console/bundles/com.day.cq.cq-mailer

                     

                    [2]

                    package com.test;

                     

                    import org.apache.sling.api.SlingHttpServletRequest;

                    import org.apache.sling.api.SlingHttpServletResponse;

                    import org.apache.sling.api.servlets.SlingAllMethodsServlet;

                    import javax.servlet.ServletException;

                    import org.apache.felix.scr.annotations.sling.SlingServlet;

                    import java.io.IOException;

                    import org.apache.felix.scr.annotations.Reference;

                    import org.apache.felix.scr.annotations.Service;

                    import javax.servlet.Servlet;

                    import com.day.cq.mailer.MessageGatewayService;

                    import org.apache.felix.scr.annotations.ReferenceCardinality;

                    import org.apache.felix.scr.annotations.ReferencePolicy;

                    import org.apache.felix.scr.annotations.Component;

                    import org.apache.felix.scr.annotations.Property;

                     

                    @Property(name = "service.description", value = "Page Email Servlet")

                    @Component(metatype = false)

                    @Service

                    @SlingServlet(

                                        resourceTypes = {"myapp/components/content/faqNewQuestions"},

                                        methods = {"POST"},

                                        selectors = {"email"},

                                        extensions = {"html"},

                        generateComponent = false

                                        )

                                       

                    @SuppressWarnings("serial")

                    public class FAQNewQuestionServlet extends SlingAllMethodsServlet {

                     

                     

                     

                    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY, policy = ReferencePolicy.STATIC)

                    protected MessageGatewayService mailService;

                     

                     

                    @Override

                        protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException

                     

                        {

                     

                                  response.setContentType("text/plain");

                                  response.getOutputStream().print("FAQNewQuestionServlet is called");

                        }

                    }

                     

                    Thanks,

                    Sham

                    Twitter @adobe_sham

                    • 7. Re: @Reference annotation causing customised servlet stop getting called
                      deepbluez Level 1

                      Thanks Sham,

                       

                      That's every helpful. I did exactly that but still no luck.

                       

                      Any chance you could help me to check the bnd file? just to see if I have missed anything?

                       

                      -----------------------------------------------

                      Export-Package: *

                      Import-Package: *

                      Private-Package: *

                      # Include-Resource:

                      Bundle-Name: faqnewq Bundle

                      Bundle-Description: This is a bundle for faqnewq classes

                      Bundle-SymbolicName: faqnewq.bundle

                      Bundle-Version: 1.0.2-SNAPSHOT

                      Bundle-Activator: com.myapp.faqnewq.Activator

                      Import-Package: com.day.commons.datasource.poolservice,org.osgi.framework,org.slf4j,com.day.cq.mailer,org .apache.felix.scr,org.apache.felix.scr.annotations,org.apache.felix.scr.annotations.sling, org.apache.sling.api,org.apache.sling.api.servlets,com.ibm.zos.batch.container.support;res olution:=optional,com.ibm.zos.batch.container.support.transaction;resolution:=optional,COM .ibm.db2os390.*;resolution:=optional,com.ibm.websphere.*;resolution:=optional,javax.*;reso lution:=optional,org.w3c.*;resolution:=optional,org.xml.*;resolution:=optional,sqlj.*;reso lution:=optional,org.ietf.jgss;resolution:=optional,sun.*;resolution:=optional

                      ------------------------------------------------

                       

                      Also I have a feeling that might have missed some configuration settings in order for the annotation to work?

                      • 8. Re: @Reference annotation causing customised servlet stop getting called
                        antonyh

                        Have a look at the component com.day.cq.mailer.impl.CqMailingService - by default this is

                         

                        com.day.cq.mailer.impl.CqMailingService

                        Bundlecom.day.cq.cq-mailer (171)
                        Implementation Classcom.day.cq.mailer.impl.CqMailingService
                        Default Stateenabled
                        Activationdelayed
                        Configuration Policyoptional
                        Service Typeservice
                        Servicescom.day.cq.mailer.MailingService
                        com.day.cq.mailer.MessageGatewayService
                        Reference mailingGateway["Satisfied","Service Name: com.day.cq.mailer.MessageGateway","Multiple: multiple","Optional: optional","Policy: dynamic","No Services bound"]
                        Reference repository["Satisfied","Service Name: org.apache.sling.jcr.api.SlingRepository","Multiple: single","Optional: mandatory","Policy: static","Bound Service ID 77 (Adobe CRX Repository)"]
                        Propertiescomponent.id = 282
                        component.name = com.day.cq.mailer.impl.CqMailingService
                        mailer.status.rootPath = /var/mailer/status
                        service.description = Day CQ Mailer Service
                        service.pid = com.day.cq.mailer.impl.CqMailingService
                        service.vendor = Day Management AG

                         

                        There seems to be no MessageGateway services provided with CQ5.5 sp2.

                        • 9. Re: @Reference annotation causing customised servlet stop getting called
                          justin_at_adobe Adobe Employee

                          The component which provides the MessageGateway service (com.day.cq.mailer.impl.DefaultMailService) needs to be properly configured for the service to be reigstered. So yes, you are correct that by default there is no MessageGateway service as you need to configure your SMTP host/port/username/password.

                          • 10. Re: @Reference annotation causing customised servlet stop getting called
                            antonyh Level 1

                            Well of course, thanks! This probably also applies to this thread too: http://forums.adobe.com/thread/1197140?tstart=0

                            • 11. Re: @Reference annotation causing customised servlet stop getting called
                              deepbluez Level 1

                              Thanks all for your replies. Really appreciate your help.

                               

                              DefaultMailService was setup and everything with proper SMTP host/port/username/password configured long time ago. So I doubt the problem is caused by this.

                               

                              Further looked into the logs I found this: (I use CRXDE lite to develope and build the bundle, not using any external tools just to rule out any possible factors)

                               

                              myapp.bundle [com.myapp.servlets.FAQNewQuestionServlet] Error during instantiation of the implementation object (java.lang.VerifyError: Expecting a stackmap frame at branch target 13 in method com.myapp.servlets.FAQNewQuestionServlet.unbindMailService(Lcom/day/cq/mailer/MailService ;)V at offset 5) java.lang.VerifyError: Expecting a stackmap frame at branch target 13 in method com.myapp.servlets.FAQNewQuestionServlet.unbindMailService(Lcom/day/cq/mailer/MailService ;)V at offset 5

                              at java.lang.Class.getDeclaredConstructors0(Native Method)

                              at java.lang.Class.privateGetDeclaredConstructors(Unknown Source)

                              at java.lang.Class.getConstructor0(Unknown Source)

                              at java.lang.Class.newInstance0(Unknown Source)

                              at java.lang.Class.newInstance(Unknown Source)

                              at org.apache.felix.scr.impl.manager.ImmediateComponentManager.createImplementationObject(Im mediateComponentManager.java:183)

                              at org.apache.felix.scr.impl.manager.ImmediateComponentManager.createComponent(ImmediateComp onentManager.java:118)

                              at org.apache.felix.scr.impl.manager.AbstractComponentManager$Unsatisfied.activate(AbstractC omponentManager.java:1140)

                              at org.apache.felix.scr.impl.manager.AbstractComponentManager.activateInternal(AbstractCompo nentManager.java:334)

                              at org.apache.felix.scr.impl.manager.AbstractComponentManager.enable(AbstractComponentManage r.java:158)

                              at org.apache.felix.scr.impl.config.ImmediateComponentHolder.enableComponents(ImmediateCompo nentHolder.java:313)

                              at org.apache.felix.scr.impl.BundleComponentActivator.loadDescriptor(BundleComponentActivato r.java:241)

                              at org.apache.felix.scr.impl.BundleComponentActivator.initialize(BundleComponentActivator.ja va:147)

                              at org.apache.felix.scr.impl.BundleComponentActivator.<init>(BundleComponentActivator.java:1 11)

                              at org.apache.felix.scr.impl.Activator.loadComponents(Activator.java:274)

                              at org.apache.felix.scr.impl.Activator.bundleChanged(Activator.java:192)

                              at org.apache.felix.framework.util.EventDispatcher.invokeBundleListenerCallback(EventDispatc her.java:807)

                              at org.apache.felix.framework.util.EventDispatcher.fireEventImmediately(EventDispatcher.java :729)

                              at org.apache.felix.framework.util.EventDispatcher.fireBundleEvent(EventDispatcher.java:610)

                              at org.apache.felix.framework.Felix.fireBundleEvent(Felix.java:3754)

                              at org.apache.felix.framework.Felix.startBundle(Felix.java:1780)

                              at org.apache.felix.framework.BundleImpl.start(BundleImpl.java:922)

                              at org.apache.felix.framework.BundleImpl.start(BundleImpl.java:909)

                              at org.apache.sling.installer.core.impl.tasks.BundleStartTask.execute(BundleStartTask.java:9 7)

                              at org.apache.sling.installer.core.impl.OsgiInstallerImpl.executeTasks(OsgiInstallerImpl.jav a:621)

                              at org.apache.sling.installer.core.impl.OsgiInstallerImpl.run(OsgiInstallerImpl.java:196)

                              23.04.2013 14:43:32.428 *ERROR* [OsgiInstallerImpl] myapp.bundle [com.myapp.servlets.FAQNewQuestionServlet] Component instance could not be created, activation failed

                              23.04.2013 14:43:32.428 *INFO* [OsgiInstallerImpl] org.apache.sling.installer.core.impl.tasks.BundleStartTask Bundle started (bundle ID=1276) : myapp.bundle

                              23.04.2013 14:43:32.428 *INFO* [FelixDispatchQueue] myapp.bundle BundleEvent STARTED

                              • 12. Re: @Reference annotation causing customised servlet stop getting called
                                antonyh Level 1

                                According to http://stackoverflow.com/questions/100107/reasons-of-getting-a-java-lang-verifyerror

                                 

                                java.lang.VerifyError can be the result when you have compiled against a different library than you are using at runtime.

                                Of course, this is very strange if you are using the built-in compiler.  Do you have an old version of the bundle jar installed (maybe manually remove it?). Are you bundling other jar files inside your jar? It's on the 'unbind' method, have you overridden this?

                                • 13. Re: @Reference annotation causing customised servlet stop getting called
                                  deepbluez Level 1

                                  Thanks Antonyh.

                                   

                                  I did not override the unbind method. Just try to get a simplest mailservice working in my servlet so I can start working on the details.

                                   

                                  Yes it is very strange to get a VerfityError while using the built-in compiler. I even removed the whole bundle (manually), removed the code, recreated the bundle in (CRXDE Lite) this time, just to rule out every possible external causes, and then deployed, still 'not-statisifed'!

                                   

                                  The Day CQ Mail service configuration is verified through telnet and al smtp server responding fine.

                                   

                                  Really don't know what else to do to make this service bound? Thanks for all your help!