Announcement Announcement Module
Collapse
No announcement yet.
loading a file via classpath: via Spring config (without using PropertiesFactoryBean) Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    Are you wanting Spring to inject in the file contents or simply a File object?

    Comment


    • #3
      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, 11:52 AM.

      Comment


      • #4
        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"/>

        Comment


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

          Comment


          • #6
            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

            Comment


            • #7
              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.

              Comment


              • #8
                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.

                Comment


                • #9
                  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*: .

                  Comment

                  Working...
                  X