views:

2915

answers:

5

I'm creating a web service, which run in GlassFish, and I want to have some custom properties. For this I'm using the Properties class. The code I'm using is:

Properties p=new Properties();
File f=new File(System.getProperty("user.dir"), "settings.properties");
p.load(new FileInputStream(f));

But how do I get the settings.properties-file in my config directory?

I'm not sure about my classpath, since this is managed by NetBeans and GlassFish. I assume my .war-file is added to the classpath, when deploying...

I've added my own solution, but if anyone could come up with a better solution, it would be highly welcome...

+1  A: 

See here for how you can read a properties file from your classpath:

URL url =  ClassLoader.getSystemResource("test.properties");
Properties p = new Properties();
p.load(new FileInputStream(new File(url.getFile())));

You then only need to add your config directory to the classpath.

If you have problems using the above code try ServletContext.getResource.

kgiannakakis
How do I add the config-directory to my classpath in GlassFish/NetBeans? I can't find it anywhere...
doekman
Just put it in WEB-INF/classes directory
kgiannakakis
I tried that, but no doesn't work...
doekman
Could you try ServletContext.getResource?
kgiannakakis
+1  A: 

+1 for putting it in your classpath.

If you're using Spring (and I'd highly recommend you do if you're not already for many reasons) when you can load a properties file like this:

database.username=scott
database.password=tiger

and put references in your application context like:

<property name="username" value="${database.username}"/>

(assuming you've configured the property configurator) and it will cause an error if the file can't be loaded or the property doesn't exist. The application will fail to start. This is actually a good thing. It lets you find problems really really quickly and much better than failing silently, which can sometimes have catastrophic effects.

cletus
Just use spring for configuration? I'd rather use some standard things.
doekman
That's not all that you use Spring for (obviously). Particularly for Web applications running in application server, I can't envision a scenario where I wouldn't use Spring. After the JDK it has to be the second most commonly deployed jar(s).
cletus
We're just creating webservices.
doekman
Spring has extensive implementation helpers for creating Webservice endpoints and consumers.
cletus
I just want to read a property-file. That shouldn't be that difficult?
doekman
+1  A: 

I've tried a lot, but I solved this with:

        // ServletContext ctx
        InputStream stream = ctx.getResourceAsStream("version.properties");
        p = new Properties();
        p.load(stream);

I have to pass the ServletContext from a jsp-page with a call to getServletContext()getServletContext(). Not ideal, but it's the only way I could get it working...

It would be nice though if anyone could come up with another solution, that could work withyout the ServletContext.

doekman
+1  A: 

Alternatives:

Depending on how your domain is configured, you might be able to use asadmin create-system-properties from the command line. Run/see asadmin create-system-properties --help for more info.

Or you might like administering system properties through the Glassfish admin interface. Here's the default link: http://localhost:4848/configuration/systemProperties.jsf?configName=server-config

fredarin
That's nice to know, and might come in handy. I'd like to have some settings outside my container. But the version-number needs to be inside the container, so I want to use a property-file. Advantage: the version number is also updateable by the build script (ant).
doekman
+2  A: 

The solution that works is actually pretty simple:

URL url =  this.getClass().getResource("/package/name/file.properties");
p = new Properties();
p.load(new FileInputStream(new File(url.getFile())));

Why didn't anybody come with this?

doekman