org.jvnet.hk2.config
Class ConfigSupport

java.lang.Object
  extended by org.jvnet.hk2.config.ConfigSupport

@Service
public class ConfigSupport
extends Object

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

Nested Class Summary
static class ConfigSupport.AttributeChanges
           
static class ConfigSupport.MultipleAttributeChanges
           
static class ConfigSupport.SingleAttributeChange
           
static interface ConfigSupport.TransactionCallBack<T>
           
 
Field Summary
static int lockTimeOutInSeconds
           
 
Constructor Summary
ConfigSupport()
           
 
Method Summary
 Object _apply(ConfigCode code, ConfigBeanProxy... objects)
          Executes some logic on some config beans protected by a transaction.
static void _deleteChild(ConfigBean parent, WriteableView writeableParent, ConfigBean child)
          Unprotected child deletion, caller must start a transaction before calling this method.
static Object apply(ConfigCode code, ConfigBeanProxy... objects)
          Executes some logic on some config beans protected by a transaction.
 void apply(Map<ConfigBean,Map<String,String>> mapOfChanges)
           
static
<T extends ConfigBeanProxy>
Object
apply(SingleConfigCode<T> code, T param)
          Execute some logic on one config bean of type T protected by a transaction
 ConfigBean createAndSet(ConfigBean parent, Class<? extends ConfigBeanProxy> childType, List<ConfigSupport.AttributeChanges> attributes)
           
static ConfigBean createAndSet(ConfigBean parent, Class<? extends ConfigBeanProxy> childType, List<ConfigSupport.AttributeChanges> attributes, ConfigSupport.TransactionCallBack<WriteableView> runnable)
          Creates a new child of the passed child and add it to the parent's live list of elements.
static ConfigBean createAndSet(ConfigBean parent, Class<? extends ConfigBeanProxy> childType, Map<String,String> attributes)
          Creates a new child of the passed child and add it to the parent's live list of elements.
static ConfigBean createAndSet(ConfigBean parent, Class<? extends ConfigBeanProxy> childType, Map<String,String> attributes, ConfigSupport.TransactionCallBack<WriteableView> runnable)
          Creates a new child of the passed child and add it to the parent's live list of elements.
static void deleteChild(ConfigBean parent, ConfigBean child)
           
 String[] getAttributesNames(ConfigBean bean)
          Returns the list of attributes names by the passed ConfigBean
 String[] getElementsNames(ConfigBean bean)
          Returns the list of elements names by the passed ConfigBean
static Class<? extends ConfigBeanProxy> getElementTypeByName(ConfigBeanProxy parent, String elementName)
           
static ConfigView getImpl(ConfigBeanProxy source)
          Return the main implementation bean for a proxy.
static Class<?>[] getSubElementsTypes(ConfigBean bean)
          Returns the list of sub-elements supported by a ConfigBean
<T extends ConfigBeanProxy>
T
getWriteableView(T source)
          Returns a writeable view of a configuration object
static
<T extends ConfigBeanProxy>
Class<T>
proxyType(T element)
          Returns the type of configuration object this config proxy represents.
static UnprocessedChangeEvents sortAndDispatch(PropertyChangeEvent[] events, Changed target, Logger logger)
          sort events and dispatch the changes.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

lockTimeOutInSeconds

public static int lockTimeOutInSeconds
Constructor Detail

ConfigSupport

public ConfigSupport()
Method Detail

apply

public static <T extends ConfigBeanProxy> Object 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 ConfigBeanProxy> Class<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.

apply

public void apply(Map<ConfigBean,Map<String,String>> mapOfChanges)
           throws TransactionFailure
Throws:
TransactionFailure

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,
                                      ConfigSupport.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<ConfigSupport.AttributeChanges> attributes,
                                      ConfigSupport.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

createAndSet

public ConfigBean createAndSet(ConfigBean parent,
                               Class<? extends ConfigBeanProxy> childType,
                               List<ConfigSupport.AttributeChanges> attributes)
                        throws TransactionFailure
Throws:
TransactionFailure

deleteChild

public static void deleteChild(ConfigBean parent,
                               ConfigBean child)
                        throws TransactionFailure
Throws:
TransactionFailure

_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.

getElementTypeByName

public static Class<? extends ConfigBeanProxy> getElementTypeByName(ConfigBeanProxy parent,
                                                                    String elementName)
                                                             throws ClassNotFoundException
Throws:
ClassNotFoundException


Copyright © 2013 Oracle Corporation. All Rights Reserved.