symphonyoss / symphony-java-client

Java client library for Symphony
Apache License 2.0
34 stars 37 forks source link

SymphonyClientConfig.java cannot support classpath file configuration #91

Closed treemanfm closed 7 years ago

treemanfm commented 7 years ago

public class SymphonyClientConfig { private static Logger log = LoggerFactory.getLogger(SymphonyClientConfig.class);

private Properties config = new Properties();

public SymphonyClientConfig() {
    String configFile = get(SymphonyClientConfigID.SYMPHONY_CONFIG_FILE);

    if (configFile != null) {
        try (Reader reader = new FileReader(configFile)) {
            config.load(reader);
        } catch (FileNotFoundException e) {
            throw new ProgramFault("Config file \"" + configFile + "\" not found");
        } catch (IOException e) {
            throw new ProgramFault("Config file \"" + configFile + "\" cannot be loaded", e);
        }
    }
ftbb commented 7 years ago

Today, you have to set the property or env variable identifying the specific configuration file to use. (symphony.config.file or SYMPHONY_CONFIG_FILE)

Assume you want to have a default configuration file (eg. sjc.properties) which is automatically searched through CLASSPATH?

I can look at putting that into the next release, but for now you can hack it by:

Refrence this: https://stackoverflow.com/questions/1464291/how-to-really-read-text-file-from-classpath-in-java

Set the property: symphony.client.config=sjc.properties

Copy the file on the classpath to local file, using something like this..

 public class CopyConfig
 {
    public static void main(String[] args)
     {
         InputStream stream = this.class.getResourceAsStream("/sjc.propertiest");
       //OR
         stream = this.class.getClassLoader().getResourceAsStream("sjc.properties");

         File config = new File("sjc.properties");
         Files.copy(stream, config.getPath());

          SyphonyClientConfig symphonyClientConfig = new SymphonyClientConfig(true);
          ......

     }
 }

Does this help?

jack20170608 commented 7 years ago

thanks @ftbb