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));
}
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);
}
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);
}
}
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);
}
}
Aggregations