ronan_40060
asked on
Using Java 11 Path to load files
Loading files using Path.resolve
Currently below code loads the scripts to resources now instead I want to use Java 11 Path.resolve API , I did look up on internet but couldn't figure out how it can be done.
Please suggest a suitable way to achieve it
PathMatchingResourcePatternResolver resolver =
new PathMatchingResourcePatternResolver();
Resource [] resources =
resolver.getResources(scriptsLoc
+ "/*sql");
Arrays.stream(resources)
.map(e -> runScript(e));
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Shouldn't that be the following?I thought that too possibly, but not wanting to assume anything, I just did it based off the author's original code.
If you're looking for .sql files I think it should be the simple, obvious glob. NB those are globs and not regexes
ASKER
Thank you for your comments I do have a question on runScript method , it is as below
Could you please tell me how to replace Resource with Path object in above ? Im thinking like below
Not sure casting path wih Resource is a good way
public boolean runScript(Resource script) {
try {
ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator(script) ;
databasePopulator.execute(getDatasource());
} catch(ScriptException ex) {
return false;
}
return true
}
Could you please tell me how to replace Resource with Path object in above ? Im thinking like below
public boolean runScript(Path path) {
try {
ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator((Resource) path) ;
databasePopulator.execute(getDatasource());
} catch(ScriptException ex) {
return false;
}
return true
Not sure casting path wih Resource is a good way
I don't know this API but it looks like you don't need a runScript method as the ResourceDatabasePopulator does the running of the scripts as a batch. You just need to call ResourceDatabasePopulator. addScript in your stream method and finally ResourceDatabasePopulator. execute(Da taSource)
ASKER
Thank you Chej , could you please show me an example on calling ResourceDatabasePopulator. addScript in your stream method ? I didnt understand this
try (final DirectoryStream<Path> stream = Files.newDirectoryStream(Paths.get(scriptsLoc), "*.sql")) {
stream.forEach(e -> rdp.addScript(new PathResource(e)));
}
// (where 'rdp' is a ResourceDatabasePopulator)
Or something similar. Obviously you would create your ResourceDatabasePopulator first.
But as I mentioned, I'm unfamiliar with this API, so perhaps not the best person to ask
ASKER
Thank you guys for your time and comments , my question is answered now .
Open in new window