0

All the examples I've found use XML configuration file. I tried the following:

    IdSClientConfigurationImpl config = new IdSClientConfigurationImpl("idsclient.properties");
    config.load();

And it gave an exception

java.io.FileNotFoundException: idsclient.properties (The system cannot find the file specified)
        at java.io.FileInputStream.open0(Native Method) ~[na:1.8.0_152]
        at java.io.FileInputStream.open(FileInputStream.java:195) ~[na:1.8.0_152]
        at java.io.FileInputStream.<init>(FileInputStream.java:138) ~[na:1.8.0_152]
        at com.cisco.ccbu.common.ids.client.impl.IdSClientConfigurationImpl.load(IdSClientConfigurationImpl.java:82) ~[idsclientlib-11.6.1.jar:na]

I placed the file in the same location as application.properties.

$ find src -name idsclient\*
src/main/resources/idsclient.properties

It's being copied to the classes folder during mvn spring-boot:run

$ find target -name idsclient\*
target/classes/idsclient.properties

So what is the real path to the file in the context of a Spring Boot app?

I also tried

IdSClientConfigurationImpl config = new IdSClientConfigurationImpl("classpath:/idsclient.properties");

But it gave

java.io.FileNotFoundException: classpath:\idsclient.properties (The filename, directory name, or volume label syntax is incorrect)

Chloe
  • 25,162
  • 40
  • 190
  • 357
  • By the way, `IdSClientConfigurationImpl` will only accept a `String`. – Chloe Aug 23 '18 at 17:45
  • You'll want to read [this](https://stackoverflow.com/questions/27845223/whats-the-difference-between-a-resource-uri-url-path-and-file-in-java). There are very important differences between physical files on your disk and classpath resources (which Spring tries to retrieve with `classpath:...`). – Sotirios Delimanolis Aug 23 '18 at 17:45
  • Giving the full relative path name works, but doesn't seem packageable. `new IdSClientConfigurationImpl("src/main/resources/idsclient.properties");` – Chloe Aug 23 '18 at 18:02
  • `src/main/resources` is a location within your development environment, don't rely on it. Place your file in a specific location which you can read. Or find a different way to load it from the classpath. – Sotirios Delimanolis Aug 23 '18 at 18:06

2 Answers2

1

You can use Spring Resources (ClassPathResource)

   //either autowire
   @Value("classpath:idsclient.properties")
   private Resource idsclientResource; 


   //or construct manually
   ClassPathResource idsclientResource = new ClassPathResource("idsclient.properties");

   ...

   IdSClientConfigurationImpl config = new IdSClientConfigurationImpl(idsclientResource.getFile().getPath()); 
Chloe
  • 25,162
  • 40
  • 190
  • 357
Kostiantyn
  • 1,856
  • 11
  • 13
-1

To use your custom properties files, you can simply annotate a configuration class with @PropertySource. Here is an example for the AWS S3 client factory class of some of my own project:

@PropertySource("classpath:aws.properties")
@Service
public class AmazonS3ClientFactory implements FactoryBean<AmazonS3> {


    @Value("${aws.access-key}")
    private String accessKey;

    @Value("${aws.pass}")
    private String pass;

    @NotNull
    @Override
    public AmazonS3 getObject() throws Exception {
        return  AmazonS3ClientBuilder.standard()
                .withRegion(Regions.EU_WEST_3)
                .withCredentials(
                        new AWSStaticCredentialsProvider(
                                new BasicAWSCredentials(accessKey, pass)
                        )
                )
                .build();
    }

    @NotNull
    @Override
    public Class<?> getObjectType() {
        return AmazonS3.class;
    }

}
Louis-wht
  • 535
  • 5
  • 17
  • Very clever, but I don't think that will work with Cisco SDK which expects a String for a file path, calling `.load()`, then passing the configuration object to another client object: `client.init(config);`. – Chloe Aug 23 '18 at 17:55
  • @Chloe What does Cisco SDK does exactly? Because the above should work whatever the case is with Spring... The thing that makes me quite unsure here is `IdSClientConfigurationImpl` which I never used before. Is it from Spring or from your Cisco SDK? I'm not 100% sure to understand what you're trying to do with your custom configuration file... Also, you should just try with `classpath:idsclient.properties` instead of `classpath:/idsclient.properties`, that might do the trick. – Louis-wht Aug 23 '18 at 18:02
  • `IdsClientConfigurationImpl` is from Cisco. `System.out.println(new File("classpath:idsclient.properties").exists());` prints `false`. – Chloe Aug 23 '18 at 18:04
  • Sounds clearer now, You should maybe precise that you are trying to load config to a Cisco SDK and that it has nothing to do directly with Spring classpath? From my own experience, the real java classpath can be very different depending on whether you're using spring boot or run it using tomcat, and it might then also depends on your tomcat installation, so I might recommend you not to use the classpath here as this might have some strange behavior depending on your environment. – Louis-wht Aug 23 '18 at 18:08