Hi,
I have a property like this:
@Property(name = "myvar", value = "myvalue")
I need to access this property inside the code.
I can do this with:
@Activate public void activate(ComponentContext ctx) { Dictionary dic = ctx.getProperties(); Enumeration keys = dic.keys(); List<String> list = Collections.list(keys); Collections.sort(list); for (String key : list) if (key.equals("myvar")) portal_server = String.valueOf(dic.get((Object) key)); }
Is there a simpler way?
Thanks
Solved! Go to Solution.
Views
Replies
Total Likes
Hi,
I use code like this:
@Activate protected void activate (ComponentContext ctx) { String myvar = PropertyUtils.toString (ctx.getProperties().get("myvar"), "defaultValue"); }
The PropertyUtils helper is very useful, as it provides many conversion tools including the support for a default value, if the property is not provided or empty.
Views
Replies
Total Likes
Invoking the https://osgi.org/javadoc/r4v42/org/osgi/service/component/ComponentContext.html#getProperties() method is the supported way to dynamically determine properties values used within OSGi.
Views
Replies
Total Likes
You can also do something like
@Activate protected void activate(final Map<String, Object> config) { String myvar = PropertiesUtil.toString(config.get(PROP_NAME),DEFAULT_VALUE); }
Views
Replies
Total Likes
Hi,
I use code like this:
@Activate protected void activate (ComponentContext ctx) { String myvar = PropertyUtils.toString (ctx.getProperties().get("myvar"), "defaultValue"); }
The PropertyUtils helper is very useful, as it provides many conversion tools including the support for a default value, if the property is not provided or empty.
Views
Replies
Total Likes
Views
Likes
Replies