Link to home
Start Free TrialLog in
Avatar of ronan_40060
ronan_40060Flag for United States of America

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 

Open in new window

Open in new window

PathMatchingResourcePatternResolver resolver =
          new PathMatchingResourcePatternResolver();
      Resource [] resources =
          resolver.getResources(scriptsLoc
          + "/*sql");
      Arrays.stream(resources)
          .map(e -> runScript(e));

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of mccarl
mccarl
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Shouldn't that be the following?

try (final DirectoryStream<Path> stream = Files.newDirectoryStream(Paths.get(scriptsLoc), "*.sql")) {
   stream.forEach(e -> runScript(e));
}

Open in new window

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
Avatar of ronan_40060

ASKER

Thank you for your comments I do have a question on runScript method , it is as below

public boolean runScript(Resource script) {

try {

ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator(script) ;
databasePopulator.execute(getDatasource());
} catch(ScriptException ex) {
return false;
}
return true
}

Open in new window


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

Open in new window


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(DataSource)
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)

Open in new window


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
Thank you guys for your time and comments , my question is answered now .