PDA

View Full Version : Path problem with FileSystemXmlApplicationContext


nmd
Apr 7th, 2007, 08:44 AM
My problem appears when I move my application from development (WinXP) to staging (Linux, Fedora).

In my web.xml, I define the following:

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/classes/resources/spring/operator_profiles.xml
/WEB-INF/classes/resources/spring/services.xml
</param-value>
</context-param>

When I instantiate the FileSystemXmlApplicationContext with a String array containing the above values, I get the following error:

org.springframework.beans.factory.BeanDefinitionSt oreException: IOException parsing XML document from file [/opt/java/apache-tomcat-5.5.12/bin/opt/java/tomcat/webapps/ff71/WEB-INF/classes/resources/spring/operator_profiles.xml]; nested exception is java.io.FileNotFoundException: opt/java/tomcat/webapps/ff71/WEB-INF/classes/resources/spring/operator_profiles.xml (No such file or directory)
org.springframework.beans.factory.xml.XmlBeanDefin itionReader.loadBeanDefinitions(XmlBeanDefinitionR eader.java:180)
org.springframework.beans.factory.xml.XmlBeanDefin itionReader.loadBeanDefinitions(XmlBeanDefinitionR eader.java:148)
org.springframework.beans.factory.support.Abstract BeanDefinitionReader.loadBeanDefinitions(AbstractB eanDefinitionReader.java:129)
org.springframework.beans.factory.support.Abstract BeanDefinitionReader.loadBeanDefinitions(AbstractB eanDefinitionReader.java:145)
org.springframework.context.support.AbstractXmlApp licationContext.loadBeanDefinitions(AbstractXmlApp licationContext.java:113)
org.springframework.context.support.AbstractXmlApp licationContext.loadBeanDefinitions(AbstractXmlApp licationContext.java:81)
org.springframework.context.support.AbstractRefres hableApplicationContext.refreshBeanFactory(Abstrac tRefreshableApplicationContext.java:89)
org.springframework.context.support.AbstractApplic ationContext.refresh(AbstractApplicationContext.ja va:262)
org.springframework.context.support.FileSystemXmlA pplicationContext.<init>(FileSystemXmlApplicationContext.java:89)
org.springframework.context.support.FileSystemXmlA pplicationContext.<init>(FileSystemXmlApplicationContext.java:74)
se.lbsoft.ff.util.SpringUtil.getBeanFactory(Spring Util.java:50)

opt/java/tomcat/webapps/ff71/WEB-INF/classes/resources/spring/operator_profiles.xml is the correct absolute path of the file, but Spring seems to be looking for /opt/java/apache-tomcat-5.5.12/bin/opt/java/tomcat/webapps/ff71/WEB-INF/classes/resources/spring/operator_profiles.xml. The absolute path to the Spring configuration files seems to get appended to the directory where I am when running TOMCAT_HOME/bin/startup.sh. It works fine if i run startup.sh from root (/).

Am I doing something wrong? I'd rather not be forced to run Tomcat from a specific location.

Regards,
Lauri Lehtinen
LB Software

Dave Syer
Apr 7th, 2007, 09:28 AM
Have you tried starting your path with a "/"?

Having your application aware of the location of config files in the file system seems a bit fragile to me. Wouldn't it be better to use ClasspathXmlApplicationContext anyway?

cwilkes
Apr 7th, 2007, 10:35 AM
Try removing the slash in front of the web-inf:


<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
WEB-INF/classes/resources/spring/operator_profiles.xml
WEB-INF/classes/resources/spring/services.xml
</param-value>
</context-param>

nmd
Apr 7th, 2007, 12:48 PM
Thanks for quick replies.

Try removing the slash in front of the web-inf:

Removing the initial slash made no difference.

Have you tried starting your path with a "/"?

You mean entering the absolute path of the xml files beginning with / meaning file system root in web.xml? I noticed that I forgot to mention one thing in the initial post: before I instantiate FileSystemXmlApplicationContext, I use servletContext.getRealPath() to change the paths I define in web.xml to absolute paths. I've done it this way since the absolute path in development is different from that in staging. I have also checked that what's sent to the constructor are the accurate absolute paths in both environments.

