Skip to content

Instantly share code, notes, and snippets.

@Genzer
Created October 27, 2016 09:16
Show Gist options
  • Save Genzer/4608c5996bd86c206737c7e5203a6d3e to your computer and use it in GitHub Desktop.
Save Genzer/4608c5996bd86c206737c7e5203a6d3e to your computer and use it in GitHub Desktop.
Get `Path` to resource located in Axon.ivy project `webContent`
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Optional;
import org.eclipse.core.resources.IFolder;
import ch.ivyteam.ivy.environment.Ivy;
import ch.ivyteam.ivy.security.internal.SecurityManager;
public class WebContentResourceLoader {
public Path getResource(String relativeToWebContent) {
return findResource(relativeToWebContent)
.orElseThrow(() -> new RuntimeException("Resource at webContent/" + relativeToWebContent + " cannot be found"));
}
public Optional<Path> findResource(String relativeToWebContent) {
return getWebContentFolder()
.map(f -> f.getFile(relativeToWebContent))
.map(f -> f.getLocationURI())
.map(u -> Paths.get(u))
.filter(p -> Files.exists(p));
}
private static Optional<IFolder> getWebContentFolder() {
try {
return Optional.ofNullable(SecurityManager
.getSecurityManager()
.executeAsSystem(() -> Ivy.request()
.getProcessModelVersion()
.getProject().getFolder("webContent")));
} catch (Exception failToGetWebContent) {
throw new RuntimeException("Fail to reach webContent folder", failToGetWebContent);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment