use same Config class everywhere

This commit is contained in:
Peter Hormanns 2017-04-28 17:07:10 +02:00
parent 254db2b9a1
commit c205ce0975
7 changed files with 41 additions and 73 deletions

View File

@ -14,6 +14,11 @@
<name>HSAdmin-Cli</name> <name>HSAdmin-Cli</name>
<dependencies> <dependencies>
<dependency>
<groupId>de.hsadmin</groupId>
<artifactId>framework</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency> <dependency>
<groupId>org.apache.xmlrpc</groupId> <groupId>org.apache.xmlrpc</groupId>
<artifactId>xmlrpc-client</artifactId> <artifactId>xmlrpc-client</artifactId>

View File

@ -11,7 +11,8 @@ import org.apache.xmlrpc.XmlRpcException;
import org.apache.xmlrpc.client.XmlRpcClient; import org.apache.xmlrpc.client.XmlRpcClient;
import org.apache.xmlrpc.client.XmlRpcClientConfigImpl; import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;
import de.hsadmin.jscli.conf.Config; import de.hsadmin.common.config.Config;
import de.hsadmin.common.error.TechnicalException;
import de.hsadmin.jscli.exception.JSCliException; import de.hsadmin.jscli.exception.JSCliException;
public class RpcClient { public class RpcClient {
@ -37,6 +38,8 @@ public class RpcClient {
} }
} catch (MalformedURLException e) { } catch (MalformedURLException e) {
throw new JSCliException(e); throw new JSCliException(e);
} catch (TechnicalException e) {
throw new JSCliException(e);
} }
} }

View File

