Skip to content

Commit

Permalink
Support data directory passed at runtime as command line arg
Browse files Browse the repository at this point in the history
  • Loading branch information
willfurnell committed Feb 20, 2023
1 parent 4198bc3 commit 76ca49a
Show file tree
Hide file tree
Showing 11 changed files with 96 additions and 186 deletions.
8 changes: 4 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>uk.ngs</groupId>
<artifactId>CertWizard</artifactId>
<version>2.0.1</version>
<version>2.1.0</version>
<dependencies>
<dependency>
<groupId>org.bouncycastle</groupId>
Expand Down Expand Up @@ -80,7 +80,7 @@
<maven.compiler.source>1.9</maven.compiler.source>
<maven.compiler.target>1.9</maven.compiler.target>
<mainClass>uk.ngs.certwizard.gui.CertWizardMain</mainClass>
<codeSignCert>C:\\Users\\fou43474\\OneDrive - Science and Technology Facilities Council\\Documents\\Certs\\ComodoCodeSign-all.p12</codeSignCert>
<codeSignCert>C:\\Users\\fou43474\\OneDrive - Science and Technology Facilities Council\\Documents\\Certs\\UKRICodeSign.p12</codeSignCert>
</properties>

<licenses>
Expand Down Expand Up @@ -182,7 +182,7 @@
<configuration>
<mainClass>${mainClass}</mainClass>
<bundleJre>true</bundleJre>
<version>2.0.1</version>
<version>2.1.0</version>
<displayName>CertWizard</displayName>
<organizationName>STFC</organizationName>
<organizationUrl>https://stfc.ukri.org</organizationUrl>
Expand Down Expand Up @@ -226,7 +226,7 @@
<configuration>
<platform>mac</platform>
<createZipball>true</createZipball>
<jdkPath>C:\jdk\mac\jdk-11.0.15+10\Contents\Home</jdkPath>
<jdkPath>C:\jdk\mac\jdk-11.0.18+10\Contents\Home</jdkPath>
<macConfig>
<icnsFile>src/main/resources/ngs-icon.icns</icnsFile>
</macConfig>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ static synchronized ClientKeyStore getClientkeyStore(char[] passphrase) throws K
* @throws IllegalStateException if the KeyStore cannot be initialized.
*/
private ClientKeyStore(char[] passphrase) throws KeyStoreException, IOException, CertificateException {
String caDir = SystemStatus.getInstance().getHomeDir() + File.separator + ".ca";
String caDir = SystemStatus.getInstance().getCwDataDirectory().toString();
this.PASSPHRASE = passphrase;
this.keyStoreFilePath = caDir + File.separator + SysProperty.getValue("ngsca.key.keystore.file");
this.backupDir = caDir + File.separator + "backup";
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/uk/ngs/ca/common/LocalBackup.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,8 @@ public LocalBackup() {
public boolean isSuccess() {
String _keyFile = SysProperty.getValue("ngsca.key.keystore.file");
String _keyBackupFile = SysProperty.getValue("ngsca.key.keystore.backup.file");
String backupDir = SystemStatus.getInstance().getHomeDir().getAbsolutePath();
backupDir = backupDir + System.getProperty("file.separator") + ".ca";
String configDir = backupDir;
String configDir = SystemStatus.getInstance().getCwDataDirectory().getAbsolutePath();
String backupDir = configDir;
backupDir = backupDir + System.getProperty("file.separator") + "backup";
String keyFile = configDir + System.getProperty("file.separator") + _keyFile;
String keyBackupFile = backupDir + System.getProperty("file.separator") + _keyBackupFile;
Expand Down
15 changes: 6 additions & 9 deletions src/main/java/uk/ngs/ca/common/SystemStatus.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,16 @@
*/
public class SystemStatus extends Observable {

private File homeDir = new File(System.getProperty("user.home"));
private File cwDataDirectory = new File(System.getProperty("user.home") + File.separator + ".ca");

private boolean isOnline = false;

public synchronized File getHomeDir() {
return new File(this.homeDir.getAbsolutePath());
public synchronized File getCwDataDirectory() {
return new File(this.cwDataDirectory.getAbsolutePath());
}

public synchronized void setHomeDir(File dir) {
this.homeDir = dir;
public synchronized void setCwDataDirectory(File dir) {
this.cwDataDirectory = dir;
}

public synchronized boolean getIsOnline() {
Expand Down Expand Up @@ -87,10 +87,7 @@ public static SystemStatus getInstance() {
public synchronized boolean isExistKeyStore() {
String key = "ngsca.key.keystore.file";
String value = SysProperty.getValue(key);
String homePath = SystemStatus.getInstance().getHomeDir().getAbsolutePath();
homePath = homePath + System.getProperty("file.separator") + ".ca";
homePath = homePath + System.getProperty("file.separator") + value;
return new File(homePath).exists();
return new File(SystemStatus.getInstance().getCwDataDirectory().getAbsolutePath() + File.separator + value).exists();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ public void done() {
if (this.exception != null) {
JOptionPane.showMessageDialog(null,
"Please contact the helpdesk. A backup of your keystore is located in:\n"
+ SystemStatus.getInstance().getHomeDir().getAbsolutePath() + File.separator + ".ca\n"
+ "Exeption message: " + this.exception.getMessage(),
+ SystemStatus.getInstance().getCwDataDirectory().getAbsolutePath() + "\n"
+ "Exception message: " + this.exception.getMessage(),
"Keystore problem",
JOptionPane.WARNING_MESSAGE);
}
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/uk/ngs/ca/tools/property/SysProperty.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,7 @@ public static void setupTrustStore() {
if (value == null) {
throw new IllegalStateException("There is no trust store file name. Please check out config.properties.");
}
String homePath = SystemStatus.getInstance().getHomeDir().getAbsolutePath();
homePath = homePath + System.getProperty("file.separator") + ".ca";
String homePath = SystemStatus.getInstance().getCwDataDirectory().getAbsolutePath();
homePath = homePath + System.getProperty("file.separator") + value;

String keyStoreFile = homePath; // ~/.ca/truststore.jks
Expand Down
34 changes: 19 additions & 15 deletions src/main/java/uk/ngs/certwizard/gui/CertWizardMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.io.IOException;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
* The main frame class.
Expand All @@ -42,12 +43,12 @@ public class CertWizardMain extends javax.swing.JFrame {
/**
* Creates new form CertWizardMainFrame
*/
public CertWizardMain() {
public CertWizardMain(String dataDirectoryLocationOverride) {
initComponents();
setupFrame();
setupFrame(dataDirectoryLocationOverride);
}

private void setupFrame() {
private void setupFrame(String bootstrapDirectoryLocationOverride) {
this.setLayout(new BorderLayout());
URL iconURL = CertWizardMain.class.getResource("/ngs-icon.png");
if (iconURL != null) {
Expand All @@ -59,13 +60,12 @@ private void setupFrame() {
this.setTitle(title);

this.createGlobusDirIfNotExistsShowWarnings();
this.setupHomeDir();
this.setupDataDirectory(bootstrapDirectoryLocationOverride);

try {
SysProperty.setupTrustStore(); // throws IllegalStateException if prob
String trustStoreFile = SysProperty.getValue("ngsca.truststore.file");
String trustStorePath = SystemStatus.getInstance().getHomeDir().getAbsolutePath();
trustStorePath = trustStorePath + System.getProperty("file.separator") + ".ca";
String trustStorePath = SystemStatus.getInstance().getCwDataDirectory().getAbsolutePath();
trustStorePath = trustStorePath + System.getProperty("file.separator") + trustStoreFile;
System.out.println("trustStore file path [" + trustStorePath + "]");

Expand All @@ -80,6 +80,7 @@ private void setupFrame() {
}

} catch (Exception ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(null, ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
Expand All @@ -102,18 +103,16 @@ private void setupFrame() {
this.pack();
}

private void setupHomeDir() {
GetBootstrapDir bootDia = new GetBootstrapDir(this, true, ".ca");
private void setupDataDirectory(String bootstrapDirectoryLocationOverride) {
Path dataDirectory;
GetBootstrapDir bootDia = new GetBootstrapDir(this, true, bootstrapDirectoryLocationOverride);
bootDia.setLocationRelativeTo(null);
bootDia.setVisible(true);
Path homeDir = bootDia.getBootDir();

if (homeDir == null) {
dataDirectory = bootDia.getBootDir();
if (dataDirectory == null) {
System.exit(0);
} else if (homeDir.endsWith(".ca")) {
homeDir = homeDir.getParent();
}
SystemStatus.getInstance().setHomeDir(homeDir.toFile());
SystemStatus.getInstance().setCwDataDirectory(dataDirectory.toFile());
}

private void createGlobusDirIfNotExistsShowWarnings() {
Expand Down Expand Up @@ -196,6 +195,10 @@ private void initComponents() {
* @param args the command line arguments
*/
public static void main(String[] args) {
String dataDirectoryLocationOverride = "";
if (args.length > 0) {
dataDirectoryLocationOverride = args[0];
}
/*
* Set the Nimbus look and feel
*/
Expand All @@ -210,8 +213,9 @@ public static void main(String[] args) {
/*
* Create and display the form
*/
String finalDataDirectoryLocationOverride = dataDirectoryLocationOverride;
java.awt.EventQueue.invokeLater(() -> {
CertWizardMain cw = new CertWizardMain();
CertWizardMain cw = new CertWizardMain(finalDataDirectoryLocationOverride);
cw.setLocationRelativeTo(null);
cw.setVisible(true);
});
Expand Down
Loading

0 comments on commit 76ca49a

Please sign in to comment.