use of org.jvnet.hk2.config.SingleConfigCode in project Payara by payara.
the class JndiResourceManager method create.
public ResourceStatus create(Resources resources, HashMap attributes, final Properties properties, String target) throws Exception {
setAttributes(attributes, target);
ResourceStatus validationStatus = isValid(resources, true, target);
if (validationStatus.getStatus() == ResourceStatus.FAILURE) {
return validationStatus;
}
try {
ConfigSupport.apply(new SingleConfigCode<Resources>() {
public Object run(Resources param) throws PropertyVetoException, TransactionFailure {
return createResource(param, properties);
}
}, resources);
resourceUtil.createResourceRef(jndiName, enabledValueForTarget, target);
} catch (TransactionFailure tfe) {
String msg = localStrings.getLocalString("create.jndi.resource.fail", "Unable to create jndi resource {0}.", jndiName) + " " + tfe.getLocalizedMessage();
return new ResourceStatus(ResourceStatus.FAILURE, msg, true);
}
String msg = localStrings.getLocalString("create.jndi.resource.success", "jndi resource {0} created.", jndiName);
return new ResourceStatus(ResourceStatus.SUCCESS, msg, true);
}
use of org.jvnet.hk2.config.SingleConfigCode in project Payara by payara.
the class DeleteJndiResourceTest method tearDown.
@After
public void tearDown() throws TransactionFailure {
parameters = new ParameterMap();
ConfigSupport.apply(new SingleConfigCode<Resources>() {
public Object run(Resources param) throws PropertyVetoException, TransactionFailure {
Resource target = null;
for (Resource resource : param.getResources()) {
if (resource instanceof BindableResource) {
BindableResource r = (BindableResource) resource;
if (r.getJndiName().equals("sample_jndi_resource") || r.getJndiName().equals("dupRes")) {
target = resource;
break;
}
}
}
if (target != null) {
param.getResources().remove(target);
}
return null;
}
}, resources);
}
use of org.jvnet.hk2.config.SingleConfigCode in project Payara by payara.
the class CreateProtocolFilter method execute.
@Override
public void execute(AdminCommandContext context) {
Target targetUtil = services.getService(Target.class);
Config newConfig = targetUtil.getConfig(target);
if (newConfig != null) {
config = newConfig;
}
report = context.getActionReport();
try {
final Protocols protocols = config.getNetworkConfig().getProtocols();
final Protocol protocol = protocols.findProtocol(protocolName);
validate(protocol, LogFacade.CREATE_HTTP_FAIL_PROTOCOL_NOT_FOUND, protocolName);
final Class<?> filterClass = Thread.currentThread().getContextClassLoader().loadClass(classname);
if (!org.glassfish.grizzly.filterchain.Filter.class.isAssignableFrom(filterClass)) {
report.setMessage(MessageFormat.format(rb.getString(LogFacade.CREATE_PORTUNIF_FAIL_NOTFILTER), name, classname));
report.setActionExitCode(ActionReport.ExitCode.FAILURE);
return;
}
ProtocolChainInstanceHandler handler = getHandler(protocol);
ProtocolChain chain = getChain(handler);
ConfigSupport.apply(new SingleConfigCode<ProtocolChain>() {
@Override
public Object run(ProtocolChain param) throws PropertyVetoException, TransactionFailure {
final List<ProtocolFilter> list = param.getProtocolFilter();
for (ProtocolFilter filter : list) {
if (name.equals(filter.getName())) {
throw new TransactionFailure(String.format("A protocol filter named %s already exists.", name));
}
}
final ProtocolFilter filter = param.createChild(ProtocolFilter.class);
filter.setName(name);
filter.setClassname(classname);
list.add(filter);
return null;
}
}, chain);
} catch (ValidationFailureException e) {
return;
} catch (Exception e) {
e.printStackTrace();
report.setMessage(MessageFormat.format(rb.getString(LogFacade.CREATE_PORTUNIF_FAIL), name, e.getMessage() == null ? "No reason given" : e.getMessage()));
report.setActionExitCode(ActionReport.ExitCode.FAILURE);
report.setFailureCause(e);
return;
}
}
use of org.jvnet.hk2.config.SingleConfigCode in project Payara by payara.
the class DeleteHttpRedirect method execute.
// ----------------------------------------------- Methods from AdminCommand
@Override
public void execute(AdminCommandContext context) {
Target targetUtil = services.getService(Target.class);
Config newConfig = targetUtil.getConfig(target);
if (newConfig != null) {
config = newConfig;
}
Protocol protocolToBeRemoved = null;
ActionReport report = context.getActionReport();
NetworkConfig networkConfig = config.getNetworkConfig();
Protocols protocols = networkConfig.getProtocols();
try {
for (Protocol protocol : protocols.getProtocol()) {
if (protocolName.equalsIgnoreCase(protocol.getName())) {
protocolToBeRemoved = protocol;
}
}
if (protocolToBeRemoved == null) {
report.setMessage(MessageFormat.format(rb.getString(LogFacade.DELETE_HTTP_NOTEXISTS), protocolName));
report.setActionExitCode(ActionReport.ExitCode.FAILURE);
return;
}
// check if the protocol whose http to be deleted is being used by
// any network listener, then do not delete it.
List<NetworkListener> nwlsnrList = protocolToBeRemoved.findNetworkListeners();
for (NetworkListener nwlsnr : nwlsnrList) {
if (protocolToBeRemoved.getName().equals(nwlsnr.getProtocol())) {
report.setMessage(MessageFormat.format(rb.getString(LogFacade.DELETE_PROTOCOL_BEING_USED), protocolName, nwlsnr.getName()));
report.setActionExitCode(ActionReport.ExitCode.FAILURE);
return;
}
}
ConfigSupport.apply(new SingleConfigCode<Protocol>() {
public Object run(Protocol param) {
param.setHttpRedirect(null);
return null;
}
}, protocolToBeRemoved);
} catch (TransactionFailure e) {
report.setMessage(MessageFormat.format(rb.getString(LogFacade.DELETE_HTTP_FAIL), protocolName) + e.getLocalizedMessage());
report.setActionExitCode(ActionReport.ExitCode.FAILURE);
report.setFailureCause(e);
return;
}
report.setActionExitCode(ActionReport.ExitCode.SUCCESS);
}
use of org.jvnet.hk2.config.SingleConfigCode in project Payara by payara.
the class DeleteProtocolFilter method execute.
@Override
public void execute(AdminCommandContext context) {
Target targetUtil = services.getService(Target.class);
Config newConfig = targetUtil.getConfig(target);
if (newConfig != null) {
config = newConfig;
}
report = context.getActionReport();
try {
final Protocols protocols = config.getNetworkConfig().getProtocols();
final Protocol protocol = protocols.findProtocol(protocolName);
validate(protocol, LogFacade.CREATE_HTTP_FAIL_PROTOCOL_NOT_FOUND, protocolName);
ProtocolChainInstanceHandler handler = getHandler(protocol);
ProtocolChain chain = getChain(handler);
ConfigSupport.apply(new SingleConfigCode<ProtocolChain>() {
@Override
public Object run(ProtocolChain param) throws PropertyVetoException, TransactionFailure {
final List<ProtocolFilter> list = param.getProtocolFilter();
List<ProtocolFilter> newList = new ArrayList<ProtocolFilter>();
for (final ProtocolFilter filter : list) {
if (!name.equals(filter.getName())) {
newList.add(filter);
}
}
if (list.size() == newList.size()) {
throw new RuntimeException(String.format("No filter named %s found for protocol %s", name, protocolName));
}
param.setProtocolFilter(newList);
return null;
}
}, chain);
cleanChain(chain);
cleanHandler(handler);
} catch (ValidationFailureException e) {
return;
} catch (Exception e) {
e.printStackTrace();
report.setMessage(MessageFormat.format(rb.getString(LogFacade.DELETE_FAIL), name, e.getMessage() == null ? "No reason given" : e.getMessage()));
report.setActionExitCode(ActionReport.ExitCode.FAILURE);
report.setFailureCause(e);
return;
}
}
Aggregations