ConfigurationManagerImpl.java 10.2 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.wcm.api.Page
 *  com.day.cq.wcm.api.Template
 *  javax.jcr.Node
 *  javax.jcr.Property
 *  javax.jcr.RepositoryException
 *  javax.jcr.Value
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.resource.ValueMap
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.wcm.webservicesupport.impl;

import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.Template;
import com.day.cq.wcm.webservicesupport.Configuration;
import com.day.cq.wcm.webservicesupport.ConfigurationManager;
import com.day.cq.wcm.webservicesupport.Service;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Value;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ConfigurationManagerImpl
implements ConfigurationManager {
    private static final Logger log = LoggerFactory.getLogger(ConfigurationManagerImpl.class);
    private ResourceResolver resolver;

    public ConfigurationManagerImpl() {
    }

    public ConfigurationManagerImpl(ResourceResolver resolver) {
        if (resolver == null) {
            throw new IllegalArgumentException("resolver may not be null");
        }
        this.resolver = resolver;
    }

    @Override
    public Iterator<Service> getServices() {
        LinkedList<Service> entries = new LinkedList<Service>();
        Resource baseResource = this.resolver.getResource("/etc/cloudservices");
        if (baseResource != null) {
            Iterator iter = baseResource.listChildren();
            while (iter.hasNext()) {
                Resource resource = (Resource)iter.next();
                Service srvc = (Service)resource.adaptTo(Service.class);
                if (srvc != null) {
                    entries.add(srvc);
                    continue;
                }
                log.debug("Resource at {} is not adaptable to com.day.cq.configuration.Service", (Object)resource.getPath());
            }
        }
        return entries.iterator();
    }

    @Override
    public Iterator<Service> getServices(String[] paths, Comparator<Service> comparator) {
        LinkedList<Service> entries = new LinkedList<Service>();
        for (String path : paths) {
            if (path.length() <= 0) continue;
            String servicename = this.getServiceName(path);
            Service service = this.getService(servicename);
            if (service == null) {
                log.warn("Cloud service {} not found", (Object)path);
                continue;
            }
            entries.add(service);
        }
        Collections.sort(entries, comparator);
        return entries.iterator();
    }

    @Override
    public Service getService(String name) {
        Service service = null;
        if (name != null) {
            String path = "/etc/cloudservices/" + name;
            Resource serviceResource = this.resolver.getResource(path);
            if (serviceResource != null) {
                service = (Service)serviceResource.adaptTo(Service.class);
            } else {
                log.debug("Resource at {} is not adaptable to com.day.cq.configuration.Service", (Object)path);
            }
        }
        return service;
    }

    @Override
    public Service getService(Configuration configuration) {
        Resource tplContent;
        Resource tplRes;
        Service service = null;
        Template tpl = configuration.getTemplate();
        if (tpl != null && (tplContent = (tplRes = (Resource)tpl.adaptTo(Resource.class)).getChild("jcr:content")) != null) {
            ValueMap props = (ValueMap)tplContent.adaptTo(ValueMap.class);
            return this.getService((String)props.get("cq:cloudservicename", String.class));
        }
        return service;
    }

    @Override
    public String getServiceName(String path) {
        if (path != null && path.startsWith("/etc/cloudservices")) {
            int idx = (path = path.replace("/etc/cloudservices/", "")).indexOf("/");
            if (idx > -1) {
                path = path.substring(0, idx);
            }
            return path;
        }
        return null;
    }

    @Override
    public Configuration getConfiguration(String path) {
        Resource cfg = this.resolver.getResource(path);
        return this.getConfigurationFromResource(path, cfg);
    }

    private Configuration getConfigurationFromResource(String path, Resource cfg) {
        String publicChildPath = path + "/" + "jcr:content" + "/" + "public";
        Configuration config = null;
        if (cfg != null) {
            config = (Configuration)cfg.adaptTo(Configuration.class);
        } else {
            cfg = this.resolver.getResource(publicChildPath);
            if (cfg != null) {
                config = (Configuration)cfg.adaptTo(Configuration.class);
            }
        }
        if (config == null) {
            log.debug("Resource at {} is not readable or adaptable to com.day.cq.configuration.Configuration ( also tried looking at {} )", (Object)path, (Object)publicChildPath);
        }
        return config;
    }

    @Override
    public Configuration getConfiguration(String servicename, String[] configurationPaths) {
        for (String path2 : configurationPaths) {
            if (path2.indexOf(servicename) <= -1) continue;
            return this.getConfiguration(path2);
        }
        for (String path2 : configurationPaths) {
            Resource cfgPage = this.resolver.getResource(path2);
            if (cfgPage != null) {
                Iterator childs = cfgPage.listChildren();
                Configuration cfg = this.searchConfiguration(servicename, childs);
                if (cfg == null) continue;
                return cfg;
            }
            log.warn("Resource with path {} does not exist!", (Object)path2);
        }
        return null;
    }

    private Configuration searchConfiguration(String servicename, Iterator<Resource> childs) {
        Resource child;
        Configuration cfg = null;
        while (childs.hasNext() && (cfg = this.searchConfiguration(servicename, (child = childs.next()).listChildren())) == null) {
            Service srv;
            Configuration adpcfg = (Configuration)child.adaptTo(Configuration.class);
            if (adpcfg == null || (srv = this.getService(adpcfg)) == null || !srv.getName().equals(servicename)) continue;
            return adpcfg;
        }
        return cfg;
    }

    @Override
    public Iterator<Configuration> getConfigurations(String rootPath) {
        LinkedList<Configuration> configurations = new LinkedList<Configuration>();
        Resource rootResource = this.resolver.getResource(rootPath);
        List<Resource> configResources = this.getChildren(rootResource);
        for (Resource resource : configResources) {
            Configuration cfg = (Configuration)resource.adaptTo(Configuration.class);
            if (cfg != null) {
                configurations.add(cfg);
                continue;
            }
            log.debug("Resource at {} is not adaptable to com.day.cq.configuration.Configuration", (Object)resource.getPath());
        }
        return configurations.iterator();
    }

    @Override
    public Iterator<Configuration> getConfigurations(Resource resource) {
        LinkedList<Configuration> configurations = new LinkedList<Configuration>();
        try {
            Page cfgPage;
            Node contentNode;
            Resource cfgRes = this.getConfigurationResource(resource);
            if (cfgRes != null && (contentNode = (Node)(cfgPage = (Page)cfgRes.adaptTo(Page.class)).getContentResource().adaptTo(Node.class)) != null && contentNode.hasProperty("cq:cloudserviceconfigs")) {
                Property prop = contentNode.getProperty("cq:cloudserviceconfigs");
                if (prop.isMultiple()) {
                    for (Value v : prop.getValues()) {
                        Configuration c = this.getConfiguration(v.getString());
                        if (c == null) continue;
                        configurations.add(c);
                    }
                } else {
                    Configuration c = this.getConfiguration(prop.getValue().getString());
                    if (c != null) {
                        configurations.add(c);
                    }
                }
            }
        }
        catch (RepositoryException cfgRes) {
            // empty catch block
        }
        return configurations.iterator();
    }

    @Override
    public Resource getConfigurationResource(Resource resource) {
        ResourceResolver resolver = resource.getResourceResolver();
        try {
            String resourcePath = resource.getPath();
            while (resourcePath.lastIndexOf("/") > 0) {
                Node jcrContent;
                Page page;
                Resource contentResource;
                resource = resolver.getResource(resourcePath);
                if (resource != null && (page = (Page)resource.adaptTo(Page.class)) != null && (contentResource = page.getContentResource()) != null && (jcrContent = (Node)contentResource.adaptTo(Node.class)).hasProperty("cq:cloudserviceconfigs")) {
                    return resource;
                }
                resourcePath = resourcePath.substring(0, resourcePath.lastIndexOf("/"));
            }
        }
        catch (Exception ignored) {
            log.warn("Exception while searching for cloudservice resource", (Throwable)ignored);
        }
        return null;
    }

    private List<Resource> getChildren(Resource resource) {
        LinkedList<Resource> newResource = new LinkedList<Resource>();
        if (resource != null) {
            Iterator iter = resource.listChildren();
            while (iter.hasNext()) {
                Resource child = (Resource)iter.next();
                newResource.add(child);
                if (!child.listChildren().hasNext()) continue;
                newResource.addAll(this.getChildren(child));
            }
        }
        return newResource;
    }
}