Search in sources :

Example 51 with TransactionFailure

use of org.jvnet.hk2.config.TransactionFailure in project Payara by payara.

the class ParentConfigListenerTest method addHttpListenerTest.

@Test
public void addHttpListenerTest() throws TransactionFailure {
    NetworkListenersContainer container = habitat.getService(NetworkListenersContainer.class);
    ConfigSupport.apply(new SingleConfigCode<NetworkListeners>() {

        public Object run(NetworkListeners param) throws TransactionFailure {
            NetworkListener newListener = param.createChild(NetworkListener.class);
            newListener.setName("Funky-Listener");
            newListener.setPort("8078");
            param.getNetworkListener().add(newListener);
            return null;
        }
    }, container.httpService);
    getHabitat().<Transactions>getService(Transactions.class).waitForDrain();
    assertTrue(container.received);
    ObservableBean bean = (ObservableBean) ConfigSupport.getImpl(container.httpService);
    // let's check that my newly added listener is available in the habitat.
    List<ServiceHandle<NetworkListener>> networkListeners = habitat.getAllServiceHandles(NetworkListener.class);
    boolean found = false;
    for (ServiceHandle<NetworkListener> nlSH : networkListeners) {
        NetworkListener nl = (NetworkListener) nlSH.getService();
        if (nl.getName().equals("Funky-Listener")) {
            found = true;
        }
    }
    Assert.assertTrue("Newly added listener not found", found);
    // direct access.
    NetworkListener nl = habitat.getService(NetworkListener.class, "Funky-Listener");
    Assert.assertTrue("Direct access to newly added listener failed", nl != null);
    bean.removeListener(container);
}
Also used : TransactionFailure(org.jvnet.hk2.config.TransactionFailure) Transactions(org.jvnet.hk2.config.Transactions) ServiceHandle(org.glassfish.hk2.api.ServiceHandle) NetworkListeners(org.glassfish.grizzly.config.dom.NetworkListeners) ObservableBean(org.jvnet.hk2.config.ObservableBean) NetworkListener(org.glassfish.grizzly.config.dom.NetworkListener) Test(org.junit.Test)

Example 52 with TransactionFailure

use of org.jvnet.hk2.config.TransactionFailure 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 53 with TransactionFailure

use of org.jvnet.hk2.config.TransactionFailure in project Payara by payara.

the class FieldsValidationTest method testNotNullField.

@Test
public void testNotNullField() {
    AdminService admin = super.getHabitat().getService(AdminService.class);
    Assert.assertNotNull(admin);
    try {
        ConfigSupport.apply(new SingleConfigCode<AdminService>() {

            @Override
            public Object run(AdminService wAdmin) throws PropertyVetoException, TransactionFailure {
                wAdmin.setDasConfig(null);
                return null;
            }
        }, admin);
        Assert.fail("Exception not raised when setting a @NotNull annotated field with null");
    } catch (TransactionFailure e) {
        if (e.getCause() != null) {
            Assert.assertTrue(e.getCause() instanceof ConstraintViolationException);
        }
    }
}
Also used : PropertyVetoException(java.beans.PropertyVetoException) TransactionFailure(org.jvnet.hk2.config.TransactionFailure) AdminService(com.sun.enterprise.config.serverbeans.AdminService) ConstraintViolationException(javax.validation.ConstraintViolationException) ConfigApiTest(com.sun.enterprise.configapi.tests.ConfigApiTest) Test(org.junit.Test)

Example 54 with TransactionFailure

use of org.jvnet.hk2.config.TransactionFailure in project Payara by payara.

the class ReferenceConstrainClusterTest method clusterServerRefInvalid.

