Search in sources :

Example 1 with TransactionListener

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

the class ConfigPersistence method test.

@Test
public void test() throws TransactionFailure {
    final DomDocument document = getDocument(getHabitat());
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    baos.reset();
    final ConfigurationPersistence testPersistence = new ConfigurationPersistence() {

        public void save(DomDocument doc) throws IOException, XMLStreamException {
            XMLOutputFactory factory = XMLOutputFactory.newInstance();
            XMLStreamWriter writer = factory.createXMLStreamWriter(baos);
            doc.writeTo(new IndentingXMLStreamWriter(writer));
            writer.close();
        }
    };
    TransactionListener testListener = new TransactionListener() {

        public void transactionCommited(List<PropertyChangeEvent> changes) {
            try {
                testPersistence.save(document);
            } catch (IOException e) {
                // To change body of catch statement use File | Settings | File Templates.
                e.printStackTrace();
            } catch (XMLStreamException e) {
                // To change body of catch statement use File | Settings | File Templates.
                e.printStackTrace();
            }
        }

        public void unprocessedTransactedEvents(List<UnprocessedChangeEvents> changes) {
        }
    };
    Transactions transactions = getHabitat().getService(Transactions.class);
    try {
        transactions.addTransactionsListener(testListener);
        doTest();
    } catch (TransactionFailure f) {
        f.printStackTrace();
        throw f;
    } finally {
        transactions.waitForDrain();
        transactions.removeTransactionsListener(testListener);
    }
    // now check if we persisted correctly...
    final String resultingXml = baos.toString();
    logger.fine(resultingXml);
    assertTrue("assertResult from " + getClass().getName() + " was false from " + resultingXml, assertResult(resultingXml));
}
Also used : TransactionListener(org.jvnet.hk2.config.TransactionListener) TransactionFailure(org.jvnet.hk2.config.TransactionFailure) IndentingXMLStreamWriter(org.jvnet.hk2.config.IndentingXMLStreamWriter) XMLOutputFactory(javax.xml.stream.XMLOutputFactory) ConfigurationPersistence(org.glassfish.config.support.ConfigurationPersistence) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) DomDocument(org.jvnet.hk2.config.DomDocument) Transactions(org.jvnet.hk2.config.Transactions) XMLStreamException(javax.xml.stream.XMLStreamException) IndentingXMLStreamWriter(org.jvnet.hk2.config.IndentingXMLStreamWriter) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) List(java.util.List) Test(org.junit.Test)

Example 2 with TransactionListener

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

the class TransactionListenerTest method transactionEvents.

@Test
public void transactionEvents() throws Exception, TransactionFailure {
    httpService = getHabitat().getService(HttpService.class);
    NetworkConfig networkConfig = getHabitat().getService(NetworkConfig.class);
    final NetworkListener netListener = networkConfig.getNetworkListeners().getNetworkListener().get(0);
    final Http http = netListener.findHttpProtocol().getHttp();
    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(httpService != null);
        logger.fine("Max connections = " + http.getMaxConnections());
        ConfigSupport.apply(new SingleConfigCode<Http>() {

            public Object run(Http param) {
                param.setMaxConnections("500");
                return null;
            }
        }, http);
        assertTrue("500".equals(http.getMaxConnections()));
        transactions.waitForDrain();
        assertTrue(events != null);
        logger.fine("Number of events " + events.size());
        assertTrue(events.size() == 1);
        PropertyChangeEvent event = events.iterator().next();
        assertTrue("max-connections".equals(event.getPropertyName()));
        assertTrue("500".equals(event.getNewValue().toString()));
        assertTrue("250".equals(event.getOldValue().toString()));
    } catch (Exception t) {
        t.printStackTrace();
        throw t;
    } finally {
        transactions.removeTransactionsListener(listener);
    }
    // put back the right values in the domain to avoid test collisions
    ConfigSupport.apply(new SingleConfigCode<Http>() {

        public Object run(Http param) {
            param.setMaxConnections("250");
            return null;
        }
    }, http);
}
Also used : TransactionListener(org.jvnet.hk2.config.TransactionListener) Transactions(org.jvnet.hk2.config.Transactions) PropertyChangeEvent(java.beans.PropertyChangeEvent) HttpService(com.sun.enterprise.config.serverbeans.HttpService) NetworkConfig(org.glassfish.grizzly.config.dom.NetworkConfig) Http(org.glassfish.grizzly.config.dom.Http) List(java.util.List) NetworkListener(org.glassfish.grizzly.config.dom.NetworkListener) Test(org.junit.Test)

