Results 1 to 9 of 9

Thread: loading a file via classpath: via Spring config (without using PropertiesFactoryBean)

  1. #1
    Join Date
    Nov 2006
    Location
    Boston, US
    Posts
    167

    Default loading a file via classpath: via Spring config (without using PropertiesFactoryBean)

    Requirement - I need to load a text file (no, not a property file) which has security settings. This file is generated by ant and included in a jar file. The jar file is on the classpath. My application needs to find this file with it's absolute name (as the API creates a new File(file)) and I would like Spring to inject this.

    How would I do this? I tried configuring via PropertiesFactoryBean but it looks like it does a lot of stuff that I dont care about and my file is NOT a property file. I tried writing my own little pojo with setters and getters but I dont want to set with an absolute file name in Spring or even worse write another .property file and ask Spring to look into it via a Config like this
    Code:
     <bean id="mainPropertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="locations">
            <list>
            	        	<value>classpath:security.properties</value>    	
            </list>
    </property>
    </bean>

    In short, I want to use classpath: findMyTextFileInTheDamnJar in spring, what class do I use?

  2. #2
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,425

    Default

    Are you wanting Spring to inject in the file contents or simply a File object?

  3. #3
    Join Date
    Nov 2006
    Location
    Boston, US
    Posts
    167

    Default

    1) I have a security.txt in myapp.jar
    2) myapp.jar is in classpath
    3) MyService uses an thirdparty API which has a method like apiMethod.checkSecurtity(String filename)
    {
    File f = new file(filename);
    ....
    }

    4) I want Spring to inject an 'absolute filename'.

    The trick is at runtime this will be in a jar file

    If I cant do this, maybe I should configure my ant to keep it
    under a folder structure in tomcat. Im not sure whats the right way to keep such files which should not be shared by outside the web app
    Last edited by infinity2heaven; Jan 18th, 2007 at 10:52 AM.

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    If you configure a ClasspathResource in Spring you could get the File from there the ClasspathResource class also has a filename property although I'm not sure what this returns.

    If you configured the ClassPathResource you can use the util library to export the properties to be used by Spring.

    Code:
    <bean id="securityFile" class="org.springframework.core.io.ClassPathResource">
     <constructor-arg value="classpath:security.properties/>
    </bean>
    
    <util:property-path id="file" path="securityFile.file"/>
    <util:property-path id="filename" path="securityFile.filename"/>
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,425

    Default

    How does your third party API expect to read files? As resources from the classpath or from a file system?

  6. #6
    Join Date
    Nov 2006
    Location
    Boston, US
    Posts
    167

    Default

    Im using handle api for security

    It has a really bad implementation for the factory, this is their source
    Code:
     public static IDSvcInterface getInterface(String s)
            throws BadConfigurationFileException, InvalidSubclassException
        {
            try
            {
                File file = new File(s);
                StreamTable streamtable = new StreamTable();
                streamtable.readFromFile(new File(file, "config.dct"));
                String s1 = streamtable.getStr("IDSvcInterface_class");
                Class class1 = Class.forName(s1);
                Constructor constructor = class1.getConstructor(new Class[] {
                    Class.forName("java.lang.String")
                });
                return (IDSvcInterface)constructor.newInstance(new Object[] {
                    s
                });
            }
    I didnt like them expecting a folder name and hardcoding the config.dct file inside their method getInterface(), so I literally copied this factory in my app and changed their implementation by one line

    File f = new File(filename)

    where I want to pass my own fully qualified filename. I can pass on Resource but I think Im just ending up doing a zillion things rather than what I want to do.

    "I just want to give an absolute filename at runtime and I want Spring to give it"

    Cant this be simple

  7. #7
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    If you have your own implementation why not change it why not change the method to take a file as an argument. That way you can use it the way I suggested earlier in this thread.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  8. #8
    Join Date
    Nov 2006
    Location
    Boston, US
    Posts
    167

    Default

    mdeinum -

    I think classpathResource cannot check resources in jars. Im trying this anyways but I couldnt understand util.properties....

    It gives me an error
    Element type - "utilroperty-path" must be declared.

  9. #9
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    You have to include the spring 2.0 schema for util, check the reference guide and the samples on how to do that.

    ClasspathResource checks the whole classpath and thus also inside jar files. If classpath: doesn't work you can also try classpath*: .
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •