convert felix @properties & @property to OSGI annotations | Community
Skip to main content
Level 3
Aug 21, 2018 7:11 AM UTC
Solved

convert felix @properties & @property to OSGI annotations

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​

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
arunpatidar
arunpatidarAccepted solution
New Participant
Aug 21, 2018 1:06 PM UTC

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

7 replies

arunpatidar
New Participant
Sep 1, 2018 11:04 AM UTC

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

Translate
Arun Patidar
Level 3
Sep 1, 2018 6:25 AM UTC

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

Translate
arunpatidar
New Participant
Aug 31, 2018 1:04 PM UTC

Hi,

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

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

Translate
Arun Patidar
Level 3
Aug 31, 2018 11:04 AM UTC

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

Translate
arunpatidar
arunpatidarAccepted solution
New Participant
Aug 21, 2018 1:06 PM UTC

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.

Translate
Arun Patidar
Level 3
Aug 21, 2018 11:57 AM UTC

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 Sling  . can you guide me on thi same.

Thanks,,

Vijay

Translate
arunpatidar
New Participant
Aug 21, 2018 8:37 AM UTC

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 Sling

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

    }

)

Translate
Arun Patidar