use of org.jvnet.hk2.config.Transactions 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);
}
use of org.jvnet.hk2.config.Transactions in project Payara by payara.
the class JdbcRecoveryResourceHandler method loadXAResourcesAndItsConnections.
/**
* {@inheritDoc}
*/
@Override
public void loadXAResourcesAndItsConnections(List xaresList, List connList) {
// Done so as to initialize connectors-runtime before loading jdbc-resources. need a better way ?
ConnectorRuntime crt = connectorRuntimeProvider.get();
Collection<JdbcResource> jdbcResources = getAllJdbcResources();
if (jdbcResources == null || jdbcResources.size() == 0) {
if (_logger.isLoggable(Level.FINEST)) {
_logger.finest("loadXAResourcesAndItsConnections : no resources");
}
return;
}
List<JdbcConnectionPool> jdbcPools = new ArrayList<JdbcConnectionPool>();
for (Resource resource : jdbcResources) {
JdbcResource jdbcResource = (JdbcResource) resource;
if (getResourcesUtil().isEnabled(jdbcResource)) {
ResourceInfo resourceInfo = ConnectorsUtil.getResourceInfo(jdbcResource);
JdbcConnectionPool pool = JdbcResourcesUtil.createInstance().getJdbcConnectionPoolOfResource(resourceInfo);
if (pool != null && "javax.sql.XADataSource".equals(pool.getResType())) {
jdbcPools.add(pool);
}
if (_logger.isLoggable(Level.FINE)) {
_logger.fine("JdbcRecoveryResourceHandler:: loadXAResourcesAndItsConnections :: " + "adding : " + (jdbcResource.getPoolName()));
}
}
}
loadAllJdbcResources();
// Read from the transaction-service , if the replacement of
// Vendor XAResource class with our version required.
// If yes, put the mapping in the xaresourcewrappers properties.
Properties XAResourceWrappers = new Properties();
XAResourceWrappers.put("oracle.jdbc.xa.client.OracleXADataSource", "com.sun.enterprise.transaction.jts.recovery.OracleXAResource");
Config c = habitat.getService(Config.class, ServerEnvironment.DEFAULT_INSTANCE_NAME);
txService = c.getExtensionByType(TransactionService.class);
List<Property> properties = txService.getProperty();
if (properties != null) {
for (Property property : properties) {
String name = property.getName();
String value = property.getValue();
if (name.equals("oracle-xa-recovery-workaround")) {
if ("false".equals(value)) {
XAResourceWrappers.remove("oracle.jdbc.xa.client.OracleXADataSource");
}
} else if (name.equals("sybase-xa-recovery-workaround")) {
if (value.equals("true")) {
XAResourceWrappers.put("com.sybase.jdbc2.jdbc.SybXADataSource", "com.sun.enterprise.transaction.jts.recovery.SybaseXAResource");
}
}
}
}
for (JdbcConnectionPool jdbcConnectionPool : jdbcPools) {
if (jdbcConnectionPool.getResType() == null || jdbcConnectionPool.getName() == null || !jdbcConnectionPool.getResType().equals("javax.sql.XADataSource")) {
if (_logger.isLoggable(Level.FINEST)) {
_logger.finest("skipping pool : " + jdbcConnectionPool.getName());
}
continue;
}
if (_logger.isLoggable(Level.FINEST)) {
_logger.finest(" using pool : " + jdbcConnectionPool.getName());
}
PoolInfo poolInfo = ConnectorsUtil.getPoolInfo(jdbcConnectionPool);
try {
String[] dbUserPassword = getdbUserPasswordOfJdbcConnectionPool(jdbcConnectionPool);
String dbUser = dbUserPassword[0];
String dbPassword = dbUserPassword[1];
if (dbPassword == null) {
dbPassword = "";
if (_logger.isLoggable(Level.FINEST)) {
_logger.log(Level.FINEST, "datasource.xadatasource_nullpassword_error", poolInfo);
}
}
if (dbUser == null) {
dbUser = "";
if (_logger.isLoggable(Level.FINEST)) {
_logger.log(Level.FINEST, "datasource.xadatasource_nulluser_error", poolInfo);
}
}
ManagedConnectionFactory fac = crt.obtainManagedConnectionFactory(poolInfo);
Subject subject = new Subject();
PasswordCredential pc = new PasswordCredential(dbUser, dbPassword.toCharArray());
pc.setManagedConnectionFactory(fac);
Principal prin = new ResourcePrincipal(dbUser, dbPassword);
subject.getPrincipals().add(prin);
subject.getPrivateCredentials().add(pc);
ManagedConnection mc = fac.createManagedConnection(subject, null);
connList.add(mc);
try {
XAResource xares = mc.getXAResource();
if (xares != null) {
// See if a wrapper class for the vendor XADataSource is
// specified if yes, replace the XAResouce class of database
// vendor with our own version
String datasourceClassname = jdbcConnectionPool.getDatasourceClassname();
String wrapperclass = (String) XAResourceWrappers.get(datasourceClassname);
if (wrapperclass != null) {
// need to load wrapper class provided by "transactions" module.
// Using connector-class-loader so as to get access to "transaction" module.
XAResourceWrapper xaresWrapper = null;
xaresWrapper = (XAResourceWrapper) crt.getConnectorClassLoader().loadClass(wrapperclass).newInstance();
xaresWrapper.init(mc, subject);
if (_logger.isLoggable(Level.FINEST)) {
_logger.finest("adding resource " + poolInfo + " -- " + xaresWrapper);
}
xaresList.add(xaresWrapper);
} else {
if (_logger.isLoggable(Level.FINEST)) {
_logger.finest("adding resource " + poolInfo + " -- " + xares);
}
xaresList.add(xares);
}
}
} catch (ResourceException ex) {
_logger.log(Level.WARNING, "datasource.xadatasource_error", poolInfo);
if (_logger.isLoggable(Level.FINE)) {
_logger.log(Level.FINE, "datasource.xadatasource_error_excp", ex);
}
// ignored. Not at XA_TRANSACTION level
}
} catch (Exception ex) {
_logger.log(Level.WARNING, "datasource.xadatasource_error", poolInfo);
if (_logger.isLoggable(Level.FINE)) {
_logger.log(Level.FINE, "datasource.xadatasource_error_excp", ex);
}
}
}
}
use of org.jvnet.hk2.config.Transactions in project Payara by payara.
the class ConfigListenerTest method removeListenerTest.
@Test
public void removeListenerTest() throws TransactionFailure {
Transactions transactions = getHabitat().getService(Transactions.class);
HttpListenerContainer container = registerAndCreateHttpListenerContainer(habitat);
ObservableBean bean = (ObservableBean) ConfigSupport.getImpl(container.httpListener);
bean.removeListener(container);
ConfigSupport.apply(new SingleConfigCode<NetworkListener>() {
@Override
public Object run(NetworkListener param) {
param.setPort("8989");
return null;
}
}, container.httpListener);
transactions.waitForDrain();
assertFalse(container.received);
// 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);
}
use of org.jvnet.hk2.config.Transactions 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.Transactions in project Payara by payara.
the class TransactionServiceConfigListener method changed.
/**
*************************************************************************
*/
/**
* Implementation of org.jvnet.hk2.config.ConfigListener ********************
*/
/**
*************************************************************************
*/
@Override
public UnprocessedChangeEvents changed(PropertyChangeEvent[] events) {
// Events that we can't process now because they require server restart.
List<UnprocessedChangeEvent> unprocessedEvents = new ArrayList<UnprocessedChangeEvent>();
for (PropertyChangeEvent event : events) {
String eventName = event.getPropertyName();
Object oldValue = event.getOldValue();
Object newValue = event.getNewValue();
boolean accepted = true;
_logger.log(Level.FINE, "Got TransactionService change event ==== {0} {1} {2} {3}", new Object[] { event.getSource(), eventName, oldValue, newValue });
if (oldValue != null && oldValue.equals(newValue)) {
_logger.log(Level.FINE, "Event {0} did not change existing value of {1}", new Object[] { eventName, oldValue });
continue;
}
if (event.getSource() instanceof ModuleMonitoringLevels) {
if (eventName.equals(ServerTags.TRANSACTION_SERVICE)) {
String newlevel = newValue.toString();
_logger.log(Level.FINE, "Changing transaction monitoring level");
if ("OFF".equals(newlevel)) {
tm.setMonitoringEnabled(false);
} else if ("LOW".equals(newlevel) || "HIGH".equals(newlevel)) {
tm.setMonitoringEnabled(true);
}
}
// else skip
} else if (eventName.equals(ServerTags.TIMEOUT_IN_SECONDS)) {
try {
tm.setDefaultTransactionTimeout(Integer.parseInt((String) newValue, 10));
_logger.log(Level.FINE, " Transaction Timeout interval event processed for: {0}", newValue);
} catch (Exception ex) {
_logger.log(Level.WARNING, "transaction.reconfig_txn_timeout_failed", ex);
}
// timeout-in-seconds
} else if (eventName.equals(ServerTags.KEYPOINT_INTERVAL) || eventName.equals(ServerTags.RETRY_TIMEOUT_IN_SECONDS)) {
tm.handlePropertyUpdate(eventName, newValue);
_logger.log(Level.FINE, "{0} reconfig event processed for new value: {1}", new Object[] { eventName, newValue });
} else if (event.getPropertyName().equals("value")) {
eventName = ((Property) event.getSource()).getName();
_logger.log(Level.FINE, "Got Property change event for {0}", eventName);
if (eventName.equals("purge-cancelled-transactions-after")) {
String v = (String) newValue;
if (v == null || v.length() == 0) {
tm.setPurgeCancelledTtransactionsAfter(0);
} else {
tm.setPurgeCancelledTtransactionsAfter(Integer.parseInt(v, 10));
}
} else {
// Not handled dynamically. Restart is required.
accepted = false;
}
} else if (event.getPropertyName().equals("name") || event.getPropertyName().equals("property")) {
// skip - means a new property added, was processed above as "value".
_logger.log(Level.FINE, "...skipped");
} else {
// Not handled dynamically. Restart is required.
accepted = false;
}
if (!accepted) {
String msg = sm.getString("enterprise_distributedtx.restart_required", eventName);
_logger.log(Level.INFO, msg);
unprocessedEvents.add(new UnprocessedChangeEvent(event, msg));
}
}
return (unprocessedEvents.size() > 0) ? new UnprocessedChangeEvents(unprocessedEvents) : null;
}
Aggregations