Expand my Community achievements bar.

SOLVED

convert felix @properties & @property to OSGI annotations

Avatar

Level 3

Hi All,

Can someone help me to convert felix @properties & @property to OSGI annotations. If I have a single @property I can bring into OSGi @Component as an attribute. But In my case there are multiple @property which has been used inside @properties. Below is the sample code and request people to help me on the highlighted portion.

import org.apache.felix.scr.annotations.Component;

import org.apache.felix.scr.annotations.Properties;

import org.apache.felix.scr.annotations.Property;

import org.apache.felix.scr.annotations.PropertyUnbounded;

import org.apache.felix.scr.annotations.Reference;

import org.apache.felix.scr.annotations.Service;

@Component(metatype = true, immediate = true, label = "sample Hardware Importer Job")

@Service(value = {Runnable.class, sampleHardwareImporterJob.class})

@Properties(value = {

    @Property(label = "Enabled", description = "Enable/Disable the Scheduled Service",

        name = "service.enabled", boolValue = false),

    @Property(label = "Allow concurrent executions",

        description = "Allow concurrent executions of this Scheduled Service. Never set to true!",

        name = Scheduler.PROPERTY_SCHEDULER_CONCURRENT, boolValue = false, propertyPrivate = true),

    @Property(label = "Period", description = "Trigger the job every n seconds",

        name = Scheduler.PROPERTY_SCHEDULER_PERIOD,

        longValue = sampleHardwareImporterJob.ONE_DAY)})

public class sampleHardwareImporterJob implements Runnable {

  @Property(value = {"en", "de", "fr", "it"}, unbounded = PropertyUnbounded.ARRAY)

public static final String LANGUAGES_PROPERTY = "sample.hardware.import.languages";

static final long ONE_DAY = 24L * 3600;

private static final Logger LOG = LoggerFactory.getLogger(sampleHardwareImporterJob.class);

private static final String[] defaultLanguages = {"en", "de", "fr", "it"};

private static final String DEFAULT_ROOT_PATH = "/content/";

private static final String sample_ROOT_PATH = "/content/sample";

private static final String DEFAULT_HARDWARE_PATH = "/content/hardware/";

private static final String DEFAULT_HARDWARE_FOLDER = "hardware";

private static final String HARDWARE_NODE_TYPE = "hardware";

private static final String XML_SOURCE = "https://sample-chsa-%s.providersaas.com/search.xml";

private static final String CQ_PAGE = "cq:Page";

  @Property(value = XML_SOURCE)

private static final String XML_SOURCE_PROPERTY = "sample.hardware.import.xml";

private List<String> languages = new ArrayList<>();

Thanks,
Vijay

Feike Visser

Arun Patidar

smacdonald2008

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi,

There are two ways to doing it.

1. move the service configuration into separate interface and use in service class like below

@Component(service = MyService.class, immediate = true)

@Designate(ocd = Config.class)

2. create service configuration interface in service class.

Both are mentioned in above example. I suggest you to move service configs to separate interface.



Arun Patidar

View solution in original post

7 Replies

Avatar

Community Advisor

Hi,

For service configuration you need to use OSGI DS annotations.

@AttributeDefinition(

name = "Dropdown property",

description = "Sample dropdown property",

options = {

@Option(label = "EN", value = "en"),

@Option(label = "DE", value = "de"),

@Option(label = "FR", value = "fr"),

@Option(label = "IT", value = "it")

        }

    )

String servicename_propertyname_dropdown() default StringUtils.EMPTY;

@AttributeDefinition(

name = "String Property",

description = "Sample String property",

type = AttributeType.STRING

    )

String servicename_propertyname_string() default "foo";

Please check below:

Official OSGi Declarative Services Annotations in AEM - Adobe Experience Manager | AEM/CQ | Apache S...

For using service properties you can do below:

@Component(

immediate = true,

service = {Runnable.class, sampleHardwareImporterJob.class},

property = {

"service.enabled=false",

Scheduler.PROPERTY_SCHEDULER_CONCURRENT+"=false",

Scheduler.PROPERTY_SCHEDULER_PERIOD+"="+sampleHardwareImporterJob.ONE_DAY

    }

)



Arun Patidar

Avatar

Level 3

Hi Arun,

Are you saying that I should move the service configuration into separate interface and leverage the interface into my actual class? that is what I can understand from this link Official OSGi Declarative Services Annotations in AEM - Adobe Experience Manager | AEM/CQ | Apache S...  . can you guide me on thi same.

Thanks,,

Vijay

Avatar

Correct answer by
Community Advisor

Hi,

There are two ways to doing it.

1. move the service configuration into separate interface and use in service class like below

@Component(service = MyService.class, immediate = true)

@Designate(ocd = Config.class)

2. create service configuration interface in service class.

Both are mentioned in above example. I suggest you to move service configs to separate interface.



Arun Patidar

Avatar

Level 3

Hi Arun,

Can you help me to understand on below, Which is best way to have the default value for the particular configuration.

1)      Using org.osgi.service.metatype.annotations.AttributeDefinition.defaultValue().

            @AttributeDefinition(name = "Cron-job expression",

                defaultValue = "*/30***?")

        String scheduler_expression();

2) @AttributeDefinition(name = "Cron-job expression")

        String scheduler_expression() default "*/30 * * * * ?";

  String scheduler_expression() default "*/30 * * * * ?";

Thanks,

Vijay

Avatar

Community Advisor

Hi,

It always worked with 2nd options, go for it.

String scheduler_expression() default "*/30 * * * * ?";



Arun Patidar

Avatar

Level 3

Hi Arun,

I could see the default value in configmgr when I used both of the two ways. But I'm just trying to understand the differences between the two options. both is doing the same work?

Thanks,

Vijay

Avatar

Community Advisor

they both do same job but default value of defaultValue always take preference.



Arun Patidar