use of org.jvnet.hk2.config.Transaction in project Payara by payara.
the class SetExampleServiceMessage method execute.
@Override
public void execute(AdminCommandContext context) {
// obtain the correct configuration
Config configVal = targetUtil.getConfig(target);
ExampleServiceConfiguration serviceConfig = configVal.getExtensionByType(ExampleServiceConfiguration.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<ExampleServiceConfiguration>() {
@Override
public Object run(ExampleServiceConfiguration config) {
config.setMessage(message);
return null;
}
}, serviceConfig);
} catch (TransactionFailure ex) {
// set failure
context.getActionReport().failure(Logger.getLogger(SetExampleServiceMessage.class.getName()), "Failed to update message", ex);
}
}
// was targetted explicitly via the following code
if (server.isDas()) {
// you would need to do this if you now want to manipulate the service based on the command parameters
if (targetUtil.getConfig(target).isDas()) {
// this command was also targetted at the DAS
service.doSomethingDirectly("Set message command was targetted at the DAS");
// as below you can now directly manipulate the service as needed
}
} else {
// if you are not the DAS then impoicitly this command was targeted to this instance
service.doSomethingDirectly("Set config command targeted at the instance");
// you can now directly manipulate the service remember though
// if it is a config listener it has already been notified of the config change
// however if it is not a config listener you can manipulate the service now
}
}
use of org.jvnet.hk2.config.Transaction in project Payara by payara.
the class LDAPAdminAccessConfigurator method configure.
private void configure(StringBuilder sb) throws TransactionFailure, PropertyVetoException, RetryableException {
// createBackupRealm(sb, getAdminRealm(asc.getSecurityService()), getNewRealmName(asc.getSecurityService()));
Transaction t = new Transaction();
final SecurityService w_asc = t.enroll(asc.getSecurityService());
AdminService w_adminSvc = t.enroll(asc.getAdminService());
deleteRealm(w_asc, sb);
createRealm(w_asc, sb);
configureAdminService(w_adminSvc);
updateSecurityProvider(t, fileRealmProvider, sb);
t.commit();
}
use of org.jvnet.hk2.config.Transaction in project Payara by payara.
the class SecurityConfigUpgradeService method postConstruct.
@Override
public void postConstruct() {
if (domain.getExtensionByType(SecurityConfigurations.class) != null) {
/*
* The domain already contains a security-configurations setting,
* so for now that's sufficient to conclude we don't need to upgrade.
*/
logger.log(Level.INFO, "SecurityConfigUpgradeService bypassing - security-configurations already present");
return;
}
Transaction t = null;
try {
t = new Transaction();
final Domain domain_w = t.enroll(domain);
/*
* Create the security configurations element and add it to the domain.
*/
final SecurityConfigurations sc_w = domain_w.createChild(SecurityConfigurations.class);
domain_w.getExtensions().add(sc_w);
/*
* Create and add the authentication service.
*/
final AuthenticationService as_w = addAuthenticationService(sc_w);
/*
* Next, add the two providers and their children.
*/
addAdmRealmProvider(as_w);
addFileRealmProvider(as_w);
/**
* Next add the authorization service
*/
final AuthorizationService authorizationService = addAuthorizationService(sc_w);
/**
* Next add the authorization service provider
*/
addSimpleAuthorizationProvider(authorizationService);
t.commit();
logger.log(Level.INFO, "SecurityConfigUpgradeService successfully completed the upgrade");
} catch (Exception ex) {
if (t != null) {
t.rollback();
}
logger.log(Level.SEVERE, null, ex);
}
}
use of org.jvnet.hk2.config.Transaction in project Payara by payara.
the class WriteableView method commit.
/**
* Commit this Transaction.
*
* @param t the transaction commiting.
* @throws TransactionFailure
* if the transaction commit failed
*/
public synchronized List<PropertyChangeEvent> commit(Transaction t) throws TransactionFailure {
if (currentTx == t) {
currentTx = null;
}
// a key attribute must be non-null and have length >= 1
final ConfigBean master = getMasterView();
final String keyStr = master.model.key;
if (keyStr != null) {
final String key = stripMarkers(keyStr);
final String value = getPropertyValue(key);
if (value == null) {
throw new TransactionFailure("Key value cannot be null: " + key);
}
if (value.length() == 0) {
throw new TransactionFailure("Key value cannot be empty string: " + key);
}
}
try {
List<PropertyChangeEvent> appliedChanges = new ArrayList<PropertyChangeEvent>();
for (PropertyChangeEvent event : changedAttributes.values()) {
ConfigModel.Property property = bean.model.findIgnoreCase(event.getPropertyName());
ConfigBeanInterceptor interceptor = bean.getOptionalFeature(ConfigBeanInterceptor.class);
try {
if (interceptor != null) {
interceptor.beforeChange(event);
}
} catch (PropertyVetoException e) {
throw new TransactionFailure(e.getMessage(), e);
}
property.set(bean, event.getNewValue());
if (interceptor != null) {
interceptor.afterChange(event, System.currentTimeMillis());
}
appliedChanges.add(event);
}
for (ProtectedList entry : changedCollections.values()) {
List<Object> originalList = entry.readOnly;
for (PropertyChangeEvent event : entry.changeEvents) {
if (event.getOldValue() == null) {
originalList.add(event.getNewValue());
} else {
final Object toBeRemovedObj = event.getOldValue();
if (toBeRemovedObj instanceof ConfigBeanProxy) {
final Dom toBeRemoved = Dom.unwrap((ConfigBeanProxy) toBeRemovedObj);
for (int index = 0; index < originalList.size(); index++) {
Object element = originalList.get(index);
Dom dom = Dom.unwrap((ConfigBeanProxy) element);
if (dom == toBeRemoved) {
Object newValue = event.getNewValue();
if (newValue == null) {
originalList.remove(index);
} else {
originalList.set(index, newValue);
}
}
}
} else if (toBeRemovedObj instanceof String) {
final String toBeRemoved = (String) toBeRemovedObj;
for (int index = 0; index < originalList.size(); index++) {
final String item = (String) originalList.get(index);
if (item.equals(toBeRemoved)) {
originalList.remove(index);
}
}
} else {
throw new IllegalArgumentException();
}
}
appliedChanges.add(event);
}
}
changedAttributes.clear();
changedCollections.clear();
return appliedChanges;
} catch (TransactionFailure e) {
throw e;
} catch (Exception e) {
throw new TransactionFailure(e.getMessage(), e);
} finally {
bean.getLock().unlock();
}
}
use of org.jvnet.hk2.config.Transaction in project Payara by payara.
the class TemplateRestResource method buildPath.
/**
* This method will build the path string as needed by ConfigModularityUtils.getOwningObject().
* There is a mismatch between what the method expects and the way the REST URIs are constructed.
* For example, for the transaction-service element, the REST URI, stripped of the HTTP and
* server context information, looks like this:
* /domain/configs/config/server-config/transaction-service. The format expected by the
* getOwningObject(), however, looks like this:
* domain/configs/server-config/transaction-service. In the REST URIs, if there is a collection of
* Named items, the type of the collection is inserted into the URI ("config" here) followed by
* the name of the particular instance ("server-config"). In building the path, we must identify
* Named instances and insert the name of the instance rather than the type. We apply this logic
* as we recurse up to the top of the Dom tree to finish building the path desired.
* @param node
* @return
*/
private String buildPath(Dom node) {
final Dom parentNode = node.parent();
String part = node.model.getTagName();
String name = node.attribute("name");
if (name != null) {
part = name;
}
return (parentNode != null) ? (buildPath(parentNode) + "/" + part) : part;
}
Aggregations