Getting the filepath of /WEB-INF/classes at runtime

Post any questions you have about using the Verj.io Studio, including client and server-side programming with Javascript or FPL, and integration with databases, web services etc.

Moderators: Jon, Steve, Ian, Dave

User avatar
dvanhussel
Ebase User
Posts: 161
Joined: Fri Oct 19, 2007 12:45 pm
Location: Haarlem, the Netherlands

Getting the filepath of /WEB-INF/classes at runtime

#1

Postby dvanhussel » Tue Feb 16, 2016 9:09 pm

Hi,

We are in the middle of defining a new development workflow, because we ar migrating to Ebase V5.0.

In the current (V4) settup, we have 3 servers (development/staging/production). We have 1 global script with environment specific settings (urls/passwords/filsystempaths etc). The correct settings are selected based on the system name. This has worked ok for us.

On V5 we want to use the 'local designer' for each developer. This means, we need settings that may differ between specific developers. Because I think it is bad practice to have these settings in source control I thought using a 'Java properties file on the server' would be a nice solution to this.

Something like this:

Code: Select all

var fileName= 'C:\\david\test.properties';
var properties =  new java.util.Properties();
properties.load(new java.io.StringReader(FileServices.readFile(fileName)));

const debug = properties.getProperty('debug');
In the webapp.properties

Code: Select all

debug=true
This all works as expected. But we still need to do something like this:

Code: Select all

if(system.systemName=='David'){
  fileName='C:\\david\\test.properties';
}
Is there a way to get the ServletContext in server side JavaScript?

In a servlet you can do something like this to find the location of WEB-INF/classes:

Code: Select all

String path=servletConfig.getServletContext().getRealPath("/WEB-INF/classes");
This way the settings could be found automaticly (convention based) on any server.

Or am I missing a better way to do this?

Regards,

David
0 x

Jon
Moderator
Moderator
Posts: 1342
Joined: Wed Sep 12, 2007 12:49 pm

#2

Postby Jon » Wed Feb 17, 2016 10:18 am

You can load a properties file directly from WEB-INF/classes using the web app's class loader. You shouldn't need direct access to the directory. Something like this..

Code: Select all

var props = new java.util.Properties();
var stream;
try
{
	stream = system.class.getResourceAsStream("/myprops.properties");
	props.load(stream);
}
finally 
{
	if (stream) stream.close();
}
log(props);
P.S. You can address the servlet context if you need to via client.httpSession.servletContext but only when a web client exists, so not for example in a form started via the scheduler.
0 x

User avatar
dvanhussel
Ebase User
Posts: 161
Joined: Fri Oct 19, 2007 12:45 pm
Location: Haarlem, the Netherlands

#3

Postby dvanhussel » Wed Feb 17, 2016 10:36 am

Thanks Jon, works like a charm!
0 x


Who is online

Users browsing this forum: No registered users and 8 guests