package com.demo.config.core;
import org.osgi.service.metatype.annotations.AttributeDefinition;
import org.osgi.service.metatype.annotations.AttributeType;
import org.osgi.service.metatype.annotations.ObjectClassDefinition;
@ObjectClassDefinition(name = "My Service Configuration", description = "Service Configuration")
public @interface MyServiceConfiguration {
@AttributeDefinition(name = "Config Value", description = "Configuration value")
String configValue();
@AttributeDefinition(name = "MultipleValues", description = "Multi Configuration values")
String[] getStringValues();
@AttributeDefinition(name = "NumberValue", description = "Number values", type=AttributeType.INTEGER)
int getNumberValue();
}
Step 2: Now create MySimpleService interface which will be used by the service:
package com.demo.config.core;
import org.osgi.service.metatype.annotations.AttributeType;
import org.osgi.service.metatype.annotations.ObjectClassDefinition;
@ObjectClassDefinition(name = "My Service Configuration", description = "Service Configuration")
public @interface MyServiceConfiguration {
@AttributeDefinition(name = "Config Value", description = "Configuration value")
String configValue();
@AttributeDefinition(name = "MultipleValues", description = "Multi Configuration values")
String[] getStringValues();
@AttributeDefinition(name = "NumberValue", description = "Number values", type=AttributeType.INTEGER)
int getNumberValue();
}
Step 2: Now create MySimpleService interface which will be used by the service:
package com.demo.config.core;
public interface MySimpleService {
String getSimpleValue();
boolean isAuthor();
}
Step 3: Now create MySimpleServiceImpl service which will read the configurations values:
@Component – defines the class as a component
@Designate – defines at the class level the object-class-definition (where configuration values are defined).
package com.demo.config.core;
import org.apache.sling.settings.SlingSettingsService;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.ConfigurationPolicy;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.metatype.annotations.Designate;
@Component(service = MySimpleService.class,configurationPolicy=ConfigurationPolicy.REQUIRE)
@Designate(ocd = MyServiceConfiguration.class)
public class MySimpleServiceImpl implements MySimpleService {
private MyServiceConfiguration config;
private boolean author;
@Reference
private SlingSettingsService settings;
@Activate
public void activate(MyServiceConfiguration config) {
this.config = config;
author = settings.getRunModes().contains("author");
}
public String getSimpleValue() {
return "hello " + config.configValue();
}
public boolean isAuthor() {
return author;
}
}
String getSimpleValue();
boolean isAuthor();
}
Step 3: Now create MySimpleServiceImpl service which will read the configurations values:
@Component – defines the class as a component
@Designate – defines at the class level the object-class-definition (where configuration values are defined).
package com.demo.config.core;
import org.apache.sling.settings.SlingSettingsService;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.ConfigurationPolicy;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.metatype.annotations.Designate;
@Component(service = MySimpleService.class,configurationPolicy=ConfigurationPolicy.REQUIRE)
@Designate(ocd = MyServiceConfiguration.class)
public class MySimpleServiceImpl implements MySimpleService {
private MyServiceConfiguration config;
private boolean author;
@Reference
private SlingSettingsService settings;
@Activate
public void activate(MyServiceConfiguration config) {
this.config = config;
author = settings.getRunModes().contains("author");
}
public String getSimpleValue() {
return "hello " + config.configValue();
}
public boolean isAuthor() {
return author;
}
}
No comments:
Post a Comment
If you have any doubts or questions, please let us know.