Package org.jvnet.hk2.config
Class ConfigSupport
- java.lang.Object
-
- org.jvnet.hk2.config.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
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
ConfigSupport.MultipleAttributeChanges
static class
ConfigSupport.SingleAttributeChange
-
Field Summary
Fields Modifier and Type Field Description static int
lockTimeOutInSeconds
-
Constructor Summary
Constructors Constructor Description ConfigSupport()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description 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.Object
addChildWithAttributes(ConfigBeanProxy param, ConfigBean parent, Class<? extends ConfigBeanProxy> childType, List<AttributeChanges> attributes, TransactionCallBack<WriteableView> runnable)
Adds this child bean to the parent with the given attributes.void
apply(Map<ConfigBean,Map<String,String>> mapOfChanges)
static Object
apply(ConfigCode code, ConfigBeanProxy... objects)
Executes some logic on some config beans protected by a transaction.static <T extends ConfigBeanProxy>
Objectapply(SingleConfigCode<T> code, T param)
Execute some logic on one config bean of type T protected by a transactionstatic List<AttributeChanges>
convertMapToAttributeChanges(Map<String,String> values)
ConfigBean
createAndSet(ConfigBean parent, Class<? extends ConfigBeanProxy> childType, List<AttributeChanges> attributes)
static ConfigBean
createAndSet(ConfigBean parent, Class<? extends ConfigBeanProxy> childType, List<AttributeChanges> attributes, 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, 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 ConfigBeanString[]
getElementsNames(ConfigBean bean)
Returns the list of elements names by the passed ConfigBeanstatic 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>
TgetWriteableView(T source)
Returns a writeable view of a configuration objectstatic <T extends ConfigBeanProxy>
Class<T>proxyType(T element)
Returns the type of configuration object this config proxy represents.static <T extends ConfigBeanProxy>
TrevealProxy(T proxy)
Unwrap HK2 proxy to ConfigBeanProxy.static UnprocessedChangeEvents
sortAndDispatch(PropertyChangeEvent[] events, Changed target, Logger logger)
sort events and dispatch the changes.
-
-
-
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 executeparam
- 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 executeobjects
- 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 executeobjects
- 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 transactiontarget
- the intended receiver of the changes notificationlogger
- 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, 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. Mapattributes = 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 typeattributes
- 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. Mapattributes = 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 typeattributes
- list of attribute changes to apply to the newly created childrunnable
- 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. Mapattributes = 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 typeattributes
- 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<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 elementwriteableParent
- the writeable view of the parent elementchild
- the child to delete- Throws:
TransactionFailure
- if something goes wrong.
-
convertMapToAttributeChanges
public static List<AttributeChanges> convertMapToAttributeChanges(Map<String,String> values)
-
getElementTypeByName
public static Class<? extends ConfigBeanProxy> getElementTypeByName(ConfigBeanProxy parent, String elementName) throws ClassNotFoundException
- Throws:
ClassNotFoundException
-
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.
-
addChildWithAttributes
public Object addChildWithAttributes(ConfigBeanProxy param, ConfigBean parent, Class<? extends ConfigBeanProxy> childType, List<AttributeChanges> attributes, TransactionCallBack<WriteableView> runnable) throws TransactionFailure
Description copied from interface:ConfigurationUtilities
Adds this child bean to the parent with the given attributes. Does not start or end a configuration transaction- Specified by:
addChildWithAttributes
in interfaceConfigurationUtilities
- Returns:
- Throws:
TransactionFailure
-
-