@Test
public void clusterServerRefInvalid() throws TransactionFailure {
    Cluster cluster = habitat.getService(Cluster.class, "clusterA");
    assertNotNull(cluster);
    ServerRef sref = cluster.getServerRef().get(0);
    ConfigBean serverConfig = (ConfigBean) ConfigBean.unwrap(sref);
    Map<ConfigBean, Map<String, String>> changes = new HashMap<ConfigBean, Map<String, String>>();
    Map<String, String> configChanges = new HashMap<String, String>();
    configChanges.put("ref", "server-nonexist");
    changes.put(serverConfig, configChanges);
    try {
        ConfigSupport cs = getHabitat().getService(ConfigSupport.class);
        cs.apply(changes);
        fail("Can not reach this point");
    } catch (TransactionFailure tf) {
        ConstraintViolationException cv = findConstrViolation(tf);
        assertNotNull(cv);
    }
}
Also used : TransactionFailure(org.jvnet.hk2.config.TransactionFailure) ConfigSupport(org.jvnet.hk2.config.ConfigSupport) HashMap(java.util.HashMap) Cluster(com.sun.enterprise.config.serverbeans.Cluster) ConstraintViolationException(javax.validation.ConstraintViolationException) ConfigBean(org.jvnet.hk2.config.ConfigBean) ServerRef(com.sun.enterprise.config.serverbeans.ServerRef) HashMap(java.util.HashMap) Map(java.util.Map) ConfigApiTest(com.sun.enterprise.configapi.tests.ConfigApiTest) Test(org.junit.Test)

Example 55 with TransactionFailure

use of org.jvnet.hk2.config.TransactionFailure in project Payara by payara.

the class ReferenceConstrainClusterTest method clusterServerRefValid.

@Test
public void clusterServerRefValid() throws TransactionFailure {
    Cluster cluster = habitat.getService(Cluster.class, "clusterA");
    assertNotNull(cluster);
    ServerRef sref = cluster.getServerRef().get(0);
    ConfigBean serverConfig = (ConfigBean) ConfigBean.unwrap(sref);
    Map<ConfigBean, Map<String, String>> changes = new HashMap<ConfigBean, Map<String, String>>();
    Map<String, String> configChanges = new HashMap<String, String>();
    configChanges.put("ref", "server");
    changes.put(serverConfig, configChanges);
    try {
        ConfigSupport cs = getHabitat().getService(ConfigSupport.class);
        cs.apply(changes);
    } catch (TransactionFailure tf) {
        fail("Can not reach this point");
    }
}
Also used : TransactionFailure(org.jvnet.hk2.config.TransactionFailure) ConfigSupport(org.jvnet.hk2.config.ConfigSupport) HashMap(java.util.HashMap) Cluster(com.sun.enterprise.config.serverbeans.Cluster) ConfigBean(org.jvnet.hk2.config.ConfigBean) ServerRef(com.sun.enterprise.config.serverbeans.ServerRef) HashMap(java.util.HashMap) Map(java.util.Map) ConfigApiTest(com.sun.enterprise.configapi.tests.ConfigApiTest) Test(org.junit.Test)

Aggregations

TransactionFailure (org.jvnet.hk2.config.TransactionFailure)191 PropertyVetoException (java.beans.PropertyVetoException)132 ActionReport (org.glassfish.api.ActionReport)86 Property (org.jvnet.hk2.config.types.Property)61 Config (com.sun.enterprise.config.serverbeans.Config)52 HashMap (java.util.HashMap)30 Test (org.junit.Test)27 Resources (com.sun.enterprise.config.serverbeans.Resources)25 Map (java.util.Map)25 List (java.util.List)21 CommandTarget (org.glassfish.config.support.CommandTarget)21 NetworkListener (org.glassfish.grizzly.config.dom.NetworkListener)21 Target (org.glassfish.internal.api.Target)21 NetworkConfig (org.glassfish.grizzly.config.dom.NetworkConfig)20 Protocol (org.glassfish.grizzly.config.dom.Protocol)20 ConfigBean (org.jvnet.hk2.config.ConfigBean)20 ConfigBeanProxy (org.jvnet.hk2.config.ConfigBeanProxy)18 ResourceStatus (org.glassfish.resourcebase.resources.api.ResourceStatus)17 SingleConfigCode (org.jvnet.hk2.config.SingleConfigCode)16 ParameterMap (org.glassfish.api.admin.ParameterMap)14