|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.clazzes.util.osgi.ConfigPropertyAccessor
public class ConfigPropertyAccessor
Helper class for accessing the properties from configuration files in OSGi. Needs to have the configuration Pid
and the ConfigurationAdmin
for the bundle injected, and then functions as a bridge to access individual
properties from the OSGi-blueprint xml-file. Example:
<type-converters> <bean id="configPropertyConverter" class="org.clazzes.util.osgi.ConfigPropertyConverter"> <argument ref="blueprintConverter"></argument> </bean> </type-converters> <reference id="configurationAdmin" interface="org.osgi.service.cm.ConfigurationAdmin"></reference> <bean id="configProps" class="orc.clazzes.util.osgi.ConfigPropertyAccessor"> <property name="configurationAdmin" ref="configurationAdmin"/> <property name="configurationPid" value="org.example.ConfigurationPid"/> </bean> <bean id="jdbcUrl" factory-ref="configProps" factory-method="getProperty"> <argument value="org.example.jdbc.url"/> </bean>
The actual values returned by getProperty(String)
are of the type ConfigProperty
.
In order to inject such property values into bean properties, please register a ConfigPropertyConverter
instance in the type-converters
section of your blueprint xml files outlined above.
If you want your bundle to restarted upon change of the referenced configuration PID,
export the ConfigPropertyAccessor
bean as a ConfigurationListener
service
and inject the blueprintBundle built-in bean as in the following example.
<type-converters> <bean id="configPropertyConverter" class="org.clazzes.util.osgi.ConfigPropertyConverter"> <argument ref="blueprintConverter"></argument> </bean> </type-converters> <reference id="configurationAdmin" interface="org.osgi.service.cm.ConfigurationAdmin"></reference> <bean id="configProps" class="org.clazzes.util.osgi.ConfigPropertyAccessor"> <property name="configurationAdmin" ref="configurationAdmin"/> <property name="configurationPid" value="org.example.ConfigurationPid"/> <property name="blueprintBundle" ref="blueprintBundle"/> </bean> <service id="org.osgi.service.cm.ConfigurationListener" interface="org.osgi.service.cm.ConfigurationListener" ref="configProps"> </service> <bean id="jdbcUrl" factory-ref="configProps" factory-method="getProperty"> <argument value="org.example.jdbc.url"/> </bean>
To set up a configuration file from default values, you must add createDefaultConfig()
as an
init-method
. Please refer to the method documentation for details.
Constructor Summary | |
---|---|
ConfigPropertyAccessor()
|
Method Summary | |
---|---|
void |
configurationEvent(org.osgi.service.cm.ConfigurationEvent event)
|
void |
createDefaultConfig()
An optional initialization method, which may be called as the bean's init-method . |
org.osgi.framework.Bundle |
getBlueprintBundle()
|
org.osgi.service.cm.ConfigurationAdmin |
getConfigurationAdmin()
|
java.lang.String |
getConfigurationPid()
|
java.util.Map<java.lang.String,java.lang.String> |
getDefaultValues()
|
java.lang.String |
getMandatoryPlaceholderValue()
|
java.util.Dictionary<java.lang.String,java.lang.String> |
getProperties()
Direct accessor to the properties dictionary as read from the configuration file, or the default values (if present). |
ConfigProperty |
getProperty(java.lang.String propName)
Returns the property for the given propertyName , read either from the configuration file
or the default values, wrapped in a ConfigProperty instance. |
void |
setBlueprintBundle(org.osgi.framework.Bundle blueprintBundle)
|
void |
setConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin configurationAdmin)
|
void |
setConfigurationPid(java.lang.String configurationPid)
|
void |
setDefaultValues(java.util.Map<java.lang.String,java.lang.String> defaultValues)
Use to set the default configuration values for the given PID. |
void |
setMandatoryPlaceholderValue(java.lang.String mandatoryPlaceholderValue)
This placeholder is written to the configuration file if a null value is set in the default Values. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public ConfigPropertyAccessor()
Method Detail |
---|
public void createDefaultConfig() throws java.io.IOException, org.osgi.service.blueprint.container.ComponentDefinitionException
An optional initialization method, which may be called as the bean's init-method
.
This method checks the existing configuration against default values set in setDefaultValues(Map)
and fills in missing properties with these defaults, or with the placeholder value configured by
setMandatoryPlaceholderValue(String)
. If at least one default value has been added, this method
triggers an update on the configuration with the configured PID.
If a property was set to the placeholder value, the method throws a ComponentDefinitionException
to
prevent the bundle from fully starting in an insufficiently configured state.
To run this method at bundle initialization, you must add the init-method
attribute in the
blueprint xml-definition file. Additionaly, the defaultValues
and the
mandatoryPlaceholderValue
must be set, as in the following example:
<bean id="configProps" class="at.ev_i.tis.graph.core.activation.ConfigPropertyAccessor" init-method="createDefaultConfig"> <property name="configurationAdmin" ref="configurationAdmin"/> <property name="configurationPid" value="org.example.ConfigurationPid"/> <property name="blueprintBundle" ref="blueprintBundle"/> </bean>
java.io.IOException
- if unable to load configuration
org.osgi.service.blueprint.container.ComponentDefinitionException
- if the config file has been populated with placeholder valuespublic void configurationEvent(org.osgi.service.cm.ConfigurationEvent event)
configurationEvent
in interface org.osgi.service.cm.ConfigurationListener
public java.util.Dictionary<java.lang.String,java.lang.String> getProperties() throws java.io.IOException
Dictionary
java.io.IOException
public ConfigProperty getProperty(java.lang.String propName) throws java.io.IOException
propertyName
, read either from the configuration file
or the default values, wrapped in a ConfigProperty
instance.
propName
- the key for the configuration parameter
ConfigProperty
java.io.IOException
public java.lang.String getConfigurationPid()
public void setConfigurationPid(java.lang.String configurationPid)
configurationPid
- the configurationPid to setpublic java.util.Map<java.lang.String,java.lang.String> getDefaultValues()
public void setDefaultValues(java.util.Map<java.lang.String,java.lang.String> defaultValues)
defaultValues
- The default values to set.createDefaultConfig()
public java.lang.String getMandatoryPlaceholderValue()
public void setMandatoryPlaceholderValue(java.lang.String mandatoryPlaceholderValue)
null
value is set in the default Values.
CAUTION: this value should be an invalid configuration value, because createDefaultConfig()
throws an exception if it finds it in the configuration file.
mandatoryPlaceholderValue
- the mandatoryPlaceholderValue to setcreateDefaultConfig()
public org.osgi.service.cm.ConfigurationAdmin getConfigurationAdmin()
public void setConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin configurationAdmin)
configurationAdmin
- the configurationAdmin to setpublic org.osgi.framework.Bundle getBlueprintBundle()
public void setBlueprintBundle(org.osgi.framework.Bundle blueprintBundle)
blueprintBundle
- the blueprintBundle to set
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |