Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Glassfish System Property + Relative Path  RSS feed

 
Marco Di Baggio
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Community,

I have recently deployed an application (myproject) within my Glassfish app server. I have a quick question. I would like to create a system property / or instance property to the relative path of a config/properties file (*.properties) located within my deployed application.

How do I specify a Glassfish property (System/Instance) to the path of the config/properties file, again, that is located within a the already deployed application (myproject)?

Thank-you kindly
 
K. Tsang
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I understand you correctly, your properties file is inside the jar/war file. To access such file use the getClassLoader().getResourceAsStream("full path to file")
 
Marco Di Baggio
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello K.Tsang,

Your almost correct, I do want to access the properties file inside my war file, however I was hoping to simply declare a system property in Glassfish (server->system properties) as I will not be able to open up the code to change it.

Is this even possible?
 
K. Tsang
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess it's possible. Just need to know the path relative to the web app.

Or if you use HTTP Apache, such file may put there instead. Reading it in code is similar (open URL or something)


EDIT: after googling, Glassfish system properties should be available through System.getProperties(). Maybe try create your system properties in Glassfish/asadmin command (eg create-system-properties mykey=myvalue) then read and display them to see if they are present.



Your output should contain lines similar to "list-system-properties" using asadmin command.
 
Tim Holloway
Bartender
Posts: 18661
71
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a very important difference between "files" inside a WAR file and actual files.

You cannot do filesystem open/close/etc. operations on the "files" inside a WAR file. Technically, those aren't files, they're resources. The actual access to them has to be done via the ZIPfile class methods.

What makes this even more confusing is that sometimes some webapp servers will unzip (explode) WAR files to construct an actual filesystem directory structure with true subdirectories and files. But that's not a safe assumption to make, since it's not always true and some of those systems (such as Tomcat) allow disabling that function. So to be safe, never use file operations on webapp resources. Use the getResource methods. And be aware that the get system file path method of a resource can return null if the WAR wasn't exploded and thus no physical file exists.

If my memory serves, there is an enumeration method that can be used to browse a WAR for resource paths, and that would allow you to search for, open and read (but not write!) any resource that matched your criteria (path ends with ".properties").

Additionally, if you put the resource in a jar in WEB-INF/lib or in the WEB-INF/classes resource sub-tree, you can use classpath methods to discover resources, since those locations automatically go into the webapp's classpath.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!