Using property files in Web Applications [duplicate] - jsf

This question already has answers here:
How to get properties file from /WEB-INF folder in JSF?
(3 answers)
Closed 7 years ago.
I'm developing a web application(this is my first time) and pretty confused about using property files. I don't know where to put the property files.
The problem is that i have put it under the WEB_INF folder. And when i test run it as a Java Application to check whether Database connections are working according to the properties in the property file it is working without any problem.
But when i run it in a Server as a Web Application it fails to load the properties file saying it could not find the file in the path specified. I tried using every possible path i could give and changing the file directories within the whole project. But I kept getting the same error.
Then i changed my class again from scratch thinking there's some kind of a bug withing my code where i load the properties file. And it seems that it could not find the file either when deployed as a Web App. But my test application works fine. Where do i put this file and how do i use it. I have read #BalusC's answer in this thread https://stackoverflow.com/a/2161583/2999358 but i have no idea why this happens. Can someone help me on this?
I'm using Tomcat 8, Eclipse IDE and building on JSF framework.
Class where i load my properties file
public class ConfigCache {
private static final File FILE = new File("./WebContent/WEB-INF/conf/config.properties");
private static final Properties PROPERTIES = new Properties();
public static final String JDBC_DRIVER = ConfigCache.getProperty("db.driverName");
public static final String DATABASE_URL = ConfigCache.getProperty("db.url");
public static final String DATABASE_USERNAME = ConfigCache.getProperty("db.user");
public static final String DATABASE_PASSWORD = ConfigCache.getProperty("db.pass");
public ConfigCache() {
}
public static String getProperty(String key) {
if (PROPERTIES.isEmpty()) {
loadProperties();
}
Object value;
return (value = PROPERTIES.get(key)) == null ? "" : value.toString();
}
private static void loadProperties() {
if (!FILE.exists()) {
throw new IllegalArgumentException("The 'config.properties' has not been found.");
}
try {
FileInputStream fis = null;
try {
fis = new FileInputStream(FILE);
PROPERTIES.load(fis);
} finally {
try {
if (fis != null) {
fis.close();
}
} catch (IOException exp) {
System.out.println("IOException #" + ConfigCache.class + " # loadProperties() : " + exp);
}
}
} catch (Exception exp) {
System.out.println("Exception #" + ConfigCache.class + " # loadProperties() : " + exp);
}
}
}
Folder Structure

Try With this.
put the property in src folder.

Your file is in the WEB-INF directory. This means it's part of the war and reachable as part of the class path. That's perfectly ok, since it makes it portable and independant of the web container installation (e.g. Tomcat).
You can load any file in the class path as a resource:
getClass().getResourceAsStream("/conf/config.properties")
This means you can write your code like this:
private static void loadProperties() {
InputStream is = getClass().getResourceAsStream("/conf/config.properties");
PROPERTIES.load(fis);
}
(Error handling omitted)

You can explode (unzip) your war/ear file and see the contents or folder structure of it and find why your code doesnt work. The reason is that the folder WebContent doesnt exist in your ear/war , but does exist only when run via eclipse. This is the reason why its always better to follow the solution provided in the link posted so that you can retrieve the porperty files from classpath. The below code fetches your property file in eclipse but not in the server.
private static final File FILE = new File("./WebContent/WEB-INF/conf/config.properties");
Contents of WAR file (from JournelDev), it contains WEB-INF directory but there would be no WebContent directory above it

Related

How to put JSF message bundle outside of WAR so it can be edited without redeployment?

