use of org.jvnet.hk2.config.TransactionFailure in project Payara by payara.
the class CreateFileUser 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
*/
public void execute(AdminCommandContext context) {
final ActionReport report = context.getActionReport();
// Get FileRealm class name, match it with what is expected.
String fileRealmClassName = fileAuthRealm.getClassname();
// Report error if provided impl is not the one expected
if (fileRealmClassName != null && !fileRealmClassName.equals("com.sun.enterprise.security.auth.realm.file.FileRealm")) {
report.setMessage(localStrings.getLocalString("create.file.user.realmnotsupported", "Configured file realm {0} is not supported.", fileRealmClassName));
report.setActionExitCode(ActionReport.ExitCode.FAILURE);
return;
}
// ensure we have the file associated with the authrealm
String keyFile = null;
for (Property fileProp : fileAuthRealm.getProperty()) {
if (fileProp.getName().equals("file"))
keyFile = fileProp.getValue();
}
final String kf = keyFile;
if (keyFile == null) {
report.setMessage(localStrings.getLocalString("create.file.user.keyfilenotfound", "There is no physical file associated with this file realm {0} ", authRealmName));
report.setActionExitCode(ActionReport.ExitCode.FAILURE);
return;
}
boolean exists = (new File(kf)).exists();
if (!exists) {
report.setMessage(localStrings.getLocalString("file.realm.keyfilenonexistent", "The specified physical file {0} associated with the file realm {1} does not exist.", new Object[] { kf, authRealmName }));
report.setActionExitCode(ActionReport.ExitCode.FAILURE);
return;
}
// Now get all inputs ready. userid and groups are straightforward but
// password is tricky. It is stored in the file passwordfile passed
// through the CLI options. It is stored under the name
// AS_ADMIN_USERPASSWORD. Fetch it from there.
// fetchPassword(report);
final String password = userpassword;
if (password == null) {
report.setMessage(localStrings.getLocalString("create.file.user.keyfilenotreadable", "Password for user {0} " + "has to be specified in --userpassword option or supplied " + "through AS_ADMIN_USERPASSWORD property in the file specified " + "in --passwordfile option", userName));
report.setActionExitCode(ActionReport.ExitCode.FAILURE);
return;
}
// Issue 17525 Fix - Check for null passwords for admin-realm if secureadmin is enabled
secureAdmin = domain.getSecureAdmin();
if ((SecureAdmin.Util.isEnabled(secureAdmin)) && (authRealmName.equals(adminService.getAuthRealmName()))) {
if (password.isEmpty()) {
report.setMessage(localStrings.getLocalString("null_empty_password", "The admin user password is null or empty"));
report.setActionExitCode(ActionReport.ExitCode.FAILURE);
return;
}
}
// now adding user
try {
// even though create-file-user is not an update to the security-service
// do we need to make it transactional by referncing the securityservice
// hypothetically ?.
ConfigSupport.apply(new SingleConfigCode<SecurityService>() {
public Object run(SecurityService param) throws PropertyVetoException, TransactionFailure {
try {
realmsManager.createRealms(config);
// If the (shared) keyfile is updated by an external process, load the users first
refreshRealm(config.getName(), authRealmName);
final FileRealm fr = (FileRealm) realmsManager.getFromLoadedRealms(config.getName(), authRealmName);
CreateFileUser.handleAdminGroup(authRealmName, groups);
String[] groups1 = groups.toArray(new String[groups.size()]);
try {
fr.addUser(userName, password.toCharArray(), groups1);
} catch (BadRealmException br) {
if (se != null && se.isDas()) {
throw new BadRealmException(br);
}
}
fr.persist();
report.setActionExitCode(ActionReport.ExitCode.SUCCESS);
} catch (Exception e) {
String localalizedErrorMsg = (e.getLocalizedMessage() == null) ? "" : e.getLocalizedMessage();
report.setMessage(localStrings.getLocalString("create.file.user.useraddfailed", "Adding User {0} to the file realm {1} failed", userName, authRealmName) + " " + localalizedErrorMsg);
report.setActionExitCode(ActionReport.ExitCode.FAILURE);
report.setFailureCause(e);
}
return null;
}
}, securityService);
} catch (Exception e) {
report.setMessage(localStrings.getLocalString("create.file.user.useraddfailed", "Adding User {0} to the file realm {1} failed", userName, authRealmName) + " " + e.getLocalizedMessage());
report.setActionExitCode(ActionReport.ExitCode.FAILURE);
report.setFailureCause(e);
}
}
use of org.jvnet.hk2.config.TransactionFailure in project Payara by payara.
the class DeleteThreadpool 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
*/
public void execute(AdminCommandContext context) {
ActionReport report = context.getActionReport();
try {
ConfigSupport.apply(new SingleConfigCode<ThreadPools>() {
public Object run(ThreadPools param) throws PropertyVetoException, TransactionFailure {
List<ThreadPool> poolList = param.getThreadPool();
for (ThreadPool pool : poolList) {
String currPoolId = pool.getName();
if (currPoolId != null && currPoolId.equals(threadpool_id)) {
poolList.remove(pool);
break;
}
}
return poolList;
}
}, threadPools);
report.setActionExitCode(ExitCode.SUCCESS);
} catch (TransactionFailure e) {
String str = e.getMessage();
report.setMessage(localStrings.getLocalString("delete.threadpool" + ".failed", "Delete Thread Pool failed because of: ", str));
report.setActionExitCode(ExitCode.FAILURE);
report.setFailureCause(e);
}
}
use of org.jvnet.hk2.config.TransactionFailure in project Payara by payara.
the class ApplicationLifecycle method prepareAppConfigChanges.
// prepare application config change for later registering
// in the domain.xml
@Override
public Transaction prepareAppConfigChanges(final DeploymentContext context) throws TransactionFailure {
final Properties appProps = context.getAppProps();
final DeployCommandParameters deployParams = context.getCommandParameters(DeployCommandParameters.class);
Transaction t = new Transaction();
try {
// prepare the application element
ConfigBean newBean = ((ConfigBean) ConfigBean.unwrap(applications)).allocate(Application.class);
Application app = newBean.createProxy();
Application app_w = t.enroll(app);
setInitialAppAttributes(app_w, deployParams, appProps, context);
context.addTransientAppMetaData(ServerTags.APPLICATION, app_w);
} catch (TransactionFailure e) {
t.rollback();
throw e;
} catch (Exception e) {
t.rollback();
throw new TransactionFailure(e.getMessage(), e);
}
return t;
}
use of org.jvnet.hk2.config.TransactionFailure in project Payara by payara.
the class UpdateResourceRef method execute.
/**
* Execution method for updating the configuration of a ResourceRef. Will be
* replicated if the target is a cluster.
*
* @param context context for the command.
*/
@Override
public void execute(AdminCommandContext context) {
final ActionReport report = context.getActionReport();
final Logger logger = context.getLogger();
// Make a list of all ResourceRefs that need to change
List<ResourceRef> resourceRefsToChange = new ArrayList<>();
// Add the ResourceRef from a named server if the target is a server
Server server = domain.getServerNamed(target);
// if the target is a server
if (server != null) {
ResourceRef serverResourceRef = server.getResourceRef(name);
// if the ResourceRef doesn't exist
if (serverResourceRef == null) {
report.failure(logger, LOCAL_STRINGS.getLocalString("resource.ref.not.exists", "Target {1} does not have a reference to resource {0}.", name, target));
return;
}
resourceRefsToChange.add(serverResourceRef);
}
// Add the ResourceRef from a named config if the target is a config
Config config = domain.getConfigNamed(target);
// if the target is a config
if (config != null) {
ResourceRef configResourceRef = config.getResourceRef(name);
// if the ResourceRef doesn't exist
if (configResourceRef == null) {
report.failure(logger, LOCAL_STRINGS.getLocalString("resource.ref.not.exists", "Target {1} does not have a reference to resource {0}.", name, target));
return;
}
resourceRefsToChange.add(configResourceRef);
}
// Add the ResourceRefs from a named cluster if the target is a cluster
Cluster cluster = domain.getClusterNamed(target);
// if the target is a cluster
if (cluster != null) {
ResourceRef clusterResourceRef = cluster.getResourceRef(name);
// if the ResourceRef doesn't exist
if (clusterResourceRef == null) {
report.failure(logger, LOCAL_STRINGS.getLocalString("resource.ref.not.exists", "Target {1} does not have a reference to resource {0}.", name, target));
return;
}
resourceRefsToChange.add(clusterResourceRef);
for (Server instance : cluster.getInstances()) {
ResourceRef instanceResourceRef = instance.getResourceRef(name);
// if the server in the cluster contains the ResourceRef
if (instanceResourceRef != null) {
resourceRefsToChange.add(instanceResourceRef);
}
}
}
// Add the ResourceRefs from a named Deployment Group if the target is a Deployment Group
DeploymentGroup dg = domain.getDeploymentGroupNamed(target);
if (dg != null) {
ResourceRef ref = dg.getResourceRef(name);
if (ref == null) {
report.failure(logger, LOCAL_STRINGS.getLocalString("resource.ref.not.exists", "Target {1} does not have a reference to resource {0}.", name, target));
return;
}
resourceRefsToChange.add(ref);
for (Server instance : dg.getInstances()) {
ResourceRef instanceResourceRef = instance.getResourceRef(name);
// if the server in the dg contains the ResourceRef
if (instanceResourceRef != null) {
resourceRefsToChange.add(instanceResourceRef);
}
}
}
// Apply the configuration to the listed ResourceRefs
try {
ConfigSupport.apply(new ConfigCode() {
@Override
public Object run(ConfigBeanProxy... params) throws PropertyVetoException, TransactionFailure {
for (ConfigBeanProxy proxy : params) {
if (proxy instanceof ResourceRef) {
ResourceRef resourceRefProxy = (ResourceRef) proxy;
if (enabled != null) {
resourceRefProxy.setEnabled(enabled.toString());
}
}
}
report.setActionExitCode(ActionReport.ExitCode.SUCCESS);
return true;
}
}, resourceRefsToChange.toArray(new ResourceRef[] {}));
} catch (TransactionFailure ex) {
report.failure(logger, ex.getLocalizedMessage());
}
}
use of org.jvnet.hk2.config.TransactionFailure in project Payara by payara.
the class CreateProfilerTest method tearDown.
@After
public void tearDown() throws TransactionFailure {
// Delete the created profiler
ConfigSupport.apply(new SingleConfigCode<JavaConfig>() {
public Object run(JavaConfig param) throws PropertyVetoException, TransactionFailure {
if (param.getProfiler() != null) {
param.setProfiler(null);
}
return null;
}
}, javaConfig);
parameters = new ParameterMap();
}
Aggregations