Placing files under WEB-INF directory and reading them - java

We are using JBOSS 5.1.0.GA and spring integration framework. We are placing the configuration files under the conf directory of the JBOSS to read them from the classpath. But now we are told that we should move all the configuration files from the conf directory to the WEB-INF directory of the war file. Everything was working fine When we placed the files under conf directory.
<bean id="xyz" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>classpath:CustomerService/property-files/*.properties</value>
</list>
</property>
</bean>
But when we move the configuration files from conf directory to WEB-INF directory by making the following changes we are getting the Exceptionjava.io.FileNotFoundException.
<bean id="xyz" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>/WEB-INF/CustomerService/property-files/*.properties</value>
</list>
</property>
</bean>
The exception details:
java.io.FileNotFoundException: URL [jndi:/localhost/pqawdTestWebApp/WEB-INF/CustomerService/spring-integration/Jobs/] cannot be resolved to absolute file path because it does not reside in the file system: jndi:/localhost/pqawdTestWebApp/WEB-INF/CustomerService/spring-integration/Jobs/
at org.springframework.util.ResourceUtils.getFile(ResourceUtils.java:205)
at org.springframework.core.io.AbstractFileResolvingResource.getFile(AbstractFileResolvingResource.java:52)
at org.springframework.core.io.UrlResource.getFile(UrlResource.java:169)
at org.springframework.core.io.support.PathMatchingResourcePatternResolver.doFindPathMatchingFileResources(PathMatchingResourcePatternResolver.java:526)
Anybody has idea on what to do?

Place them in the class path (by some build means).
/WEB-INF/classes/CustomerService/property-files/*.properties

WEB-INF directory path will not be available as classpath in the standalone Spring project. So, I have moved the configuration files to src/resources folder to import them without any hassle.

Related

can i know the log directory path in applicationcontext.xml?

I'm writing a java Application for Tomcat 7.
I have a bean configuration for a class that creates a log file and appends information to it.
now the question is how can I know the tomcat log directory path in bean configuration.
for now I have the following bean:
<bean id="foo_logger" class="com.bar.LoggerBean">
<!-- <property name="logPath" value="/path/DWHEventGenerator.log"/> -->
<property name="logPath" value="/var/lib/tomcat7/logs/mylog.log"/>
<property name="logLevel" value="ALL"/> <!-- ALL, FINE/R/ST, INFO, SEVERE, WARNING, OFF -->
</bean>
what i'd like to do is instead of specify /var/log/tomcat7/log, is to specify some variable that will indicate the actual path of the logs directory of tomcat. is that possible ?
thank you.
The simplest approach would be to use catalina.base from the system properties in the logPath property value
<property name="logPath" value="${catalina.base}/logs/mylog.log"/>
This property will be set by Tomcat's launch script (catalina.sh/catalina.bat) so will be available for use when Spring loads the application context file.

Upload Files In Spring misses jar file

I tried to have an upload option for my spring web app, and I add following resolver
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="500000"></property>
</bean>
I also included two jar files in my WEB-INF/lib folder: commons-fileupload-1.3.jar and commons-io-2.4.jar. But when I run it, it still reports the error:
java.lang.NoClassDefFoundError: org/apache/commons/fileupload/FileItemFactory
java.lang.Class.getDeclaredConstructors0(Native Method)
java.lang.Class.privateGetDeclaredConstructors(Class.java:2398)
java.lang.Class.getDeclaredConstructors(Class.java:1838)
WHat am I still missing? I checked that the FileItemFactory is indeed in my Web App Library.
Thanks
It seems I put some of the jar files to my tomcat common shared lib folder, and some to my WEB-INF/lib folder. I moved everything to my webapp lib folder, and now it works fine.

Configure hibernate.cfg.xml in OpenShift RedHat servers

I'm trying to configure a xWiki server on a OpenShift hosting (Tomcat 6 (JBoss EWS 1.0)). I've never configured a Java server before and I have a issue:
I followed THIS tuto on my local Ubuntu and worked. But reproducing the steps in the OpenShift server I can't find the hibernate.cfg.xml. Looking for files in the directory tree with hibernate I've found the folder jbossews-1.0/jbossews-1.0/work/Catalina/localhost/xwiki/WEB-INF/lib/ with the files:
hibernate-c3p0-3.6.9.Final.jar
hibernate-core-3.6.9.Final.jar
hibernate-validator-4.3.0.Final.jar
hibernate-jpa-2.0-api-1.0.1.Final.jar
hibernate-commons-annotations-3.2.0.Final.jar
But nothing similar to hibernate.cfg.xml. How can I fix it?
hibernate.cfg.xml is your configuration file for Hibernate, where you specify the dialect, connection driver, url, username, password, etc. of the database.
Example:
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://host/database</property>
<property name="connection.username">username</property>
<property name="connection.password">password</property>
<property name="cache.provider_class">org.hibernate.cache.HashtableCacheProvider</property>
<property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<property name="current_session_context_class">thread</property>
<property name="hibernate.show_sql">false</property>
</session-factory>
</hibernate-configuration>
Place this file in the Java resources classpath.
For detail information look hibernate.cfg.xml for Oracle or example by mkyong
This is (should be, anyway) an xWiki installation issue. If you are using the WAR file, then when you put the WAR into the tomcat webapps directory, Tomcat normally will expand the WAR into a directory with the same name. In that directory, in the WEB-INF directory, should be the hibernate.cfg.xml file. Typically, here:
/var/lib/tomcat5.5/webapps/xwiki/WEB-INF/hibernate.cfg.xml
If you have configured your Tomcat to run the WAR without expanding it, then the file will be in the WAR at:
WEB-INF/hibernate.cfg.xml
But in this instance you will have to edit the XML file and put it back into the WAR to configure the application.
I had the same problem, and by stracing tomcat I see it looks at the following places, the long one being in the git repository in my openstack gear:
/var/lib/openshift/518f381fe0b8cd1de2000181/git/tomcat.git/hibernate.cfg.xml
/usr/share/java/tomcat7/hibernate.cfg.xml
I cannot see any correlation between these locations and the classpath either given in the command line or in catalina.properties. The git repository is the cwd of tomcat. Putting hibernate.cfg.xml there does work for now. But I believe as soon as openstack guys realize how messy is to put the cwd there, they will move it away.

Spring properties priority

I've got standart maven folder structure:
src/main/java
src/main/resources
src/test/java
src/test/resources
My appicationContext contains following:
<!-- load properties files -->
<context:property-placeholder location="classpath*:*.properties"/>
I have defined 2 hibernate.properties files - one for src/main/resources and one for src/ test/resources. I have expected that when I will run tests my test hibernate.properties will override production hibernate.properties. Instead of that both files are loaded and production version is used:
Loading properties file from file [D:\projects\video_crawler_v3\out\test\core\hibernate.properties]
Loading properties file from file [D:\projects\video_crawler_v3\out\production\core\hibernate.properties]
How can I correctly setup my properties files? I'm using Intellij IDEA to compile and run tests
One of the options is Spring Profiles http://blog.springsource.com/2011/02/11/spring-framework-3-1-m1-released/
Put two "properties" versions in your context.xml, eg:
<beans>
... your beans
<beans profile="prod">
<context:property-placeholder location="classpath:/hibernate.properties" />
</beans>
<beans profile="test">
<context:property-placeholder location="classpath:/test-hibernate.properties" />
</beans>
</beans>
Activate required profile with -Dspring.profiles.active=test.
Note: use www.springframework.org/schema/beans/spring-beans-3.1.xsd
The files in src/main/resources are always added to the classpath, even when running the unit tests. See this: Common strategies when defining Spring beans for different environments

Could not open ServletContext resource

This is quite similar question to one older but the solution did not work for me.
I have a WAR package.
In web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:application-context.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
In application-context.xml
<bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:social.properties</value>
</property>
</bean>
But getting this:
org.springframework.beans.factory.BeanInitializationException: Could not load properties; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/social.properties]
I checked the WAR package - .xml and .properties files are both in /WEB-INF/classes
.properties file is in src/main/resources and .xml in src/main/java (in default package both) and maven transports them (I think) correctly in the default package of WEB-INF/classes
Does anyone know why i could get this exception? Thank you.
EDIT: I just want to add that JUnit tests goes correctly (i mean they load what they should from social.properties) but when running the app it ignores my classpath: prefix
Do not use classpath. This may cause problems with different ClassLoaders (container vs. application). WEB-INF is always the better choice.
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-config.xml</param-value>
</context-param>
and
<bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>/WEB-INF/social.properties</value>
</property>
</bean>
Put the things like /src/main/resources/foo/bar.properties and then reference them as classpath:/foo/bar.properties.
Try to use classpath*: prefix instead.
http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/resources.html#resources-classpath-wildcards
Also please try to deploy exploded war, to ensure that all files are there.
I think currently the application-context.xml file is into src/main/resources AND the social.properties file is into src/main/java... so when you package (mvn package) or when you run tomcat (mvn tomcat:run) your social.properties disappeared (I know you said when you checked into the .war the files are here... but your exception says the opposite).
The solution is simply to put all your configuration files (application-context.xml and social.properties) into src/main/resources to follow the maven standard structure.
Are you having Tomcat unpack the WAR file? It seems that the files cannot be found on the classpath when a WAR file is loaded and it is not being unpacked.
try with this code...
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreUnresolvablePlaceholders" value="true"/>
<property name="locations">
<list>
<value>/social.properties</value>
</list>
</property>
</bean>
Mark sure propertie file is in "/WEB-INF/classes" try to use
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>/WEB-INF/classes/social.properties</value>
</property>
</bean>
I had the same error.
My filename was jpaContext.xml and it was placed in src/main/resources. I specified param value="classpath:/jpaContext.xml".
Finally I renamed the file to applicationContext.xml and moved it to the WEB-INF directory and changed param value to /WEB-INF/applicationContext.xml, then it worked!

Categories

Resources