Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

AEM 6.4 - Component can't be initialized by use-api

Avatar

Level 4

Hi all,

I have made a relatively simple component to try to test out some java code. This component doesnt do much, but when it is called it throws this error on the page.

Cannot get DefaultSlingScript: org.apache.sling.api.SlingException: Cannot get DefaultSlingScript: Identifier com.amsurg.core.components.DynamicCharts cannot be correctly instantiated by the Use API

and this one in the error.log

*ERROR* [0:0:0:0:0:0:0:1 [1530045468524] GET /content/amsurg/template_a_test/en.html HTTP/1.1] com.day.cq.wcm.core.impl.WCMDeveloperModeFilter Error during include of SlingRequestPathInfo: path='/content/amsurg/template_a_test/en/jcr:content/main-par/section_1970079346/parsysLeft/dynamic_charts', selectorString='null', extension='html', suffix='null'

org.apache.sling.api.SlingException: Cannot get DefaultSlingScript: Identifier com.amsurg.core.components.DynamicCharts cannot be correctly instantiated by the Use API

at org.apache.sling.scripting.core.impl.DefaultSlingScript.service(DefaultSlingScript.java:510) [org.apache.sling.scripting.core:2.0.54]

I've been looking around and it looks like the java component is not being loaded properly, but I don't know where I went wrong. It's not doing anything extravagent, and I have other components in my bundle that are working as well. Anyone have any ideas? Here is my code

<div data-sly-use.myChart="${'com.amsurg.core.components.DynamicCharts'}">

test

</div>

package com.amsurg.core.components;

import com.adobe.cq.sightly.WCMUsePojo;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

//import javax.jcr.Node;
import java.io.InputStream;

public class DynamicCharts extends WCMUsePojo {

   private static final Logger logger = LoggerFactory.getLogger(DynamicCharts.class);
  private ResourceResolver resolver = this.getResourceResolver();

   @Override
   public void activate() throws Exception {

   try {

  ValueMap pageProperties = getProperties();
   String path = pageProperties.get("filePath", String.class);
   //Node node = resolver.getResource(path).adaptTo(Node.class);
  //Node jcrContent = node.getNode("jcr:content/renditions/original/jcr:content");
  //InputStream content = jcrContent.getProperty("jcr:data").getBinary().getStream();

   }

   catch(Exception e) {

   logger.error("Error getting the information from the xcel sheet" + e.getMessage());
   }

  }

}

And lastly, here is my POM

