Retriving properties values using jboss 6 properties service - java

I have a properties file inside conf directory in a jboss 6 server.
How i can get a group or all the key and values from that file inside my application:
<attribute name="URLList">
./conf/crawler.properties
</attribute>
thanks

Did this, but using java.util.Properties:
String path = System.getProperty("jboss.server.config.url") + propertiesFileName;
Properties props = new Properties();
URL url = new URL(path);
props.load(url.openStream());
The Properties class has all the neccessary methods for reading key-value-pairs (it also implements Map<Object,Object>).

Related

Externalize a property file which is inside a jar file

I have jar with application.properties file as below-
BASE_ENVIRONMENT = http://localhost
This is my utility class to read the property file
Properties prop = new Properties();
ClassLoader loader = Thread.currentThread().getContextClassLoader();
InputStream in = loader.getResourceAsStream(fileName + ".properties");
the property file is in <default package>
This jar is used as a dependent jar in my web application which is deployed in tomcat server.
But i need to change the BASE_ENVIRONMENT for production environment.
Is there a way i can externalize this property file value?
You could add a system parameter to act as a profile:
// default to DEV
String profile = "dev"
if (System.getProperty("ENV") != null) {
profile = System.getProperty("ENV");
}
Properties prop = new Properties();
ClassLoader loader = Thread.currentThread().getContextClassLoader();
InputStream in = loader.getResourceAsStream(fileName + "_" + profile + ".properties");
Then you would start your app with
.... -DENV=prod
and the file like config_prod.properties would be found in the classpath, the default would be config_dev.properties.

how to read a file from resources folder in properties file

I have a file under resources folder src/test/resources/file.xml
and another under src/test/resources/test.properties. I want to set a property in properties file to point to file.xml. How can I achieve this?
Say I have a property
test.file = file.xml
and my java class reads the file as follows:
File cert = new File(fileName); // fileName is the value of test.file
This does not work however.
You can use Properties class to read and write to config files.
Firstly, you need to find the relative path for the resources using
below steps
Secondly, you can configure and load the test properties file
Finally, read the property value and append with the resource
directory
Code:
String rootDirectory=System.getProperty("user.dir");
String resourceDirectory=rootDirectory+"src/test/resources/";
//Configure property File
Properties properties = new Properties();
properties.load(new FileInputStream(resourceDirectory+"test.properties"));
PropertyConfigurator.configure(properties);
//To get the property value
String tempFileName=properties.getProperty("test.file");
//filename Needs to be changed as below
File cert = new File(resourceDirectory+tempFileName);

How to read from properties file and get values in wsdl file java

In my soap webservice java project i have a properties file in the class path for reading some constants in the project. Its a non maven project, all jars are added directly to the lib folder. Here i want to read from properties file and refer those keys in wsdl file, which is inside WEB-INF folder. How can i add those key in the wsdl file to get the values inside it. Please help and many thanks in advance for your valuable replys.
it's not that hard, you can do it like that:
you have a config.properties file which contains :
database=xxxx
dbuser=myusername
dbpassword=xxxxx
IP= 192.xxxxxx
When you want now to get the username you do :
private static Properties prop;
private String username;
prop = new Properties();
username = prop.getProperty("username"); // you get myusername

Configuration File for Java Application to change path

I have made a Java (.jar) application that uses and external image and MS Access database.
Both things are accessed using a path. This won't work if I give the application to my friend to test as the path wont match.
I was wondering if I could make configuration settings file that would change the path by editing the settings file and make the application work fine instead of opening the source code in editor and editing there.
Yes you can do it by creating a configuration file. Lets say your configuration file is "config.properties". You can mention properties required in file like
#comment
imageFile=C://imagePath
database=<path to db>
username=
password=
Then read the file
Properties properties = new Properties();
InputStream in = null;
in= new FileInputStream("config.properties");
//load a properties file
properties.load(input);
// get the property value and print it out
System.out.println(properties.getProperty("imageFile"));
---
Make sure file is accessible by keeping it in classpath.
Properties properties;
try(InputStream input = this.getClass().getClassLoader().getResourceAsStream("app.properties")) {
properties = new Properties();
properties.load(input);
} catch (IOException e) {
throw new RuntimeException(e);
}
System.out.println(properties.getProperty("my.path"));
Property file format is very simple
my.path = /home/file.txt
semicolon_Also_delimiter:value
semicolon\:can\:be\:escaped:value

Getting a config file outside of a jar file?

I want to open a .config file outside of the .jar file so that I can use its properties inside of the .jar. How would I do this?
For clearance you will use a config file from the (executable) jar like this:
InputStream in = this.getClass.getResourceAsStream("config.properties");
Properties p = new Properties();
p.load(in);
that loads config.properties relative to the class of the current object.
For a config outside you propably use a file:
InputStream in = new FileInputStream(System.getProperty("user.dir")+"/"+"config.properties");
Properties p = new Properties();
p.load(in);
Use this.
String path = System.getProperty("user.dir")
path += "/config/myApp.properties"
Now you have the path of your properties file.
You know what to do next

Categories

Resources