External properties file with JSF2 - jsf-2

I'd like to NOT put my properties file in the war file's classes directory. Can I do this and what would I specify in faces-config.xml for it to use the correct resource-bundle ?
Thanks
Binh Nguyen

Put it in an external folder and add its path to the runtime classpath. Then you can access it from the classpath the usual way as if it's in /WEB-INF/classes (which is just by default part of the classpath).
Adding the path to an external folder to the classpath is best to be configured at the webserver level. In Tomcat for example, you can specify it in the shared.loader or common.loader property of Tomcat's /conf/catalina.properties file.
shared.loader = /path/to/propertiesfiles

Related

How to set log4j.property to .jar location

I'm Setting up Log4j2 in a Spring-boot application. I now want to create a /log directory exactly where the .jar file is located.
This is needed as we start the java application from a startup script and the configuration should work on both windows and unix developer machines as well as a server.
I already tried with:
<RollingFile name="FileAppender" fileName="./logs/mylog.log"
filePattern="logs/mylog-%d{yyyy-MM-dd}-%i.log">
which just creates a log folder at the directory where the jar gets started.
then I read i should use .\log/mylog.log as .\ points to the directory of the jar file.
But then it just creates a folder called .\log.
I also tried with configuration with jvm arguments and calling them at the log4j2.xml with: ${logFile}. Now a directory gets created called '${logFile}.
The only ${} command working is the directory of the log4j configuration file. But as this is inside the jar it just gets me a pretty useless folder structure
Thanks in Advance
EDIT: In the End what I did was setting up two configuration files, log4j2.xml and log4j2-prod.xml
The log4j2.xml took the system property as Vikas Sachdeva mentioned, while the prod.xml got the location hard coded.
Not really the solution I was looking for but made it work.
One solution is to pass log directory location through system properties.
Configuration file will look like -
<RollingFile name="FileAppender" fileName="${sys:basePath}/mylog.log"
filePattern="${sys:basePath}/mylog-%d{yyyy-MM-dd}-%i.log">
Now, pass VM argument basePath with absolute path of directory containing JAR file -
java -jar myapp.jar -DbasePath=/home/ubuntu/app

Websphere Liberty: How to specify log4j2 configuration location?

I'm trying to tell Websphere Liberty where is located my log4j2.xmlfile, but it isn't working.
In my file jvm.options I configure:
-Dlog4j.configurationFile=file:///${server.config.dir}/log4j2.xml
but it looks like Liberty does not understand the variable ${server.config.dir} in the jvm.options file. The file is in the same directory of the server.xml file.
How would I specify the log4j2.xml location for Liberty?
I think it should be possible by creating a Library entry in your server.xml such as:
<library id="log4jLib">
<folder dir="/opt/log4j2/config"/>
</library>
where the directory specified contains the log4j2 properties or xml file.
Then specify a classloader for your application like this:
<application id="test" name="test" type="ear" location="test.ear">
<classloader commonLibraryRef="log4jLib" />
</application>
The ${server.config.dir} variable is one of the Liberty built-in server config variables, these only apply within the server.xml (and included configurations).
When you run a Liberty server, the user.dir gets set to the same thing as ${server.config.dir}, so you could just specify the relative path to your log4j2.xml file in jvm.options as:
-Dlog4j.configurationFile=log4j2.xml
For Liberty I do the following;
I have a jvm.options file containing:
-Dlog4j.configurationFile=log4j2.xml
I place both files (log4j2.xml and jvm.options) in the server config.
I.e where tour server.xml etc is placed:
usr/servers/<myserver>:
server.xml
jvm.options
log4j2.xml
That does the trick for me.
There are a few ways of configuring log4j in WebSphere. Aside from the library entries method mentioned by pseudonym, you can simply drop the log4j2 configuration file to the global library directory at
wlp/usr/servers//lib/global
If there are files present in above location at the time an application is started, and that application does not have a classloader element configured, the application uses these libraries. If a class loader configuration is present, these libraries are not used unless the global library is explicitly referenced.
You can find more details about the global libraries in this link
WebSphere Liberty Shared Libraries

Using JNDI properties file in OpenEJB embedded mode

Eventhough the jndi properties file is kept in the classpath its not loaded and JNDI properties when configured in JVM arguments in ANT its working fine.
<jvmarg value="-Djava.naming.factory.initial=org.openejb.client.LocalInitialContextFactory"/>
How to configure the JNDI thorugh properties so that even when
Context=new InititalContext() is used in the EJB's the JNDI properties that are configured in properties needs to be loaded.
Thanks,
Velmurugan R
Definitely make sure the jndi.properties file is at the root of the classpath and not in a META-INF/ directory or any other location. By root, I mean some directory that is parallel to a where a META-INF/ would be. For example:
foo.jar/jndi.properties
foo.jar/META-INF/MANIFEST.MF
That file is processed by the JVM itself, so if it doesn't work, odds are it is related to it not being on the classpath correctly.

JSF with External Resource Bundle

I'm doing a JSF 2.0 application on Tomcat 6.x. I have a resource bundle in different languages, is it possible to externalize the properties files outside the webapp?
For the moment I have this in my faces-config.xml:
<locale-config>
<default-locale>fr</default-locale>
<supported-locale>en</supported-locale>
<supported-locale>es</supported-locale>
</locale-config>
<resource-bundle>
<base-name>front</base-name>
<var>messages</var>
</resource-bundle>
What should I do?
Thanks.
Yes, that's definitely possible. To the point, just put the files in the classpath. You can do that by placing it in any of existing paths covered by the webapp's runtime classpath, or by adding the new path to the webapp's runtime classpath.
You could add a new path to the classpath by specifying it in shared.loader of Tomcat's /conf/catalina.properties. Assuming that you've placed front*.properties files in /var/webapp/conf folder, then you need to specify the shared.loader as follows:
shared.loader = /var/webapp/conf
May be because of the spaces in the folder name; try to put the resource bundle file in another place, eg.: c:/temp/resources/

hibernate.cfg.xml path

I have a java project and I am using hibernate.The thing here is I want to place the hibernate.cfg.xml file outside "src" folder, but when I am configuring this file it is showing FileNotFoundException. If I put it inside src folder its ok. But I want to separate the java src and all config file in separate folder.
root
|____src
|____conf
|____mapping
|____(all xml file with hibernate.cfg.xml)
SessionFactory _sessionFactory = (new Configuration()).configure("./conf/mapping/hibernate.cfg.xml").buildSessionFactory();
Its showing exception......
Add conf/mapping directory to your CLASSPATH and load the configuration file
new Configuration().configure("/hibernate.cfg.xml").buildSessionFactory();
or just
new Configuration().buildSessionFactory();
as this is the standard name.
Regardless there would be no ./ at the beginning of the resource path.
What you want to do is
include conf/mapping in your project build path(With Eclipse properties->javabuild path->source->add Folder)
then no need to precise the hibernate config file in your code
new Configuration().buildSessionFactory(); is enough
This manipulation is simple and works like a charm.

Resources