1

We recently had to set up one of the tomcat servers from scratch. Tomcat version is 8.0.20. Deploying a war file, now System.getProperty("mode") returns "null" where it should return PREPROD.

It should read this "mode" from a mode.properties file which is located in the webapps directory. The two lines commented out show another part of code that does not work anymore on the new tomcat server. I replaced it with code that should work.

//String pathOfWebInf = sce.getServletContext().getRealPath("WEB-INF");
//String pathOfLocalhostFile = pathOfWebInf + File.separator + "classes"
//      + File.separator;
String pathOfLocalhostFile = this.getClass().getResource("/").getPath();

String mode = System.getProperty("mode");
String fileName = "localhost-oracle.properties." + mode;

StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
encryptor.setPassword("xxx");

Properties dbProps = new EncryptableProperties(encryptor);
try
{
    InputStream is = new FileInputStream(pathOfLocalhostFile + fileName);
    dbProps.load(is);
} catch (Exception e)
{
    throw new IOException("Could not read properties file " + pathOfLocalhostFile + fileName);
}
Adder
  • 5,708
  • 1
  • 28
  • 56
  • 1
    I would suggest putting your properties into catalina.properties in the servers /conf folder. – Chris M Jan 16 '17 at 09:48
  • I solved the problem by reverting to Tomcat 7, Tomcat 8 had issues like in http://stackoverflow.com/questions/32197494/why-does-servletcontext-getrealpath-returns-null-on-tomcat-8 – Adder Jan 16 '17 at 14:32

2 Answers2

2

System.properties is related to all properties in the Computer where the JVM is running... there is no mode key defined there, that is why you get null as value....

check out all the properties in the pc by doing:

final Properties props = System.getProperties();
props.list(System.out);

and verify yourself, there is no mode key in that map...

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
  • My question then would be: What can I change so that it reads properties from the webapps/mode.properties file like it did on the old tomcat? – Adder Jan 16 '17 at 09:48
1

You have to load mode.properties first, like this way

private Properties mode=null;
mode = new Properties();
mode.load(new FileInputStream(pathtoMODE));

String mode = mode.getProperty("mode");
AMB
  • 995
  • 1
  • 12
  • 26
  • Ideally I would not have to hardcode a path, maybe it should check the class path for properties files. Also I would like to make a minimum set of changes to the code and the configuration, as the "old" code is working on "old" tomcats in the deployment chain. – Adder Jan 16 '17 at 10:16