Class ConfigSupport

  • All Implemented Interfaces:
    ConfigurationUtilities

    @Service
    public class ConfigSupport
    extends Object
    implements ConfigurationUtilities

    Helper class to execute some code on configuration objects while taking care of the transaction boiler plate code.

    Programmers that wish to apply some changes to configuration objects can use these convenience methods to reduce the complexity of handling transactions.

    For instance, say a programmer need to change the HttpListener port from 8080 to 8989, it just needs to do :

         ... in his code somewhere ...
         HttpListener httpListener = domain.get...
    
         // If the programmer tries to modify the httpListener directly
         // it will get an exception
         httpListener.setPort("8989"); // will generate a PropertyVetoException
    
         // instead he needs to use a transaction and can use the helper services
         ConfigSupport.apply(new SingleConfigCode() {
             public Object run(HttpListener okToChange) throws PropertyException {
                 okToChange.setPort("8989"); // good...
                 httpListener.setPort("7878"); // not good, exceptions still raised...
                 return null;
             });
    
         // Note that after this code
         System.out.println("Port is " + httpListener.getPort());
         // will display 8989
     }
     
    Author:
    Jerome Dochez
    • Field Detail

      • lockTimeOutInSeconds

        public static int lockTimeOutInSeconds
    • Constructor Detail

      • ConfigSupport

        public ConfigSupport()
    • Method Detail

      • apply

        public static <T extends ConfigBeanProxyObject apply​(SingleConfigCode<T> code,
                                                               T param)
                                                        throws TransactionFailure
        Execute some logic on one config bean of type T protected by a transaction
        Parameters:
        code - code to execute
        param - config object participating in the transaction
        Returns:
        list of events that represents the modified config elements.
        Throws:
        TransactionFailure - when code did not run successfully
      • apply

        public static Object apply​(ConfigCode code,
                                   ConfigBeanProxy... objects)
                            throws TransactionFailure
        Executes some logic on some config beans protected by a transaction.
        Parameters:
        code - code to execute
        objects - config beans participating to the transaction
        Returns:
        list of property change events
        Throws:
        TransactionFailure - when the code did run successfully due to a transaction exception
      • _apply

        public Object _apply​(ConfigCode code,
                             ConfigBeanProxy... objects)
                      throws TransactionFailure
        Executes some logic on some config beans protected by a transaction.
        Parameters:
        code - code to execute
        objects - config beans participating to the transaction
        Returns:
        list of property change events
        Throws:
        TransactionFailure - when the code did run successfully due to a transaction exception
      • getWriteableView

        public <T extends ConfigBeanProxy> T getWriteableView​(T source)
                                                       throws TransactionFailure
        Returns a writeable view of a configuration object
        Parameters:
        source - the configured interface implementation
        Returns:
        the new interface implementation providing write access
        Throws:
        TransactionFailure - if the object cannot be enrolled (probably already enrolled in another transaction).
      • getImpl

        public static ConfigView getImpl​(ConfigBeanProxy source)
        Return the main implementation bean for a proxy.
        Parameters:
        source - configuration interface proxy
        Returns:
        the implementation bean
      • proxyType

        public static <T extends ConfigBeanProxyClass<T> proxyType​(T element)
        Returns the type of configuration object this config proxy represents.
        Parameters:
        element - is the configuration object
        Returns:
        the configuration interface class
      • sortAndDispatch

        public static UnprocessedChangeEvents sortAndDispatch​(PropertyChangeEvent[] events,
                                                              Changed target,
                                                              Logger logger)
        sort events and dispatch the changes. There will be only one notification of event per event type, per object, meaning that if an object has had 3 attributes changes, the Changed interface implementation will get notified only once.
        Parameters:
        events - of events that resulted of a successful configuration transaction
        target - the intended receiver of the changes notification
        logger - to log any issues.
      • getSubElementsTypes

        public static Class<?>[] getSubElementsTypes​(ConfigBean bean)
                                              throws ClassNotFoundException
        Returns the list of sub-elements supported by a ConfigBean
        Returns:
        array of classes reprensenting the sub elements of a particular
        Throws:
        ClassNotFoundException - for severe errors with the model associated with the passed config bean.
      • getAttributesNames

        public String[] getAttributesNames​(ConfigBean bean)
        Returns the list of attributes names by the passed ConfigBean
        Returns:
        array of String for all the attributes names
      • getElementsNames

        public String[] getElementsNames​(ConfigBean bean)
        Returns the list of elements names by the passed ConfigBean
        Returns:
        array of String for all the elements names
      • createAndSet

        public static ConfigBean createAndSet​(ConfigBean parent,
                                              Class<? extends ConfigBeanProxy> childType,
                                              Map<String,​String> attributes,
                                              TransactionCallBack<WriteableView> runnable)
                                       throws TransactionFailure
        Creates a new child of the passed child and add it to the parent's live list of elements. The child is also initialized with the attributes passed where each key represent the xml property name for the attribute and the value represent the attribute's value. This code will be executed within a Transaction and can therefore throw a TransactionFailure when the creation or settings of attributes failed. Example creating a new http-listener element under http-service ConfigBean httpService = ... // got it from somwhere. Map attributes = new HashMap(); attributes.put("id", "jerome-listener"); attributes.put("enabled", "true"); ConfigSupport.createAndSet(httpService, HttpListener.class, attributes);
        Parameters:
        parent - parent config bean to which the child will be added.
        childType - child type
        attributes - map of key value pair to set on the newly created child
        Throws:
        TransactionFailure - if the creation or attribute settings failed
      • createAndSet

        public static ConfigBean createAndSet​(ConfigBean parent,
                                              Class<? extends ConfigBeanProxy> childType,
                                              List<AttributeChanges> attributes,
                                              TransactionCallBack<WriteableView> runnable)
                                       throws TransactionFailure
        Creates a new child of the passed child and add it to the parent's live list of elements. The child is also initialized with the attributes passed where each key represent the xml property name for the attribute and the value represent the attribute's value. This code will be executed within a Transaction and can therefore throw a TransactionFailure when the creation or settings of attributes failed. Example creating a new http-listener element under http-service ConfigBean httpService = ... // got it from somwhere. Map attributes = new HashMap(); attributes.put("id", "jerome-listener"); attributes.put("enabled", "true"); ConfigSupport.createAndSet(httpService, HttpListener.class, attributes);
        Parameters:
        parent - parent config bean to which the child will be added.
        childType - child type
        attributes - list of attribute changes to apply to the newly created child
        runnable - code that will be invoked as part of the transaction to add more attributes or elements to the newly create type
        Throws:
        TransactionFailure - if the creation or attribute settings failed
      • createAndSet

        public static ConfigBean createAndSet​(ConfigBean parent,
                                              Class<? extends ConfigBeanProxy> childType,
                                              Map<String,​String> attributes)
                                       throws TransactionFailure
        Creates a new child of the passed child and add it to the parent's live list of elements. The child is also initialized with the attributes passed where each key represent the xml property name for the attribute and the value represent the attribute's value. This code will be executed within a Transaction and can therefore throw a TransactionFailure when the creation or settings of attributes failed. Example creating a new http-listener element under http-service ConfigBean httpService = ... // got it from somwhere. Map attributes = new HashMap(); attributes.put("id", "jerome-listener"); attributes.put("enabled", "true"); ConfigSupport.createAndSet(httpService, HttpListener.class, attributes);
        Parameters:
        parent - parent config bean to which the child will be added.
        childType - child type
        attributes - list of attributes changes to apply to the new created child
        Throws:
        TransactionFailure - if the creation or attribute settings failed
      • _deleteChild

        public static void _deleteChild​(ConfigBean parent,
                                        WriteableView writeableParent,
                                        ConfigBean child)
                                 throws TransactionFailure
        Unprotected child deletion, caller must start a transaction before calling this method.
        Parameters:
        parent - the parent element
        writeableParent - the writeable view of the parent element
        child - the child to delete
        Throws:
        TransactionFailure - if something goes wrong.
      • revealProxy

        public static <T extends ConfigBeanProxy> T revealProxy​(T proxy)
        Unwrap HK2 proxy to ConfigBeanProxy.
        Parameters:
        ConfigBeanProxy - probably proxied by HK2.
        Returns:
        actual ConfigBeanProxy.
        Throws:
        org.glassfish.hk2.api.MultiException - If there was an error resolving the proxy.