Skip to content

Commit

Permalink
Additional cleanups - formatting, syntax, logging
Browse files Browse the repository at this point in the history
Signed-off-by: David Matějček <[email protected]>
  • Loading branch information
dmatej committed Jan 20, 2025
1 parent e384bdb commit dbcb8eb
Show file tree
Hide file tree
Showing 10 changed files with 137 additions and 162 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,13 @@ public String[] addJvmOptions(String[] options) throws InvalidJvmOptionException
}
final Set alreadyExist = new HashSet();
JvmOptionsElement last = last();
for (int i = 0; i < options.length; i++) {
if (!head.hasOption(options[i])) {
JvmOptionsElement x = new JvmOptionsElement(options[i]);
for (String option : options) {
if (!head.hasOption(option)) {
JvmOptionsElement x = new JvmOptionsElement(option);
last.setNext(x);
last = x;
} else {
alreadyExist.add(options[i]);
alreadyExist.add(option);
}
}
return toStringArray(alreadyExist);
Expand Down Expand Up @@ -115,9 +115,9 @@ public String[] deleteJvmOptions(String[] options) {
}

final Set donotExist = new HashSet();
for (int i = 0; i < options.length; i++) {
if (!head.deleteJvmOption(options[i])) {
donotExist.add(options[i]);
for (String option : options) {
if (!head.deleteJvmOption(option)) {
donotExist.add(option);
}
}
return toStringArray(donotExist);
Expand Down Expand Up @@ -209,6 +209,7 @@ void setNext(JvmOptionsElement element) {
throw new UnsupportedOperationException();
}
};

private final Set jvmOptions = new LinkedHashSet();
private JvmOptionsElement next;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/*
* Copyright (c) 2025 Contributors to the Eclipse Foundation
* Copyright (c) 2010, 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
Expand All @@ -24,6 +25,7 @@
import jakarta.inject.Inject;

import java.io.File;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;

Expand Down Expand Up @@ -86,15 +88,12 @@ public void execute(AdminCommandContext context) {
TokenResolver resolver = null;

// Create a resolver that can replace system properties in strings
Map<String, String> systemPropsMap =
new HashMap<String, String>((Map)(System.getProperties()));
Map<String, String> systemPropsMap = new HashMap<String, String>((Map) (System.getProperties()));
resolver = new TokenResolver(systemPropsMap);
String resolvedInstallDir = resolver.resolve(installdir);
File actualInstallDir = new File( resolvedInstallDir+"/" + NodeUtils.LANDMARK_FILE);


if (!actualInstallDir.exists()){
report.setMessage(Strings.get("invalid.installdir",installdir));
Path resolvedInstallDir = new File(resolver.resolve(installdir)).toPath();
Path actualInstallDir = resolvedInstallDir.resolve(NodeUtils.LANDMARK_FILE);
if (!actualInstallDir.toFile().exists()) {
report.setMessage(Strings.get("invalid.installdir", installdir));
report.setActionExitCode(ActionReport.ExitCode.FAILURE);
return;
}
Expand All @@ -103,12 +102,15 @@ public void execute(AdminCommandContext context) {
CommandInvocation ci = cr.getCommandInvocation("_create-node", report, context.getSubject());
ParameterMap map = new ParameterMap();
map.add("DEFAULT", name);
if (StringUtils.ok(nodedir))
if (StringUtils.ok(nodedir)) {
map.add(NodeUtils.PARAM_NODEDIR, nodedir);
if (StringUtils.ok(installdir))
}
if (StringUtils.ok(installdir)) {
map.add(NodeUtils.PARAM_INSTALLDIR, installdir);
if (StringUtils.ok(nodehost))
}
if (StringUtils.ok(nodehost)) {
map.add(NodeUtils.PARAM_NODEHOST, nodehost);
}
map.add(NodeUtils.PARAM_TYPE,"CONFIG");
ci.parameters(map);
ci.execute();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,12 @@ public abstract class SecureAdminBootstrapHelper implements AutoCloseable {
Path.of("config", "keystore.jks"),
Path.of("config", "cacerts.jks")
};
private static final String[] SECURE_ADMIN_FILE_DIRS_TO_CREATE = new String[] {"config"};
private static final Path[] SECURE_ADMIN_FILE_DIRS_TO_CREATE = new Path[] {Path.of("config")};

/**
* Creates a new helper for delivering files needed for secure admin to the remote instance.
* @param sshL
*
* @param adminHost host or ip address of host which hosts DAS.
* @param sshL
* @param dasInstanceDir directory of the local instance - source for the required files
* @param remoteNodeDir directory of the remote node on the remote system
* @param instance name of the instance on the remote node to bootstrap
Expand Down Expand Up @@ -111,7 +110,7 @@ public static SecureAdminBootstrapHelper getLocalHelper(final File existingInsta
/**
* Cleans up any allocated resources.
*/
protected abstract void mkdirs(String dirURI) throws IOException;
protected abstract void mkdirs(Path dir) throws IOException;

@Override
public void close() {
Expand Down Expand Up @@ -164,7 +163,7 @@ public void bootstrapInstance() throws BootstrapException {
}

private void mkdirs() throws IOException {
for (String dirPath : SECURE_ADMIN_FILE_DIRS_TO_CREATE) {
for (Path dirPath : SECURE_ADMIN_FILE_DIRS_TO_CREATE) {
mkdirs(dirPath);
}
}
Expand Down Expand Up @@ -248,8 +247,8 @@ private SSHHelper(
}

@Override
protected void mkdirs(String dir) throws IOException {
Path remoteDir = remoteInstanceDir.resolve(Path.of(dir));
protected void mkdirs(Path dir) throws IOException {
Path remoteDir = remoteInstanceDir.resolve(dir);
LOG.log(Level.DEBUG, "Trying to create directories for remote path {0}", remoteDir);
int instanceDirPermissions;
try {
Expand Down Expand Up @@ -329,8 +328,8 @@ private LocalHelper(final File existingInstanceDir, final File newInstanceDir) {
}

@Override
protected void mkdirs(String dir) {
final File newDir = new File(newInstanceDirURI.resolve(dir));
protected void mkdirs(Path dir) {
final File newDir = Path.of(newInstanceDirURI).resolve(dir).toFile();
if (!newDir.exists() && !newDir.mkdirs()) {
throw new RuntimeException(Strings.get("secure.admin.boot.errCreDir", newDir.getAbsolutePath()));
}
Expand All @@ -339,15 +338,15 @@ protected void mkdirs(String dir) {
@Override
public void copyBootstrapFiles() throws IOException {
for (Path relativePathToFile : SECURE_ADMIN_FILE_REL_URIS_TO_COPY) {
final File origin = new File(existingInstanceDirURI.resolve(relativePathToFile.toString()));
final File dest = new File(newInstanceDirURI.resolve(relativePathToFile.toString()));
final File origin = Path.of(existingInstanceDirURI).resolve(relativePathToFile).toFile();
final File dest = Path.of(newInstanceDirURI).resolve(relativePathToFile).toFile();
FileUtils.copy(origin, dest);
}
}

@Override
protected void backdateInstanceDomainXML() throws BootstrapException {
final File newDomainXMLFile = new File(newInstanceDirURI.resolve(DOMAIN_XML_PATH.toString()));
final File newDomainXMLFile = Path.of(newInstanceDirURI).resolve(DOMAIN_XML_PATH).toFile();
if (!newDomainXMLFile.setLastModified(0)) {
throw new RuntimeException(Strings.get("secure.admin.boot.errSetLastMod", newDomainXMLFile.getAbsolutePath()));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/*
* Copyright (c) 2025 Contributors to the Eclipse Foundation
* Copyright (c) 2010, 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
Expand Down Expand Up @@ -29,6 +30,7 @@

import java.beans.PropertyVetoException;
import java.io.File;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;
Expand Down Expand Up @@ -99,20 +101,20 @@ public class UpdateNodeCommand implements AdminCommand {
@Param(name="sshnodehost", optional=true)
String sshnodehost;

@Param(name="sshkeyfile", optional=true)
@Param(name = "sshkeyfile", optional = true)
String sshkeyfile;

@Param(name = "sshpassword", optional = true, password=true)
String sshpassword;
@Param(name = "sshpassword", optional = true, password = true)
String sshpassword;

@Param(name = "sshkeypassphrase", optional = true, password=true)
String sshkeypassphrase;
@Param(name = "sshkeypassphrase", optional = true, password = true)
String sshkeypassphrase;

@Param(name = "windowsdomain", optional = true)
String windowsdomain;
String windowsdomain;

@Param(name = "type", optional=true)
String type;
@Param(name = "type", optional = true)
String type;

@Override
public void execute(AdminCommandContext context) {
Expand All @@ -134,15 +136,12 @@ public void execute(AdminCommandContext context) {
TokenResolver resolver = null;

// Create a resolver that can replace system properties in strings
Map<String, String> systemPropsMap =
new HashMap<String, String>((Map)(System.getProperties()));
Map<String, String> systemPropsMap = new HashMap<String, String>((Map) (System.getProperties()));
resolver = new TokenResolver(systemPropsMap);
String resolvedInstallDir = resolver.resolve(installdir);
File actualInstallDir = new File( resolvedInstallDir+"/" + NodeUtils.LANDMARK_FILE);


if (!actualInstallDir.exists()){
report.setMessage(Strings.get("invalid.installdir",installdir));
Path resolvedInstallDir = new File(resolver.resolve(installdir)).toPath();
Path actualInstallDir = resolvedInstallDir.resolve(NodeUtils.LANDMARK_FILE);
if (!actualInstallDir.toFile().exists()) {
report.setMessage(Strings.get("invalid.installdir", installdir));
report.setActionExitCode(ActionReport.ExitCode.FAILURE);
return;
}
Expand Down Expand Up @@ -195,43 +194,56 @@ public Object run(ConfigBeanProxy param) throws PropertyVetoException, Transacti
Nodes nodes = ((Domain)param).getNodes();
Node node = nodes.getNode(nodeName);
Node writeableNode = t.enroll(node);
if (windowsdomain != null)
if (windowsdomain != null) {
writeableNode.setWindowsDomain(windowsdomain);
if (nodedir != null)
}
if (nodedir != null) {
writeableNode.setNodeDir(nodedir);
if (nodehost != null)
}
if (nodehost != null) {
writeableNode.setNodeHost(nodehost);
if (installdir != null)
}
if (installdir != null) {
writeableNode.setInstallDir(installdir);
if (type != null)
}
if (type != null) {
writeableNode.setType(type);
}
if (sshport != null || sshnodehost != null ||sshuser != null || sshkeyfile != null){
SshConnector sshC = writeableNode.getSshConnector();
if (sshC == null) {
sshC =writeableNode.createChild(SshConnector.class);
}else
} else {
sshC = t.enroll(sshC);
}

if (sshport != null)
if (sshport != null) {
sshC.setSshPort(sshport);
if(sshnodehost != null)
}
if(sshnodehost != null) {
sshC.setSshHost(sshnodehost);
}

if (sshuser != null || sshkeyfile != null || sshpassword != null || sshkeypassphrase != null ) {
SshAuth sshA = sshC.getSshAuth();
if (sshA == null) {
sshA = sshC.createChild(SshAuth.class);
} else
} else {
sshA = t.enroll(sshA);
}

if (sshuser != null)
if (sshuser != null) {
sshA.setUserName(sshuser);
if (sshkeyfile != null)
}
if (sshkeyfile != null) {
sshA.setKeyfile(sshkeyfile);
if(sshpassword != null)
}
if(sshpassword != null) {
sshA.setPassword(sshpassword);
if(sshkeypassphrase != null)
}
if(sshkeypassphrase != null) {
sshA.setKeyPassphrase(sshkeypassphrase);
}
sshC.setSshAuth(sshA);
}
writeableNode.setSshConnector(sshC);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/*
* Copyright (c) 2025 Contributors to the Eclipse Foundation
* Copyright (c) 1997, 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
Expand All @@ -13,7 +14,6 @@
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/

package com.sun.enterprise.admin.cli.cluster;

import com.sun.enterprise.util.net.NetUtils;
Expand Down Expand Up @@ -70,10 +70,11 @@ public class CreateLocalInstanceFilesystemCommand extends LocalInstanceCommand {
protected void validate()
throws CommandException {

if(ok(instanceName0))
if (ok(instanceName0)) {
instanceName = instanceName0;
else
} else {
throw new CommandException(Strings.get("Instance.badInstanceName"));
}

isCreateInstanceFilesystem = true;

Expand Down Expand Up @@ -108,8 +109,6 @@ protected void validate()

}

/**
*/
@Override
protected int executeCommand()
throws CommandException {
Expand Down Expand Up @@ -188,17 +187,15 @@ private void checkDASCoordinates() throws CommandException {
InetAddress.getByName(DASHost);
} catch (UnknownHostException e) {
String thisHost = NetUtils.getHostName();
String msg = Strings.get("Instance.DasHostUnknown",
DASHost, thisHost);
String msg = Strings.get("Instance.DasHostUnknown", DASHost, thisHost);
throw new CommandException(msg, e);
}

// See if DAS is reachable
if (! NetUtils.isRunning(DASHost, DASPort)) {
if (!NetUtils.isRunning(DASHost, DASPort)) {
// DAS provided host and port
String thisHost = NetUtils.getHostName();
String msg = Strings.get("Instance.DasHostUnreachable",
DASHost, Integer.toString(DASPort), thisHost);
String msg = Strings.get("Instance.DasHostUnreachable", DASHost, Integer.toString(DASPort), thisHost);
throw new CommandException(msg);
}
}
Expand Down
Loading

0 comments on commit dbcb8eb

Please sign in to comment.