3 Replies Latest reply on Jul 4, 2013 12:52 AM by rush_pawan

    Getting error message “Only a type can be imported…” when trying to execute jsp page

    anderson.nt Level 1

       

      I get the error: "Only a type can be imported. com.adobe.cq.TestServiceImpl resolves to a package in adobe day cq5

       

      I have created one bundle and installed in felix console. The status of the bundle is also Active. And also it is available in both service and component console.

       

      Here is my TestServiceImpl class in com.adobe.cq bundle


      package com.adobe.cq;

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

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

      import org.osgi.framework.BundleContext;

      import org.osgi.service.component.ComponentContext;

      import javax.jcr.RepositoryException;

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

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

      //src.component

      //declares the class component. This will provide a wrapped ManagedService component in the OSGI container.

      //src.service interface="SampleService"

      //declares the service we are offering. Actually, the interface attribute is superflous, as by default, all implemented Interfaces are used.

      /**

      @scr.component

      @scr.service interface="testService"

      */

      @Component(immediate = true, metatype = true)

      @Service

      @Properties( {

      @Property(name = "service.description", value = "abcd"),

      @Property(name = "label", value = "myLabel") })

      public class TestServiceImpl implements TestService {

      public String sayHello() {

           return "Hello World!";

      }

      }

       

       

      Here is my jsp code

       

       

      <%@include file="/libs/foundation/global.jsp"%><%

      %><%@include file="/apps/mine/includes/functions.jsp"%><%

      %><%@ page import="com.adobe.cq.TestServiceImpl,

      org.apache.sling.api.SlingHttpServletRequest,

                      java.util.List"%><%

      %><%

      SlingHttpServletRequest r = (SlingHttpServletRequest)request;   

                      TestServiceImpl testService = sling.getService(TestServiceImpl.class);

                      out.println("testService:::"+testService);

      %>

      <div id="te-nav" style="display:block !important;">

      </div>

       

       

      Is it because of am using annotation in TestServiceImpl class?

       

      Could you please tell me how to fix it?

       

      Regards,

      Anderson

        • 1. Re: Getting error message “Only a type can be imported…” when trying to execute jsp page
          rush_pawan Level 4

          Hi,

           

          The service call should always be made using the interface name which you have registered. So if you see you have registered the service as

           

          @scr.service interface="testService"    // Please define the correct name as in your case its TestService, so make it @scr.service interface="TestService"

           

          Now you can call your service

           

          SlingHttpServletRequest r = (SlingHttpServletRequest)request;  

                          TestService testService = (TestService) sling.getService(TestService.class);  // instead using implementor.

           

          I hope it helps you. Let me know for more information.

           

          Thanks,

          Pawan


          • 2. Re: Getting error message “Only a type can be imported…” when trying to execute jsp page
            anderson.nt Level 1

            Hi Pawan,

             

            Thanks for your information. It was problem with my pom.xml It started to work after changing my POM.xml

             

            I am not able to get sling service for TestServiceImpl. But I am only able to create TestService which is my interface. I need to get reference for TestServiceImpl class not for TestService interface. Please help me to make it.

             

            i have created TestServiceImpl and TestService class for testing whether my bubdle is crating all the folder structure or not.

             

            But my requirement is like assume class A has some methods like getMenuLink, setMenuLink.. And In class B i am calling some methods in class A and doing some manipulation in createLink method.

             

            Here class B does not have any interface. Not implementing  any interface. But I need to get class B refererence via sling.getService.

             

            I know that if we are using maven SCR plugin..we need to have @component, @service. And All java classes are not services. Will it work if I add activate, deactivate method in class B?

             

            Could you please tell me how to proceed next?

             

            Thanks,

            Anderson

            • 3. Re: Getting error message “Only a type can be imported…” when trying to execute jsp page
              rush_pawan Level 4

              Hi Anderson,

               

              Answers to your both questions

               

              1. The TestService instance will have reference to TestServiceImpl only and you can access all its method through same instance.

               

              2. Not sure why you want to use sling.getService to retrive reference of any class instead of directly creating the instance in your target class (basic java approach - as both classes are your custom so you can design that way)

               

              If you still have such requirement then create B class as service and register it. so like this create B as interface and BImpl as implementor (same as TestServiceImpl) and then

               

              B b=(B)sling.getService(B.class);   // will return you instance reference in your A class

               

              Let me know for more information.

               

              Thanks,

              Pawan