<?xml version="1.0" encoding="UTF-8"?>
<!-- | Copyright 2015 Adobe Systems Incorporated | | Licensed under the Apache
  License, Version 2.0 (the "License"); | you may not use this file except
  in compliance with the License. | You may obtain a copy of the License at
  | | http://www.apache.org/licenses/LICENSE-2.0 | | Unless required by applicable
  law or agreed to in writing, software | distributed under the License is
  distributed on an "AS IS" BASIS, | WITHOUT WARRANTIES OR CONDITIONS OF ANY
  KIND, either express or implied. | See the License for the specific language
  governing permissions and | limitations under the License. -->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
  <groupId>com.amsurg</groupId>
  <artifactId>amsurg</artifactId>
  <version>1.5.27-SNAPSHOT</version>
  <relativePath>../pom.xml</relativePath>
  </parent>
  <artifactId>amsurg.core</artifactId>
  <packaging>bundle</packaging>
  <name>Amsurg - Core</name>
  <description>Core bundle for Amsurg</description>
  <build>
  <plugins>
  <plugin>
  <groupId>org.apache.felix</groupId>
  <artifactId>maven-scr-plugin</artifactId>
  </plugin>
  <plugin>
  <groupId>org.apache.sling</groupId>
  <artifactId>maven-sling-plugin</artifactId>
  </plugin>
  <plugin>
  <groupId>org.apache.felix</groupId>
  <artifactId>maven-bundle-plugin</artifactId>
  <extensions>true</extensions>
  <configuration>
  <instructions>
   <!-- Import any version of javax.inject, to allow running on multiple
  versions of AEM -->
   <Embed-Transitive>true</Embed-Transitive>
  <Embed-Dependency>unirest-java, httpclient, httpasyncclient, httpmime, jackson-dataformat-xml;scope=compile|runtime

   </Embed-Dependency>
  <Import-Package>
   com.fasterxml.jackson.*,

  org.codehaus.stax2.*,

  javax.inject*;version=0.0.0,*;

   </Import-Package>
  <Sling-Model-Packages>
   com.amsurg.core

   </Sling-Model-Packages>
  <Export-Package>com.amsurg.core.*,com.fasterxml.jackson.*, org.codehaus.stax2.*</Export-Package>
  </instructions>
  </configuration>
  </plugin>

  </plugins>
  </build>

  <dependencies>
   <!-- OSGi Dependencies -->
   <dependency>
  <groupId>org.apache.felix</groupId>
  <artifactId>org.apache.felix.scr</artifactId>
  </dependency>
  <dependency>
  <groupId>org.apache.felix</groupId>
  <artifactId>org.apache.felix.scr.annotations</artifactId>
  </dependency>
  <dependency>
  <groupId>biz.aQute</groupId>
  <artifactId>bndlib</artifactId>
  </dependency>
  <dependency>
  <groupId>org.osgi</groupId>
  <artifactId>org.osgi.core</artifactId>
  </dependency>
  <dependency>
  <groupId>org.osgi</groupId>
  <artifactId>org.osgi.compendium</artifactId>
  </dependency>
   <!-- Other Dependencies -->
   <dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-api</artifactId>
  </dependency>
  <dependency>
  <groupId>javax.jcr</groupId>
  <artifactId>jcr</artifactId>
  </dependency>
  <dependency>
  <groupId>javax.servlet</groupId>
  <artifactId>servlet-api</artifactId>
  </dependency>
  <dependency>
  <groupId>com.adobe.aem</groupId>
  <artifactId>uber-jar</artifactId>
  <classifier>obfuscated-apis</classifier>
  </dependency>
  <dependency>
  <groupId>org.apache.sling</groupId>
  <artifactId>org.apache.sling.models.api</artifactId>
  </dependency>
  <dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  </dependency>
  <dependency>
  <groupId>org.mockito</groupId>
  <artifactId>mockito-all</artifactId>
  </dependency>
  <dependency>
  <groupId>junit-addons</groupId>
  <artifactId>junit-addons</artifactId>
  </dependency>
  <dependency>
  <groupId>commons-collections</groupId>
  <artifactId>commons-collections</artifactId>
  </dependency>
  <dependency>
  <groupId>com.r2i.commons</groupId>
  <artifactId>r2i-commons.core</artifactId>
  </dependency>
  <dependency>
  <groupId>com.day.commons.osgi.wrapper</groupId>
  <artifactId>com.day.commons.osgi.wrapper.commons-email</artifactId>
  <version>1.2.0-0002</version>
  <scope>provided</scope>
  </dependency>
  <dependency>
  <groupId>commons-io</groupId>
  <artifactId>commons-io</artifactId>
  <version>2.4</version>
  <scope>provided</scope>
  </dependency>
  <dependency>
  <groupId>javax.mail</groupId>
  <artifactId>mail</artifactId>
  <version>1.5.0-b01</version>
  <scope>provided</scope>
  </dependency>
  <dependency>
  <groupId>com.adobe.acs</groupId>
  <artifactId>acs-aem-commons-bundle</artifactId>
  <version>3.14.10</version>
  <scope>provided</scope>
  </dependency>
   <!--<dependency>
  <groupId>com.adobe.acs</groupId>
  <artifactId>acs-aem-commons-content</artifactId>
  <version>3.14.10</version>
  <type>content-package</type>
  </dependency> -->
   <dependency>
  <groupId>com.adobe.granite.bundles</groupId>
  <artifactId>json</artifactId>
  <version>20090211_1</version>
  <scope>provided</scope>
  </dependency>
   <!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
   <dependency>
  <groupId>com.google.code.gson</groupId>
  <artifactId>gson</artifactId>
  <version>2.8.2</version>
  </dependency>
  <dependency>
  <groupId>com.day.commons.osgi.wrapper</groupId>
  <artifactId>com.day.commons.osgi.wrapper.commons-httpclient</artifactId>
  </dependency>
  <dependency>
  <groupId>org.apache.commons</groupId>
  <artifactId>commons-lang3</artifactId>
  <version>3.4</version>
  <scope>provided</scope>
  </dependency>
  <dependency>
  <groupId>com.mashape.unirest</groupId>
  <artifactId>unirest-java</artifactId>
  <version>1.4.9</version>
  </dependency>
  <dependency>
  <groupId>org.apache.httpcomponents</groupId>
  <artifactId>httpclient</artifactId>
  <version>4.3.6</version>
  </dependency>
  <dependency>
  <groupId>org.apache.httpcomponents</groupId>
  <artifactId>httpasyncclient</artifactId>
  <version>4.0.2</version>
  </dependency>
  <dependency>
  <groupId>org.apache.httpcomponents</groupId>
  <artifactId>httpmime</artifactId>
  <version>4.3.6</version>
  </dependency>
  <dependency>
  <groupId>org.json</groupId>
  <artifactId>json</artifactId>
  <version>20140107</version>
  </dependency>
  <dependency>
  <groupId>xerces</groupId>
  <artifactId>xmlParserAPIs</artifactId>
  <version>2.6.2</version>
  </dependency>
  <dependency>
  <groupId>com.fasterxml.jackson.dataformat</groupId>
  <artifactId>jackson-dataformat-xml</artifactId>
  <version>2.9.0</version>
  </dependency>
  </dependencies>
