Skip to content

Commit

Permalink
[FIX] Core: global system properties
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristianGruen committed Jun 2, 2015
1 parent f6fab17 commit 0e44488
Show file tree
Hide file tree
Showing 19 changed files with 117 additions and 82 deletions.
11 changes: 6 additions & 5 deletions basex-api/src/main/java/org/basex/BaseXHTTP.java
Expand Up @@ -97,8 +97,8 @@ public BaseXHTTP(final String... args) throws Exception {
}

// start web server in a new process
final Connector connector = jetty.getConnectors()[0];
if(service) {
final Connector connector = jetty.getConnectors()[0];
start(connector.getPort(), connector instanceof SslSelectChannelConnector, args);

for(final Connector c : jetty.getConnectors()) {
Expand All @@ -121,7 +121,7 @@ public BaseXHTTP(final String... args) throws Exception {
try {
jetty.start();
} catch(final BindException ex) {
throw new IOException(HTTP + ' ' + SRV_RUNNING, ex);
throw new IOException(Util.info(HTTP + ' ' + SRV_RUNNING_X, connector.getPort()), ex);
}
// throw cached exception that did not break the servlet architecture
final IOException ex = HTTPContext.exception();
Expand Down Expand Up @@ -331,7 +331,7 @@ private static void start(final int port, final boolean ssl, final String... arg
if(ping(S_LOCALHOST, port, ssl)) return;
Performance.sleep(c * 100L);
}
throw new BaseXException(CONNECTION_ERROR);
throw new BaseXException(CONNECTION_ERROR_X, port);
}

/**
Expand All @@ -355,9 +355,10 @@ private static void stop(final int port) throws IOException {
new Socket(S_LOCALHOST, port).close();
// give the notified process some time to quit
Performance.sleep(100);
} catch(final IOException ex) {
} catch(final ConnectException ex) {
throw new IOException(Util.info(CONNECTION_ERROR_X, port));
} finally {
stop.delete();
throw ex;
}
}

Expand Down
10 changes: 5 additions & 5 deletions basex-core/src/main/java/org/basex/BaseX.java
Expand Up @@ -86,9 +86,6 @@ public BaseX(final String... args) throws IOException {
execute(val);
execute(new Set(MainOptions.QUERYPATH, ""), false);
console = false;
} else if(c == 'd') {
// toggle debug mode
Prop.debug ^= true;
} else if(c == 'D') {
// hidden option: show/hide dot query graph
execute(new Set(MainOptions.DOTPLAN, null), false);
Expand Down Expand Up @@ -232,8 +229,11 @@ protected final void parseArgs() throws IOException {
String v = null;
if(arg.dash()) {
c = arg.next();
if(c == 'b' || c == 'c' || c == 'C' || c == 'i' || c == 'I' || c == 'o' || c == 'q' ||
c == 'r' || c == 's' || c == 't' && local()) {
if(c == 'd') {
// activate debug mode
Prop.debug = true;
} else if(c == 'b' || c == 'c' || c == 'C' || c == 'i' || c == 'I' || c == 'o' ||
c == 'q' || c == 'r' || c == 's' || c == 't' && local()) {
// options followed by a string
v = arg.string();
} else if(c == 'd' || c == 'D' && local() || c == 'u' && local() || c == 'R' ||
Expand Down
10 changes: 9 additions & 1 deletion basex-core/src/main/java/org/basex/BaseXClient.java
Expand Up @@ -3,6 +3,7 @@
import static org.basex.core.Text.*;

import java.io.*;
import java.net.*;

import org.basex.api.client.*;
import org.basex.core.*;
Expand Down Expand Up @@ -57,6 +58,13 @@ protected Session init() throws IOException {
Util.out(PASSWORD + COLS);
pass = Util.password();
}
return new ClientSession(context, user, pass, out);

final String host = context.soptions.get(StaticOptions.HOST);
final int port = context.soptions.get(StaticOptions.PORT);
try {
return new ClientSession(host, port, user, pass, out);
} catch(final ConnectException ex) {
throw new BaseXException(CONNECTION_ERROR_X, port);
}
}
}
14 changes: 8 additions & 6 deletions basex-core/src/main/java/org/basex/BaseXServer.java
Expand Up @@ -102,7 +102,7 @@ public BaseXServer(final Context ctx, final String... args) throws IOException {
stopFile = stopFile(port);
} catch(final IOException ex) {
context.log.writeServer(LogType.ERROR, Util.message(ex));
throw ex;
throw ex instanceof BindException ? new IOException(Util.info(SRV_RUNNING_X, port)) : ex;
}

new Thread(this).start();
Expand Down Expand Up @@ -264,16 +264,17 @@ public void stop() throws IOException {
*/
public static void start(final int port, final String... args) throws BaseXException {
// check if server is already running (needs some time)
if(ping(S_LOCALHOST, port)) throw new BaseXException(SRV_RUNNING);
final String host = S_LOCALHOST;
if(ping(host, port)) throw new BaseXException(SRV_RUNNING_X, port);

Util.start(BaseXServer.class, args);

// try to connect to the new server instance
for(int c = 1; c < 10; ++c) {
Performance.sleep(c * 100L);
if(ping(S_LOCALHOST, port)) return;
if(ping(host, port)) return;
}
throw new BaseXException(CONNECTION_ERROR);
throw new BaseXException(CONNECTION_ERROR_X, port);
}

/**
Expand Down Expand Up @@ -307,9 +308,10 @@ public static void stop(final int port) throws IOException {
new Socket(S_LOCALHOST, port).close();
// wait and check if server was really stopped
do Performance.sleep(100); while(ping(S_LOCALHOST, port));
} catch(final IOException ex) {
} catch(final ConnectException ex) {
throw new IOException(Util.info(CONNECTION_ERROR_X, port));
} finally {
stop.delete();
throw ex;
}
}

Expand Down
20 changes: 9 additions & 11 deletions basex-core/src/main/java/org/basex/core/StaticOptions.java
@@ -1,7 +1,5 @@
package org.basex.core;

import static org.basex.util.Prop.*;

import java.util.*;

import org.basex.io.*;
Expand All @@ -17,7 +15,7 @@
*/
public final class StaticOptions extends Options {
/** Indicates if the user's home directory has been chosen as home directory. */
private static final boolean USERHOME = HOME.equals(Prop.USERHOME);
private static final boolean USERHOME = Prop.HOME.equals(Prop.USERHOME);

/** Comment: written to options file. */
public static final Comment C_GENERAL = new Comment("General Options");
Expand All @@ -26,12 +24,12 @@ public final class StaticOptions extends Options {
public static final BooleanOption DEBUG = new BooleanOption("DEBUG", false);
/** Database path. */
public static final StringOption DBPATH = new StringOption("DBPATH",
HOME + (USERHOME ? NAME + "Data" : "data"));
Prop.HOME + (USERHOME ? Prop.NAME + "Data" : "data"));
/** Package repository path. */
public static final StringOption REPOPATH = new StringOption("REPOPATH",
HOME + (USERHOME ? NAME + "Repo" : "repo"));
Prop.HOME + (USERHOME ? Prop.NAME + "Repo" : "repo"));
/** Language name. */
public static final StringOption LANG = new StringOption("LANG", language);
public static final StringOption LANG = new StringOption("LANG", Prop.language);
/** Flag to include key names in the language strings. */
public static final BooleanOption LANGKEYS = new BooleanOption("LANGKEYS", false);
/** Applied locking algorithm: local (database) vs. global (process) locking. */
Expand Down Expand Up @@ -77,7 +75,7 @@ public final class StaticOptions extends Options {

/** Web path. */
public static final StringOption WEBPATH = new StringOption("WEBPATH",
HOME + (USERHOME ? NAME + "Web" : "webapp"));
Prop.HOME + (USERHOME ? Prop.NAME + "Web" : "webapp"));
/** REST path (relative to web path). */
public static final StringOption RESTPATH = new StringOption("RESTPATH", "");
/** RESTXQ path (relative to web path). */
Expand Down Expand Up @@ -109,13 +107,13 @@ public String toString() {
* @param file if {@code true}, options will be read from disk
*/
StaticOptions(final boolean file) {
super(file ? new IOFile(HOME, IO.BASEXSUFFIX) : null);
super(file ? new IOFile(Prop.HOME, IO.BASEXSUFFIX) : null);
setSystem();

// set some static options
language = get(LANG);
langkeys = get(LANGKEYS);
debug = get(DEBUG);
Prop.language = get(LANG);
Prop.langkeys = get(LANGKEYS);
Prop.debug = get(DEBUG);
final String ph = get(PROXYHOST);
if(!ph.isEmpty()) {
Prop.setSystem("http.proxyHost", ph);
Expand Down
16 changes: 11 additions & 5 deletions basex-core/src/main/java/org/basex/core/Text.java
Expand Up @@ -239,17 +239,23 @@ public interface Text {
// SERVER ===================================================================

/** Server was started. */
String SRV_STARTED_PORT_X = lang("srv_started_port_%");
String PORT_X = " (" + lang("port") + ": %).";
/** Server was started. */
String SRV_STARTED_PORT_X = lang("srv_started") + PORT_X;
/** Server was stopped. */
String SRV_STOPPED_PORT_X = lang("srv_stopped_port_%");
String SRV_STOPPED_PORT_X = lang("srv_stopped") + PORT_X;
/** Server is running or permission was denied. */
String SRV_RUNNING = lang("srv_running") + '.';
/** Server is running or permission was denied. */
String SRV_RUNNING = lang("srv_running");
String SRV_RUNNING_X = lang("srv_running") + PORT_X;
/** Connection error. */
String CONNECTION_ERROR = lang("connection_error") + '.';
/** Connection error. */
String CONNECTION_ERROR_X = lang("connection_error") + PORT_X;
/** Unknown host. */
String UNKNOWN_HOST_X = lang("unknown_host_x");
/** Timeout exceeded. */
String TIMEOUT_EXCEEDED = lang("timeout_exceeded");
/** Connection error. */
String CONNECTION_ERROR = lang("connection_error");
/** Access denied. */
String ACCESS_DENIED = lang("access_denied");
/** User name. */
Expand Down
16 changes: 12 additions & 4 deletions basex-core/src/main/java/org/basex/util/Prop.java
Expand Up @@ -201,7 +201,7 @@ public static void put(final Option<?> option, final Object value) {
* @param value value
*/
public static void put(final String name, final Object value) {
OPTIONS.put(name, value.toString());
OPTIONS.put(normalizeKey(name), value.toString());
}

/**
Expand Down Expand Up @@ -233,11 +233,9 @@ public static Set<Entry<String, String>> entries() {
final HashMap<String, String> entries = new HashMap<>();
entries.putAll(OPTIONS);
// override with system properties
final int l = DBPREFIX.length();
for(final Object key : System.getProperties().keySet()) {
final String name = key.toString();
if(name.startsWith(DBPREFIX))
entries.put(name.substring(l).toUpperCase(Locale.ENGLISH), System.getProperty(name));
if(name.startsWith(DBPREFIX)) entries.put(normalizeKey(name), System.getProperty(name));
}
return entries.entrySet();
}
Expand All @@ -250,4 +248,14 @@ public static Set<Entry<String, String>> entries() {
public static void setSystem(final String key, final String value) {
if(System.getProperty(key) == null) System.setProperty(key, value);
}

/**
* Normalizes the key of an option. Removes {@link #DBPREFIX} and converts the key to upper-case.
* @param name name of the option
* @return normalized string
*/
private static String normalizeKey(final String name) {
final String n = name.startsWith(DBPREFIX) ? name.substring(DBPREFIX.length()) : name;
return n.toUpperCase(Locale.ENGLISH);
}
}
9 changes: 5 additions & 4 deletions basex-core/src/main/resources/lang/Dutch.lang
Expand Up @@ -53,7 +53,7 @@ command_canceled = Commando is geannuleerd.
comment = Commentaar
community = Gemeenschap
compiling = Compileren
connection_error = Communicatie met de server mislukt.
connection_error = Communicatie met de server mislukt
copy = Kopieer
copy_db = Kopieer database
copy_path = Kopieer pad
Expand Down Expand Up @@ -270,6 +270,7 @@ pkg_installed_%_% = Package '%' geinstalleerd in %.
pkg_replaced_%_% = Package '%' vervangen in %.
please_wait = Een ogenblik
plot = Plot
port = poort
preferences = Voorkeuren
printed = Geprint
printing = Printen
Expand Down Expand Up @@ -335,9 +336,9 @@ skip_corrupt_files = Sla non-well-formed bestanden over
skipped = Overgeslagen
sort = Sorteer
split_input_lines = Splits input op in regels
srv_running = Server loopt al of toegang ontzegd.
srv_started_port_% = Server is gestart (poort: %)
srv_stopped_port_% = Server is gestopt (poort: %)
srv_running = Server loopt al of toegang ontzegd
srv_started = Server is gestart
srv_stopped = Server is gestopt
standard = Standaard
status_bar = Statusbalk
stemming = Stam zoeken
Expand Down
9 changes: 5 additions & 4 deletions basex-core/src/main/resources/lang/English.lang
Expand Up @@ -53,7 +53,7 @@ command_canceled = Command was canceled.
comment = Comment
community = Community
compiling = Compiling
connection_error = Can't communicate with the server.
connection_error = Connection failed
copy = Copy
copy_db = Copy Database
copy_path = Copy Path
Expand Down Expand Up @@ -270,6 +270,7 @@ pkg_installed_%_% = Package '%' installed in %.
pkg_replaced_%_% = Package '%' replaced in %.
please_wait = Please Wait
plot = Plot
port = port
preferences = Preferences
printed = Printed
printing = Printing
Expand Down Expand Up @@ -335,9 +336,9 @@ skip_corrupt_files = Skip corrupt (non-well-formed) files
skipped = Skipped
sort = Sort
split_input_lines = Splits input into lines
srv_running = Server is running or permission was denied.
srv_started_port_% = Server was started (port: %)
srv_stopped_port_% = Server was stopped (port: %)
srv_running = Server is running or permission was denied
srv_started = Server was started
srv_stopped = Server was stopped
standard = Standard
status_bar = Status Bar
stemming = Stemming
Expand Down
9 changes: 5 additions & 4 deletions basex-core/src/main/resources/lang/French.lang
Expand Up @@ -53,7 +53,7 @@ command_canceled = La commande a été annulée.
comment = Commentaire
community = Communauté
compiling = Compilation
connection_error = Pas de communication avec le serveur.
connection_error = Pas de communication avec le serveur
copy = Copier
copy_db = Copier la base de données
copy_path = Copier le chemin
Expand Down Expand Up @@ -270,6 +270,7 @@ pkg_installed_%_% = Paquet '%' installé (%)
pkg_replaced_%_% = Paquet '%' remplacé (%)
please_wait = Veuillez patienter
plot = Diagramme
port = port
preferences = Préférences
printed = Imprimé
printing = Impression
Expand Down Expand Up @@ -335,9 +336,9 @@ skip_corrupt_files = Ignorer les fichiers corrompus (mal formés)
skipped = Ignoré
sort = Sort
split_input_lines = Éclater l'entrée en lignes
srv_running = Serveur déjà démarré ou permission refusée.
srv_started_port_% = Serveur démarré (port: %)
srv_stopped_port_% = Serveur arrêté (port: %)
srv_running = Serveur déjà démarré ou permission refusée
srv_started = Serveur démarré
srv_stopped = Serveur arrêté
standard = Standard
status_bar = Barre d'état
stemming = Stemming
Expand Down
9 changes: 5 additions & 4 deletions basex-core/src/main/resources/lang/German.lang
Expand Up @@ -53,7 +53,7 @@ command_canceled = Der Befehl wurde abgebrochen.
comment = Kommentar
community = Community
compiling = Kompilierung
connection_error = Der Server ist nicht erreichbar.
connection_error = Der Server ist nicht erreichbar
copy = Kopieren
copy_db = Datebank kopieren
copy_path = Pfad kopieren
Expand Down Expand Up @@ -270,6 +270,7 @@ pkg_installed_%_% = Das Paket '%' wurde installiert (%).
pkg_replaced_%_% = Das Paket '%' wurde ersetzt (%).
please_wait = Bitte warten
plot = Plot
port = Port
preferences = Einstellungen
printed = Ausgegeben
printing = Ausgabe
Expand Down Expand Up @@ -335,9 +336,9 @@ skip_corrupt_files = Ignoriere korrupte Dateien
skipped = Übersprungen
sort = Sortieren
split_input_lines = Trennt den Text in einzelne Zeilen auf
srv_running = Der Server läuft, oder der Zugriff wurde untersagt.
srv_started_port_% = Server wurde gestartet (Port: %)
srv_stopped_port_% = Server wurde gestoppt (Port: %)
srv_running = Der Server läuft, oder der Zugriff wurde untersagt
srv_started = Server wurde gestartet
srv_stopped = Server wurde gestoppt
standard = Standard
status_bar = Statusleiste
stemming = Stemming
Expand Down

0 comments on commit 0e44488

Please sign in to comment.