Java extended properties configuration - java

I am looking for implementation of properties configuration where properties are considered in the following order (or similar):
Command line arguments.
Java System properties(System.getProperties()).
OS environment variables.
Application properties outside of your packaged jar.
It means there would be application.properties file. This can be overwritten by OS environment variables and so on. Command line arguments overwrites all previous properties.

That is actually the way Springs PropertiesPlaceholderConfigurer is working, if you provide different PropertySources in the disired priority.
Unfortunatly it does only work with the Spring Framework.

you can do something like this
Properties properties = new Properties();
InputStream input = new FileInputStream(new File("settings.properties"));
properties.load(input);
String ipAddress = properties.getProperty("ip");
And save it when you exit for exemple
File f = new File("settings.properties");
OutputStream out = new FileOutputStream(f);
properties.setProperty("ip", ipAddress);
properties.store(out, "properties");

Related

Can I get properties defined in "gradle.properties" in JAVA STATEMENT?

I defined a property in gradle.properties file like below:
user.password=mypassword
Can I use it as a variable value in my java statement?
Yes you can, however this is not a good idea nor a good practice. gradle.properties file is meant to keep gradle's properties itself, e.g. JVM args used at build time.
If you need to store user/pass pair in properties file, it should be placed under src/main/resources or other appropriate folder and separate from gradle.properties.
Side note: Not sure if keeping properties file in mobile application is safe in general.
You would have to read the properties file and extract the property first.
Properties prop = new Properties();
InputStream input = null;
try {
input = new FileInputStream("gradle.properties");
// load a properties file
prop.load(input);
// get the property value and print it out
System.out.println(prop.getProperty("user.password"));
} catch (IOException ex) {
ex.printStackTrace();
}
You can find the detailed tutorial here

Is there any way to change a properties file that is in my classpath?

I have a properties file that is in my classpath. My requirement is to change some properties in this file based on some inputs I get from arguments. The nature of arguments decide whether I need to change the properties and if yes which properties to change. The problem is that all classpath entries are loaded at the application startup time, so changing from within my application would not have any effect. How do I overcome this problem?
One possible solution I can think of is to not add this properties file in classpath but add after modifications are done. Is it viable? What can be a good solution?
It doesn't matter whether this file is on your classpath or not. It is a file: if you overwrite its contents, it will have changed. There isn't some in-memory copy that magically gets made at startup. This is very different from classes that are loaded in and which might need change at runtime.
Properties files that adhere to the right format can be read into a java.util.Properties object. You could do that, use the object to alter the properties as needed, then write it back out to the file. Check the store and load methods in that class. Mind that if you use the versions that take an Output/InputStream, the encoding is hard-coded. If the file's encoding is anything else than ISO-8859-1, use a method with an appropriate Writer/Reader.
Depends on how your application is deployed. If your properties files is inside a jar, you won't be able to directly change that properties file since its packaged and zipped up in an archive. You can instead as someone else mentioned load those properties into an object, and then store/write out to an external location, probably a URL based location. URL is convenient because it gets you access to virtually any location and it has that nifty openStream() method for loading properties. Your application could then look for the new file on load, and default to the application startup version if it fails to read/load from the new location.
Here is a sample code:
Properties p = new Properties();
File f = new File("file");
InputStream in = new FileInputStream(f);
p.load(in);
p.put("key", "blah");
OutputStream out = new FileOutputStream(f);
// If no comments p.store(writer);
p.store(out, "properties");
You need to first remove that property from the property file and then re-define it. Their is no way to directly modify the properties file.
Below is an example:
Properties pproperties = new Properties();
if (properties.containsKey("key1")) {
properties.remove("key1");
properties.setProperty("key1", "value1");
properties.store(new FileOutputStream("file.properties"), null);
}

Is there default properties file to put configurations into?

Any default properties file which java can automatcially load?
The short answer is no.
The somewhat longer answer starts with a question itself: What should be configured by this file?
For the logging-API of java (java.util.logging) exists a standard-properties-file to configure it. Other frameworks may as well use standard-configuration files. But that always configure only stuff specific for this framework.
If you want to have persistent configuration, you probably want to use the Preference-API. That allows you to save configuration-data, that stays with the user or the JVM.
You can use your own default properties file. You can do it with just few lines.
There is also built in properties, however, these are no simpler, but they are standard.
IMHO Most of the time a plain Proeprties file is used.
This example from the tutorial. This is a more complex example, you can have just one properties file.
// create and load default properties
Properties defaultProps = new Properties();
FileInputStream in = new FileInputStream("defaultProperties");
defaultProps.load(in);
in.close();
// create application properties with default
Properties applicationProps = new Properties(defaultProps);
// now load properties from last invocation
in = new FileInputStream("appProperties");
applicationProps.load(in);
in.close();
You have to load your properties file yourself or pass arguments at command line.
There is a small library that you can use which will load properties automatically for you: Confucius.

Storing changes in .properties file that has been read via getClass().getResourceAsStream

I am writing a java web application that reads properties from a .properties file. Since I do not know the absolute path of the .properties file, because it depends on the environment the application will run on in the future, I have to load it with "getClass().getResourceAsStream":
Properties props = new Properties();
props.load(getClass().getResourceAsStream("test.properties"));
message = props.getProperty("testdata");
This works as expected. Now I want to change the value for testdata in the file. But I cannot open an Outputstream to write to, because I still don't know the path of the .properties file.
props.setProperty("testdata", "foooo");
props.store(new FileOutputStream("?????"), null);
Is there a way to get the path of the file or can I use the established Properties-object somehow? Any ideas are welcome that allow me to change the .properties file.
You can get an URL by using getResource() rather than using getResourceAsStream()
You can then use that URL to read from and write to your properties file.
File myProps = new File(myUrl.toURI());
FileInputStream in = new FileInputStream(myProps);
Etc.
The Properties class includes a store method that can be used to save the properties back to the stream that was read in getClass().getResourceAsStream("test.properties").

Accessing and encoding of properties files

I'm used to work with properties files, for example from Ant. Where I can simply reference the property file doing something like that:
<property file="webapp_DO_NOT_COMMIT.properties"/>
(the file is so named because our DVCS is configured as to never commit files containing "DO_NOT_COMMIT" to prevent committing credentials/passwords/etc.)
Here's a very simple .properties file example:
passwd=brokencleartextpassword
Now I want to put some configuration in another, similar, properties file that I need to access from my Java code. How should I go about it?
I also have another related question: is the character encoding of .properties file defined by any spec?
Put it in the classpath and then load it by java.util.Properties API.
Properties properties = new Properties();
properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("filename.properties"));
As to the encoding, it's ISO-8859-1, as specified in the API javadoc.
... the input/output stream is encoded in ISO 8859-1 character encoding. ...
But you can go around it by feeding a Reader instead (which is new since Java 1.6).
Properties properties = new Properties();
InputStream input = Thread.currentThread().getContextClassLoader().getResourceAsStream("filename.properties");
properties.load(new InputStreamReader(input, "UTF-8"));
See also:
Properties tutorial

Categories

Resources