0

I am trying to create an OSGi Service using OSGi R6 annotations and then injecting it in the Sling Model class like this:

   package com.aem.sites.models;

import javax.annotation.PostConstruct;
import javax.inject.Inject;

import org.apache.sling.api.resource.Resource;
import org.apache.sling.models.annotations.Model;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.aem.sites.services.WeatherService;

@Model(adaptables=Resource.class)
public class Banner {

    final static Logger logger = LoggerFactory.getLogger(Banner.class);

    @Inject
    private WeatherService weatherService;

    private String message;

        @PostConstruct
        public void init() {
            logger.info("##############################################################calling the init method");
            message = weatherService.getName();
        }

        public String getMessage() {
            logger.info("##############################################################inside the get message method");
            return message;
        }

}

The OSGi configuration interface looks like this:

    package com.aem.sites.interfaces;

import org.osgi.service.metatype.annotations.AttributeDefinition;
import org.osgi.service.metatype.annotations.AttributeType;
import org.osgi.service.metatype.annotations.ObjectClassDefinition;

@ObjectClassDefinition(name = "Configuration", description = "Configuration file")
public @interface Configuration {

     @AttributeDefinition(
                name = "String Property",
                description = "Sample String property",
                type = AttributeType.STRING
            )
     public String getText() default "It's a test";
}

and the service class looks like this:

   package com.aem.sites.services.impl;


import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Deactivate;
import org.osgi.service.component.annotations.Modified;
import org.osgi.service.component.annotations.ConfigurationPolicy;
import org.osgi.service.metatype.annotations.Designate;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import com.aem.sites.interfaces.Configuration;
import com.aem.sites.services.WeatherService;

@Component(service=WeatherService.class,
immediate=true,
configurationPid = "com.aem.sites.services.impl.WeatherServiceImpl",
configurationPolicy=ConfigurationPolicy.REQUIRE
)
@Designate(ocd = Configuration.class)
public class WeatherServiceImpl implements WeatherService{

    final static Logger logger =LoggerFactory.getLogger(WeatherServiceImpl.class);

    private Configuration config;

    private String name;

    @Activate
    @Modified
    protected final void activate(Configuration configuration) {
        logger.info("##############################################################calling activate method inside the weather service impl class");
        this.config = configuration;
        name = config.getText();
    }

    @Deactivate
    protected void deactivate() {
    }

    @Override
    public String getName() {
        logger.info("##############################################################calling get name method inside the weather service impl class");
        return name;
    }
}

and finally the service interface:

package com.aem.sites.services;

public interface WeatherService {

    public String getName();

}

I am trying to use the Sling Model Pojo in the HTL class like this:

<sly data-sly-use.banner="com.aem.sites.models.Banner">

    <div class="inner">
        <h2>${banner.message}</h2

    </div>

</sly>

But I am not able to see any texts. I have used the logger.info but can't see it in the log files either. I am sure I am doing something very wrong here but unable to locate since I have just started playing with OSGi R6 annotations and Sling Models. Any help is appreciated.

Adding Maven dependencies:

Parent pom.xml

<!-- <plugin>
                    <groupId>org.apache.felix</groupId>
                    <artifactId>maven-bundle-plugin</artifactId>
                    <version>2.5.3</version>
                </plugin>  -->             
                <plugin>
                      <groupId>org.apache.felix</groupId>
                      <artifactId>maven-bundle-plugin</artifactId>
                      <version>3.3.0</version>
                      <inherited>true</inherited>
                </plugin>

<dependency>
              <groupId>org.osgi</groupId>
              <artifactId>org.osgi.service.component.annotations</artifactId>
              <version>1.3.0</version>
               <scope>compile</scope>
            </dependency>

            <dependency>
              <groupId>org.osgi</groupId>
              <artifactId>org.osgi.annotation</artifactId>
              <version>6.0.0</version>
            </dependency>

            <dependency>
              <groupId>org.osgi</groupId>
              <artifactId>org.osgi.service.metatype.annotations</artifactId>
              <version>1.3.0</version>
            </dependency>

Core pom.xml

<dependency>
              <groupId>org.osgi</groupId>
              <artifactId>org.osgi.service.component.annotations</artifactId>
            </dependency>
            <dependency>
              <groupId>org.osgi</groupId>
              <artifactId>org.osgi.annotation</artifactId>
            </dependency>
            <dependency>
              <groupId>org.osgi</groupId>
              <artifactId>org.osgi.service.metatype.annotations</artifactId>
            </dependency>

