Skip to content
Snippets Groups Projects
Unverified Commit 42b7438f authored by Timm Fitschen's avatar Timm Fitschen
Browse files

EHN: set timezone with various methods

parent 60bbe6a6
Branches
Tags
No related merge requests found
TIMEZONE=
SERVER_OWNER= SERVER_OWNER=
SERVER_NAME=CaosDB Server SERVER_NAME=CaosDB Server
SERVER_SIDE_SCRIPTING_BIN_DIR=./scripting/bin/ SERVER_SIDE_SCRIPTING_BIN_DIR=./scripting/bin/
...@@ -16,7 +17,7 @@ MYSQL_DATABASE_NAME=caosdb ...@@ -16,7 +17,7 @@ MYSQL_DATABASE_NAME=caosdb
MYSQL_USER_NAME=caosdb MYSQL_USER_NAME=caosdb
MYSQL_USER_PASSWORD=caosdb MYSQL_USER_PASSWORD=caosdb
MYSQL_SCHEMA_VERSION=v2.1.0 MYSQL_SCHEMA_VERSION=v2.1.1
CONTEXT_ROOT= CONTEXT_ROOT=
......
...@@ -64,6 +64,7 @@ import caosdb.server.utils.FileUtils; ...@@ -64,6 +64,7 @@ import caosdb.server.utils.FileUtils;
import caosdb.server.utils.Initialization; import caosdb.server.utils.Initialization;
import caosdb.server.utils.NullPrintStream; import caosdb.server.utils.NullPrintStream;
import caosdb.server.utils.Utils; import caosdb.server.utils.Utils;
import com.ibm.icu.util.TimeZone;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
...@@ -101,8 +102,6 @@ import org.restlet.routing.TemplateRoute; ...@@ -101,8 +102,6 @@ import org.restlet.routing.TemplateRoute;
import org.restlet.routing.Variable; import org.restlet.routing.Variable;
import org.restlet.util.Series; import org.restlet.util.Series;
import com.ibm.icu.util.TimeZone;
public class CaosDBServer extends Application { public class CaosDBServer extends Application {
private static Logger logger = Logger.getLogger(CaosDBServer.class.getName()); private static Logger logger = Logger.getLogger(CaosDBServer.class.getName());
...@@ -123,6 +122,59 @@ public class CaosDBServer extends Application { ...@@ -123,6 +122,59 @@ public class CaosDBServer extends Application {
SERVER_PROPERTIES = ServerProperties.initServerProperties(); SERVER_PROPERTIES = ServerProperties.initServerProperties();
} }
/**
* Precedence order:
*
* <p>1) ServerProperty "TIMEZONE" 2) JVM property "user.timezone" 3) Environment variable "TZ" 4)
* Output of posix' "date +%Z"
*
* @throws InterruptedException
* @throws IOException
*/
public static void initTimeZone() throws InterruptedException, IOException {
String serverProperty = getServerProperty(ServerProperties.KEY_TIMEZONE);
if (serverProperty != null && !serverProperty.isEmpty()) {
logger.config("SET TIMEZONE = " + serverProperty);
TimeZone.setDefault(TimeZone.getTimeZone(serverProperty));
return;
}
String prop = System.getProperty("user.timezone");
if (prop != null && !prop.isEmpty()) {
logger.config("SET TIMEZONE = " + prop);
TimeZone.setDefault(TimeZone.getTimeZone(prop));
return;
}
String envVar = System.getenv("TZ");
if (envVar != null && !envVar.isEmpty()) {
logger.config("SET TIMEZONE = " + envVar);
TimeZone.setDefault(TimeZone.getTimeZone(envVar));
return;
}
String fromDate = getTimeZoneFromDate();
if (fromDate != null && fromDate.isEmpty()) {
logger.config("SET TIMEZONE = " + fromDate);
TimeZone.setDefault(TimeZone.getTimeZone(fromDate));
return;
}
logger.warning("COULD NOT SET TIMEZONE. DEFAULTS TO " + TimeZone.getDefault());
}
private static String getTimeZoneFromDate() throws InterruptedException, IOException {
final Process cmd = Runtime.getRuntime().exec(new String[] {"date", "+%Z"});
if (cmd.waitFor() == 0) {
return Utils.InputStream2String(cmd.getInputStream());
}
logger.warning(
"Could not determine time zone from `date +%Z`. The command returned with exit code "
+ cmd.exitValue());
return null;
}
/** /**
* This main method starts up a web application that will listen on a port defined in the config * This main method starts up a web application that will listen on a port defined in the config
* file. * file.
...@@ -138,9 +190,10 @@ public class CaosDBServer extends Application { ...@@ -138,9 +190,10 @@ public class CaosDBServer extends Application {
logger.info("TimeZone: " + TimeZone.getDefault()); logger.info("TimeZone: " + TimeZone.getDefault());
try { try {
initServerProperties(); initServerProperties();
} catch (IOException e1) { initTimeZone();
} catch (IOException | InterruptedException e1) {
e1.printStackTrace(); e1.printStackTrace();
System.err.println("Could not read config files."); System.err.println("Could not configure the server.");
System.exit(1); System.exit(1);
} }
boolean startGui = true; boolean startGui = true;
......
...@@ -127,6 +127,7 @@ public class ServerProperties extends Properties { ...@@ -127,6 +127,7 @@ public class ServerProperties extends Properties {
public static final String KEY_CHECK_ENTITY_ACL_ROLES_MODE = "CHECK_ENTITY_ACL_ROLES_MODE"; public static final String KEY_CHECK_ENTITY_ACL_ROLES_MODE = "CHECK_ENTITY_ACL_ROLES_MODE";
public static final String KEY_GLOBAL_ENTITY_PERMISSIONS_FILE = "GLOBAL_ENTITY_PERMISSIONS_FILE"; public static final String KEY_GLOBAL_ENTITY_PERMISSIONS_FILE = "GLOBAL_ENTITY_PERMISSIONS_FILE";
public static final String KEY_TIMEZONE = "TIMEZONE";
/** /**
* Read the config files and initialize the server properties. * Read the config files and initialize the server properties.
......
...@@ -68,16 +68,16 @@ public class FileUtilsTest { ...@@ -68,16 +68,16 @@ public class FileUtilsTest {
@BeforeClass @BeforeClass
public static void setup() throws Message, IOException { public static void setup() throws Message, IOException {
Assert.assertTrue(new File(FileSystem.getBasepath()).canWrite()); Assert.assertTrue(new File(FileSystem.getBasepath()).canWrite());
Assert.assertTrue(new File(FileSystem.getBasepath()).canRead()); Assert.assertTrue(new File(FileSystem.getBasepath()).canRead());
Assert.assertTrue(new File(FileSystem.getBasepath()).canExecute()); Assert.assertTrue(new File(FileSystem.getBasepath()).canExecute());
Assert.assertTrue(new File(FileSystem.getTmp()).canWrite()); Assert.assertTrue(new File(FileSystem.getTmp()).canWrite());
Assert.assertTrue(new File(FileSystem.getTmp()).canRead()); Assert.assertTrue(new File(FileSystem.getTmp()).canRead());
Assert.assertTrue(new File(FileSystem.getTmp()).canExecute()); Assert.assertTrue(new File(FileSystem.getTmp()).canExecute());
Assert.assertTrue(new File(FileSystem.getDropOffBox()).canWrite()); Assert.assertTrue(new File(FileSystem.getDropOffBox()).canWrite());
Assert.assertTrue(new File(FileSystem.getDropOffBox()).canRead()); Assert.assertTrue(new File(FileSystem.getDropOffBox()).canRead());
Assert.assertTrue(new File(FileSystem.getDropOffBox()).canExecute()); Assert.assertTrue(new File(FileSystem.getDropOffBox()).canExecute());
deleteTmp(); deleteTmp();
FileUtils.createFolders(testRoot); FileUtils.createFolders(testRoot);
FileUtils.createFolders(someDir); FileUtils.createFolders(someDir);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment