TaskManagerAdapterFactory.java 6.43 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.granite.taskmanagement.TaskManager
 *  javax.jcr.Session
 *  org.apache.felix.scr.annotations.Activate
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Modified
 *  org.apache.felix.scr.annotations.Properties
 *  org.apache.felix.scr.annotations.Property
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.ReferencePolicy
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.sling.api.adapter.AdapterFactory
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.jcr.api.SlingRepository
 *  org.osgi.service.event.EventAdmin
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.granite.taskmanagement.impl.service;

import com.adobe.granite.taskmanagement.TaskManager;
import com.adobe.granite.taskmanagement.impl.jcr.TaskStorageProvider;
import com.adobe.granite.taskmanagement.impl.service.TaskManagerImpl;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import javax.jcr.Session;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Modified;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.ReferencePolicy;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.adapter.AdapterFactory;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.jcr.api.SlingRepository;
import org.osgi.service.event.EventAdmin;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/*
 * This class specifies class file version 49.0 but uses Java 6 signatures.  Assumed Java 6.
 */
@Component(metatype=1, label="Adobe Granite Task Manager Adapter Factory", description="Adapts JCR sessions or Apache Sling resource resolvers to a taskmanager instance.")
@Properties(value={@Property(name="service.description", value={"Adapts JCR sessions or Apache Sling resource resolvers to a taskmanager instance."}), @Property(name="adapter.condition", value={"If the ResourceResolver is a JcrResourceResolver or a Session."})})
@Service(value={AdapterFactory.class, TaskManagerAdapterFactory.class})
public class TaskManagerAdapterFactory
implements AdapterFactory {
    private static Logger log = LoggerFactory.getLogger(TaskManagerAdapterFactory.class);
    @Reference(policy=ReferencePolicy.DYNAMIC)
    private volatile TaskStorageProvider taskStorageProvider;
    @Reference
    protected SlingRepository repository;
    @Reference
    private EventAdmin eventAdminService;
    @Property(name="adapters", propertyPrivate=1)
    private static final String[] ADAPTER_CLASSES = new String[]{TaskManager.class.getName()};
    @Property(name="adaptables", propertyPrivate=1)
    private static final String[] ADAPTABLE_CLASSES = new String[]{Session.class.getName(), ResourceResolver.class.getName(), Resource.class.getName()};
    public static final String DEFAULT_TASK_ADMIN_GROUP = "administrators";
    @Property(value={"administrators", "taskmanagement-service", "projects-service"}, label="Task Administrator Group", description="The name of the group of users with task administrative rights.")
    public static final String TM_ADMIN_GROUP_NAMES = "taskmanager.admingroups";
    private List<String> superusers = new ArrayList<String>();

    @Activate
    @Modified
    private void configure(Map<String, Object> properties) {
        this.superusers.addAll(Arrays.asList((String[])properties.get("taskmanager.admingroups")));
    }

    public TaskManager getTaskManager(Session userSession, String rootPath) {
        if (log.isDebugEnabled()) {
            log.debug("Creating TaskManager for user: " + (userSession == null ? "no user" : userSession.getUserID()));
        }
        if (rootPath == null) {
            return new TaskManagerImpl(userSession, this.eventAdminService, this.taskStorageProvider, this.repository, this.superusers);
        }
        TaskStorageProvider localStorageProvider = new TaskStorageProvider(this.repository, this.eventAdminService, rootPath, userSession);
        return new TaskManagerImpl(userSession, this.eventAdminService, localStorageProvider, this.repository, this.superusers);
    }

    public <AdapterType> AdapterType getAdapter(Object adaptable, Class<AdapterType> type) {
        if (TaskManager.class == type) {
            Session userSession = null;
            String rootPath = null;
            if (adaptable instanceof Session) {
                userSession = (Session)adaptable;
            } else if (adaptable instanceof ResourceResolver) {
                userSession = (Session)((ResourceResolver)adaptable).adaptTo(Session.class);
            } else if (adaptable instanceof Resource) {
                Resource resource = (Resource)adaptable;
                userSession = (Session)resource.getResourceResolver().adaptTo(Session.class);
                rootPath = resource.getPath();
            } else {
                log.info("Can't adapt {} to TaskManager", (Object)(adaptable == null ? "null" : adaptable.getClass().getName()));
            }
            if (userSession != null) {
                return (AdapterType)this.getTaskManager(userSession, rootPath);
            }
        }
        return null;
    }

    protected void bindTaskStorageProvider(TaskStorageProvider taskStorageProvider) {
        this.taskStorageProvider = taskStorageProvider;
    }

    protected void unbindTaskStorageProvider(TaskStorageProvider taskStorageProvider) {
        if (this.taskStorageProvider == taskStorageProvider) {
            this.taskStorageProvider = null;
        }
    }

    protected void bindRepository(SlingRepository slingRepository) {
        this.repository = slingRepository;
    }

    protected void unbindRepository(SlingRepository slingRepository) {
        if (this.repository == slingRepository) {
            this.repository = null;
        }
    }

    protected void bindEventAdminService(EventAdmin eventAdmin) {
        this.eventAdminService = eventAdmin;
    }

    protected void unbindEventAdminService(EventAdmin eventAdmin) {
        if (this.eventAdminService == eventAdmin) {
            this.eventAdminService = null;
        }
    }
}