-
Notifications
You must be signed in to change notification settings - Fork 306
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7133 from Viii3/FISH-10318-System-Property
FISH-10318 Add create-system-property command
- Loading branch information
Showing
7 changed files
with
285 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
149 changes: 149 additions & 0 deletions
149
nucleus/core/kernel/src/main/java/fish/payara/enterprise/admin/CreateSystemProperty.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
/* | ||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. | ||
* | ||
* Copyright (c) 2024 Payara Foundation and/or its affiliates. All rights reserved. | ||
* | ||
* The contents of this file are subject to the terms of either the GNU | ||
* General Public License Version 2 only ("GPL") or the Common Development | ||
* and Distribution License("CDDL") (collectively, the "License"). You | ||
* may not use this file except in compliance with the License. You can | ||
* obtain a copy of the License at | ||
* https://github.com/payara/Payara/blob/main/LICENSE.txt | ||
* See the License for the specific | ||
* language governing permissions and limitations under the License. | ||
* | ||
* When distributing the software, include this License Header Notice in each | ||
* file and include the License file at glassfish/legal/LICENSE.txt. | ||
* | ||
* GPL Classpath Exception: | ||
* The Payara Foundation designates this particular file as subject to the "Classpath" | ||
* exception as provided by the Payara Foundation in the GPL Version 2 section of the License | ||
* file that accompanied this code. | ||
* | ||
* Modifications: | ||
* If applicable, add the following below the License Header, with the fields | ||
* enclosed by brackets [] replaced by your own identifying information: | ||
* "Portions Copyright [year] [name of copyright owner]" | ||
* | ||
* Contributor(s): | ||
* If you wish your version of this file to be governed by only the CDDL or | ||
* only the GPL Version 2, indicate your decision by adding "[Contributor] | ||
* elects to include this software in this distribution under the [CDDL or GPL | ||
* Version 2] license." If you don't indicate a single choice of license, a | ||
* recipient has the option to distribute your version of this file under | ||
* either the CDDL, the GPL Version 2 or to extend the choice of license to | ||
* its licensees as provided above. However, if you add GPL Version 2 code | ||
* and therefore, elected the GPL Version 2 license, then the option applies | ||
* only if the new code is made subject to such option by the copyright | ||
* holder. | ||
*/ | ||
|
||
package fish.payara.enterprise.admin; | ||
|
||
import com.sun.enterprise.config.serverbeans.Domain; | ||
import com.sun.enterprise.config.serverbeans.SystemProperty; | ||
import com.sun.enterprise.config.serverbeans.SystemPropertyBag; | ||
import com.sun.enterprise.util.LocalStringManagerImpl; | ||
import com.sun.enterprise.util.SystemPropertyConstants; | ||
import com.sun.enterprise.v3.admin.CLIUtil; | ||
import com.sun.enterprise.v3.admin.CreateSystemProperties; | ||
import jakarta.inject.Inject; | ||
import org.glassfish.api.ActionReport; | ||
import org.glassfish.api.I18n; | ||
import org.glassfish.api.Param; | ||
import org.glassfish.api.admin.AccessRequired; | ||
import org.glassfish.api.admin.AdminCommand; | ||
import org.glassfish.api.admin.AdminCommandContext; | ||
import org.glassfish.api.admin.AdminCommandSecurity; | ||
import org.glassfish.api.admin.ExecuteOn; | ||
import org.glassfish.api.admin.RuntimeType; | ||
import org.glassfish.config.support.CommandTarget; | ||
import org.glassfish.config.support.TargetType; | ||
import org.glassfish.hk2.api.PerLookup; | ||
import org.jvnet.hk2.annotations.Service; | ||
import org.jvnet.hk2.config.ConfigSupport; | ||
import org.jvnet.hk2.config.Transaction; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
|
||
@Service(name="create-system-property") | ||
@PerLookup | ||
@ExecuteOn({RuntimeType.DAS, RuntimeType.INSTANCE}) | ||
@TargetType(value={CommandTarget.CLUSTER, | ||
CommandTarget.CONFIG, CommandTarget.DAS, CommandTarget.DOMAIN, CommandTarget.STANDALONE_INSTANCE,CommandTarget.CLUSTERED_INSTANCE}) | ||
@I18n("create.system.property") | ||
public class CreateSystemProperty implements AdminCommand, AdminCommandSecurity.Preauthorization, AdminCommandSecurity.AccessCheckProvider { | ||
final private static LocalStringManagerImpl localStrings = new LocalStringManagerImpl(CreateSystemProperties.class); | ||
|
||
@Param(optional=true, defaultValue=SystemPropertyConstants.DAS_SERVER_NAME) | ||
String target; | ||
|
||
@Param(name="name_value", primary=true) | ||
String propertyAssignment; | ||
|
||
@Inject | ||
Domain domain; | ||
|
||
private SystemPropertyBag spb; | ||
|
||
@Override | ||
public void execute (AdminCommandContext context) { | ||
final ActionReport report = context.getActionReport(); | ||
String[] assignment = propertyAssignment.split("="); | ||
String propertyName; | ||
String propertyValue; | ||
|
||
try { | ||
propertyName = assignment[0]; | ||
propertyValue = assignment[1]; | ||
ConfigSupport.apply(propertyBag -> { | ||
// update existing system property | ||
for (SystemProperty property : propertyBag.getSystemProperty()) { | ||
if (property.getName().equals(propertyName)) { | ||
Transaction transaction = Transaction.getTransaction(propertyBag); | ||
property = transaction.enroll(property); | ||
property.setValue(propertyValue); | ||
return property; | ||
} | ||
} | ||
|
||
// create system-property | ||
SystemProperty newSysProp = propertyBag.createChild(SystemProperty.class); | ||
newSysProp.setName(propertyName); | ||
newSysProp.setValue(propertyValue); | ||
propertyBag.getSystemProperty().add(newSysProp); | ||
return newSysProp; | ||
}, spb); | ||
report.setActionExitCode(ActionReport.ExitCode.SUCCESS); | ||
} catch(Exception e) { | ||
report.setMessage(localStrings.getLocalString("create.system.property.failed", | ||
"System property {0} creation failed", propertyAssignment)); | ||
report.setActionExitCode(ActionReport.ExitCode.FAILURE); | ||
report.setFailureCause(e); | ||
} | ||
} | ||
|
||
@Override | ||
public Collection<? extends AccessRequired.AccessCheck<?>> getAccessChecks () { | ||
final Collection<AccessRequired.AccessCheck<?>> result = new ArrayList<>(); | ||
result.add(new AccessRequired.AccessCheck<>(AccessRequired.Util.resourceNameFromConfigBeanProxy(spb), "update")); | ||
return result; | ||
} | ||
|
||
@Override | ||
public boolean preAuthorization (AdminCommandContext context) { | ||
this.spb = CLIUtil.chooseTarget(domain, target); | ||
if (this.spb == null) { | ||
final ActionReport report = context.getActionReport(); | ||
report.setActionExitCode(ActionReport.ExitCode.FAILURE); | ||
String msg = localStrings.getLocalString( | ||
"invalid.target.sys.props", | ||
"Invalid target:{0}. Valid targets types are domain, config, cluster, default server, clustered instance, stand alone instance", target | ||
); | ||
report.setMessage(msg); | ||
return false; | ||
} | ||
return true; | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
nucleus/core/kernel/src/main/java/fish/payara/enterprise/admin/LocalStrings.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# | ||
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. | ||
# | ||
# Copyright (c) 2024 Payara Foundation and/or its affiliates. All rights reserved. | ||
# | ||
# The contents of this file are subject to the terms of either the GNU | ||
# General Public License Version 2 only ("GPL") or the Common Development | ||
# and Distribution License("CDDL") (collectively, the "License"). You | ||
# may not use this file except in compliance with the License. You can | ||
# obtain a copy of the License at | ||
# https://github.com/payara/Payara/blob/main/LICENSE.txt | ||
# See the License for the specific | ||
# language governing permissions and limitations under the License. | ||
# | ||
# When distributing the software, include this License Header Notice in each | ||
# file and include the License file at glassfish/legal/LICENSE.txt. | ||
# | ||
# GPL Classpath Exception: | ||
# The Payara Foundation designates this particular file as subject to the "Classpath" | ||
# exception as provided by the Payara Foundation in the GPL Version 2 section of the License | ||
# file that accompanied this code. | ||
# | ||
# Modifications: | ||
# If applicable, add the following below the License Header, with the fields | ||
# enclosed by brackets [] replaced by your own identifying information: | ||
# "Portions Copyright [year] [name of copyright owner]" | ||
# | ||
# Contributor(s): | ||
# If you wish your version of this file to be governed by only the CDDL or | ||
# only the GPL Version 2, indicate your decision by adding "[Contributor] | ||
# elects to include this software in this distribution under the [CDDL or GPL | ||
# Version 2] license." If you don't indicate a single choice of license, a | ||
# recipient has the option to distribute your version of this file under | ||
# either the CDDL, the GPL Version 2 or to extend the choice of license to | ||
# its licensees as provided above. However, if you add GPL Version 2 code | ||
# and therefore, elected the GPL Version 2 license, then the option applies | ||
# only if the new code is made subject to such option by the copyright | ||
# holder. | ||
# | ||
|
||
create.system.property=adds or updates a single system property of the domain, configuration, cluster, or server instance | ||
create.system.property.name_value=The system property assignment to create. | ||
create.system.property.existsAlready=System property {0} already exists. | ||
create.system.property.failed=System property {0} creation failed. | ||
create.system.property.success=System property {0} created successfully. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
nucleus/core/kernel/src/main/manpages/fish/payara/enterprise/admin/create-system-property.1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
create-system-property(1) asadmin Utility Subcommands create-system-property(1) | ||
|
||
NAME | ||
create-system-property - adds or updates a single system property | ||
of the domain, configuration, cluster, or server instance | ||
|
||
SYNOPSIS | ||
create-system-property [--help] | ||
[--target target] | ||
[name=value] | ||
|
||
DESCRIPTION | ||
The create-system-property subcommand adds or updates a single system | ||
property that can be referenced elsewhere on the server. | ||
|
||
Payara Server provides hooks where tokens (system properties) can be | ||
specified. Because Payara Server does not have multiple server | ||
elements, you can specify a particular token at any level. When a | ||
domain supports multiple servers, the override potential can be | ||
exploited. When a domain is started or restarted, all <system-property> | ||
elements are resolved and available to the Java Virtual Machine by | ||
using the System.setProperty() call on each of them (with its name and | ||
value derived from the corresponding attributes of the element). This | ||
is analogous to sending the elements as -D parameters on the Java | ||
command line. | ||
|
||
This subcommand is supported in remote mode only. | ||
|
||
OPTIONS | ||
--help, -? | ||
Displays the help text for the subcommand. | ||
|
||
--target | ||
The target on which you are creating the system property. | ||
|
||
OPERANDS | ||
target | ||
The valid targets for this subcommand are instance, cluster, | ||
configuration, domain, and server. Server is the default option. | ||
Valid values are: | ||
|
||
server | ||
Creates the property on the default server instance. This is | ||
the default value. | ||
|
||
domain | ||
Creates the property for all server instances in the default | ||
domain. | ||
|
||
configuration_name | ||
Creates the property in the specified configuration. | ||
|
||
cluster_name | ||
Creates the property on all server instances in the specified | ||
cluster. | ||
|
||
instance_name | ||
Creates the property on a specified server instance. | ||
|
||
name_value | ||
The system property to assign. In the form of "PropertyName=PropertyValue" | ||
|
||
EXAMPLES | ||
Example 1, Creating System Property | ||
This example creates a system property associated with an HTTP | ||
listener on a server instance named myserver. | ||
|
||
asadmin> create-system-property --target myserver http-listener-port=1088 | ||
Command create-system-property executed successfully. | ||
|
||
EXIT STATUS | ||
0 | ||
subcommand executed successfully | ||
|
||
1 | ||
error in executing the subcommand | ||
|
||
SEE ALSO | ||
create-system-properties(1), delete-system-property(1), list-system-properties(1) | ||
|
||
asadmin(1M) | ||
|
||
Java EE 8 17 Dec 2024 create-system-property(1) |