tags:

views:

27

answers:

2

Hi,

I need to pass a folder (java.io.File) as a function parameter.

I tried to just declare the location of the folder, but it looks in SERVER_HOME (/home/user/tomcat).

So my next try is to inject a File (directory) which is located in WEB-INF/myFolder.

my first try failed:
<bean name="path" class="java.io.File">
<constructor-arg value="classpath:WEB-INF/myFolder" />
</bean>

But it looks for /home/user/tomcat/classpath:WEB-INF/myFolder

I've been messing around but I can't figure out how to do it.

Any help or advice would be great.

Thank you all!

A: 

You could use spring's FileSystemResource class

<bean id="bean" class="org.springframework.core.io.FileSystemResource">
        <constructor-arg><value>your.file</value></constructor-arg>
</bean>
Raghuram
Thanks, it is possible to "your.file" being a folder and how to access it if it is in WEB-INF/folder. myApp/WEB-INF/folder. Thank you !
Udo Fholl
+2  A: 

You are not supposed to access the file system from a webapp, especially not to make assumptions about the webapps own file representation. WEB-INF/myfolder may or may not be a directory, depending if you are dealing with a WAR (no) or an exploded WAR (yes). If you absolutely need a file system resource, try to acquire it using System.getPreference("java.io.tmpdir").


OK, here are a few hints:

Use a Factory bean to retrieve the File. Let it

  1. Create a temporary directory
  2. Extract the contents of the jar to that temporary directory
  3. return the temporary directory in it's getObject() method

Then inject the factory bean into your bean:

<!-- This is a Factorybean that creates a file -->
<bean class="com.yourcompany.ConfigFolderCreator" id="configDir">
    <property name="packageToUnpack" value="com.yourcompany.yourpackage" />
</bean>

<bean class="com.your.legacy.Api">
     <!-- inject the factory bean instead of a file -->
     <property name="configFolder" ref="configDir" />
</bean>
seanizer
@seanizer: thanks for your answer, what I need is to access, via a java.io.File a folder containing some configuration files. The piece of code which need those files is legacy code which I cannot modify. Those files can be in a jar file? if not, and to make it environment independent I though of the classpath. Any Idea? Sorry for this mess of a comment, but my english is rather poor.
Udo Fholl
No, 'files' in a Jar file or not files, they are zip entries. And hence they can not be accessed via File methods. So if you absolutely need to use File methods, you will have to extract the jar somewhere.
seanizer
@seanizer: Ok, so what I was thinking is to ship the application with a folder under WEB-INF and place the config files there. Then access via Spring injection to that directory. It is possible?
Udo Fholl
It's possible if you use exploded wars but it's not a recommended approach
seanizer
Could you please point out the recommended aproach, so I can read some documentation? Thanks for your time!
Udo Fholl
Otherwise can you please tell me how to inject into a bean that directory? thank you
Udo Fholl
see my updated answer
seanizer
Thank you, I answer this last night at home and I was not logged. When I get at home I will log on with the user and check it as solved!
Udo Fholl