<plugin>
                <groupId>org.apache.felix</groupId>
                <artifactId>maven-bundle-plugin</artifactId>
                <extensions>true</extensions>
                <configuration>
                    <instructions>
                        <Bundle-SymbolicName>com.aem.site.aem-site</Bundle-SymbolicName>
                        <Sling-Model-Packages>
                          com.aem.sites.models
                        </Sling-Model-Packages>
                         <Import-Package>javax.inject;version=0.0.0,*</Import-Package>
                    </instructions>
                </configuration>
            </plugin>   
user972418
  • 817
  • 3
  • 25
  • 58
  • In your model: Can you please replace `@Inject` with `@OSGiService`? In the official documentation the `@Inject` was used for a model that was adapted from `SlingHttpServletRequest`. You are adapting from `Resource`. This might make a difference, although I am not 100% sure. – Jens Oct 10 '17 at 10:16

2 Answers2

1

In the service impl you have put service=WeatherServiceImpl.class which is incorrect, it should be the service interface name.

so, in WeatherServiceImpl change

 @Component(service=WeatherServiceImpl.class,

to

 @Component(service=WeatherService.class,

..

EDIT: also configurationPolicy=ConfigurationPolicy.REQUIRE means that there should be at least one config in order for the DS component to be active. (config from code wont work)

so you can go to system /system/console/configMgr/com.aem.sites.services.impl.WeatherServiceImpl and put a value and save. or you can set configurationPolicy to optional and your code will work without config.

awd
  • 2,302
  • 1
  • 22
  • 29
  • 1
    Thank you for the response. Already did that but nothing changed. – user972418 Oct 08 '17 at 14:27
  • I have edited the post to include the changes made in both the pom files. Please have a look. Thanks. – user972418 Oct 08 '17 at 14:58
  • this is strange. I tried on 6.3 and was able to get the message output in sightly. I made the following changes though - 1. change package names to match my sandbox setup. 2. replaced log4j logger with slf4j logger – awd Oct 08 '17 at 15:09
  • I have updated my code with minor edits. Not that it has made any difference though. Can you please check if everything still works fine ? I have also implemented slf4j – user972418 Oct 08 '17 at 15:39
  • I am pretty much positive there is nothing wrong with the code at this point. also I am assuming you already checked that the bundle is active in system console, the service component is active etc. maybe you can put all your code on github and share link. – awd Oct 08 '17 at 15:44
  • Here is the bitbucket URL https://bitbucket.org/ankit909/aem-site. Thank you for all the help. – user972418 Oct 08 '17 at 15:49
  • you will have to make the repo public – awd Oct 08 '17 at 15:51
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/156211/discussion-between-user972418-and-awd). – user972418 Oct 08 '17 at 15:53
0

In order to make the Sling Models available you have to properly configure <Sling-Model-Packages> section of the maven-bundle-plugin, see https://sling.apache.org/documentation/bundles/models.html#basic-usage

You can check the models are properly exposed by using the Sling Models Web Console: http://localhost:4502/system/console/status-slingmodels

Vlad
  • 10,602
  • 2
  • 36
  • 38
  • Thank you for the response. Do you think the section is not properly configured In the same package i.e. I have other sling model classes and they seem to be working fine. But this is one class that is not working as expected. I also tried using WCMUsePojo but even that didn't work. So, I don't know if the pom.xml have some issues or is it something else. – user972418 Oct 10 '17 at 00:23
  • The latest update of the `pom.xml` fragment looks ok. I verified and it's working ok on AEM 6.3. I suspect you're not including the HTL script correctly, try to define a minimal HTL script with the above snippet at `/apps/test/banner/banner.html` and a node at `/content/test` with `sling:resourceType=test/banner` and see it gets rendered correctly at `/content/test.html` – Vlad Oct 10 '17 at 06:37
  • Are you able to build the project correctly ? I am asking this is because I observed some issues with maven build. https://stackoverflow.com/questions/46659511/aem-6-3-maven-build-is-successful-but-still-doesnt-install-all-the-content – user972418 Oct 10 '17 at 12:34
  • I copy pasted your classes and script into a working project – Vlad Oct 10 '17 at 12:35