Search in sources :

Example 1 with WriteableView

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);
            }
        }
    });
}
Also used : TransactionFailure(org.jvnet.hk2.config.TransactionFailure) HashMap(java.util.HashMap) ConfigBean(org.jvnet.hk2.config.ConfigBean) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) WriteableView(org.jvnet.hk2.config.WriteableView) NetworkListeners(org.glassfish.grizzly.config.dom.NetworkListeners) NetworkListener(org.glassfish.grizzly.config.dom.NetworkListener)

Example 2 with WriteableView

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;
}
Also used : Property(org.jvnet.hk2.config.ConfigModel.Property)

Example 3 with WriteableView

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);
}
Also used : TransactionFailure(org.jvnet.hk2.config.TransactionFailure) ConfigBean(org.jvnet.hk2.config.ConfigBean) Method(java.lang.reflect.Method) PropertyVetoException(java.beans.PropertyVetoException) ParameterizedType(java.lang.reflect.ParameterizedType) WriteableView(org.jvnet.hk2.config.WriteableView) StringTokenizer(java.util.StringTokenizer) ConfigBeanProxy(org.jvnet.hk2.config.ConfigBeanProxy) List(java.util.List) ArrayList(java.util.ArrayList) Property(org.jvnet.hk2.config.types.Property)

Aggregations

Method (java.lang.reflect.Method)2 ConfigBean (org.jvnet.hk2.config.ConfigBean)2 TransactionFailure (org.jvnet.hk2.config.TransactionFailure)2 WriteableView (org.jvnet.hk2.config.WriteableView)2 PropertyVetoException (java.beans.PropertyVetoException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 StringTokenizer (java.util.StringTokenizer)1 NetworkListener (org.glassfish.grizzly.config.dom.NetworkListener)1 NetworkListeners (org.glassfish.grizzly.config.dom.NetworkListeners)1 ConfigBeanProxy (org.jvnet.hk2.config.ConfigBeanProxy)1 Property (org.jvnet.hk2.config.ConfigModel.Property)1 Property (org.jvnet.hk2.config.types.Property)1