Hi all

Sometime i want to lookup some resources in webcontent folder of current ivy model. then i com up with this code snippet.

public static String getFolderInWebContent(String folderName) throws Exception {
    String rootFolder = getProjectRootFolder() + "/webContent/";
    if (folderName != null && !folderName.isEmpty()) {
        if (!folderName.endsWith("/") && !folderName.endsWith("\\")) {
            folderName = folderName + "/";
        }
        File f = new File(rootFolder + folderName);
        if (f.isDirectory()) {
            return f.getAbsolutePath();
        }
    }
    return rootFolder;
}

public static String getProjectRootFolder() throws Exception {
    return SecurityManagerFactory.getSecurityManager().executeAsSystem(
            new Callable<String>() {
                @Override
                public String call() throws Exception {
                    return Ivy.request().getProcessModel()
                            .getReleasedProcessModelVersion()
                            .getProjectDirectory();
                }

            });
}

I hope it's helpful for someone who need it or if you have a better solution, just drop here. i also want to extend my knowledge about ivy.

Thanks

asked 27.10.2016 at 10:38

trungdv's gravatar image

trungdv
450648798
accept rate: 55%


i just got this solution from @hau.tran(TA of axon active vietnam). i think it's better :)

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);
        }
    }
}
link

answered 27.10.2016 at 12:30

trungdv's gravatar image

trungdv
450648798
accept rate: 55%

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "Title")
  • image?![alt text](/path/img.jpg "Title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×30

Asked: 27.10.2016 at 10:38

Seen: 660 times

Last updated: 27.10.2016 at 12:30