Created
October 27, 2016 09:16
-
-
Save Genzer/4608c5996bd86c206737c7e5203a6d3e to your computer and use it in GitHub Desktop.
Get `Path` to resource located in Axon.ivy project `webContent`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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