And what comes to using ClasspathXmlApplicationContext, it sure might be a better way. I just thought I'd post the question before I change my approach.

javagrannie
Apr 7th, 2007, 09:00 PM
Are WEB-INF/classes/resources/spring/operator_profiles.xml
WEB-INF/classes/resources/spring/services.xml

really in WEB-INF/classes or is that where you are anticipating they will be after it is compiled?

nmd
Apr 8th, 2007, 05:06 AM
Are WEB-INF/classes/resources/spring/operator_profiles.xml
WEB-INF/classes/resources/spring/services.xml

really in WEB-INF/classes or is that where you are anticipating they will be after it is compiled?

I have confirmed that the files really are there. However, they are not in the directory that Spring seems to end up looking for them (such directory doesn't even exist):

IOException parsing XML document from file [/opt/java/apache-tomcat-5.5.12/bin/opt/java/tomcat/webapps/ff71/WEB-INF/classes/resources/spring/operator_profiles.xml];

/opt/java/apache-tomcat-5.5.12/bin <- directory where I run startup.sh
/opt/java/tomcat/webapps/ff71/WEB-INF/classes/resources/spring/operator_profiles.xml <- correct path to existing file

And as I mentioned earlier, if i do

cd /
/opt/java/apache-tomcat-5.5.12/bin/startup.sh

instead of running startup.sh from the directory it's in, this problem does not occur.

Dave Syer
Apr 8th, 2007, 05:25 AM
When you modify the file locations by prepending the servlet context path do you check that it is actually an absolute path, beginning with "/" - it looks from your stack trace like the path is relative (for some strange reason). You aren't stripping the initial "/" from it are you?

ClasspathXmlApplicationContext seems like the way to go anyway.

cwilkes
Apr 8th, 2007, 09:38 AM
Why are you using FileSystemXmlApplicationContext in a web application?

karldmoore
Apr 8th, 2007, 01:57 PM
Why are you using FileSystemXmlApplicationContext in a web application?

Yeah, I was wondering that as well. If this is a web application, then presumably you are using ContextLoaderListener to initialise the Spring container. Why do you then create another in se.lbsoft.ff.util.SpringUtil? Don't you actually want to get hold of the existing ApplicationContext instead?

timkaltenbrunner
May 28th, 2009, 11:38 AM
Your problem seems to be a strange logic in the FileSystemXmlApplicationContext.getResourceByPath. Have a look at the following source.

protected Resource getResourceByPath(String path) {
if (path != null && path.startsWith("/")) {
path = path.substring(1);
}
return new FileSystemResource(path);
}

When using an absolute path it becomes relative (the "/" is removed). It would be nice if someone could tell the reason for this strange logic.

Cheers Tim

NuSin
Jun 24th, 2009, 04:09 AM
Did you try a "file:" before the physical path?

timkaltenbrunner
Jun 24th, 2009, 06:54 AM
No, but that should work. The problem is that our application is developed and executed under windows. When we some day have a customer using Linux (or in our case Solaris) we need to change all our paths to get it running again. The point is that it is not good to have methodes within the spring source with different behaviour for windows and linux.

samspot
Aug 21st, 2009, 11:55 AM
I wanted to add to this thread as I found it useful on a project. Adding the 'file:' notation before my path caused the absolute path to be interpreted correctly. Additionally I found a comment explaining why the leading '/' is removed when paths are given to getResourceByPath.

From: http://www.docjar.com/docs/api/org/springframework/context/support/FileSystemXmlApplicationContext.html

protected Resource getResourceByPath(String path)

{
if (path != null && path.startsWith("/")) {
path = path.substring(1);
}
return new FileSystemResource(path);
}

Resolve resource paths as file system paths.

Note: Even if a given path starts with a slash, it will get interpreted as relative to the current VM working directory. This is consistent with the semantics in a Servlet container.


Apologies in advance if anyone is offended by me bringing back a zombie thread.