Thursday 11 June 2020

Create OSGI Config

How to create OSGI Config and use it in java file ?

Step 1) Create Interface

                public interface MyConfigurationService {
public String getConfigurationValue(String property);
public String[] getConfigurationMultiValue(String property) ;
                }

Step 2) Create Implementation

@Component(immediate = true, metatype = true, label = "My Configuration Service", description = "This is my config service")
@Service(value = MyConfigurationService.class)
public class MyConfigurationServiceImpl implements MyConfigurationService{

@Property(unbounded = PropertyUnbounded.ARRAY, label = "Components", description = "Enter in format ComponentNodeName:ResourceType Eg: text:weretail/components/content/text")
private static final String MY_COMPONENTS = Constants.MY_COMPONENTS;
@Property(unbounded = PropertyUnbounded.DEFAULT, label = "Layout Container", description = "Enter layout container path from root Eg: root/responsivegrid_123456")
private static final String LAYOUT_CONTAINER = Constants.MY_LAYOUT_CONT;
private Dictionary properties;

@Activate
    protected void activate(ComponentContext context) {
        properties = context.getProperties();
    }
@Deactivate
    protected void deactivate(ComponentContext context) {
        properties = null;
    }
@Override
public String getConfigurationValue(String property) {
return (String) properties.get(property);
}

@Override
public String[] getConfigurationMultiValue(String property) {
return  PropertiesUtil.toStringArray(properties.get(property));
}

}

Step 3) Define constants used in constant file

        public final class Constants {
         public static final String MY_COMPONENTS= "my.project.components";
         public static final String MY_LAYOUT_CONT= "my.project.layoutcontainer";
        }

Step 4) Add required vaues in config file

    Example:
    Path: /apps/project/runmodes/config
    Filecom.project.services.impl.MyConfigurationServiceImpl.xml
    
<?xml version="1.0" encoding="UTF-8"?>
<jcr:root xmlns:sling="http://sling.apache.org/jcr/sling/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0"
    jcr:primaryType="sling:OsgiConfig"
    my.project.layoutcontainer="root/responsivegrid_123456"
    my.project.components="[text:weretail/components/content/text,image:weretail/components/content/image]"/>

Step 5) Use it now in java class

MyConfigurationService config = new MyConfigurationServiceImpl ();
logger.debug("Get Layout container from config: {}",config.getConfigurationValue("LAYOUT_CONTAINER "));


OR
 

Step 1) Create Configuration Interface

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

@ObjectClassDefinition(name = "MyConfiguration", description = "Service Configuration")
public @interface MyConfiguration {
@AttributeDefinition(name = "MyConfiguration", description = "Add JSON Configuration file path")
String getMyMapping();

  //  @AttributeDefinition(name = "MultipleValues", description = "Multi Configuration values")
  //  String[] getStringValues();
     
  //  @AttributeDefinition(name = "NumberValue", description = "Number values", type=AttributeType.INTEGER)
   // int getNumberValue();
}

Step 2) Create Service Interface

public interface MyConfigurationService {
public String getMyMapping();
}

Step 3) Create Service Implementation

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

@Component(service = MyConfigurationService.class)
@Designate(ocd = MyConfiguration.class)
public class MyConfigurationServiceImpl implements MyConfigurationService{

private String jsonPath;
@Activate
protected void activate(MyConfiguration myConfig) {
jsonPath = myConfig.getMyMapping();
}

@Override
public String getMyMapping() {
return jsonPath;
}
}

Step 4) Create xml config file

<?xml version="1.0" encoding="UTF-8"?>
<jcr:root xmlns:sling="http://sling.apache.org/jcr/sling/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0"
    jcr:primaryType="sling:OsgiConfig" 
MyConfiguration="/content/dam/someFile.json"/>

Step 5) Use it in java file

@Reference
MyConfigurationService configService;
configService.getMyMapping());


                                                OR

In case if its a Helper file, where we cannot inject service as service inject does not work
Follow below approach

File 1: 
public interface PromoConfigurationService {
public String[] getTemplates();

File 2:
@Component(service = PromoConfigurationService.class, immediate=true, enabled=true)
@Designate(ocd = PromoConfiguration.class)
public class PromoConfigurationServiceImpl implements PromoConfigurationService{
@ObjectClassDefinition(name="Promo Configuration",
            description = "Promo Template Configuration")
public static @interface PromoConfiguration {

@AttributeDefinition(
        name = "template.path",
        description = "Allowed template paths for promos",
        type = AttributeType.STRING
    )
    String[] templatePath(); 
    }
String[] templates;
@Activate
protected void activate(PromoConfiguration promoConfig) {
templates = promoConfig.templatePath();
}

@Override
public String[] getTemplates() {
return templates;
}
}

File 3: [Helper file]

BundleContext bundleContext = FrameworkUtil.getBundle(CommonUtils.class).getBundleContext();

        ServiceReference promoServiceRef = bundleContext.getServiceReference(PromoConfigurationService.class.getName());
        PromoConfigurationService promoConfig = (PromoConfigurationService) bundleContext.getService(promoServiceRef);

String[] templateNames = promoConfig.getTemplates();


No comments:

Post a Comment