org.clazzes.util.osgi
Class ConfigPropertyAccessor

java.lang.Object
  extended by org.clazzes.util.osgi.ConfigPropertyAccessor
All Implemented Interfaces:
org.osgi.service.cm.ConfigurationListener

public class ConfigPropertyAccessor
extends java.lang.Object
implements org.osgi.service.cm.ConfigurationListener

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

ConfigPropertyAccessor

public ConfigPropertyAccessor()
Method Detail

createDefaultConfig

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>
 

Throws:
java.io.IOException - if unable to load configuration
org.osgi.service.blueprint.container.ComponentDefinitionException - if the config file has been populated with placeholder values

configurationEvent

public void configurationEvent(org.osgi.service.cm.ConfigurationEvent event)
Specified by:
configurationEvent in interface org.osgi.service.cm.ConfigurationListener

getProperties

public java.util.Dictionary<java.lang.String,java.lang.String> getProperties()
                                                                      throws java.io.IOException
Direct accessor to the properties dictionary as read from the configuration file, or the default values (if present).

Returns:
properties from config file, default values or an empty Dictionary
Throws:
java.io.IOException

getProperty

public ConfigProperty getProperty(java.lang.String propName)
                           throws java.io.IOException
Returns the property for the given propertyName, read either from the configuration file or the default values, wrapped in a ConfigProperty instance.

Parameters:
propName - the key for the configuration parameter
Returns:
the corresponding value, wrapped in a ConfigProperty
Throws:
java.io.IOException

getConfigurationPid

public java.lang.String getConfigurationPid()
Returns:
the configurationPid

setConfigurationPid

public void setConfigurationPid(java.lang.String configurationPid)
Parameters:
configurationPid - the configurationPid to set

getDefaultValues

public java.util.Map<java.lang.String,java.lang.String> getDefaultValues()
Returns:
The default values.

setDefaultValues

public void setDefaultValues(java.util.Map<java.lang.String,java.lang.String> defaultValues)
Use to set the default configuration values for the given PID.

Parameters:
defaultValues - The default values to set.
See Also:
createDefaultConfig()

getMandatoryPlaceholderValue

public java.lang.String getMandatoryPlaceholderValue()
Returns:
the mandatoryPlaceholderValue

setMandatoryPlaceholderValue

public void setMandatoryPlaceholderValue(java.lang.String mandatoryPlaceholderValue)
This placeholder is written to the configuration file if a 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.

Parameters:
mandatoryPlaceholderValue - the mandatoryPlaceholderValue to set
See Also:
createDefaultConfig()

getConfigurationAdmin

public org.osgi.service.cm.ConfigurationAdmin getConfigurationAdmin()
Returns:
the configurationAdmin

setConfigurationAdmin

public void setConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin configurationAdmin)
Parameters:
configurationAdmin - the configurationAdmin to set

getBlueprintBundle

public org.osgi.framework.Bundle getBlueprintBundle()
Returns:
the blueprintBundle

setBlueprintBundle

public void setBlueprintBundle(org.osgi.framework.Bundle blueprintBundle)
Parameters:
blueprintBundle - the blueprintBundle to set


Copyright © 2010. All Rights Reserved.