Home
Categories
Dictionary
Download
Project Details
Changes Log
Tutorials
FAQ
License

Accessing configuration properties by name



The default way of accessing configuration properties for a Plugin is done directly from the Plugin configuation instance. However it is possible to get or set the properties values by their names. This is especially useful when you want to use named configurations, where one configuration can be shared by several Plugins.

For the moment accessing configurations properties by their names is not supported for the application configuration. It is only supported for Plugins.

Default way to access properties

The default way of accessing configuration properties for a Plugin is done directly from the Plugin configuation instance.

For example for this configuration class:
   public class OpenPNGImagePluginConfiguration implements Configuration {
      private static OpenPNGImagePluginConfiguration conf = null;
      public int maxWidth = 1000;
      
      private OpenPNGImagePluginConfiguration() {
      }

      public static OpenPNGImagePluginConfiguration getInstance() {
        if (conf == null) {
          conf = new OpenPNGImagePluginConfiguration();
        }
        return conf;
      }      
   }
You can just perform:
   OpenPNGImagePluginConfiguration conf = OpenPNGImagePluginConfiguration.getInstance();
   int maxWidth = conf.maxWidth;

Implement the configuration API

Main Article: ConfigPropertiesHelper

The NamedConfiguration interface (which is a superinterface of the Configuration class) has an API which allow to get or set configuration propeties by their names.

You will need to implement the following methods[1]
Their default implementations do nothing
: The simplest way to implement these methods is to use the ConfigPropertiesHelper class. See also ConfigPropertiesHelper.

To use this without having a named configuration, just use null for the configuration name.


You don't need to implement this API for all Plugins. You can use the default way to access properties for some Plugins, and this API for others.

Example

The following example shows a Plugin configuration implementing the API for two propertyies:
   public class OpenPNGImagePluginConfiguration implements Configuration {
      private static OpenPNGImagePluginConfiguration conf = null;
      private Set<String> set = new HashSet<>();
      public int maxWidth = 1000;
      public boolean limitWidth = false;

      private OpenPNGImagePluginConfiguration() {
        set.add("maxWidth");
        set.add("limitWidth");      
      }

      public static OpenPNGImagePluginConfiguration getInstance() {
        if (conf == null) {
          conf = new OpenPNGImagePluginConfiguration();
        }
        return conf;
      }

      @Override
      public Set<String> getConfigurationPropertiesNames(String confName) {
        return set;
      }

      @Override
      public boolean hasConfigurationProperty(String propertyName, String confName) {
      return propertyName.equals("maxWidth") || propertyName.equals("limitWidth");
      }

      @Override
      public Object getConfigurationProperty(File dir, String propertyName, String confName) {
      switch (propertyName) {
        case "maxWidth":
          return maxWidth;
        case "limitWidth":
          return limitWidth;
        default:
          return null;
        }
      }

      @Override
      public Class getConfigurationPropertyType(String propertyName, String confName) {
        switch (propertyName) {
          case "maxWidth":
            return Integer.class;
          case "limitWidth":
            return Boolean.class;
          default:
            return null;
        }
      }

      @Override
      public void setConfigurationProperty(File dir, String propertyName, String confName, Object property) {
        switch (propertyName) {
          case "maxWidth":
            maxWidth = (Integer) property;
            break;
          case "limitWidth":
            limitWidth = (Boolean) property;
            break;
          }
      }
   }      

Example with a ConfigPropertiesHelper

The same example using a ConfigPropertiesHelper is shown in the ConfigPropertiesHelper article.

Accessing properties by name

There are two methods which allow to access te properties by name:

Serializing and deserializing the Plugin configuration


If you implemented the configuration API, you don't need to implement both the Configuration.putConfiguration(Preferences pref, File dir) and Configuration.getConfiguration(Preferences pref, File dir) methods, because the framework will do it for you.

Notes

  1. ^ Their default implementations do nothing

See also


Categories: Conf

Copyright 2006-2023 Herve Girod. All Rights Reserved. Documentation and source under the LGPL v2 and Apache 2.0 licences