Class PluginConfigDataHandler

  • All Implemented Interfaces:
    java.io.Serializable

    public class PluginConfigDataHandler
    extends java.lang.Object
    implements java.io.Serializable
    The PluginConfigDataHandler class wraps access to a Plugin configuration provided either as String (XML), Object (PluginConfig instance) ore byte array.
    The class will translate to either representation on demand and cache the results.

    Examples:

      PluginConfigDataHandler configuration = new PluginConfigDataHandler(xmlString, MyConfigClass.class);
    
      // triggers unmarshalling
      PluginConfig config = configuration.readConfig();
     
    or vice versa
      config = new PluginConfigDataHandler(config, MyConfigClass.class);
      // triggers marshalling
      String xml = config.readXml();
    
      // returns the cached XML representation
      String xml2 = config.readXml();
    
      // this will invalidate the XML String representation
      handle.writeConfig(anotherConfig);
    
      // so next time we get an updated XML String representation
      xml = config.readXml();
     
    See Also:
    Serialized Form
    • Constructor Summary

      Constructors 
      Constructor Description
      PluginConfigDataHandler​(byte[] data, java.lang.Class<?>... customClazz)
      Instantiates a new Plugin config data handler.
      PluginConfigDataHandler​(PluginConfig object, java.lang.Class<?>... customClazz)
      Instantiates a new Plugin config data handler.
      PluginConfigDataHandler​(java.lang.Class<?>... customClazz)
      Instantiates a new Plugin config data handler.
      PluginConfigDataHandler​(java.lang.String xml, java.lang.Class<?>... customClazz)
      Instantiates a new Plugin config data handler.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static ConfigFileProperties createDefaultProperties​(java.lang.String user, java.lang.String path)
      Factory method to create an empty default ConfigFileProperties.
      java.lang.Class<?>[] getCustomClass()
      Get the custom classes provided for this configuration
      java.lang.String getDebugString()
      Get internal debugging information for the data handler.
      ConfigFileProperties getProperties()
      Get properties.
      PluginConfigDataHandler.SourceType getSource()
      Get the source.
      PluginConfig readConfig()
      Initialize (if required) and return the PluginConfig representation of this configuration.
      byte[] readData()
      Initialize (if required) and return the byte array representation of this configuration.
      java.lang.String readXml()
      Initialize (if required) and return the XML string representation of this configuration.
      Please note:
      Changing this string does not change the content of the data handler, unless you write the string back to the data handler by calling writeXml(String).
      Example:
      void setCustomClass​(java.lang.Class<?>... customClazz)
      Set the custom classes required for serialization / deserialization and invalidate all calculated representations of this configuration.
      Examples if the original source of this data handler is a PluginConfig object, the XML string and byte array representations are nulled, because different custom classes might affect marshalling of this object if the original source of this data handler is a XML string or byte array, the PluginConfig representation is nulled, because different custom classes might affect unmarshalling of this data Nothing will happen, if the classes provided are "superset" of the custom classed already defined for this data handler.
      void setProperties​(ConfigFileProperties properties)
      Set properties.
      void setSource​(PluginConfigDataHandler.SourceType source)
      Set the source.
      java.lang.String toString()  
      void writeConfig​(PluginConfig config)
      Change the source of this data handler to the PluginConfig object provided and invalidate all other representations (xml, byte array) of this configuration.
      void writeData​(byte[] data)
      Change the source of this data handler to the byte array provided and invalidate all other representations (PluginConfig, XML string) of this configuration.
      void writeXml​(java.lang.String xml)
      Change the source of this data handler to the XML string provided and invalidate all other representations (PluginConfig, byte array) of this configuration.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • PluginConfigDataHandler

        public PluginConfigDataHandler​(java.lang.String xml,
                                       java.lang.Class<?>... customClazz)
        Instantiates a new Plugin config data handler.
        Parameters:
        xml - the xml
        customClazz - the custom clazz
      • PluginConfigDataHandler

        public PluginConfigDataHandler​(PluginConfig object,
                                       java.lang.Class<?>... customClazz)
        Instantiates a new Plugin config data handler.
        Parameters:
        object - the object
        customClazz - the custom clazz
      • PluginConfigDataHandler

        public PluginConfigDataHandler​(byte[] data,
                                       java.lang.Class<?>... customClazz)
        Instantiates a new Plugin config data handler.
        Parameters:
        data - the data
        customClazz - the custom clazz
      • PluginConfigDataHandler

        public PluginConfigDataHandler​(java.lang.Class<?>... customClazz)
        Instantiates a new Plugin config data handler.
        Parameters:
        customClazz - the custom clazz
    • Method Detail

      • readConfig

        public PluginConfig readConfig()
                                throws javax.xml.bind.JAXBException
        Initialize (if required) and return the PluginConfig representation of this configuration.
        • if the original source of this data handler is a PluginConfig object, the method just returns the object provided upon initialization. Be aware that changing this object does change the content of the data handler in this case.
        • if the original source of this data handler is a XML string or byte array, a newly unmarshalled or cached object is returned. Be aware that changing this object does not change the content of the data handler in this case, unless you write the changed object back by calling writeConfig(PluginConfig)
        Since this turned out to be a common pitfall, it's good practice to follow this pattern:
          try {
                PluginConfigDataHandler dataHandler;
                PluginConfig config = dataHandler.readConfig();
        
                // change common PluginConfig properties
                config.setName("A descriptive name");
        
                MyCustomType custom = config.getCustomConfig(myCustomType.class);
        
                // change whatever required and supported for your custom type
                custom.setCustomValue(12345);
        
                // to be on the very safe side: update the PluginConfig wrapper
                config.replaceCustomConfig(custom);
        
                // finally write back to data handler
                dataHandler.writeConfig(config);
          }
          catch (JAXBException e) {
                // because the object might have been unmarshalled / marshalled
                // in the code fragment above ...
          }
         
        Returns:
        PluginConfig object
        Throws:
        javax.xml.bind.JAXBException - the jaxb exception
      • writeConfig

        public void writeConfig​(PluginConfig config)
        Change the source of this data handler to the PluginConfig object provided and invalidate all other representations (xml, byte array) of this configuration.
        Parameters:
        config - the new PluginConfig source
      • readXml

        public java.lang.String readXml()
                                 throws javax.xml.bind.JAXBException
        Initialize (if required) and return the XML string representation of this configuration.
        Please note:
        Changing this string does not change the content of the data handler, unless you write the string back to the data handler by calling writeXml(String).
        Example:
                try {
                        PluginConfigDataHandler dataHandler;
                        String xml = dataHandler.readXml();
        
                        // enter the next level of digital publishing:
                        xml = xml.replace("print", "priint");
        
                        // finally update content fo the data handler
                        dataHandler.writeXml(xml);
            }
                catch (JAXBEception e) {
            }
         
        Returns:
        XML string
        Throws:
        javax.xml.bind.JAXBException - the jaxb exception
      • writeXml

        public void writeXml​(java.lang.String xml)
        Change the source of this data handler to the XML string provided and invalidate all other representations (PluginConfig, byte array) of this configuration.
        Parameters:
        xml - the xml
      • readData

        public byte[] readData()
                        throws javax.xml.bind.JAXBException
        Initialize (if required) and return the byte array representation of this configuration.
        • if the original source of this data handler is a byte array, the method just returns the data provided upon initialization. Be aware that changing this array does change the content of the data handler in this case.
        • if the original source of this data handler is a XML string or PluginConfig object, a newly allocated or cached byte array is returned. Be aware that changing this array does not change the content of the data handler in this case, unless you write the changed data back by calling writeData(byte[])
        See the readConfig() method documentation for an example.
        Returns:
        binary data for the configuration
        Throws:
        javax.xml.bind.JAXBException - the jaxb exception
      • writeData

        public void writeData​(byte[] data)
        Change the source of this data handler to the byte array provided and invalidate all other representations (PluginConfig, XML string) of this configuration.
        Parameters:
        data - the data
      • getCustomClass

        public java.lang.Class<?>[] getCustomClass()
        Get the custom classes provided for this configuration
        Returns:
        list of custom classes
      • setCustomClass

        public void setCustomClass​(java.lang.Class<?>... customClazz)
        Set the custom classes required for serialization / deserialization and invalidate all calculated representations of this configuration.
        Examples
        • if the original source of this data handler is a PluginConfig object, the XML string and byte array representations are nulled, because different custom classes might affect marshalling of this object
        • if the original source of this data handler is a XML string or byte array, the PluginConfig representation is nulled, because different custom classes might affect unmarshalling of this data
        Nothing will happen, if the classes provided are "superset" of the custom classed already defined for this data handler. See the PubServerUtils documentation for more information.
        Parameters:
        customClazz - zero, one or several custom classes
      • getProperties

        public ConfigFileProperties getProperties()
        Get properties.
        Returns:
        properties properties
      • setProperties

        public void setProperties​(ConfigFileProperties properties)
        Set properties.
        Parameters:
        properties - the properties
      • createDefaultProperties

        public static ConfigFileProperties createDefaultProperties​(java.lang.String user,
                                                                   java.lang.String path)
        Factory method to create an empty default ConfigFileProperties.
        Parameters:
        user - the user
        path - the path
        Returns:
        new default properties object
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • getDebugString

        public java.lang.String getDebugString()
        Get internal debugging information for the data handler.
        Returns:
        debug information string