use of org.jvnet.hk2.config.WriteableView in project Payara by payara.
the class TransactionCallBackTest method doTest.
public void doTest() throws TransactionFailure {
ConfigBean serviceBean = (ConfigBean) ConfigBean.unwrap(habitat.<NetworkListeners>getService(NetworkListeners.class));
Map<String, String> configChanges = new HashMap<String, String>();
configChanges.put("name", "funky-listener");
ConfigSupport.createAndSet(serviceBean, NetworkListener.class, configChanges, new TransactionCallBack<WriteableView>() {
@SuppressWarnings({ "unchecked" })
public void performOn(WriteableView param) throws TransactionFailure {
// if you know the type...
NetworkListener listener = param.getProxy(NetworkListener.class);
listener.setName("Aleksey");
// if you don't know the type
Method m;
try {
m = param.getProxyType().getMethod("setAddress", String.class);
m.invoke(param.getProxy(param.getProxyType()), "localhost");
} catch (NoSuchMethodException e) {
throw new TransactionFailure("Cannot find getProperty method", e);
} catch (IllegalAccessException e) {
throw new TransactionFailure("Cannot call getProperty method", e);
} catch (InvocationTargetException e) {
throw new TransactionFailure("Cannot call getProperty method", e);
}
}
});
}
use of org.jvnet.hk2.config.WriteableView in project Payara by payara.
the class WriteableView method removeNestedElements.
boolean removeNestedElements(Object object) {
InvocationHandler h = Proxy.getInvocationHandler(object);
if (!(h instanceof WriteableView)) {
// h instanceof ConfigView
ConfigBean bean = (ConfigBean) ((ConfigView) h).getMasterView();
h = bean.getWriteableView();
if (h == null) {
ConfigBeanProxy writable;
try {
writable = currentTx.enroll((ConfigBeanProxy) object);
} catch (TransactionFailure e) {
// something is seriously wrong
throw new RuntimeException(e);
}
h = Proxy.getInvocationHandler(writable);
} else {
// so oldValue was already processed
return false;
}
}
WriteableView writableView = (WriteableView) h;
synchronized (writableView) {
writableView.isDeleted = true;
}
boolean removed = false;
for (Property property : writableView.bean.model.elements.values()) {
if (property.isCollection()) {
Object nested = writableView.getter(property, parameterizedType);
ProtectedList<?> list = (ProtectedList<?>) nested;
if (list.size() > 0) {
list.clear();
removed = true;
}
} else if (!property.isLeaf()) {
// Element
Object oldValue = writableView.getter(property, ConfigBeanProxy.class);
if (oldValue != null) {
writableView.setter(property, null, Dom.class);
removed = true;
removeNestedElements(oldValue);
}
}
}
return removed;
}
use of org.jvnet.hk2.config.WriteableView in project Payara by payara.
the class SetCommand method setLeafElement.
public static void setLeafElement(final ConfigBean node, final String elementName, final String values) throws TransactionFailure {
ConfigBeanProxy readableView = node.getProxy(node.getProxyType());
ConfigSupport.apply(new SingleConfigCode<ConfigBeanProxy>() {
/**
* Runs the following command passing the configuration object. The
* code will be run within a transaction, returning true will commit
* the transaction, false will abort it.
*
* @param param is the configuration object protected by the
* transaction
* @return any object that should be returned from within the
* transaction code
* @throws java.beans.PropertyVetoException if the changes cannot be
* applied to the configuration
*/
@Override
public Object run(ConfigBeanProxy param) throws PropertyVetoException, TransactionFailure {
WriteableView writeableParent = (WriteableView) Proxy.getInvocationHandler(param);
StringTokenizer st = new StringTokenizer(values, ",");
List<String> valList = new ArrayList<>();
while (st.hasMoreTokens()) {
valList.add(st.nextToken());
}
ConfigBean bean = writeableParent.getMasterView();
for (Method m : writeableParent.getProxyType().getMethods()) {
// Check to see if the method is a setter for the element
// An element setter has to have the right name, take a single
// collection parameter that parameterized with the right type
Class[] argClasses = m.getParameterTypes();
Type[] argTypes = m.getGenericParameterTypes();
ConfigModel.Property prop = bean.model.toProperty(m);
if (prop == null || !prop.xmlName().equals(elementName) || argClasses.length != 1 || !Collection.class.isAssignableFrom(argClasses[0]) || argTypes.length != 1 || !(argTypes[0] instanceof ParameterizedType) || !Types.erasure(Types.getTypeArgument(argTypes[0], 0)).isAssignableFrom(values.getClass())) {
continue;
}
// we have the right method. Now call it
try {
m.invoke(writeableParent.getProxy(writeableParent.<ConfigBeanProxy>getProxyType()), valList);
} catch (IllegalAccessException | InvocationTargetException e) {
throw new TransactionFailure("Exception while setting element", e);
}
return node;
}
throw new TransactionFailure("No method found for setting element");
}
}, readableView);
}
Aggregations