Using external properties files in weblogic - java

I am working on deploying a J2ee application that I have previously been deploying in JBOSS into Weblogic 10.3.1.0. I am running into an issue with external properties files. In Jboss I can just put the properties files into $JBOSS_HOME/server/default/conf, and they are loaded onto the system classpath and I can access them without any problems. I was able to put shared libraries into $MIDDLEWAREHOME/user_projects/domains/mydomain/lib and they were loaded into the system classpath without any problems but I am unable to load properties files.
Does anyone know how to include external properties files in Weblogic?
Thanks,

I figured this out and have it working the way I would expect. First I did try the suggestions as above. If i added a folder to my classpath, or put the properties files in a folder on my classpath, the jars in the file were picked up, but not properties files. If i put my properties files in a jar, and put them in a folder on my classpath everything worked. But I did not want to have jar my files everytime a change was made. The following works in my env.
If i place the properties files in %WEBLOGIC_HOME%/user_projects/domains/MYDOMAIN then they are getting picked up, without having to be placed in a jar file.

In weblogic jars will be loaded from the lib and the non jar files will be loaded from the domain folder

There are ways to read properties file in Java from weblogic classpath
One (Properties file located in the weblogic domain): Drop the properties file inside the Domain directory. This way the properties file is added to the weblogic classpath automatically and we can read from Java using resourceAsStream.
Two (Properties file from a User defined location):The advantage with this approach is that the property file can reside outside the JAR or EAR file and can be modified conveniently.
package com.test;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class PropertyFileExample {
private static Properties prop;
public static void myMethod() {
InputStream is = null;
try {
prop = new Properties();
String propFilePath = System.getProperty(“propFileLocation“);
InputStream iStream = PropertyFileExample.class.getClassLoader().getResourceAsStream(propFilePath);
//Note that the propFilePath is a -Dparam defined below in the setDomainEnv
prop.load(iStream);
prop.getProperty(“dbuser”);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
In the weblogic setDomainEnv (under bin) => we need to pass the location of the property file as a -D argument to JAVA_OPTIONS
set JAVA_OPTIONS=%JAVA_OPTIONS% -DpropFileLocation =/dev/file/properties/some.properties

You can set a directory on the classpath and Place your custom properties file in that folder/directory. So that, the entire directory along with property file will be on classpath.
To set the directory on the classpath in weblogic 10.3.x
Create a folder in %DOMAIN_HOME%\config\ folder. example appConfig.
Place your custom property file (Let's say config.properties) in appConfig directory/folder.
Modify the setDomainEnv.cmd (Windows) to include appConfig in the classpath by setting %DOMAIN_HOME%\config\appConfig as value to EXT_POST_CLASSPATH(this variable is already defined in the setDomainEnv.cmd file) variable as below:
set EXT_POST_CLASSPATH=%EXT_POST_CLASSPATH%;%DOMAIN_HOME%\config\appConfig
You can access that file in you java code as below:
InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream ("config.properties");
Properties prop = new Properties();
prop.load(inputStream);
String value = prop.getProperty("key");
Hope this helps.

The most flexible way is to use weblogic deployment plans and Generic File Loading overrides
External properties file with Weblogic
http://docs.oracle.com/cd/E21764_01/web.1111/e13702/config.htm#DEPGD188

Although it may be a little extra effort, if you put the files into a JAR before dropping them into that lib directory, that should work.

You can look at your setDomainEnv.cmd (Windows) or setDomainEnv.sh (Unix/Linux) script in your domain files and see what locations are added in the CLASSPATH for your domain. Then just choose one folder and place the properties file there, if you want a specific location for your properties file just edit the script.

that was my solution:
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
InputStream is = null;
String urlExte = System.getenv("DOMAIN_HOME")+"/properties/SmsBalanceadoWS/";
org.springframework.core.io.Resource resource = ctx.getResource( "file:"+urlExte+"/application.properties");
try {
is = resource.getInputStream();
} catch (IOException e) {
LOGGER.debug("ERROR"+ e.getMessage());
}

Related

prunsrv service with external properties file

I have a java .jar file I created using NetBeans. I am using apaches procrun (prunsrv.exe) to install that .jar as a Windows Service. I modified the code to get a property from a config.properties file.
I added the config.properties file to the same folder that my .jar file resides in.
My code is as follows:
Properties props = new Properties();
InputStream inputStream = MyService.class.getClassLoader().getResourceAsStream("config.properties");
props.load(inputStream);
On the last line of my code, I am getting a NPE when I attempt to start my service. I assume this is because the file is not found.
I modified the manifest.mf as follows:
Class-Path: .
I also tried copying config.properties to the "lib" folder (subfolder to where my .jar file is located). Same results.
I modified the "set PR_CLASSPATH" line in the batch file that installs the service as follows:
set PR_CLASSPATH=MyService.jar;.
Still same NPE.
How can I get my code to recognize my config.properties file once the service has been installed?
Thanks,
Raymond
This is what I use to load resources in these situations and seems to work most of the time:
public static InputStream getResourceAsStream(String path) {
return Thread.currentThread().getContextClassLoader().getResourceAsStream(path);
}
Could you check, if this helps in your case?
Another strategy to overcome this problem is the one described in my comment:
"copy the configuration file to a specific absolute folder path (i.e. c:\test) and change the classpath to point to that folder (set PR_CLASSPATH=MyService.jar;.;c:\test)"

Tomcat7 deploying app with custom files

This is the content of my netBeans project:
To use the folder called "ErrorSet" i use this line:
File file = new File("ErrorSet/error_list.xml");
It is necessary to import this file because it contains custom error codes, to the point:
When you want to import something in a netbeans project, the default "root" from where you use the files is the project name folder like [projectName]/ErrorSet/error_list.xml ...
Where do i need to place the ErrorSet folder when deploying the [projectName].war from dist folder in tocmat7 so that i can use new File properly? What does the File("ErrorSet/error_list.xml") parent directory become since its in tomcat7?
Keep in mind that Web Pages and Source packages are different things.
To use the classes inside Source packages with custom files, you have to place your files inside a Package and use getClass().gerResource() like this:
In case the ErrorSet folder is a folder inside another package use this:
File file = new File(getClass().getResource("ErrorSet/error_list.xml").toURI());
And if the error_list.xml is in the same package as the class, simply use getResource("error_list.xml").
Don't use java.io.File.
Instead, use a stream that you can get from the ClassLoader, like this:
InputStream in = null;
try {
in = request.getServletContext().getResourceAsStream("/WEB-INF/ErrorSet/error_list.xml");
if(null != in) {
// read the XML
}
} finally {
if(null != in) try { in.close(); }
catch (IOException ioe) { /* log this */ }
}
Now, put your file into /WEB-INF/ErrorSet/error_list.xml in your deployment.
This will work whether the file is on the file system or packaged up in an unexploded WAR file. It will also work in environments with a SecurityManager installed that won't allow the web application to read files, because the servlet container probably does have privileges to read those files.

Loading Properties from a JAR file (java 1.6)

I manually inject a properties file inside a jar.
How to load properties from a jar file before java 1.7 ?
I tried many workarounds and nothing worked so far.
There's plenty questions about it, but everything is focused on ClassLoader methods from java 1.7.
When you have a properties file inside your classpath or inside your jar file it becomes a resource. Any other case is a simple file.
What you need to do, before you package your jar file, is add to your classpath the folder where the properties files are (i.e myproject/src/main/resources/) then wherever you do a
Properties properties = new Properties();
properties.load(MyClass.class.getResourceAsStream("/yourPropsFileName"));
it will load it!
Although, if you are using an external property file you can also load it by using:
Properties properties = new Properties();
properties.load(new FileInputStream("extenalPropsFileLocation"));
Hope it helps!
From some class, call:
getClass().getResourceAsStream("/path/to/props.props")
Make sure that the path matches up with a classpath location.

How to use a .properties file in Eclipse Java Dynamic Web Project?

I'm developing a Dynamic Web Project in Eclipse. I created a .properties file for store database details (Username, Password etc.). I added it by right clicking on the project and New -> File . I used the Java util package Properties class. But it does not working. I can not retrieve any property from the file. Here is the code I used,
Properties prop = new Properties();
try {
prop.load(new FileInputStream("database.properties"));
String db = prop.getProperty("database");
String userName = prop.getProperty("dbuser");
String password = prop.getProperty("dbpassword");
} catch (IOException ex) {
ex.printStackTrace();
}
Is there something wrong or Is there any particular place where I should put properties file.
What you did is correct, ie right clicking the project and new--file.You have to Put your properties where you start your jvm from. Please look into the attached image. The properties file is marked in red. Look if your properties file is also located something like this.
Also add this in your code to find out where to put your file:
System.out.println(new File(".").getAbsolutePath());
For more details please follow this link- FileNotFoundException when using java properties file
Normally, you make sure the properties file is in the project runtime classpath (e.g. WEB-INF/classes) and then load it using either the System classloader or the property file handler's classloader, i.e. (Freehand typing from memory -- NOT COMPILED)
try{
Properties p = new Properties();
InputStream in = MyPropertyHandler.getClass()
.getClassLoader()
.getResourceAsStream("com/package/props/database.properties");
p.load(in);
catch(IOException e){
e.printStackTrace(System.err);
}
I'm betting you aren't pointing at the correct location. Make sure you're properties file is in the correct place. Using that code, I believe it is looking for ${CURRENT_WORKING_DIR}/database.properties, which is the case of a web app in eclipse is WEB-INF/classes (i think).
You should instead be using the more portable java.util.Properties#load(InputStream) with the result of javax.servlet.ServletContext#getResourceAsStream(String).
Try to give absolute path or relative path to the proprty file, also check this propery file path has been add to source folders or not, if not it will not be copied to your classes folder. (Right cclick on project , check java build path under source tab.
You should have .properties file in same package as class that is using it.
Or better, read properties file with getResourceAsStream method (otherwise you can have some problem later when you'll have file in .war archive).
InputStream inputStream =
getClass().getClassLoader().getResourceAsStream("database.properties");

How to Specify Path for Properties file

I am using Config. properties file for passing parameters to my methods Now i am loading file from
Properties Config= new Properties();
Config.load(new FileInputStream("C:\\Config. properties "));
As i don't want to keep it hard coded how can i set it with package level. or within application.
Thanks in Advance.
Make use of ResourceBundle Class. You just need to specify the properties file name. It will take the file from any path,provided the path should be in the classpath.
Example:
// abc.properties is the properties file,which is placed in the class path.You just need to
// specify its name and the properties file gets loaded.
ResourceBundle s=ResourceBundle.getBundle("abc");
s.getString("key"); //any key from properties file...
I was also just going to suggest that but you can also pass in the full path to the config file via a command line argument for example:
java YourApp -config C:\\config.properties
A properties file packaged with the application should not be loaded using the file system, but using the class loader. Indeed, the properties file, once the application is packaged, will be embedded inside a jar file, with the .class files.
If the config.properties file is in the package com.foo.bar, then you should load it using
InputStream in = SomeClass.class.getResourceAsStream("/com/foo/bar/config.properties");
Or with
InputStream in = SomeClass.class.getClassLoader().getResourceAsStream("com/foo/bar/config.properties");
You may also load it with a relative path. If SomeClass is also in the package com.foo.bar, then you may load it with.
InputStream in = SomeClass.class.getResourceAsStream("config.properties");
Note that Java variables should always start with a lowercase letter: config and not Config.
If it's just the path you're worried about then you can use a relative path:
Config.load(new FileInputStream("Config.properties"));
This will look in the current working directory. The upsdie: dead simple. The downside: it's not that robust. If you start your application from somewhere else without changing the working directory before, the file won't be found.
Put the config file in the classpath (where your .class files are), and access it using
getClass().getClassLoader().getResourceAsStream(_path_to_config_file);
There are two ways to get the path of the config files at runtime.
a) Getting it from database.
b) Getting it from custom properties of JVM configured at server level
Best process is "b" , you can change the properties of JVM at any time if path is changed and just restart the server.

Categories