We have a JSF application on WildFly 8 which uses the traditionally mechanism with internationalizing text by having message bundles for German and English in the WEB-INF\classes folder of the WAR and a configuration in faces-config.xml mapping a name to it and listing the locales. The application does not have a database connection, but uses REST services to communicate with a 2nd application.
Now we need to be able to change text more easily, meaning not having to build a new WAR file and do a deployment when changing a text. So I need a mechanism to have the message bundles outside of the WAR while being able to use it as before within the XHTML pages.
Two optional requirements would be to change the text and refresh the messages in the application without having to restart the application (priority 2), and to have a default bundle within the WAR, which is overwritten by the external bundle (priority 3).
My thought was to use something like Apache commons configuration to read a property file within an Application scoped bean and expose a getter under the EL name used before. But somehow it feels like having to re-implement an existing mechanism and that this should somehow be easier, maybe even with Java EE core only.
Has someone used this mechanism in such a way and can point me to some example/description on the details or has a better idea to implement the listed requirement(s)?
How to put JSF message bundle outside of WAR?
Two ways:
Add its path to the runtime classpath of the server.
Create a custom ResourceBundle implementation with a Control.
change the text and refresh the messages in the application without having to restart the application
Changing the text will be trivial. However, refreshing is not trivial. Mojarra internally caches it agressively. This has to be taken into account in case you want to go for way 1. Arjan Tijms has posted a Mojarra specific trick to clear its internal resource bundle cache in this related question: How to reload resource bundle in web application?
If changing the text happens in the webapp itself, then you could simply perform the cache cleanup in the save method. If changing the text however can happen externally, then you'd need to register a file system watch service to listen on changes (tutorial here) and then either for way 1 clear the bundle cache, or for way 2 reload internally in handleGetObject().
have a default bundle within the WAR, which is overwritten by the external bundle
When loading them from classpath, the default behavior is the other way round (resources in WAR have higher classloading precedence), so this definitely scratches way 1 and leaves us with way 2.
Below is a kickoff example of way 2. This assumes that you're using property resource bundles with a base name of text (i.e. no package) and that the external path is located in /var/webapp/i18n.
public class YourBundle extends ResourceBundle {
protected static final Path EXTERNAL_PATH = Paths.get("/var/webapp/i18n");
protected static final String BASE_NAME = "text";
protected static final Control CONTROL = new YourControl();
private static final WatchKey watcher;
static {
try {
watcher = EXTERNAL_PATH.register(FileSystems.getDefault().newWatchService(), StandardWatchEventKinds.ENTRY_MODIFY);
} catch (IOException e) {
throw new ExceptionInInitializerError(e);
}
}
private Path externalResource;
private Properties properties;
public YourBundle() {
Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale();
setParent(ResourceBundle.getBundle(BASE_NAME, locale, CONTROL));
}
private YourBundle(Path externalResource, Properties properties) {
this.externalResource = externalResource;
this.properties = properties;
}
#Override
protected Object handleGetObject(String key) {
if (properties != null) {
if (!watcher.pollEvents().isEmpty()) { // TODO: this is naive, you'd better check resource name if you've multiple files in the folder and keep track of others.
synchronized(properties) {
try (InputStream input = new FileInputStream(externalResource.toFile())) {
properties.load(input);
} catch (IOException e) {
throw new IllegalStateException(e);
}
}
}
return properties.get(key);
}
return parent.getObject(key);
}
#Override
#SuppressWarnings({ "rawtypes", "unchecked" })
public Enumeration<String> getKeys() {
if (properties != null) {
Set keys = properties.keySet();
return Collections.enumeration(keys);
}
return parent.getKeys();
}
protected static class YourControl extends Control {
#Override
public ResourceBundle newBundle
(String baseName, Locale locale, String format, ClassLoader loader, boolean reload)
throws IllegalAccessException, InstantiationException, IOException
{
String resourceName = toResourceName(toBundleName(baseName, locale), "properties");
Path externalResource = EXTERNAL_PATH.resolve(resourceName);
Properties properties = new Properties();
try (InputStream input = loader.getResourceAsStream(resourceName)) {
properties.load(input); // Default (internal) bundle.
}
try (InputStream input = new FileInputStream(externalResource.toFile())) {
properties.load(input); // External bundle (will overwrite same keys).
}
return new YourBundle(externalResource, properties);
}
}
}
In order to get it to run, register as below in faces-config.xml.
<application>
<resource-bundle>
<base-name>com.example.YourBundle</base-name>
<var>i18n</var>
</resource-bundle>
</application>

How to load properties file in a JSF application? [duplicate]

