use of org.jvnet.hk2.config.Transaction in project Payara by payara.
the class SetConfigOrdinal method execute.
@Override
public void execute(AdminCommandContext context) {
Config configVal = targetUtil.getConfig(target);
MicroprofileConfigConfiguration serviceConfig = configVal.getExtensionByType(MicroprofileConfigConfiguration.class);
if (serviceConfig != null) {
try {
// to perform a transaction on the domain.xml you need to use this construct
// see https://github.com/hk2-project/hk2/blob/master/hk2-configuration/persistence/hk2-xml-dom/hk2-config/src/main/java/org/jvnet/hk2/config/ConfigSupport.java
ConfigSupport.apply(new SingleConfigCode<MicroprofileConfigConfiguration>() {
@Override
public Object run(MicroprofileConfigConfiguration config) {
switch(source) {
case "domain":
{
config.setDomainOrdinality(Integer.toString(ordinal));
break;
}
case "config":
{
config.setConfigOrdinality(Integer.toString(ordinal));
break;
}
case "server":
{
config.setServerOrdinality(Integer.toString(ordinal));
break;
}
case "application":
{
config.setApplicationOrdinality(Integer.toString(ordinal));
break;
}
case "module":
{
config.setModuleOrdinality(Integer.toString(ordinal));
break;
}
case "cluster":
{
config.setClusterOrdinality(Integer.toString(ordinal));
break;
}
case "jndi":
{
config.setJNDIOrdinality(Integer.toString(ordinal));
break;
}
case "secrets":
{
config.setSecretDirOrdinality(Integer.toString(ordinal));
break;
}
}
return null;
}
}, serviceConfig);
} catch (TransactionFailure ex) {
// set failure
context.getActionReport().failure(Logger.getLogger(SetConfigOrdinal.class.getName()), "Failed to update message", ex);
}
} else {
context.getActionReport().failure(Logger.getLogger(SetConfigOrdinal.class.getName()), "No configuration with name " + target);
}
}
use of org.jvnet.hk2.config.Transaction in project Payara by payara.
the class CreateSystemProperties method execute.
/**
* Executes the command with the command parameters passed as Properties
* where the keys are the paramter names and the values the parameter values
*
* @param context information
*/
@Override
public void execute(AdminCommandContext context) {
final ActionReport report = context.getActionReport();
String sysPropName = "";
try {
for (final Object key : properties.keySet()) {
final String propName = (String) key;
sysPropName = propName;
// value of an existing property
try {
TranslatedConfigView.doSubstitution.set(Boolean.FALSE);
if (spb.containsProperty(sysPropName) && spb.getSystemProperty(sysPropName).getValue().equals(properties.getProperty(propName))) {
continue;
}
} finally {
TranslatedConfigView.doSubstitution.set(Boolean.TRUE);
}
ConfigSupport.apply(new SingleConfigCode<SystemPropertyBag>() {
@Override
public Object run(SystemPropertyBag param) throws PropertyVetoException, TransactionFailure {
// update existing system property
for (SystemProperty sysProperty : param.getSystemProperty()) {
if (sysProperty.getName().equals(propName)) {
Transaction t = Transaction.getTransaction(param);
sysProperty = t.enroll(sysProperty);
sysProperty.setValue(properties.getProperty(propName));
return sysProperty;
}
}
// create system-property
SystemProperty newSysProp = param.createChild(SystemProperty.class);
newSysProp.setName(propName);
newSysProp.setValue(properties.getProperty(propName));
param.getSystemProperty().add(newSysProp);
return newSysProp;
}
}, spb);
report.setActionExitCode(ActionReport.ExitCode.SUCCESS);
}
} catch (TransactionFailure tfe) {
report.setMessage(localStrings.getLocalString("create.system.properties.failed", "System property {0} creation failed", sysPropName));
report.setActionExitCode(ActionReport.ExitCode.FAILURE);
report.setFailureCause(tfe);
} catch (Exception e) {
report.setMessage(localStrings.getLocalString("create.system.properties.failed", "System property {0} creation failed", sysPropName));
report.setActionExitCode(ActionReport.ExitCode.FAILURE);
report.setFailureCause(e);
}
}
use of org.jvnet.hk2.config.Transaction in project Payara by payara.
the class ConcurrentModificationsTest method collectionTest.
@Test(expected = TransactionFailure.class)
public void collectionTest() throws TransactionFailure {
ServiceLocator habitat = super.getHabitat();
final Resources resources = habitat.<Domain>getService(Domain.class).getResources();
assertTrue(resources != null);
ConfigSupport.apply(new SingleConfigCode<Resources>() {
public Object run(Resources writeableResources) throws PropertyVetoException, TransactionFailure {
assertTrue(writeableResources != null);
JdbcResource newResource = writeableResources.createChild(JdbcResource.class);
newResource.setJndiName("foo");
newResource.setDescription("Random ");
newResource.setPoolName("bar");
newResource.setEnabled("true");
writeableResources.getResources().add(newResource);
// now let's check I have my copy...
boolean found = false;
for (Resource resource : writeableResources.getResources()) {
if (resource instanceof JdbcResource) {
JdbcResource jdbc = (JdbcResource) resource;
if (jdbc.getJndiName().equals("foo")) {
found = true;
break;
}
}
}
assertTrue(found);
// now let's check that my readonly copy does not see it...
boolean shouldNot = false;
for (Resource resource : resources.getResources()) {
if (resource instanceof JdbcResource) {
JdbcResource jdbc = (JdbcResource) resource;
if (jdbc.getJndiName().equals("foo")) {
shouldNot = true;
break;
}
}
}
assertFalse(shouldNot);
// now I am throwing a transaction failure since I don't care about saving it
throw new TransactionFailure("Test passed", null);
}
}, resources);
}
use of org.jvnet.hk2.config.Transaction in project Payara by payara.
the class CreateHTTPLoadBalancerCommand method addLoadBalancer.
private void addLoadBalancer(final String lbConfigName) {
LoadBalancers loadBalancers = domain.getExtensionByType(LoadBalancers.class);
// create load-balancers parent element if it does not exist
if (loadBalancers == null) {
Transaction transaction = new Transaction();
try {
ConfigBeanProxy domainProxy = transaction.enroll(domain);
loadBalancers = domainProxy.createChild(LoadBalancers.class);
((Domain) domainProxy).getExtensions().add(loadBalancers);
transaction.commit();
} catch (TransactionFailure ex) {
transaction.rollback();
String msg = localStrings.getLocalString("FailedToUpdateLB", "Failed to update load-balancers");
report.setActionExitCode(ActionReport.ExitCode.FAILURE);
report.setMessage(msg);
return;
} catch (RetryableException ex) {
transaction.rollback();
String msg = localStrings.getLocalString("FailedToUpdateLB", "Failed to update load-balancers");
report.setActionExitCode(ActionReport.ExitCode.FAILURE);
report.setMessage(msg);
return;
}
}
try {
ConfigSupport.apply(new SingleConfigCode<LoadBalancers>() {
@Override
public Object run(LoadBalancers param) throws PropertyVetoException, TransactionFailure {
LoadBalancer lb = param.createChild(LoadBalancer.class);
lb.setDeviceHost(devicehost);
lb.setDevicePort(deviceport);
lb.setLbConfigName(lbConfigName);
lb.setName(load_balancer_name);
// add properties
if (properties != null) {
for (Object propname : properties.keySet()) {
Property newprop = lb.createChild(Property.class);
newprop.setName((String) propname);
newprop.setValue(properties.getProperty((String) propname));
lb.getProperty().add(newprop);
}
}
if (sslproxyhost != null) {
Property newprop = lb.createChild(Property.class);
newprop.setName("ssl-proxy-host");
newprop.setValue(sslproxyhost);
lb.getProperty().add(newprop);
}
if (sslproxyport != null) {
Property newprop = lb.createChild(Property.class);
newprop.setName("ssl-proxy-port");
newprop.setValue(sslproxyport);
lb.getProperty().add(newprop);
}
param.getLoadBalancer().add(lb);
return Boolean.TRUE;
}
}, loadBalancers);
} catch (TransactionFailure ex) {
String msg = localStrings.getLocalString("FailedToUpdateLB", "Failed to update load-balancers");
report.setActionExitCode(ActionReport.ExitCode.FAILURE);
report.setMessage(msg);
return;
}
}
use of org.jvnet.hk2.config.Transaction 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