10 Replies Latest reply on Mar 18, 2013 6:48 AM by patK

    Can not access bundle context from JSP in CQ5.5

    patK Level 1

      I created an OGI bundle , named :

       

       

      com.sdl.ws.integration.profserv.webservice.connector.server.external.beans

      I can see the bundle under :

       

       

      http://localhost:4502/system/console/bundles

      Following is my JSP code , using which I am trying to access a bundlecontext

       

       

      <%@ page import="org.osgi.framework.BundleContext"%>

      <%@ page import="org.osgi.framework.FrameworkUtil"%>

      <%@ page import="com.sample.osgi.components.FormattingServiceImpl.*"%>

      <%@ page import="org.osgi.service.cm.ConfigurationAdmin"%>

      <%@ page import="org.osgi.service.cm.Configuration"%>

      <%@ page import="org.osgi.service.packageadmin.PackageAdmin"%>

      <% BundleContext bundleContext =      FrameworkUtil.getBundle(FormattingServiceImpl).getBundleContext();  %>

       

      I am getting the following error : FormattingServiceImpl cannot be resolved , Class name "FormattingServiceImpl" is missing

       

      I tried to use multiple ways to import the jar but nothing is working ,

      Here is a structure how my bundle is configured :

      snap.png

       

       

      I am stuck on the point how to access the bundle context from JSP , does any one has any suggestions / corrections ?

        • 1. Re: Can not access bundle context from JSP in CQ5.5
          rush_pawan Level 4

          Hello,

           

          1. There could be the reason that your bundle is active but the service is not active. Can you check the status of your service in felix console at http://localhost:4502/system/console/components  and related configuration if any required.

           

          If you service is not active then try to reactivate it from there only and check error.log. If you do not create your service correctly as per sling service notations then it does not deploy correctly and create issue.

           

          reference:

          http://blogs.adobe.com/kmossman/2012/04/osgi-sling-service-example.html

          http://sling.apache.org/site/scheduler-service-commons-scheduler.html

           

          2. If your serice also looks good and active state then try to access the service in your JSP just for testing why executing one of the method. For example

               TestService service=sling.getService(TestService.class);

               service.somemethode();

           

          Let me know for more information.

           

          Thanks,

          Pawan

          • 2. Re: Can not access bundle context from JSP in CQ5.5
            patK Level 1

            Thanks for your reply.

             

              My bundle and service are running fine.

             

              I am getting the error on line , TestService.class saying tha TstService Can not be resolved to a type  (In my case  FormattingServiceImpl or FormattingService can not be resolved).

             

            Is it becuase there are no class files generated for these classes so I am not able to see them and not able to recognize ?

             

            How do I check if class files are generated or not ?

            • 3. Re: Can not access bundle context from JSP in CQ5.5
              rush_pawan Level 4

              1. I think there is a problem in your code. you are trying to access the service using the implementor class which is "FormattingServiceImpl" instead of actual service interface i hope "FormattingService" because service always exposed through its interface in this case "FormattingService"

               

              and same i mentioned in my last example. I hope that will work.

               

              FormattingService service=sling.getService(FormattingService.class);

                   service.somemethode();

               

               

              Else

               

              2. As you mentioned that service is also running believe so but looking at error what you are getting indicating that service is not active.

               

              Now if you are saying service is running then there is no question about class is not deployed because without class the service will not so active.

               

              For your confirmation you can open the bundle from http://localhost:4502/system/console/bundles and you can see your service package exported under the "exported package" section and also you can see your service registered with "Service ID" number if it is deployed and active correctly.

               

              Can you try this too.   If your service is active the just deactivate it and again activate it and see where it is still running because if service is running and you can not access the service is odd situation. Also look at error.log

               

               

              Thanks,

              Pawan

              1 person found this helpful
              • 4. Re: Can not access bundle context from JSP in CQ5.5
                patK Level 1

                I tried the option that you mentioned , and you are right my earlie code snippet was confusing and wrong.  I found my service under Components was not registered so now I can see the service as registered.

                 

                Here is how it looks now :

                 

                sam.png

                Currnt JSP code I have is :

                 

                <%@ include file="/apps/thermoScientific/components/shared/global.jsp" %>

                 

                 

                <%@ page import="java.net.URL"%>

                <%@ page import="org.osgi.framework.BundleContext"%> 

                <%@ page import="org.osgi.framework.BundleReference"%> 

                <%@ page import="org.osgi.framework.ServiceReference"%> 

                <%@ page import="org.osgi.service.packageadmin.PackageAdmin"%> 

                <%@ page import="org.osgi.framework.FrameworkUtil"%>

                <%@ page import="com.sample.osgi.bundle.*"%>

                <%@ page import="org.osgi.service.cm.ConfigurationAdmin"%>

                <%@ page import="com.sample.osgi.components.FormattingServiceImpl"%>

                <%@ page import="com.sample.osgi.components.FormattingService"%>

                <%@ page import="org.osgi.service.cm.Configuration"%>  

                <%

                              FormattingService service=sling.getService(FormattingService.class);

                %> 

                      

                 

                 

                When I compile the JSP file , I get the following errors  :

                 

                An error occurred at line: 25 in the generated java file

                Only a type can be imported. com.sample.osgi.components.FormattingServiceImpl resolves to a package

                 

                An error occurred at line: 26 in the generated java file

                Only a type can be imported. com.sample.osgi.components.FormattingService resolves to a package

                 

                An error occurred at line: 20 in the jsp file: /apps/sdl/components/content/sdl/sdl.jsp

                FormattingService cannot be resolved to a type

                17: 

                18:       <%

                19:      

                20:         FormattingService service=sling.getService(FormattingService.class);

                21:         %> 

                22:       

                23:       

                 

                 

                An error occurred at line: 20 in the jsp file: /apps/sdl/components/content/sdl/sdl.jsp

                FormattingService cannot be resolved to a type

                17: 

                18:       <%

                19:      

                20:         FormattingService service=sling.getService(FormattingService.class);

                21:         %> 

                22:       

                23:

                 

                 

                The error makes me think that I am not either accessing the classes in right fashion.

                • 5. Re: Can not access bundle context from JSP in CQ5.5
                  Yogesh Upadhyay Level 4

                  Can you check if your service is exposed through more than one bundles ? Also what is CQ and java version ? There are a lot of in necessary import in your code, however I don't think that is problem.

                   

                  Yogesh

                  • 6. Re: Can not access bundle context from JSP in CQ5.5
                    patK Level 1

                    Yes , there are  many imports , this is just sample test code I am trying things so did not reomved tose imports. I am using CQ 5.5  and JDK 1.6.

                     

                    This service is not exposed using other bundles.

                    • 7. Re: Can not access bundle context from JSP in CQ5.5
                      Yogesh Upadhyay Level 4

                      Can you remove import of implementation and try ?

                       

                      Sent from my iPad

                      • 8. Re: Can not access bundle context from JSP in CQ5.5
                        patK Level 1

                        I tried that as well , I am still getting same errors. To give more references , I am trying to replicate the code @ http://suryakand-shinde.blogspot.com/search?go=&q=OSGI    to my CQ

                        • 9. Re: Can not access bundle context from JSP in CQ5.5
                          tyge68 Level 1

                          Did you check that the bundle export the class you mention ? as if the classes are not visible to the JSP Scripting support bundle then it cannot compile your code and so it is expected that it does a class not found.

                           

                          Usually you define that in your .bnd file or pom.xml depending on how you develop.

                          1 person found this helpful
                          • 10. Re: Can not access bundle context from JSP in CQ5.5
                            patK Level 1

                            You were right , it was a very stupid mistake from my end , this is how my *.bnd file was ; Thanks All for you help

                             

                            #Export-Package: com.sample.osgi.components      ; This section was commented

                            # Import-Package: 

                            Private-Package:

                            # Include-Resource:

                            Bundle-Name: OSGi Sample

                            Bundle-Description:

                            Bundle-SymbolicName: com.sample.osgi.bundle

                            Bundle-Version: 1.0.0-SNAPSHOT

                            Bundle-Activator: com.sample.osgi.components.Activator