Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.
SOLVED

problem instantiating osgi bundle sightly helper java class

Avatar

Level 3

Earlier I was able to instantiate sightly helper java class(extends wcmuse) of osgi bundle in SlingAllMethodsServlet context and even able to access all the getter methods in that class.But in our project we started working with spring mvc and now I am unable to instantiate the sightly helper class.I have even mentioned all the package info in pom.xml(import and export packages).

Error Message:

org.apache.sling.scripting.sightly.SightlyException: Identifier com.test.sightly.TestSightly cannot be correctly instantiated by the Use API
1 Accepted Solution

Avatar

Correct answer by
Level 3

Issue resolved.I imported custom packages in pom.xml but forgot to import java use api related packages.Now I am able to instantiate the sightly helper class(OSGI bundle based) after importing com.adobe.cq.sightly package.

View solution in original post

12 Replies

Avatar

Level 8

swathivall wrote...

Earlier I was able to instantiate sightly helper java class(extends wcmuse) of osgi bundle in SlingAllMethodsServlet context and even able to access all the getter methods in that class.But in our project we started working with spring mvc and now I am unable to instantiate the sightly helper class.I have even mentioned all the package info in pom.xml(import and export packages).

Error Message:

org.apache.sling.scripting.sightly.SightlyException: Identifier com.test.sightly.TestSightly cannot be correctly instantiated by the Use API

 

 

Can you please provide your code and also a screenshot of the bundle definition in the OSGI console?

Avatar

Level 3

Below is the sample class which I am testing,

import com.adobe.cq.sightly.WCMUse;

public class TestSightly extends WCMUse {
   
    private String test;
    @Override
    public void activate() throws Exception {
        test="hello test from test class";
    }
    
    public String getTest(){
        return test;
    }

}

 

And I am new to AEM, are you talking about felix console osgi bundle definition?? 

Avatar

Level 3

ALso please find the below sightly code to instantiate osgi bundle level java class:

<div data-sly-include="/libs/wcm/core/components/init/init.jsp"></div>
<div data-sly-use.res ="com.abg.dro.sling.service.impl.TestSightly">

<div>${res.test}</div>

</div>

Avatar

Level 10

I have not heard about using Spring MVC in an OSGi used by a Java class used by Sightly. I am wondering if that is playing a role in it not working. 

Here is a community article that shows how to get this to work: 

https://helpx.adobe.com/experience-manager/using/creating-sightly-component.html

Avatar

Level 5

Hello Swathi,

1) Does above code works when you use it in OOTB AEM without sprint MVC ?

2) What is resourceSuperType of component for which you are adding this script ? (Not sure why you are using <div data-sly-include="/libs/wcm/core/components/init/init.jsp"></div>)

3) Does it works if you use JS Use instead ?

Yogesh

Avatar

Level 3

Hi Yogesh,

It worked fine in simple sling servlet context.But when I tried to implement in spring mvc it throwed SightlyException.I dont know where I am going wrong.

sling:resourceSuperType : wcm/foundation/components/page

And I never tried with JS use API.

Avatar

Correct answer by
Level 3

Issue resolved.I imported custom packages in pom.xml but forgot to import java use api related packages.Now I am able to instantiate the sightly helper class(OSGI bundle based) after importing com.adobe.cq.sightly package.

Avatar

Level 1

@swathi07

Can you please be more specific about the imports you added to correct this?

It doesn't work for me, either. I getting the same exception you were and nothing seems to correct it.

My class extends WCMUsePojo and my bundle is pulling in the following dependencies in the pom.xml (for both parent and child projects):

<dependency> <groupId>org.apache.sling</groupId> <artifactId>org.apache.sling.scripting.sightly</artifactId> <version>1.0.2</version> <scope>provided</scope> </dependency> <dependency> <groupId>com.adobe.cq.sightly</groupId> <artifactId>cq-wcm-sightly-extension</artifactId> <version>1.2.30</version> <scope>provided</scope> </dependency> <dependency> <groupId>com.adobe.granite</groupId> <artifactId>com.adobe.granite.sightly.engine</artifactId> <version>1.1.72</version> <scope>provided</scope> </dependency>

I cracked open my bundle and the class is indeed in there. The bundle is being successfully loaded. The use bean class should be visible to Sling...

Any advice would be greatly appreciated.

Avatar

Level 3

Hi TaylorBastien,

Whenever your are using external packages(third party or pre-defined api's bundles ) in your osgi bundle class, then there is a need to import those classes into your bundle to make that them visible to the osgi bundle.So when you import com.adobe.cq.sightly.WCMUse class in sightly helper class, sometimes this WCMUse class will not be visible to your bundle,even though it is adobe provided package.So you need to import this package.

Please try to place the below in the maven-bundle-plugin of pom.xml where sightly helper class is defined,

 

<plugin>
                <groupId>org.apache.felix</groupId>
                <artifactId>maven-bundle-plugin</artifactId>
                <!-- <version>2.3.5</version> -->
                <extensions>true</extensions>
                <configuration>
                    <instructions>
                        <Export-Package>
                            <!-- Let's export our primary package -->
                            com.test.yourpackage.model,
                            <!-- By default, don't export any other packages -->
                            !*
                        </Export-Package>
                        <Import-Package>
                            
                            <!-- JSR 250 && 330 -->
                            javax.annotation;version=0.0.0.1_007_JavaSE,
                            javax.inject,

                            <!-- JEE Servlet -->
                            javax.servlet,
                            javax.servlet.http,

                            <!-- Sling -->
                            org.apache.sling.api,
                            org.apache.sling.api.resource,
                            org.apache.sling.auth.core,

                            

                            org.apache.sling.jcr.api,
                            com.adobe.cq.sightly,
                            !*
                        </Import-Package>
                    </instructions>
                </configuration>
            </plugin>

 

Exporting the package and importing other api's(like javax.servlet) is optional(Do this only if an exception is throwed for that corresponding package).Try to import  com.adobe.cq.sightly package.

Hope this works.

 

Thanks,

Swati

Avatar

Level 1

Thanks so much for responding, Swati. That's exactly what was missing! 

I'm so used to using Felix SCR annotations on my bundled classes that I forgot to check whether my use class was being added to the exported-packages manifest. Everything seems to work fine now.

Much appreciated!

 

Taylor

Avatar

Level 2

Hi Swati,

I am also getting the same error.
Have added custom packages in pom.xml, but still the error persists.
Kindly guide me in this.

Regards,
Maria Anto

Avatar

Level 2

Hi,

To solve this, you can change the WCMUse to WCMUsePojo

import com.adobe.cq.sightly.WCMUse;

to

import com.adobe.cq.sightly.WCMUsePojo;

Cheers!