@ -18,8 +18,9 @@ import java.util.Properties;
import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.HttpsURLConnection;
import de.hsadmin.common.config.Config;
import de.hsadmin.common.error.TechnicalException;
import de.hsadmin.jscli.TicketProvider; import de.hsadmin.jscli.TicketProvider;
import de.hsadmin.jscli.conf.Config;
import de.hsadmin.jscli.console.PasswordReader; import de.hsadmin.jscli.console.PasswordReader;
import de.hsadmin.jscli.exception.JSCliException; import de.hsadmin.jscli.exception.JSCliException;
@ -81,11 +82,11 @@ public class CASTicketProvider implements TicketProvider {
private String getGrantingTicket() throws JSCliException { private String getGrantingTicket() throws JSCliException {
grantingTicket = null; grantingTicket = null;
try {
String password = Config.getInstance().getProperty(user + ".passWord"); String password = Config.getInstance().getProperty(user + ".passWord");
if (password == null || password.length() <= 0) { if (password == null || password.length() <= 0) {
password = readPasswordFromConsole(); password = readPasswordFromConsole();
} }
try {
String encodedParams = URLEncoder.encode("username", "UTF-8") String encodedParams = URLEncoder.encode("username", "UTF-8")
+ "=" + URLEncoder.encode(user, "UTF-8") + "=" + URLEncoder.encode(user, "UTF-8")
+ "&" + URLEncoder.encode("password", "UTF-8") + "&" + URLEncoder.encode("password", "UTF-8")
@ -95,6 +96,8 @@ public class CASTicketProvider implements TicketProvider {
throw new JSCliException(e); throw new JSCliException(e);
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
throw new JSCliException("cas server not available: " + loginURL); throw new JSCliException("cas server not available: " + loginURL);
} catch (TechnicalException e) {
throw new JSCliException(e);
} }
return grantingTicket; return grantingTicket;
} }

View File

@ -1,7 +1,8 @@
package de.hsadmin.jscli.cas; package de.hsadmin.jscli.cas;
import de.hsadmin.common.config.Config;
import de.hsadmin.common.error.TechnicalException;
import de.hsadmin.jscli.TicketProvider; import de.hsadmin.jscli.TicketProvider;
import de.hsadmin.jscli.conf.Config;
import de.hsadmin.jscli.console.PasswordReader; import de.hsadmin.jscli.console.PasswordReader;
import de.hsadmin.jscli.exception.JSCliException; import de.hsadmin.jscli.exception.JSCliException;
@ -12,7 +13,9 @@ public class TicketProviderFactory {
public static TicketProvider getInstance(final PasswordReader console, final String user, final String runAs) throws JSCliException public static TicketProvider getInstance(final PasswordReader console, final String user, final String runAs) throws JSCliException
{ {
final Config config = Config.getInstance(); Config config;
try {
config = Config.getInstance();
final String backendURL = config.getProperty("backendURL", HOSTSHARING_BACKEND_URL); final String backendURL = config.getProperty("backendURL", HOSTSHARING_BACKEND_URL);
final String loginURL = config.getProperty("loginURL", HOSTSHARING_LOGIN_URL); final String loginURL = config.getProperty("loginURL", HOSTSHARING_LOGIN_URL);
if ("TestUmgebung".equalsIgnoreCase(loginURL)) { if ("TestUmgebung".equalsIgnoreCase(loginURL)) {
@ -20,6 +23,9 @@ public class TicketProviderFactory {
} else { } else {
return new CASTicketProvider(console, user, runAs, backendURL, loginURL); return new CASTicketProvider(console, user, runAs, backendURL, loginURL);
} }
} catch (TechnicalException e) {
throw new JSCliException(e);
}
} }
} }

View File

@ -6,6 +6,8 @@ import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException; import org.apache.commons.cli.ParseException;
import org.apache.commons.cli.PosixParser; import org.apache.commons.cli.PosixParser;
import de.hsadmin.common.config.Config;
import de.hsadmin.common.error.TechnicalException;
import de.hsadmin.jscli.exception.JSCliException; import de.hsadmin.jscli.exception.JSCliException;
public class CommandlineParser { public class CommandlineParser {
@ -38,13 +40,13 @@ public class CommandlineParser {
} }
} }
public String getUser() { public String getUser() throws TechnicalException {
final String systemUser = System.getProperty("user.name"); final String systemUser = System.getProperty("user.name");
final String configUser = Config.getInstance().getProperty("userName", systemUser); final String configUser = Config.getInstance().getProperty("userName", systemUser);
return cmd.getOptionValue("user", configUser); return cmd.getOptionValue("user", configUser);
} }
public String getRunAs() { public String getRunAs() throws TechnicalException {
return cmd.getOptionValue("runas", getUser()); return cmd.getOptionValue("runas", getUser());
} }

View File

@ -1,57 +0,0 @@
package de.hsadmin.jscli.conf;
import java.io.File;
import java.io.FileReader;
import java.util.Properties;
public class Config {
private static Config instance;
private Properties props;
private Config() {
props = new Properties();
File file = new File(System.getProperty("user.dir") + "/hsadmin.properties");
if (!file.canRead()) {
file = new File(System.getProperty("user.dir") + "/conf/hsadmin.properties");
}
if (!file.canRead()) {
file = new File(System.getProperty("user.home") + "/.hsadmin.properties");
}
if (!file.canRead()) {
file = new File("/etc/hsadmin.properties");
}
if (!file.canRead()) {
file = new File("/etc/hsadmin/hsadmin.properties");
}
if (file.canRead()) {
try {
props.load(new FileReader(file));
} catch (Exception e) {
// should not happen
e.printStackTrace();
}
}
}
public static Config getInstance() {
if (instance == null) {
instance = new Config();
}
return instance;
}
public String getProperty(String propertyName) {
String property = props.getProperty(propertyName);
if (property == null) {
return null;
}
return property.trim();
}
public String getProperty(String propertyName, String defaultValue) {
return props.getProperty(propertyName, defaultValue).trim();
}
}

View File

@ -21,6 +21,12 @@
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency>
<groupId>de.hsadmin</groupId>
<artifactId>framework</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency> <dependency>
<groupId>org.apache.xmlrpc</groupId> <groupId>org.apache.xmlrpc</groupId>
<artifactId>xmlrpc-client</artifactId> <artifactId>xmlrpc-client</artifactId>