This question already has answers here:
Where to place and how to read configuration resource files in servlet based application?
(6 answers)
Closed 7 years ago.
I'm trying to load a properties file in a JSF application I'm working on, though I can't manage to reference the file.
package com.nivis.util;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class PropHandler {
String result = "";
InputStream inputStream;
public void loadProp() {
try {
inputStream = this.getClass().getResourceAsStream("prop.properties");
if (inputStream == null) {
System.err.println("===== Did not load =====");
} else {
System.err.println("===== Loaded =====");
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
inputStream.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
public static void main(String[] args) {
PropHandler ph = new PropHandler();
ph.loadProp();
}
}
The file is located in the same package and in the different examples I've found when searching for this, that should work. I've also tried to put the file in every conceivable place in the application and reference it to the best of my knowledge, but it does not work.
(only some of the folders that I've tested to put the file)
What am I doing wrong?
Optimally I'd like to have it in the same folder that I use for the msg.properties file.
As this answer elaborates, com/nivis/prop.properties should be the right way to reference the file nested in your resources folder.
But because you're not using ClassLoader classloader = Thread.currentThread().getContextClassLoader(); to locate the Classloader you have to use an absolute path starting with "/" resulting in /com/nivis/prop.properties.
try something like this
ClassLoader classloader = Thread.currentThread().getContextClassLoader();
InputStream is = classloader.getResourceAsStream("prop.properties");

SXSSFWorkbook with Custom temp file

SXSSFWorkbook does what I want, but I would like to use a different type of temp file then what is provided and seemingly baked into the implementation.
In SheetDataWriter
public File createTempFile() throws IOException {
return TempFile.createTempFile("poi-sxssf-sheet", ".xml");
}
So...I can extend this by making a MySheetDataWriter and Overriding the call to createTempFile. However, there is no way to for me to use MySheetDataWriter in the SXSSFWorkbook...if I try to extendt it then the package protected method...could not be overidden, because it is not visible.
from SXSSFWorkbook
SheetDataWriter createSheetDataWriter() throws IOException {
if(_compressTmpFiles) {
return new GZIPSheetDataWriter(_sharedStringSource);
}
return new SheetDataWriter(_sharedStringSource);
}
So the bottom line is that I can use the implementation almost exactly as is, but I need a different kind of Temp file...not even just a different directory to put it in, but a completely different implementation. Any ideas on how to do this?
Starting at version 3.11, the createTempFile method you mention (from class TempFile) uses a replaceable TempFileCreationStrategy that can be chosen with the setTempFileCreationStrategy method.
The following example extends the default strategy to log every temp file that is created, but you could change it to return a custom File instance.
TempFile.setTempFileCreationStrategy(new TempFile.DefaultTempFileCreationStrategy() {
#Override
public File createTempFile(String prefix, String suffix) throws IOException {
File f = super.createTempFile(prefix, suffix);
log.debug("Created temp file: " + f);
return f;
}
});

jsf 2.2 get the path for a propertie file

i want to read out from a propertie file in my jsf 2.2 project. i use eclipse kepler.
i try to use this in my java-bean in the folder src with the package de.exanple. The file of the bean is called PageServiceBean.java.
The propertie file is in the WEB-INF/resources/prop folder. The propertie file is called config.properties.
I have read that i can change the resouce folder in jsf 2.2 in the web.xml file with the javax.faces.WEBAPP_RESOUCRES_DIRECTORY param name and the param value like /WEB_INF/resoucres
But i don't get the path to the config file.
Can you tell where i can get the path name. I think i must use a relativ path name.
Can you please help me?
Update
I execute the second code fragment from you like:
private Properties getProperties() {
Properties prop = new Properties();
try {
//load a properties file
prop.load(new FileInputStream("config2.properties"));
} catch(Exception e) {
}
return prop;
}
public void setProperty1(Integer value) {
Properties prop = getProperties();
prop.setProperty("ort", value.toString());
try {
prop.store(new FileOutputStream("config2.properties"), null);
Properties prop2 = getProperties();
} catch (IOException ex) {
Logger.getLogger(PageServiceBean.class.getName()).log(Level.SEVERE, null, ex);
}
}
It works! I use the Properties prop3 = getProperties(); to read the the propertie file config2.properties. The File is Store in the eclipse home path ECLIPSE_HOME = C:\elipse_jee\eclipse. Can i change the path into a specific path, like WEB_INF/resources?
I will show you my approach to your need, but I won't try to answer your question.
To use properties files in a JEE application I create a Stateless bean that serves the rest of the application with the getter and setter for the properties. Only this EJB will access the property file in the server and I use the java.util.Properties.
private Properties getProperties() {
Properties prop = new Properties();
try {
//load a properties file
prop.load(new FileInputStream("config.properties"));
} catch(Exception e) {
}
return prop;
}
After I have the access methods for a specifc property:
public Integer getProperty1() {
Properties prop = getProperties();
String value = prop.getProperty("myProperty1Name");
if(value != null) {
return Integer.parseInt(value );
}
return 0;
}
public void setProperty1(Integer value) {
Properties prop = getProperties();
prop.setProperty("myProperty1Name", value.toString());
try {
prop.store(new FileOutputStream("config.properties"), null);
} catch (IOException ex) {
Logger.getLogger(PropertiesManager.class.getName()).log(Level.SEVERE, null, ex);
}
}
In this approach, if the file doesn't exist it will be created. The default value of a property will be hard coded though. For this approach, it doesn't matter where your file is placed. The actual location will depend on your JEE server configuration, domain configuration, application deployment files, etc.
Web content resources are available by ServletContext#getResourceAsStream() and its JSF delegator ExternalContext#getResourceAsStream(). So, this should do:
ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
prop.load(ec.getResourceAsStream("/WEB-INF/resources/prop/config2.properties"));
See also:
Where to place and how to read configuration resource files in servlet based application?

NHibernate configuration issue: "entry point was not found"

I'm configuring NHibernate on SharePoint 2010 web application.
Previously it works fine when mappings and domain were in one project. But during refactoring process I splitted solution on several projects. I've also implemented my custom IHttpModule where I want to initialize nhibernate configuration
protected void context_BeginRequest(object sender, EventArgs e)
{
var httpApplication = sender as HttpApplication;
lock (httpApplication )
{
if (!httpApplication.Context.Items.Contains(ApplicationConstants.IsApplicationInitialized))
{
httpApplication.Context.Items.Add(ApplicationConstants.IsApplicationInitialized, true);
InitInRequest(httpApplication);
}
}
httpApplication.Context.Items.Add(ApplicationConstants.SESSION, NhibernateManager.GetSession());
}
private void InitInRequest(HttpApplication httpApplication)
{
NhibernateManager.Init(ApplicationVariables.ApplicationSettingsPath);
}
And NHibernateManager.Init():
public static void Init(string configurationFilePath)
{
specifiedConfigurationFilePath = configurationFilePath;
Configure();
InitSessionFactory();
}
private static void Configure()
{
if (config == null)
{
if (string.IsNullOrEmpty(specifiedConfigurationFilePath) == false)
{
config = new Configuration();
config = config.Configure(specifiedConfigurationFilePath);
config = Fluently.Configure(config)
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<ItemMap>())
.BuildConfiguration();
}
else
{
config = Fluently.Configure()
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<ItemMap>())
.BuildConfiguration();
}
}
}
And in BuildConfiguration() I have very strange error (InnerException): "Entry point was not found." Stack trace shows that getting mapping information is cause of error:
at System.Collections.Generic.IDictionary`2.TryGetValue(TKey key, TValue& value)
at NHibernate.Cfg.Configuration.GetClassMapping(String entityName)
at NHibernate.Cfg.Configuration.GetClassMapping(Type persistentClass)
at FluentNHibernate.PersistenceModel.Configure(Configuration cfg)
at FluentNHibernate.Cfg.MappingConfiguration.Apply(Configuration cfg)
at FluentNHibernate.Cfg.FluentConfiguration.BuildConfiguration()
All assemblies are in the GAC. I tried to copy them in _app_bin or bin but without success.
UPDATE
Please, help me! I'm stuck with this weird problem :(
I found solution.
Take a look at my configuration lines:
config = new Configuration();
config = config.Configure(specifiedConfigurationFilePath);
config = Fluently.Configure(config)
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<ItemMap>())
.BuildConfiguration();
I'm creating new nhibernate configuration object passing it to fluent-nhibernate static initializer method. This is a point where things happen. Fluent-nhibernate can't take configuration from specified file. Instead it can take nhibernate configuration object with file path specified which it then uses to build configuration.
Previous version of my application was in one assembly and this way of configuration seems to work fine. But when I split up application the issue appeared.
So to solve the problem I should to take nhibernate configuration info from web.config file. I can't merge settings of timer job and web-application in one file as I could in single-assembly project. So I had to have several configuration files and always use classic lines like that:
Fluently
.Configure()
.Mappings(p => p.FluentMappings
.AddFromAssemblyOf<ItemMap>())
.BuildConfiguration()
It's appear to be some sort of fluent-nhibernate bug or something...

Resources