Changeset 6627


Ignore:
Timestamp:
Nov 25, 2014, 12:57:24 PM (8 years ago)
Author:
Nicklas Nordborg
Message:

Fixes #1891: Allow extensions to store settings in extension-settings.xml configuration file

Extensions should use ExtensionsControl to get/set settings. Settings that match factory configuration parameters are used ny the XmlLoader when creating factories. Eg. it will be possible to override the default settings in extensions.xml (but only if there at least is an empty placeholder to begin with).

Location:
trunk/src
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/clients/web/net/sf/basedb/clients/web/extensions/ExtensionsControl.java

    r6600 r6627  
    694694  }
    695695
     696 
     697  /**
     698    Get a configuration setting for an extension.
     699    @param extensionId The ID of the extension to save a setting for
     700    @param key The name of the setting
     701    @return The value or null if it doesn't exists
     702    @since 3.4
     703  */
     704  public String getSetting(String extensionId, String key)
     705  {
     706    return settings.getSetting(extensionId, key);
     707  }
     708 
     709  /**
     710    Set a configuration setting for an extension. Do not forget to
     711    {@link #saveSettings()}.
     712    @param extensionId The ID of the extension to save a setting for
     713    @param key The name of the setting
     714    @param value The value of the setting, use null to remove
     715    @since 3.4
     716  */
     717  public void setSetting(String extensionId, String key, String value)
     718  {
     719    checkPermission(Permission.WRITE, "Extension[id=" + extensionId + "]");
     720    settings.setSetting(extensionId, key, value);
     721  }
     722 
    696723  /**
    697724    Get an list returning all XML/JAR files which contains installed
  • trunk/src/core/net/sf/basedb/core/Presets.java

    r6473 r6627  
    201201 
    202202  /**
     203    Check if a preset with the given name exists.
     204    @since 3.4
     205  */
     206  public boolean exists(String name)
     207  {
     208    return presets.containsKey(name);
     209  }
     210 
     211  /**
    203212    Delete a named preset.
    204213    @param name The name of the preset to delete
     
    348357   
    349358    /**
     359      Get the number of settings in the preset.
     360      @since 3.4
     361    */
     362    public int size()
     363    {
     364      return settings.size();
     365    }
     366   
     367    /**
    350368      Get all keys in this preset. The returned list is a
    351369      copy of the settings and are not affected by later
  • trunk/src/core/net/sf/basedb/util/extensions/manager/Settings.java

    r5616 r6627  
    155155 
    156156  /**
     157    Get a preset containing settings for the given extesion. If no
     158    settings exists, null is returned. Do not make modifications to
     159    the returned preset since the result is undefined. Use
     160    {@link #setSetting(String, String, String)} to set values.
     161    @since 3.4
     162  */
     163  public Preset getSettingsForExtension(String extensionId)
     164  {
     165    return presets.exists(extensionId) ? presets.getPreset(extensionId) : null;
     166  }
     167 
     168  /**
     169    Set a configuration setting for an extension. Do not forget to
     170    {@link #save()}.
     171    @param extensionId The ID of the extension to save a setting for
     172    @param key The name of the setting
     173    @param value The value of the setting, use null to remove
     174    @since 3.4
     175  */
     176  public synchronized void setSetting(String extensionId, String key, String value)
     177  {
     178    if (value == null && !presets.exists(extensionId)) return;
     179   
     180    Preset p = presets.getPreset(extensionId);
     181    p.setSetting(key, value);
     182    if (p.size() == 0) presets.deletePreset(extensionId);
     183    hasChanged = true;
     184  }
     185
     186  /**
     187    Get a configuration setting for an extension.
     188    @param extensionId The ID of the extension to save a setting for
     189    @param key The name of the setting
     190    @return The value or null if it doesn't exists
     191    @since 3.4
     192  */
     193  public String getSetting(String extensionId, String key)
     194  {
     195    Preset p = getSettingsForExtension(extensionId);
     196    return p == null ? null : p.getSetting(key);
     197  }
     198 
     199  /**
    157200    Check if the given file is marked as installed.
    158201    @since 3.0
  • trunk/src/core/net/sf/basedb/util/extensions/xml/XmlLoader.java

    r6473 r6627  
    4343import org.jdom2.output.XMLOutputter;
    4444
     45import net.sf.basedb.core.Application;
    4546import net.sf.basedb.core.BaseException;
    4647import net.sf.basedb.core.InvalidUseOfNullException;
     48import net.sf.basedb.core.Presets.Preset;
    4749import net.sf.basedb.core.StringUtil;
    4850import net.sf.basedb.core.Version;
     
    608610        if (rfTag != null)
    609611        {
    610           RendererFactory rf = createFactory(rfTag, classLoader, RendererFactory.class);
     612          RendererFactory rf = createFactory(rfTag, classLoader, RendererFactory.class, null);
    611613          extensionPoint.setRendererFactory(rf);
    612614          extensionPoint.setAllowRendererOverrider(
     
    617619        if (ehfTag != null)
    618620        {
    619           ErrorHandlerFactory ehf = createFactory(ehfTag, classLoader, ErrorHandlerFactory.class);
     621          ErrorHandlerFactory ehf = createFactory(ehfTag, classLoader, ErrorHandlerFactory.class, null);
    620622          extensionPoint.setErrorHandlerFactory(ehf);
    621623        }
     
    745747          Element afTag = epTag.getChild("action-factory", ns);
    746748          ActionFactory af = null;
     749          Preset config = Application.getExtensionsManager().getSettings().getSettingsForExtension(id);
    747750          if (afTag != null)
    748751          {
    749             af = createFactory(afTag, classLoader, ActionFactory.class);
     752            af = createFactory(afTag, classLoader, ActionFactory.class, config);
    750753          }
    751754       
     
    755758          if (rfTag != null)
    756759          {
    757             rf = createFactory(rfTag, classLoader, RendererFactory.class);
     760            rf = createFactory(rfTag, classLoader, RendererFactory.class, config);
    758761          }
    759762         
     
    899902    @return An initialised factory
    900903  */
    901   protected <F> F createFactory(Element factoryTag, ClassLoader classLoader, Class<F> factoryType)
     904  protected <F> F createFactory(Element factoryTag, ClassLoader classLoader, Class<F> factoryType, Preset config)
    902905    throws ClassNotFoundException, NoSuchMethodException,
    903906      IllegalAccessException, InstantiationException
     
    908911      ClassUtil.checkAndLoadClass(classLoader, factoryClassName, true, factoryType);
    909912    F factory = factoryType.cast(factoryClass.newInstance());
    910     initBeanWithReflection(factory, factoryTag.getChild("parameters", ns));
     913    initBeanWithReflection(factory, factoryTag.getChild("parameters", ns), config);
    911914    return factory;
    912915  }
     
    927930    @param root The root element, if null nothing is done
    928931  */
    929   protected void initBeanWithReflection(Object bean, Element root)
     932  protected void initBeanWithReflection(Object bean, Element root, Preset config)
    930933  {
    931934    if (root == null) return;
     
    945948
    946949      // The value passed as an argument to the setter method
    947       String value = Values.getStringOrNull(child.getText());
     950      String value = config != null ? Values.getStringOrNull(config.getSetting(childName)) : null;
     951      if (value == null)
     952      {
     953        value = Values.getStringOrNull(child.getText());
     954      }
    948955     
    949956      if (value == null)
Note: See TracChangeset for help on using the changeset viewer.