Example 3 with TransactionListener

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

the class TranslatedViewCreationTest method createVirtualServerTest.

@Test
public void createVirtualServerTest() throws TransactionFailure {
    httpService = getHabitat().getService(HttpService.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(httpService != null);
        ConfigSupport.apply(new SingleConfigCode<HttpService>() {

            public Object run(HttpService param) throws PropertyVetoException, TransactionFailure {
                VirtualServer newVirtualServer = param.createChild(VirtualServer.class);
                newVirtualServer.setDocroot("${" + propName + "}");
                newVirtualServer.setId("translated-view-creation");
                param.getVirtualServer().add(newVirtualServer);
                return null;
            }
        }, httpService);
        // first let check that our new virtual server has the right translated value
        VirtualServer vs = httpService.getVirtualServerByName("translated-view-creation");
        assertTrue(vs != null);
        String docRoot = vs.getDocroot();
        assertTrue("/foo/bar/docroot".equals(docRoot));
        transactions.waitForDrain();
        assertTrue(events != null);
        logger.fine("Number of events " + events.size());
        assertTrue(events.size() == 3);
        for (PropertyChangeEvent event : events) {
            if ("virtual-server".equals(event.getPropertyName())) {
                VirtualServer newVS = (VirtualServer) event.getNewValue();
                assertTrue(event.getOldValue() == null);
                docRoot = newVS.getDocroot();
                assertTrue("/foo/bar/docroot".equals(docRoot));
                VirtualServer rawView = GlassFishConfigBean.getRawView(newVS);
                assertTrue(rawView != null);
                assertTrue(rawView.getDocroot().equalsIgnoreCase("${" + propName + "}"));
                return;
            }
        }
        assertTrue(false);
    } finally {
        transactions.removeTransactionsListener(listener);
    }
}
Also used : TransactionListener(org.jvnet.hk2.config.TransactionListener) PropertyVetoException(java.beans.PropertyVetoException) TransactionFailure(org.jvnet.hk2.config.TransactionFailure) Transactions(org.jvnet.hk2.config.Transactions) PropertyChangeEvent(java.beans.PropertyChangeEvent) HttpService(com.sun.enterprise.config.serverbeans.HttpService) List(java.util.List) VirtualServer(com.sun.enterprise.config.serverbeans.VirtualServer) Test(org.junit.Test)

Example 4 with TransactionListener

use of org.jvnet.hk2.config.TransactionListener 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)

Aggregations

List (java.util.List)4 Test (org.junit.Test)4 TransactionListener (org.jvnet.hk2.config.TransactionListener)4 Transactions (org.jvnet.hk2.config.Transactions)4 PropertyChangeEvent (java.beans.PropertyChangeEvent)3 TransactionFailure (org.jvnet.hk2.config.TransactionFailure)3 HttpService (com.sun.enterprise.config.serverbeans.HttpService)2 PropertyVetoException (java.beans.PropertyVetoException)2 Domain (com.sun.enterprise.config.serverbeans.Domain)1 VirtualServer (com.sun.enterprise.config.serverbeans.VirtualServer)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 XMLOutputFactory (javax.xml.stream.XMLOutputFactory)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)1 ConfigurationPersistence (org.glassfish.config.support.ConfigurationPersistence)1 Http (org.glassfish.grizzly.config.dom.Http)1 NetworkConfig (org.glassfish.grizzly.config.dom.NetworkConfig)1 NetworkListener (org.glassfish.grizzly.config.dom.NetworkListener)1