diff --git a/nunaliit2-couch-command/src/main/java/ca/carleton/gcrc/couch/command/AtlasProperties.java b/nunaliit2-couch-command/src/main/java/ca/carleton/gcrc/couch/command/AtlasProperties.java index f7a35e7bc..290f1fbd3 100644 --- a/nunaliit2-couch-command/src/main/java/ca/carleton/gcrc/couch/command/AtlasProperties.java +++ b/nunaliit2-couch-command/src/main/java/ca/carleton/gcrc/couch/command/AtlasProperties.java @@ -12,26 +12,41 @@ import java.util.Properties; import java.util.Set; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import ca.carleton.gcrc.utils.PropertiesWriter; public class AtlasProperties { + static final private Logger logger = LoggerFactory.getLogger(AtlasProperties.class); + static public AtlasProperties fromAtlasDir(File atlasDir) throws Exception { Properties props = new Properties(); readProperties(atlasDir, props); - + return fromProperties(props); } static public AtlasProperties fromProperties(Properties props) throws Exception { AtlasProperties atlasProps = new AtlasProperties(); - + atlasProps.setAtlasName( props.getProperty("atlas.name") ); atlasProps.setCouchDbName( props.getProperty("couchdb.dbName") ); atlasProps.setCouchDbSubmissionDbName( props.getProperty("couchdb.submission.dbName") ); atlasProps.setCouchDbAdminUser( props.getProperty("couchdb.admin.user") ); - atlasProps.setCouchDbAdminPassword( props.getProperty("couchdb.admin.password") ); - + + // CouchDb password + try { + String couchDbPass = props.getProperty("couchdb.admin.password"); + if(null == couchDbPass) { + throw new Exception("Couchdb password not set. Run config command"); + } + atlasProps.setCouchDbAdminPassword(couchDbPass); + } catch(Exception e) { + throw new Exception("Unable to interpret couchdb password",e); + } + // CouchDb URL try { String urlStr = props.getProperty("couchdb.url"); @@ -40,7 +55,7 @@ static public AtlasProperties fromProperties(Properties props) throws Exception } catch(Exception e) { throw new Exception("Unable to decode CouchDB URL",e); } - + // Server port try { String portString = props.getProperty("servlet.url.port"); @@ -72,7 +87,7 @@ static public AtlasProperties fromProperties(Properties props) throws Exception } catch(Exception e) { throw new Exception("Unable to interpret server key",e); } - + // Submission DB enabled { String enabledString = props.getProperty("couchdb.submission.enabled","false"); @@ -96,62 +111,43 @@ static public AtlasProperties fromProperties(Properties props) throws Exception String key = props.getProperty("google.mapapi.key",""); atlasProps.setGoogleMapApiKey(key); } - + return atlasProps; } static public void readProperties(File atlasDir, Properties props) throws Exception { // install.properties - { - File installPropFile = new File(atlasDir,"config/install.properties"); - if( installPropFile.exists() && installPropFile.isFile() ){ - FileInputStream fis = null; - try { - fis = new FileInputStream(installPropFile); - InputStreamReader reader = new InputStreamReader(fis,"UTF-8"); - props.load(reader); - - } catch(Exception e) { - throw new Exception("Unable to read config properties from: "+installPropFile.getAbsolutePath(), e); - - } finally { - if( null != fis ){ - try{ - fis.close(); - } catch(Exception e) { - // Ignore - } - } - } - } - } + File installPropFile = new File(atlasDir,"config/install.properties"); + readConfigFile(installPropFile, props); // sensitive.properties - { - File sensitivePropFile = new File(atlasDir,"config/sensitive.properties"); - if( sensitivePropFile.exists() && sensitivePropFile.isFile() ){ - FileInputStream fis = null; + File sensitivePropFile = new File(atlasDir,"config/sensitive.properties"); + readConfigFile(sensitivePropFile, props); + } + + static public void readConfigFile(File configFile, Properties props) throws Exception { + CommandScenario commandToExecute = CommandScenario.getInstance(); + FileInputStream fis = null; + try { + fis = new FileInputStream(configFile); + InputStreamReader reader = new InputStreamReader(fis,"UTF-8"); + props.load(reader); + } catch(Exception e) { + if(!commandToExecute.getCommand().equals(CommandScenario.CONFIG_COMMAND)) { + logger.error("Unable to read config properties from: " + configFile.getAbsolutePath()); + throw new Exception("Unable to read config properties from: " + configFile.getAbsolutePath(), e); + } + } finally { + if( null != fis ){ try { - fis = new FileInputStream(sensitivePropFile); - InputStreamReader reader = new InputStreamReader(fis,"UTF-8"); - props.load(reader); - - } catch(Exception e) { - throw new Exception("Unable to read config properties from: "+sensitivePropFile.getAbsolutePath(), e); - - } finally { - if( null != fis ){ - try{ - fis.close(); - } catch(Exception e) { - // Ignore - } - } + fis.close(); + } catch (Exception e) { + // Ignore } } } } - + static public void writeProperties(File atlasDir, Properties props) throws Exception { // Create config directory, if needed File configDir = new File(atlasDir,"config"); @@ -164,14 +160,14 @@ static public void writeProperties(File atlasDir, Properties props) throws Excep } catch(Exception e) { throw new Exception("Unable to create config directory",e); } - + // Figure out which properties are saved in the sensitive file Set sensitivePropertyNames = new HashSet(); { sensitivePropertyNames.add("couchdb.admin.password"); sensitivePropertyNames.add("server.key"); sensitivePropertyNames.add("google.mapapi.key"); - + File sensitivePropFile = new File(atlasDir,"config/sensitive.properties"); if( sensitivePropFile.exists() && sensitivePropFile.isFile() ){ FileInputStream fis = null; @@ -181,7 +177,7 @@ static public void writeProperties(File atlasDir, Properties props) throws Excep fis = new FileInputStream(sensitivePropFile); InputStreamReader reader = new InputStreamReader(fis,"UTF-8"); sensitivePropsCopy.load(reader); - + Enumeration keyEnum = sensitivePropsCopy.propertyNames(); while( keyEnum.hasMoreElements() ){ Object keyObj = keyEnum.nextElement(); @@ -190,10 +186,10 @@ static public void writeProperties(File atlasDir, Properties props) throws Excep sensitivePropertyNames.add(key); } } - + } catch(Exception e) { // Just ignore - + } finally { if( null != fis ){ try{ @@ -205,11 +201,11 @@ static public void writeProperties(File atlasDir, Properties props) throws Excep } } } - + // Divide public and sensitive properties Properties publicProps = new Properties(); Properties sensitiveProps = new Properties(); - + Enumeration namesEnum = props.propertyNames(); while( namesEnum.hasMoreElements() ){ Object keyObj = namesEnum.nextElement(); @@ -223,7 +219,7 @@ static public void writeProperties(File atlasDir, Properties props) throws Excep } } } - + // Write public file { File installPropFile = new File(configDir,"install.properties"); @@ -233,12 +229,12 @@ static public void writeProperties(File atlasDir, Properties props) throws Excep OutputStreamWriter osw = new OutputStreamWriter(fos,"UTF-8"); PropertiesWriter propWriter = new PropertiesWriter(osw); propWriter.write(publicProps); - + osw.flush(); - + } catch(Exception e) { throw new Exception("Unable to write config properties to: "+installPropFile.getAbsolutePath(), e); - + } finally { if( null != fos ){ try{ @@ -249,7 +245,7 @@ static public void writeProperties(File atlasDir, Properties props) throws Excep } } } - + // Write sensitive file { File sensitivePropFile = new File(configDir,"sensitive.properties"); @@ -259,12 +255,12 @@ static public void writeProperties(File atlasDir, Properties props) throws Excep OutputStreamWriter osw = new OutputStreamWriter(fos,"UTF-8"); PropertiesWriter propWriter = new PropertiesWriter(osw); propWriter.write(sensitiveProps); - + osw.flush(); - + } catch(Exception e) { throw new Exception("Unable to write config properties to: "+sensitivePropFile.getAbsolutePath(), e); - + } finally { if( null != fos ){ try{ @@ -276,7 +272,7 @@ static public void writeProperties(File atlasDir, Properties props) throws Excep } } } - + private String atlasName; private URL couchDbUrl; private String couchDbName; @@ -296,7 +292,7 @@ public String getAtlasName() { public void setAtlasName(String atlasName) { this.atlasName = atlasName; } - + public URL getCouchDbUrl() { return couchDbUrl; } @@ -345,7 +341,7 @@ public int getServerPort() { public void setServerPort(int serverPort) { this.serverPort = serverPort; } - + public boolean isRestricted() { return restricted; } diff --git a/nunaliit2-couch-command/src/main/java/ca/carleton/gcrc/couch/command/CommandScanario.java b/nunaliit2-couch-command/src/main/java/ca/carleton/gcrc/couch/command/CommandScanario.java new file mode 100644 index 000000000..595f76d5a --- /dev/null +++ b/nunaliit2-couch-command/src/main/java/ca/carleton/gcrc/couch/command/CommandScanario.java @@ -0,0 +1,36 @@ +package ca.carleton.gcrc.couch.command; + +/** + * This class is being used to set/get the nunaliit command being executed globally. + * Commands scanario can be one of: update, run or config. + * + * This class is used in the following files: + * - Main.java + * - AtlasProperties.java + */ +public class CommandScanario { + + private static CommandScanario instance; + + public static final String CONFIG_COMMAND = "config"; + public static final String UPDATE_COMMAND = "update"; + public static final String RUN_COMMAND = "run"; + + private String command = ""; + + public static CommandScanario getInstance() { + if (instance == null) { + instance = new CommandScanario(); + } + + return instance; + } + + public String getCommand() { + return this.command; + } + + public void setCommand(String command) { + this.command = command; + } +} diff --git a/nunaliit2-couch-command/src/main/java/ca/carleton/gcrc/couch/command/CommandScenario.java b/nunaliit2-couch-command/src/main/java/ca/carleton/gcrc/couch/command/CommandScenario.java new file mode 100644 index 000000000..c1b7127c7 --- /dev/null +++ b/nunaliit2-couch-command/src/main/java/ca/carleton/gcrc/couch/command/CommandScenario.java @@ -0,0 +1,36 @@ +package ca.carleton.gcrc.couch.command; + +/** + * This class is being used to set/get the nunaliit command being executed globally. + * Commands scenario can be one of: update, run or config. + * + * This class is used in the following files: + * - Main.java + * - AtlasProperties.java + */ +public class CommandScenario { + + private static CommandScenario instance; + + public static final String CONFIG_COMMAND = "config"; + public static final String UPDATE_COMMAND = "update"; + public static final String RUN_COMMAND = "run"; + + private String command = ""; + + public static CommandScenario getInstance() { + if (instance == null) { + instance = new CommandScenario(); + } + + return instance; + } + + public String getCommand() { + return this.command; + } + + public void setCommand(String command) { + this.command = command; + } +} diff --git a/nunaliit2-couch-command/src/main/java/ca/carleton/gcrc/couch/command/Main.java b/nunaliit2-couch-command/src/main/java/ca/carleton/gcrc/couch/command/Main.java index 97ea98d71..ecf6c96d3 100644 --- a/nunaliit2-couch-command/src/main/java/ca/carleton/gcrc/couch/command/Main.java +++ b/nunaliit2-couch-command/src/main/java/ca/carleton/gcrc/couch/command/Main.java @@ -154,11 +154,12 @@ public void execute(GlobalSettings globalSettings, List args) throws Exc if( arguments.size() < 1 ){ throw new Exception("No command provided. Try 'help'."); } + CommandScenario cmdScenario = CommandScenario.getInstance(); String commandKeyword = arguments.get(0); for(Command command : getCommands()){ if( command.matchesKeyword(commandKeyword) ) { // Found the command in question - + cmdScenario.setCommand(commandKeyword); // Check options for this command String[] expectedOptions = command.getExpectedOptions(); options.validateExpectedOptions(expectedOptions); diff --git a/nunaliit2-couch-sdk/src/main/templates/config/sensitive.properties b/nunaliit2-couch-sdk/src/main/templates/config/sensitive.properties new file mode 100644 index 000000000..c4a5a5dca --- /dev/null +++ b/nunaliit2-couch-sdk/src/main/templates/config/sensitive.properties @@ -0,0 +1,3 @@ +couchdb.admin.password= +google.mapapi.key= +server.key=