Expand my Community achievements bar.

SOLVED

how to read OSGI configuration via plain Java class

Avatar

Level 3

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 ?

1 Accepted Solution

Avatar

Correct answer by
Employee Advisor

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

View solution in original post

1 Reply

Avatar

Correct answer by
Employee Advisor

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