Currently within my Java Application I have the following Class that I use in order to retrieve values from my properties file (application.properties):
public class MyProperties {
private static Properties defaultProps = new Properties();
static {
try {
java.io.InputStream in= MyProperties.class.getClassLoader().getResourceAsStream("application.properties");
defaultProps.load(in);
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static String getProperty(String key) {
return defaultProps.getProperty(key);
}
}
An example of instantiating an int using the MyProperties class:
int maxNumberOfPeople = Integer.parseInt(MyProperties.getProperty("maximumPeople"));
I would like to change this class in order to read an XML properties file rather than e.g. application.Properties.
How can I do so, and still keep the ability to still instantiate values using the MyProperties class?
Read the javadoc for the Properties.loadFromXML(...) method.
Method summary:
Loads all of the properties represented by the XML document on the specified input stream into this properties table.
The Properties javadoc includes the DTD for the XML document (file).
It would be better to write your loader using a try-with-resources like this:
try (java.io.InputStream in = MyProperties.class.getClassLoader().
getResourceAsStream("application.properties")) {
// load properties
} catch (Exception e) {
e.printStackTrace();
}
Also, it is a bad idea to catch and squash exceptions like that.
Don't catch Exception.
If the properties failed to load, you most likely want the application to "bail out".
Finally, you probably shouldn't load the properties in a static initializer, because that leaves you with no clean way to deal with any exceptions that might arise.
Related
I try to load a property file in Java running on JBossFuse/karaf.
The file is located at $[karaf.home]/etc/bean.properties
The Code is able to load properties inside the bundle fine, but now I try to exclude the properties from the project itself and the code throws a Nullpointer-Exception.
The Path is properly resolved on my development machine as
C:\Users\someone\devstudio\runtimes\jboss-fuse-6.3.0.redhat-135\etc\bean.properties
The property-File can be loaded in the blueprint-XML to configure beans, but to access the bean my code needs the CamelContext. As I have some static codeblocks that are accessed without an exchange/context/registry, I also wanted to be able to load the properties in Java.
Both the functions throw the NullPointerException and I guess, it is because the code runs in Fuse.
public static Properties getProperties(String location) {
Properties prop = new Properties();
InputStream input = null;
try {
input = PropertyLoader.class.getClassLoader().getResourceAsStream(location);
prop.load(input);
} catch (IOException ex) {
log.error("Error loading properties file from: " + location, ex);
return null;
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
log.error(e);
}
}
}
return prop;
}
public static Properties getPropertiesFromFilesystem(String location) {
Properties prop = new Properties();
InputStream input = null;
try {
input = new FileInputStream(location);
prop.load(input);
} catch (IOException ex) {
log.error("Error loading properties file from: " + location, ex);
return null;
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
log.error(e);
}
}
}
return prop;
}
The Exception:
java.lang.NullPointerException
at java.util.Properties$LineReader.readLine(Properties.java:434)[:1.8.0_91]
at java.util.Properties.load0(Properties.java:353)[:1.8.0_91]
at java.util.Properties.load(Properties.java:341)[:1.8.0_91]
at com.mycompany.util.PropertyLoader.getProperties(PropertyLoader.java:19)[319:camel-archetype-blueprint:0.0.14]
at com.mycompany.camel.blueprint.MyProcessor.process(MyProcessor.java:21)[319:camel-archetype-blueprint:0.0.14]
at org.apache.camel.processor.DelegateSyncProcessor.process(DelegateSyncProcessor.java:63)[231:org.apache.camel.camel-core:2.17.0.redhat-630135]
at org.apache.camel.management.InstrumentationProcessor.process(InstrumentationProcessor.java:77)[231:org.apache.camel.camel-core:2.17.0.redhat-630135]
at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:468)[231:org.apache.camel.camel-core:2.17.0.redhat-630135]
at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:196)[231:org.apache.camel.camel-core:2.17.0.redhat-630135]
at org.apache.camel.processor.Pipeline.process(Pipeline.java:121)[231:org.apache.camel.camel-core:2.17.0.redhat-630135]
at org.apache.camel.processor.Pipeline.process(Pipeline.java:83)[231:org.apache.camel.camel-core:2.17.0.redhat-630135]
at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:196)[231:org.apache.camel.camel-core:2.17.0.redhat-630135]
at org.apache.camel.component.timer.TimerConsumer.sendTimerExchange(TimerConsumer.java:192)[231:org.apache.camel.camel-core:2.17.0.redhat-630135]
at org.apache.camel.component.timer.TimerConsumer$1.run(TimerConsumer.java:76)[231:org.apache.camel.camel-core:2.17.0.redhat-630135]
at java.util.TimerThread.mainLoop(Timer.java:555)[:1.8.0_91]
at java.util.TimerThread.run(Timer.java:505)[:1.8.0_91]
Any help would be highly appreciated.
Do not do that. You are looking for trouble.
Load properties the OSGi way (use .cfg as extension and a blueprint property-placeholder bean)
You have the added benefit of getting notified if the file changes (if you wish)
Inject them in a bean EVEN IF you are using only static methods.
Don't mix managed beans with unmanaged static code unless you know very well what you are doing.
If some "static" code requires properties means that it is stateful, and this class deserves to be instantiated to a bean.
Not sure why you are getting an NPE without a more complete example. If you need to use properties without a route, you should be using Camel's property placeholder facilities:
https://access.redhat.com/documentation/en-us/red_hat_jboss_fuse/6.3/html/apache_camel_development_guide/basicprinciples#BasicPrinciples-PropPlaceholders
I followed in the code in the Android tutorial https://developer.android.com/training/basics/data-storage/files.html
public File getTempFile(Context context, String url) {
File file;
try {
String fileName = Uri.parse(url).getLastPathSegment();
file = File.createTempFile(fileName, null, context.getCacheDir());
} catch (IOException e) {
// Error while creating file
}
return file;
}
When I add this method to my class, there was an error on this code:
return file;
That says "File may not have been initialized"
What I did is I transferred the initialization code
File file;
To the class variable as a class field instead of a local method variable and the error disappeared. Is this the correct way to resolve the issue? Is there no workaround that would keep the variable local in the method and not global in the class?
I would prefer to limit visibility. Fields have a default null initialization. So the equivalent in the method is
File file = null;
The other answers are fine ; especially the one about avoiding the variable completely, but let's answer your question, too:
You put variables where you need them, preferably on the "smallest" scope possible. Meaning that you prefer local variables over class wide fields. In other words: you only use fields when two or more methods need to use the same information!
How about getting rid of it completely
public File getTempFile(Context context, String url) {
try {
String fileName = Uri.parse(url).getLastPathSegment();
return File.createTempFile(fileName, null, context.getCacheDir());
} catch (IOException e) {
// Error while creating file
// well lets say so then
System.err.println ("Error while creating file ");
e.printStackTrace ();
return null;
}
}
public File getTempFile(Context context, String url) {
File file = null;
try {
String fileName = Uri.parse(url).getLastPathSegment();
file = File.createTempFile(fileName, null, context.getCacheDir());
} catch (IOException e) {
// Error while creating file
}
return file;
}
You can get rid of local variable file, or just initialize it with null.
I am using EJB 3.0 and CDI to develop a java ee application which will be deployed in Websphere application server.
I have a requirement to have a property file from which i read certain configuration parameters and this property file should reside in the filesystem of the host system where my code will be deployed.
The base path ( directory where the property file will be placed ) for the property file is configured as a Name space binding String resource in Websphere application server.
Currently i have coded a Utility class to retrieve and use the property file which looks as below.
#Singleton
public class AppPropertyUtil {
private static Hashtable apppProperties;
#Resource(name="jndi/basePath",lookup="jndi/basePath")
private static String basePath;
private static final Logger LOGGER = LoggerFactory.getLogger(AppPropertyUtil.class);
protected void loadPropertyBundleFromFileSystem(String path)
{
InputStream inputStream = null;
Properties properties = null;
try {String fullPath=basePath+"/"+path+".properties";
LOGGER.info("Property file path : "+fullPath);
inputStream = new FileInputStream(new File(fullPath));
if (inputStream != null) {
properties = new Properties();
properties.load(inputStream);
LOGGER.info("Properties loaded");
apppProperties = (Hashtable)properties;
}
} catch (FileNotFoundException exception) {
LOGGER.error("Cannot read property bundle ",exception);
}
catch (IOException | IllegalArgumentException exception) {
LOGGER.error("Unable to loadproperties ",
exception);
}
}
public String getProperty(String key)
{
if(apppProperties == null)
{
loadPropertyBundleFromFileSystem("AppProps");
}
Object value = apppProperties.get(key);
if(value != null){
return (String) value;
}
return null;
}
}
But having the #Resource annotation will mandate that the AppPropertyUtil class be injected inside any class that wishes to use it. So, I will not be able to use this in any POJO classes which are not managed.
Please help me understand whether this is the best approach to go with for the above requirement or could this be improved. I would also like to make the getProperty method and loadPropertyBundleFromFileSystem method static to have it being used from a static context, but it is not possible as the class should be injected to be used.
Thanks in advance
Ideally, you shouldn't need to access to your class from any POJO. Use design patterns and separate your concerns. Use your class as a service to load up your property file and serve out a Map or Set of it's contents. POJO Models shouldn't care, but your other classes can read that map/set of properties and pass them to your pojo's or other classes which need it but don't have direct access to it.
The other option is to remove the #Resource and make this just a plain old util class that you pass in the filename to read in it's constructor, then when you call getProperty, you do what you do to check to see if it's been loaded already, if not, load it.
Wrap that in a factory that supplies the #Resource parts and it keeps your EE code from bleeding into your other jars (utils).
I want to create a config.properties file, in which I want to store all the key and values instead of hard coding them in the Java code.
However, I do not know how to create a properties file in eclipse.
I researched and found help on how to read a properties file.
I need help with how to create it.
Here are my specific questions:
Can a config.properties file be created in eclipse, and data be
typed directly into it as though the config.properties is similar to
text editor?
If it can be directly created, the can you please let me know the
steps to create this properties file?
I am assuming that properties file can be created just like how java
project, java class etc are created (by right clicking at package or
project level). Is this correct assumption?
Or creating a properties file and adding data to it needs to be done
by java coding?
I will greatly appreciate any help.
Create a new file from file menu Or press Ctrl+N
In place of file name write config.properties then click finish
Then you can add properties your property file like this
dbpassword=password
database=localhost
dbuser=user
Example of loading properties
public class App {
public static void main(String[] args) {
Properties prop = new Properties();
InputStream input = null;
try {
input = new FileInputStream("config.properties");
// load a properties file
prop.load(input);
// get the property value and print it out
System.out.println(prop.getProperty("database"));
System.out.println(prop.getProperty("dbuser"));
System.out.println(prop.getProperty("dbpassword"));
} catch (IOException ex) {
ex.printStackTrace();
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
Probably it is discussed somewhere but I failed to find it.
I need to load class properties (java.util.Properties) inside class static initialization block. This is to make possible to access some class general options even without its objects creation. To do so I need appropriate Class object. But of course access to such Class object fails on null object. Something like this.
Class Name {
private static Properties properties;
static {
Name.properties = new Properties();
Name.properties.load(Name.class.getResourceAsStream("Name.properties"));
}
}
Any idea how to handle this situation?
UPDATE:
It was resource name (should be "/Name.properties" for my case). Everything else was OK.
+1 for all meaningful answers from me and ... don't forget to check operations one by one :-).
properties field must be static. And before load you need to initialize static variable with proeprties = new Properties() after that you can invoke load
Declare properties as static and initialize
static Properties properties;
or
static Properties properties = new Properties();
and static block should be
static {
try {
properties = new Properties(); //if you have not initialize it already
Name.properties.load(Name.class.getResourceAsStream("Name.properties"));
} catch (IOException e) {
throw new ExceptionInInitializerError(e); //or some message in constructor
}
}
You need to catch IOException while loading properties file
Final code based on all suggestions is like this:
Class Name {
private static final Properties properties = new Properties();
static {
try {
InputStream stream = Name.class.getResourceAsStream("/Name.properties");
if (stream == null) {
throw new ExceptionInInitializerError("Failed to open properties stream.");
}
Name.properties.load(stream);
} catch (IOException e) {
throw new ExceptionInInitializerError("Failed to load properties.");
}
}
}