how to read OSGI configuration via plain Java class | Community
Skip to main content
May 18, 2018
Solved

how to read OSGI configuration via plain Java class

  • May 18, 2018
  • 1 reply
  • 1218 views

I need to get some OSGI configuration values via plain Java class which is not registered as service so I cannot use @Reference or @Inject annotation. I have used Bundle context to get the config but it is working.

public void getArticleName() {

        final BundleContext bundleContext = FrameworkUtil.getBundle(ArticleNameService.class).getBundleContext();

        try {

            String articleName = (String) bundleContext.getService(

                    (bundleContext.getServiceReferences(ArticleNameService.class.getName(), " article.name "))[0]);

            LOG.info("articleName......" + articleName);

        } catch (InvalidSyntaxException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }

    }

Service class

@Service(ArticleNameService.class)

@Component(

        metatype = true)

@Properties({

        @Property(

                name = "article.name", unbounded = PropertyUnbounded.ARRAY, cardinality = Integer.MAX_VALUE,

                label = "article addrnameess"),

         })

public class ArticleNameServiceImpl implements ArticleNameService

{

   

    private static final String ARTICLE_NAME = "article.name";

    private String[] articleName;

   

    protected final void activate(final ComponentContext componentContext)

    {

        final Dictionary<String, Object> configurationProperties = componentContext.getProperties();

        if (configurationProperties != null)

        {

            articleName = PropertiesUtil.toStringArray(configurationProperties.get(ARTICLE_NAME));

        }

    }

    @Override

    public final String[] getArticeName()

    {

        return articleName;

    }

}

Can anyone help ?

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by joerghoh

When you have access to the BundleContext, you can try to get the OSGI ConfigAdmin (read the OSGI spec for details) service and read properties through that.

Note: Not best best practice, not recommended.

Jörg

1 reply

joerghoh
Adobe Employee
joerghohAdobe EmployeeAccepted solution
Adobe Employee
May 18, 2018

When you have access to the BundleContext, you can try to get the OSGI ConfigAdmin (read the OSGI spec for details) service and read properties through that.

Note: Not best best practice, not recommended.

Jörg