</project>

Any help would be appreciated! Thank you!

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi,

You need to define ResourceResolver object inside activate method

Change 2 line of your code like below highlighted in bold:

public class DynamicCharts extends WCMUsePojo {

   private static final Logger logger = LoggerFactory.getLogger(DynamicCharts.class);
  private ResourceResolver resolver = null;

   @Override
   public void activate() throws Exception {

   try {

  resolver = this.getResourceResolver();

  ValueMap pageProperties = getProperties();
   String path = pageProperties.get("filePath", String.class);
   //Node node = resolver.getResource(path).adaptTo(Node.class);
  //Node jcrContent = node.getNode("jcr:content/renditions/original/jcr:content");
  //InputStream content = jcrContent.getProperty("jcr:data").getBinary().getStream();

   }

   catch(Exception e) {

   logger.error("Error getting the information from the xcel sheet" + e.getMessage());
   }

  }

}

Thanks

Arun

View solution in original post

0 Replies

Avatar

Correct answer by
Community Advisor

Hi,

You need to define ResourceResolver object inside activate method

Change 2 line of your code like below highlighted in bold:

public class DynamicCharts extends WCMUsePojo {

   private static final Logger logger = LoggerFactory.getLogger(DynamicCharts.class);
  private ResourceResolver resolver = null;

   @Override
   public void activate() throws Exception {

   try {

  resolver = this.getResourceResolver();

  ValueMap pageProperties = getProperties();
   String path = pageProperties.get("filePath", String.class);
   //Node node = resolver.getResource(path).adaptTo(Node.class);
  //Node jcrContent = node.getNode("jcr:content/renditions/original/jcr:content");
  //InputStream content = jcrContent.getProperty("jcr:data").getBinary().getStream();

   }

   catch(Exception e) {

   logger.error("Error getting the information from the xcel sheet" + e.getMessage());
   }

  }

}

Thanks

Arun