Search in sources :

Example 11 with Transactions

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

the class UnprocessedEventsTest method unprocessedEventsTest.

@Test
public void unprocessedEventsTest() throws TransactionFailure {
    // let's find our target
    NetworkConfig service = habitat.getService(NetworkConfig.class);
    NetworkListener listener = service.getNetworkListener("http-listener-1");
    assertNotNull(listener);
    // Let's register a listener
    ObservableBean bean = (ObservableBean) ConfigSupport.getImpl(listener);
    bean.addListener(this);
    Transactions transactions = getHabitat().getService(Transactions.class);
    try {
        transactions.addTransactionsListener(this);
        String originalPort = listener.getPort();
        try {
            ConfigSupport.apply(param -> {
                param.setPort("8908");
                return null;
            }, listener);
            // Check the result.
            assertEquals(listener.getPort(), "8908");
        } finally {
            // Restore the original port
            ConfigSupport.apply(param -> {
                param.setPort(originalPort);
                return null;
            }, listener);
        }
        // ensure events are delivered.
        transactions.waitForDrain();
        assertNotNull(unprocessed);
        // finally
        bean.removeListener(this);
    } finally {
        // check we recevied the event
        transactions.removeTransactionsListener(this);
    }
}
Also used : Transactions(org.jvnet.hk2.config.Transactions) NetworkConfig(org.glassfish.grizzly.config.dom.NetworkConfig) ObservableBean(org.jvnet.hk2.config.ObservableBean) NetworkListener(org.glassfish.grizzly.config.dom.NetworkListener) Test(org.junit.Test)

Example 12 with Transactions

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

the class AddPropertyTest method transactionEvents.

@Test
public void transactionEvents() throws TransactionFailure {
    final Domain domain = getHabitat().getService(Domain.class);
    final TransactionListener listener = new TransactionListener() {

        public void transactionCommited(List<PropertyChangeEvent> changes) {
            events = changes;
        }

        public void unprocessedTransactedEvents(List<UnprocessedChangeEvents> changes) {
        }
    };
    Transactions transactions = getHabitat().getService(Transactions.class);
    try {
        transactions.addTransactionsListener(listener);
        assertTrue(domain != null);
        ConfigSupport.apply(new SingleConfigCode<Domain>() {

            public Object run(Domain domain) throws PropertyVetoException, TransactionFailure {
                Property prop = domain.createChild(Property.class);
                domain.getProperty().add(prop);
                prop.setName("Jerome");
                prop.setValue("was here");
                return prop;
            }
        }, domain);
        transactions.waitForDrain();
        assertTrue(events != null);
        logger.fine("Number of events " + events.size());
        assertTrue(events.size() == 3);
        for (PropertyChangeEvent event : events) {
            logger.fine(event.toString());
        }
        Map<String, String> configChanges = new HashMap<String, String>();
        configChanges.put("name", "julien");
        configChanges.put("value", "petit clown");
        ConfigBean domainBean = (ConfigBean) Dom.unwrap(domain);
        ConfigSupport.createAndSet(domainBean, Property.class, configChanges);
        transactions.waitForDrain();
        assertTrue(events != null);
        logger.fine("Number of events " + events.size());
        assertTrue(events.size() == 3);
        for (PropertyChangeEvent event : events) {
            logger.fine(event.toString());
        }
        final UnprocessedChangeEvents unprocessed = ConfigSupport.sortAndDispatch(events.toArray(new PropertyChangeEvent[0]), new Changed() {

            /**
             * Notification of a change on a configuration object
             *
             * @param type            type of change : ADD mean the changedInstance was added to the parent
             *                        REMOVE means the changedInstance was removed from the parent, CHANGE means the
             *                        changedInstance has mutated.
             * @param changedType     type of the configuration object
             * @param changedInstance changed instance.
             */
            public <T extends ConfigBeanProxy> NotProcessed changed(TYPE type, Class<T> changedType, T changedInstance) {
                return new NotProcessed("unimplemented by AddPropertyTest");
            }
        }, logger);
    } finally {
        transactions.removeTransactionsListener(listener);
    }
}
Also used : TransactionListener(org.jvnet.hk2.config.TransactionListener) TransactionFailure(org.jvnet.hk2.config.TransactionFailure) UnprocessedChangeEvents(org.jvnet.hk2.config.UnprocessedChangeEvents) PropertyChangeEvent(java.beans.PropertyChangeEvent) HashMap(java.util.HashMap) ConfigBean(org.jvnet.hk2.config.ConfigBean) PropertyVetoException(java.beans.PropertyVetoException) Transactions(org.jvnet.hk2.config.Transactions) Changed(org.jvnet.hk2.config.Changed) NotProcessed(org.jvnet.hk2.config.NotProcessed) List(java.util.List) Domain(com.sun.enterprise.config.serverbeans.Domain) Property(org.jvnet.hk2.config.types.Property) Test(org.junit.Test)

Example 13 with Transactions

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

the class ConfigListenerTest method changedTest.

@Test
public void changedTest() throws TransactionFailure {
    Transactions transactions = getHabitat().getService(Transactions.class);
    HttpListenerContainer container = registerAndCreateHttpListenerContainer(habitat);
    ConfigSupport.apply(new SingleConfigCode<NetworkListener>() {

        @Override
        public Object run(NetworkListener param) {
            param.setPort("8989");
            return null;
        }
    }, container.httpListener);
    transactions.waitForDrain();
    assertTrue(container.received);
    ObservableBean bean = (ObservableBean) ConfigSupport.getImpl(container.httpListener);
    bean.removeListener(container);
    // put back the right values in the domain to avoid test collisions
    ConfigSupport.apply(new SingleConfigCode<NetworkListener>() {

        @Override
        public Object run(NetworkListener param) {
            param.setPort("8080");
            return null;
        }
    }, container.httpListener);
}
Also used : Transactions(org.jvnet.hk2.config.Transactions) ObservableBean(org.jvnet.hk2.config.ObservableBean) NetworkListener(org.glassfish.grizzly.config.dom.NetworkListener) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)10 Transactions (org.jvnet.hk2.config.Transactions)10 NetworkListener (org.glassfish.grizzly.config.dom.NetworkListener)6 ObservableBean (org.jvnet.hk2.config.ObservableBean)5 PropertyChangeEvent (java.beans.PropertyChangeEvent)4 List (java.util.List)4 TransactionFailure (org.jvnet.hk2.config.TransactionFailure)4 TransactionListener (org.jvnet.hk2.config.TransactionListener)4 Property (org.jvnet.hk2.config.types.Property)4 PropertyVetoException (java.beans.PropertyVetoException)3 HttpService (com.sun.enterprise.config.serverbeans.HttpService)2 NetworkConfig (org.glassfish.grizzly.config.dom.NetworkConfig)2 NetworkListeners (org.glassfish.grizzly.config.dom.NetworkListeners)2 UnprocessedChangeEvents (org.jvnet.hk2.config.UnprocessedChangeEvents)2 ConnectorRuntime (com.sun.appserv.connectors.internal.api.ConnectorRuntime)1 Config (com.sun.enterprise.config.serverbeans.Config)1 Domain (com.sun.enterprise.config.serverbeans.Domain)1 ModuleMonitoringLevels (com.sun.enterprise.config.serverbeans.ModuleMonitoringLevels)1 Resource (com.sun.enterprise.config.serverbeans.Resource)1 VirtualServer (com.sun.enterprise.config.serverbeans.VirtualServer)1