2

I am using ff4j like this:

FF4J ff4j = new FF4J("config.xml");

Internally, FF4J calls getClass().getClassLoader().getResourceAsStream("config.xml").

I want to be able to choose the config.xml location at deployment time (for example, /etc for linux).

How can I achieve that, without having to hardcode an absolute path? Is it possible to set the JVM / tomcat to look for the file in /etc for example? Or maybe there's another way to achieve what I want with FF4J?

Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
RonW
  • 41
  • 3
  • Set it as an environment variable – Federico klez Culloca Jan 28 '20 at 13:16
  • Makes perfect sense, I don't know why I didn't think of that. This is the way to go and solves my issue. Just out of curiosity then, is it possible to set the JVM / tomcat to look for resources in specific folders ? – RonW Jan 28 '20 at 13:20
  • Not that I know of, but if anyone knows I'd be curious myself. – Federico klez Culloca Jan 28 '20 at 13:22
  • A resource gotten via getResource cannot stem from a file system path as shown. You could have different class paths per platform – Joop Eggen Jan 28 '20 at 13:22
  • Check https://stackoverflow.com/questions/19035407/classloader-getresourceasstream-returns-null and https://stackoverflow.com/questions/1464291/how-to-really-read-text-file-from-classpath-in-java/1464366#1464366 – Arvind Kumar Avinash Jan 28 '20 at 13:23
  • set up environment variables or system variables. – Vishwa Ratna Jan 28 '20 at 13:24
  • Please note that the `FF4j` class also accept an `inputStream` as as direct constructor parameter @see https://github.com/ff4j/ff4j/blob/master/ff4j-core/src/main/java/org/ff4j/FF4j.java#L149, as such you can create some `FileInputStream` or your own or any inputstream you like. – clunven Jan 29 '20 at 21:31

1 Answers1

0

As creator of FF4j I would add a couple of solutions.

Before 1.8.9

File configFile = new File("/tmp/ff4j.xml");
FileInputStream fis = new FileInputStream(configFile);
FF4j ff4j = new FF4j(fis);

Since 1.8.9

 File configFile = new File("/tmp/ff4j.xml");
 FileInputStream fis = new FileInputStream(configFile);
 FF4j ff4j= new FF4j(new XmlParser(), fis);

As such you can now also import Yaml or properties files. (thinking about configMap in K8s world here). More samples here

clunven
  • 1,360
  • 6
  • 13