hi all

i have a project A with a startlink, this project depend on project B (B contain some commons ivy component). Now user start page by that startlink, it will open a page which use come commons component from B)

Now i need to get root folder of project A, i refer this post but it just work fine if it run in project A, when i call it on commons component (project B) then it return the folder of B.

My question is: is there any api to get root folder (maybe webcontent) of project where user start the startlink (like project A in above example)?

Thanks

asked 27.03.2017 at 11:33

trungdv's gravatar image

trungdv
(suspended)
accept rate: 52%


Ivy puts all requests on a stack. There is an API to access this stack. But you can't directly access the initial process model version over Ivy.request(). I extended the example with the method findRootProcessModelVersionRequest(). This method finds the initial (root) ProcessModelVersionRequest.

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.request.IProcessModelVersionRequest;
import ch.ivyteam.ivy.request.IRequest;
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(
                            () -> findRootProcessModelVersionRequest().getProcessModelVersion().getProject().getFolder("webContent")
            ));
        } catch (Exception failToGetWebContent) {
            throw new RuntimeException("Fail to reach webContent folder",
                    failToGetWebContent);
        }
    }

    private static IProcessModelVersionRequest findRootProcessModelVersionRequest() 
    {
        IProcessModelVersionRequest rootProcessModelVersionRequest = Ivy.request();
        IRequest request = rootProcessModelVersionRequest;
        do {
            request = request.getParentRequest();
            if (request instanceof IProcessModelVersionRequest) {
                rootProcessModelVersionRequest = (IProcessModelVersionRequest) request;
            }
        } while (request != null);
        return rootProcessModelVersionRequest;
    }
}

This will work if B is a Callable Sub Process. If you call B with a Trigger, this won't work. There will be a new task, which will ends up in a complete new request. In this case you have to pass the parameters through process data.

Note: You have to think about your design. Projects A depends on project B (A -> B). Now you create a implicit dependency backwards. A knows B and B knows A implicit. In my opinion that only makes sense, if B is something like a framework. Which seems to be the case - you call it "common components".

link

answered 27.03.2017 at 20:24

Alex%20Suter's gravatar image

Alex Suter ♦♦
3.1k122247
accept rate: 84%

hi @Alex Suter ♦♦

Thanks for your fast response, yes you're right, it's B is a framework. Can you tell me what will happen if i use signal or overriding process? can i find root request on that stack?

(28.03.2017 at 04:13) trungdv trungdv's gravatar image

This concept only works for Callable Subprocess.

(28.03.2017 at 08:50) Alex Suter ♦♦ Alex%20Suter's gravatar image

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:

×35

Asked: 27.03.2017 at 11:33

Seen: 2,149 times

Last updated: 28.03.2017 at 08:50