Skip to content

Instantly share code, notes, and snippets.

@aanchalsikka
Last active November 13, 2017 12:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aanchalsikka/48d175ca46e98138d2ded979ab081e06 to your computer and use it in GitHub Desktop.
Save aanchalsikka/48d175ca46e98138d2ded979ab081e06 to your computer and use it in GitHub Desktop.
Creating configurable properties
package blog.techrevel.component.impl;
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.AttributeType;
import org.osgi.service.metatype.annotations.Designate;
import org.osgi.service.metatype.annotations.ObjectClassDefinition;
import org.osgi.service.metatype.annotations.Option;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component
@Designate(ocd = ConfigurableComponentPorpertiesImpl.Config.class)
public class ConfigurableComponentPorpertiesImpl {
private static final Logger LOGGER = LoggerFactory.getLogger(ConfigurableComponentPorpertiesImpl.class);
@ObjectClassDefinition(name = "Techrevel Sample Configuration", description = "This is sample configuration")
public @interface Config {
@AttributeDefinition(name = "Blog name", defaultValue = "techrevel", description = "Name of the blog")
String blog_name();
@AttributeDefinition(name = "Blog Url")
String blog_URL() default "https://techrevel.blog";
// Multi-values property
@AttributeDefinition(name = "Blog Topics")
String[] blog_Topics() default { "OSGi", "AEM" };
@AttributeDefinition(name = "Blog Count", description = "Total number of blogs", type = AttributeType.INTEGER)
int blogCount() default 0;
// Password
@AttributeDefinition(name = "password", type = AttributeType.PASSWORD)
String password();
// Checkbox
@AttributeDefinition(name = "Blog is active?")
boolean blogIsActive() default true;
// Dropdown
@AttributeDefinition(name = "Blog is hosted at?", options = { @Option(label = "WordPress", value = "wordpress"),
@Option(label = "Blogspot", value = "blogspot") })
String hostedAt() default "";
}
@Activate
protected void activate(final Config config) {
LOGGER.info("Blog name: " + config.blog_name());
LOGGER.info("Blog URL: " + config.blog_URL());
for (String topic : config.blog_Topics()) {
LOGGER.info("Blog Topics: " + topic);
}
LOGGER.info("Blog Count: " + config.blogCount());
LOGGER.info("Blog Password: " + config.password());
LOGGER.info("Blog Is Active? " + config.blogIsActive());
LOGGER.info("Blog is hosted at? " + config.hostedAt());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment