use of org.jvnet.hk2.config.types.Property in project Payara by payara.
the class ConfigConfigSource method deleteValue.
public boolean deleteValue(String propertyName) throws TransactionFailure {
boolean result = false;
Config config = domainConfiguration.getConfigs().getConfigByName(configurationName);
if (config != null) {
for (Property object : config.getProperty()) {
if ((PROPERTY_PREFIX + propertyName).equals(object.getName())) {
ConfigSupport.deleteChild((ConfigBean) ConfigBean.unwrap(config), (ConfigBean) ConfigBean.unwrap(object));
result = true;
}
}
}
return result;
}
use of org.jvnet.hk2.config.types.Property in project Payara by payara.
the class GlassfishNetworkListener method configureHttpProtocol.
@Override
protected void configureHttpProtocol(final ServiceLocator habitat, final NetworkListener networkListener, final Http http, final FilterChainBuilder filterChainBuilder, boolean securityEnabled) {
if (httpAdapter == null) {
registerMonitoringStatsProviders();
final V3Mapper mapper = new V3Mapper(logger);
mapper.setPort(port);
mapper.setId(name);
final ContainerMapper containerMapper = new ContainerMapper(grizzlyService, this);
containerMapper.setMapper(mapper);
containerMapper.setDefaultHost(http.getDefaultVirtualServer());
containerMapper.setRequestURIEncoding(http.getUriEncoding());
containerMapper.configureMapper();
VirtualServer vs = null;
String webAppRootPath = null;
final Collection<VirtualServer> list = grizzlyService.getHabitat().getAllServices(VirtualServer.class);
final String vsName = http.getDefaultVirtualServer();
for (final VirtualServer virtualServer : list) {
if (virtualServer.getId().equals(vsName)) {
vs = virtualServer;
webAppRootPath = vs.getDocroot();
if (!grizzlyService.hasMapperUpdateListener() && vs.getProperty() != null && !vs.getProperty().isEmpty()) {
for (final Property p : vs.getProperty()) {
final String propertyName = p.getName();
if (propertyName.startsWith("alternatedocroot")) {
String value = p.getValue();
String[] mapping = value.split(" ");
if (mapping.length != 2) {
logger.log(Level.WARNING, "Invalid alternate_docroot {0}", value);
continue;
}
String docBase = mapping[1].substring("dir=".length());
String urlPattern = mapping[0].substring("from=".length());
containerMapper.addAlternateDocBase(urlPattern, docBase);
}
}
}
break;
}
}
httpAdapter = new HttpAdapterImpl(vs, containerMapper, webAppRootPath);
containerMapper.addDocRoot(webAppRootPath);
AbstractActiveDescriptor<V3Mapper> aad = BuilderHelper.createConstantDescriptor(mapper);
aad.addContractType(Mapper.class);
aad.setName(address.toString() + port);
ServiceLocatorUtilities.addOneDescriptor(grizzlyService.getHabitat(), aad);
super.configureHttpProtocol(habitat, networkListener, http, filterChainBuilder, securityEnabled);
final Protocol protocol = http.getParent();
for (NetworkListener listener : protocol.findNetworkListeners()) {
grizzlyService.notifyMapperUpdateListeners(listener, mapper);
}
} else {
super.configureHttpProtocol(habitat, networkListener, http, filterChainBuilder, securityEnabled);
}
}
use of org.jvnet.hk2.config.types.Property 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.types.Property in project Payara by payara.
the class DeleteSystemProperty 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();
Property domainProp = domain.getProperty("administrative.domain.name");
String domainName = domainProp.getValue();
if (!spb.containsProperty(propName)) {
report.setActionExitCode(ActionReport.ExitCode.FAILURE);
String msg = localStrings.getLocalString("no.such.property", "System Property named {0} does not exist at the given target {1}", propName, target);
report.setMessage(msg);
return;
}
if (definitions(propName) == 1) {
// implying user is deleting the "last" definition of this property
List<String> refs = new ArrayList<String>();
List<Dom> doms = new ArrayList<Dom>();
if ("domain".equals(target) || target.equals(domainName)) {
for (Server s : domain.getServers().getServer()) {
Config config = s.getConfig();
Cluster cluster = s.getCluster();
if (!s.containsProperty(propName) && !config.containsProperty(propName)) {
if (cluster != null) {
if (!cluster.containsProperty(propName)) {
doms.add(Dom.unwrap(s));
}
} else {
doms.add(Dom.unwrap(s));
}
}
}
} else {
Config config = domain.getConfigNamed(target);
if (config != null) {
doms.add(Dom.unwrap(config));
String configName = config.getName();
for (Server s : domain.getServers().getServer()) {
String configRef = s.getConfigRef();
if (configRef.equals(configName)) {
if (!s.containsProperty(propName)) {
doms.add(Dom.unwrap(s));
}
}
}
for (Cluster c : domain.getClusters().getCluster()) {
String configRef = c.getConfigRef();
if (configRef.equals(configName)) {
if (!c.containsProperty(propName)) {
doms.add(Dom.unwrap(c));
}
}
}
} else {
Cluster cluster = domain.getClusterNamed(target);
if (cluster != null) {
doms.add(Dom.unwrap(cluster));
Config clusterConfig = domain.getConfigNamed(cluster.getConfigRef());
doms.add(Dom.unwrap(clusterConfig));
for (Server s : cluster.getInstances()) {
if (!s.containsProperty(propName)) {
doms.add(Dom.unwrap(s));
}
}
} else {
Server server = domain.getServerNamed(target);
doms.add(Dom.unwrap(server));
doms.add(Dom.unwrap(domain.getConfigNamed(server.getConfigRef())));
}
}
}
String sysPropName = SystemPropertyConstants.getPropertyAsValue(propName);
for (Dom d : doms) {
listRefs(d, sysPropName, refs);
}
if (!refs.isEmpty()) {
// there are some references
String msg = localStrings.getLocalString("cant.delete.referenced.property", "System Property {0} is referenced by {1} in the configuration. Please remove the references first.", propName, Arrays.toString(refs.toArray()));
report.setMessage(msg);
report.setActionExitCode(ActionReport.ExitCode.FAILURE);
return;
}
}
// now we are sure that the target exits in the config, just remove the given property
try {
ConfigSupport.apply(new SingleConfigCode<SystemPropertyBag>() {
public Object run(SystemPropertyBag param) throws PropertyVetoException, TransactionFailure {
param.getSystemProperty().remove(param.getSystemProperty(propName));
return param;
}
}, spb);
report.setActionExitCode(ActionReport.ExitCode.SUCCESS);
String msg = localStrings.getLocalString("delete.sysprops.ok", "System Property named {0} deleted from given target {1}. Make sure you check its references.", propName, target);
report.setMessage(msg);
} catch (TransactionFailure tf) {
report.setActionExitCode(ActionReport.ExitCode.FAILURE);
report.setFailureCause(tf);
}
}
use of org.jvnet.hk2.config.types.Property in project Payara by payara.
the class SetCommand method set.
private boolean set(AdminCommandContext context, SetOperation op) {
String pattern = op.pattern;
String value = op.value;
String target = op.target;
String attrName = op.attrName;
boolean isProperty = op.isProperty;
// now
// first let's get the parent for this pattern.
TreeNode[] parentNodes = getAliasedParent(domain, pattern);
// reset the pattern.
String prefix;
boolean lookAtSubNodes = true;
if (parentNodes[0].relativeName.length() == 0 || parentNodes[0].relativeName.equals("domain")) {
// handle the case where the pattern references an attribute of the top-level node
prefix = "";
// pattern is already set properly
lookAtSubNodes = false;
} else if (!pattern.startsWith(parentNodes[0].relativeName)) {
prefix = pattern.substring(0, pattern.indexOf(parentNodes[0].relativeName));
pattern = parentNodes[0].relativeName;
} else {
prefix = "";
pattern = parentNodes[0].relativeName;
}
String targetName = prefix + pattern;
if (modularityHelper != null) {
synchronized (utils) {
boolean oldv = utils.isCommandInvocation();
utils.setCommandInvocation(true);
modularityHelper.getLocationForDottedName(targetName);
utils.setCommandInvocation(oldv);
}
}
Map<Dom, String> matchingNodes;
boolean applyOverrideRules = false;
Map<Dom, String> dottedNames = new HashMap<Dom, String>();
if (lookAtSubNodes) {
for (TreeNode parentNode : parentNodes) {
dottedNames.putAll(getAllDottedNodes(parentNode.node));
}
matchingNodes = getMatchingNodes(dottedNames, pattern);
applyOverrideRules = true;
} else {
matchingNodes = new HashMap<Dom, String>();
for (TreeNode parentNode : parentNodes) {
matchingNodes.put(parentNode.node, pattern);
}
}
if (matchingNodes.isEmpty()) {
// it's possible they are trying to create a property object.. lets check this.
// strip out the property name
pattern = target.substring(0, trueLastIndexOf(target, '.'));
if (pattern.endsWith("property")) {
// need to find the right parent.
Dom parentNode = null;
if ("property".equals(pattern)) {
// create and set the property
try {
final String fname = attrName;
final String fvalue = value;
ConfigSupport.apply(new SingleConfigCode<Domain>() {
@Override
public Object run(Domain domain) throws PropertyVetoException, TransactionFailure {
Property p = domain.createChild(Property.class);
p.setName(fname);
p.setValue(fvalue);
domain.getProperty().add(p);
return p;
}
}, domain);
success(context, targetName, value);
runLegacyChecks(context);
if (targetService.isThisDAS() && !replicateSetCommand(context, target, value)) {
return false;
}
return true;
} catch (TransactionFailure transactionFailure) {
// fail(context, "Could not change the attributes: " +
// transactionFailure.getMessage(), transactionFailure);
fail(context, localStrings.getLocalString("admin.set.attribute.change.failure", "Could not change the attributes: {0}", transactionFailure.getMessage()), transactionFailure);
return false;
}
} else {
pattern = pattern.substring(0, trueLastIndexOf(pattern, '.'));
parentNodes = getAliasedParent(domain, pattern);
}
pattern = parentNodes[0].relativeName;
matchingNodes = getMatchingNodes(dottedNames, pattern);
if (matchingNodes.isEmpty()) {
// fail(context, "No configuration found for " + targetName);
fail(context, localStrings.getLocalString("admin.set.configuration.notfound", "No configuration found for {0}", targetName));
return false;
}
for (Map.Entry<Dom, String> node : matchingNodes.entrySet()) {
if (node.getValue().equals(pattern)) {
parentNode = node.getKey();
}
}
if (parentNode == null) {
// fail(context, "No configuration found for " + targetName);
fail(context, localStrings.getLocalString("admin.set.configuration.notfound", "No configuration found for {0}", targetName));
return false;
}
if (value == null || value.length() == 0) {
// setting to the empty string means to remove the property, so don't create it
success(context, targetName, value);
return true;
}
// create and set the property
Map<String, String> attributes = new HashMap<String, String>();
attributes.put("value", value);
attributes.put("name", attrName);
try {
if (!(parentNode instanceof ConfigBean)) {
final ClassCastException cce = new ClassCastException(parentNode.getClass().getName());
fail(context, localStrings.getLocalString("admin.set.attribute.change.failure", "Could not change the attributes: {0}", cce.getMessage(), cce));
return false;
}
ConfigSupport.createAndSet((ConfigBean) parentNode, Property.class, attributes);
success(context, targetName, value);
runLegacyChecks(context);
if (targetService.isThisDAS() && !replicateSetCommand(context, target, value)) {
return false;
}
return true;
} catch (TransactionFailure transactionFailure) {
// fail(context, "Could not change the attributes: " +
// transactionFailure.getMessage(), transactionFailure);
fail(context, localStrings.getLocalString("admin.set.attribute.change.failure", "Could not change the attributes: {0}", transactionFailure.getMessage()), transactionFailure);
return false;
}
}
}
Map<ConfigBean, Map<String, String>> changes = new HashMap<ConfigBean, Map<String, String>>();
boolean setElementSuccess = false;
boolean delPropertySuccess = false;
boolean delProperty = false;
Map<String, String> attrChanges = new HashMap<String, String>();
if (isProperty) {
attrName = "value";
if ((value == null) || (value.length() == 0)) {
delProperty = true;
}
attrChanges.put(attrName, value);
}
List<Map.Entry> mNodes = new ArrayList(matchingNodes.entrySet());
if (applyOverrideRules) {
mNodes = applyOverrideRules(mNodes);
}
for (Map.Entry<Dom, String> node : mNodes) {
final Dom targetNode = node.getKey();
for (String name : targetNode.model.getAttributeNames()) {
String finalDottedName = node.getValue() + "." + name;
if (matches(finalDottedName, pattern)) {
if (attrName.equals(name) || attrName.replace('_', '-').equals(name.replace('_', '-'))) {
if (isDeprecatedAttr(targetNode, name)) {
warning(context, localStrings.getLocalString("admin.set.deprecated", "Warning: The attribute {0} is deprecated.", finalDottedName));
}
if (!isProperty) {
targetName = prefix + finalDottedName;
if (value != null && value.length() > 0) {
attrChanges.put(name, value);
} else {
attrChanges.put(name, null);
}
} else {
targetName = prefix + node.getValue();
}
if (delProperty) {
// delete property element
String str = node.getValue();
if (trueLastIndexOf(str, '.') != -1) {
str = str.substring(trueLastIndexOf(str, '.') + 1);
}
try {
if (str != null) {
ConfigSupport.deleteChild((ConfigBean) targetNode.parent(), (ConfigBean) targetNode);
delPropertySuccess = true;
}
} catch (IllegalArgumentException ie) {
fail(context, localStrings.getLocalString("admin.set.delete.property.failure", "Could not delete the property: {0}", ie.getMessage()), ie);
return false;
} catch (TransactionFailure transactionFailure) {
fail(context, localStrings.getLocalString("admin.set.attribute.change.failure", "Could not change the attributes: {0}", transactionFailure.getMessage()), transactionFailure);
return false;
}
} else {
changes.put((ConfigBean) node.getKey(), attrChanges);
}
}
}
}
for (String name : targetNode.model.getLeafElementNames()) {
String finalDottedName = node.getValue() + "." + name;
if (matches(finalDottedName, pattern)) {
if (attrName.equals(name) || attrName.replace('_', '-').equals(name.replace('_', '-'))) {
if (isDeprecatedAttr(targetNode, name)) {
warning(context, localStrings.getLocalString("admin.set.elementdeprecated", "Warning: The element {0} is deprecated.", finalDottedName));
}
try {
setLeafElement((ConfigBean) targetNode, name, value);
} catch (TransactionFailure ex) {
fail(context, localStrings.getLocalString("admin.set.badelement", "Cannot change the element: {0}", ex.getMessage()), ex);
return false;
}
setElementSuccess = true;
break;
}
}
}
}
if (!changes.isEmpty()) {
try {
config.apply(changes);
success(context, targetName, value);
runLegacyChecks(context);
} catch (TransactionFailure transactionFailure) {
// fail(context, "Could not change the attributes: " +
// transactionFailure.getMessage(), transactionFailure);
fail(context, localStrings.getLocalString("admin.set.attribute.change.failure", "Could not change the attributes: {0}", transactionFailure.getMessage()), transactionFailure);
return false;
}
} else if (delPropertySuccess || setElementSuccess) {
success(context, targetName, value);
} else {
fail(context, localStrings.getLocalString("admin.set.configuration.notfound", "No configuration found for {0}", targetName));
return false;
}
if (targetService.isThisDAS() && !replicateSetCommand(context, target, value)) {
return false;
}
return true;
}
Aggregations