avatar
Creating your custom OSGi Configuration AEM

> Be assumption that here is an example RDS Postgres configuration was created in AWS.

Endpoint     : powerbi.ccsmkmf70ics.us-west-1.rds.amazonaws.com
Database Name: powerbi
Username     : flagtickpostgres
Password     : qOj841&^oKNV

> In term of connecting out of AEM, this configuration easily configured without any roadblocks. Of jumping into AEM project, we will custom OSGi configuration and conduct as below:

+ ui.config/src/main/content/jcr_root/apps/flagtick/osgiconfig/
config/com.flagtick.core.services.impl.RdsConnectionServiceImpl.cfg.json

> In Adobe Cloud, to configure OSGi for Adobe Experience Manager as a Cloud Service. Please refer read more.

{
  "hostName":"$[secret:ENDPOINT]",
  "databaseName": "$[secret:DATABASE_NAME]",
  "userName": "$[secret:DATABASE_USER]",
  "password": "$[secret:DATABASE_PASSWORD]",
  "port": "$[secret:DATABASE_PORT]"
}

> Implemented Class as below

package com.flagtick.core.services.impl;


import com.flagtick.core.services.RdsConnectionService;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.metatype.annotations.AttributeDefinition;
import org.osgi.service.metatype.annotations.Designate;
import org.osgi.service.metatype.annotations.ObjectClassDefinition;

@Component(service = RdsConnectionService.class, immediate = true)
@Designate(ocd = RdsConnectionServiceImpl.Config.class)
public class RdsConnectionServiceImpl implements RdsConnectionService {
    private RdsConnectionServiceImpl.Config configuration;

    @Activate
    protected void activate(RdsConnectionServiceImpl.Config configuration) {
        this.configuration = configuration;
    }

    @Override
    public String getHostName() {
        return configuration.hostName();
    }

    @Override
    public String getDatabaseName() {
        return configuration.databaseName();
    }

    @Override
    public String getUserName() {
        return configuration.userName();
    }

    @Override
    public String getPassword() {
        return configuration.password();
    }

    @Override
    public String getPort() {
        return configuration.port();
    }

    @ObjectClassDefinition(
            name = "Rds Connection Configuration",
            description = "This configuration reads the key value of Rds Database"
    )

    protected @interface Config {
        /**
         * This method returns the protocol that is being used
         *
         * @return Protocol
         */
        @AttributeDefinition(
                name = "hostName",
                description = "Host name of database")
        String hostName() default "powerbi.ccsmkmf70ics.us-west-1.rds.amazonaws.com";

        /**
         * This method returns the protocol that is being used
         *
         * @return Protocol
         */
        @AttributeDefinition(
                name = "databasename",
                description = "Name of database")
        String databaseName() default "powerbi";

        /**
         * This method returns the protocol that is being used
         *
         * @return Protocol
         */
        @AttributeDefinition(
                name = "userName",
                description = "User name of database")
        String userName() default "flagtickpostgres";

        /**
         * This method returns the protocol that is being used
         *
         * @return Protocol
         */
        @AttributeDefinition(
                name = "password",
                description = "Password of database")
        String password() default "qOj841&^oKNV";

        /**
         * This method returns the protocol that is being used
         *
         * @return Protocol
         */
        @AttributeDefinition(
                name = "port",
                description = "Port of database")
        String port() default "5432";
    }
}

Note: In some cases, you don't want to get

> Initialize interface to get / set value

package com.flagtick.core.services;

public interface RdsConnectionService {
    String getHostName();
    String getDatabaseName();
    String getUserName();
    String getPassword();
    String getPort();
}

> How can call this interface from Java class base

@Reference
transient private RdsConnectionService rdsConnectionService;
You need to login to do this manipulation!