Search in sources :

Example 1 with ServiceTreeCrawler

use of org.ovirt.engine.api.rsdl.ServiceTreeCrawler in project ovirt-engine by oVirt.

the class ResourceLocator method locateResource.

/**
 * Get the Resource class corresponding to the provided href
 * Assumption: href is of a location, not an action
 * (e.g: .../vms/{id} - good, .../vms/{id}/start - bad).
 * Another assumption is that href is not empty.
 */
public BaseBackendResource locateResource(String href) throws Exception {
    href = removePrefix(href);
    ServiceTreeCrawler crawler = new ServiceTreeCrawler(Arrays.asList(href.split("/")));
    BaseBackendResource resource = BackendApiResource.getInstance();
    ServiceTreeNode node = null;
    while (crawler.hasNext()) {
        node = crawler.next();
        if (node.isCollection()) {
            // collection context
            // getSubResource()
            Method method = resource.getClass().getMethod(node.getGetter());
            resource = (BaseBackendResource) method.invoke(resource);
        } else {
            // single entity context
            // getSubResource(String id)
            Method method = resource.getClass().getMethod(node.getGetter(), String.class);
            resource = (BaseBackendResource) method.invoke(resource, crawler.getCurrentPathSegment());
        }
    }
    return resource;
}
Also used : ServiceTreeCrawler(org.ovirt.engine.api.rsdl.ServiceTreeCrawler) Method(java.lang.reflect.Method) ServiceTreeNode(org.ovirt.engine.api.rsdl.ServiceTreeNode)

Aggregations

Method (java.lang.reflect.Method)1 ServiceTreeCrawler (org.ovirt.engine.api.rsdl.ServiceTreeCrawler)1 ServiceTreeNode (org.ovirt.engine.api.rsdl.ServiceTreeNode)1