MobileAppsConfigUpdateHandler.java 5.71 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.contentsync.config.ConfigEntry
 *  com.day.cq.contentsync.handler.ContentUpdateHandler
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  org.apache.commons.lang.StringUtils
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.ReferenceCardinality
 *  org.apache.felix.scr.annotations.ReferencePolicy
 *  org.apache.felix.scr.annotations.References
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.commons.json.io.JSONWriter
 *  org.apache.sling.jcr.resource.JcrResourceResolverFactory
 *  org.osgi.service.component.ComponentContext
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.cq.mobile.platform.impl.contentsync.handler;

import com.adobe.cq.mobile.platform.MobileAppsInfoProvider;
import com.adobe.cq.mobile.platform.MobileResource;
import com.adobe.cq.mobile.platform.impl.contentsync.handler.MobileAppsUpdateHandler;
import com.adobe.cq.mobile.platform.impl.contentsync.handler.MobileContentPackagesListingHandler;
import com.day.cq.contentsync.config.ConfigEntry;
import com.day.cq.contentsync.handler.ContentUpdateHandler;
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.commons.lang.StringUtils;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.ReferenceCardinality;
import org.apache.felix.scr.annotations.ReferencePolicy;
import org.apache.felix.scr.annotations.References;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.commons.json.io.JSONWriter;
import org.apache.sling.jcr.resource.JcrResourceResolverFactory;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(factory="com.day.cq.contentsync.handler.ContentUpdateHandler/mobileappconfig")
@Service(value={ContentUpdateHandler.class})
@References(value={@Reference(name="MobileAppsInfoProvider", cardinality=ReferenceCardinality.OPTIONAL_MULTIPLE, referenceInterface=MobileAppsInfoProvider.class, policy=ReferencePolicy.DYNAMIC)})
public class MobileAppsConfigUpdateHandler
extends MobileAppsUpdateHandler {
    private static final Logger LOGGER = LoggerFactory.getLogger(MobileContentPackagesListingHandler.class);
    public static final String APP_INIT_FILE = "MobileAppsConfig.json";
    public static final String PROPERTY_PROVIDER = "providers";
    public static final String PROPERTY_FILENAME = "fileName";
    private Map<String, MobileAppsInfoProvider> providers = new HashMap<String, MobileAppsInfoProvider>();
    private ComponentContext componentContext;

    @Override
    public boolean updateCacheEntry(ConfigEntry configEntry, Long lastUpdated, String configCacheRoot, Session adminSession, Session userSession) {
        configCacheRoot = this.getConfigCacheRoot(configEntry, configCacheRoot);
        String[] providerNames = configEntry.getValues("providers");
        String fileName = configEntry.getValue("fileName");
        if (providerNames == null || providerNames.length == 0) {
            LOGGER.debug("The providers property is empty, skipping handler");
            return false;
        }
        if (StringUtils.isBlank((String)fileName)) {
            LOGGER.warn("The property {} is missing from the config entry, defaulting to {}", (Object)"fileName", (Object)"MobileAppsConfig.json");
            fileName = "MobileAppsConfig.json";
        }
        ResourceResolver resourceResolver = this.resolverFactory.getResourceResolver(adminSession);
        Resource shellResource = resourceResolver.getResource(this.getResolvedContentPath(configEntry));
        ByteArrayOutputStream outputStream = null;
        OutputStreamWriter outputStreamWriter = null;
        try {
            outputStream = new ByteArrayOutputStream();
            outputStreamWriter = new OutputStreamWriter(outputStream);
            JSONWriter writer = new JSONWriter((Writer)outputStreamWriter);
            writer.setTidy(true);
            writer.object();
            for (String providerName : providerNames) {
                MobileAppsInfoProvider provider = this.providers.get(providerName);
                if (provider == null) continue;
                provider.updateInfo(writer, (MobileResource)shellResource.adaptTo(MobileResource.class));
            }
            writer.endObject();
            outputStream.close();
            outputStreamWriter.close();
            this.writeToCacheFile(outputStream, configCacheRoot + "/" + fileName, adminSession);
            adminSession.save();
        }
        catch (Exception e) {
            LOGGER.error("Unexpected error while updating cache for config: " + configEntry.getPath(), (Throwable)e);
        }
        return false;
    }

    protected void activate(ComponentContext context) throws Exception {
        this.componentContext = context;
    }

    protected void deactivate(ComponentContext context) throws RepositoryException {
        this.componentContext = null;
    }

    public void bindMobileAppsInfoProvider(MobileAppsInfoProvider provider) {
        this.providers.put(provider.getClass().getName(), provider);
    }

    public void unbindMobileAppsInfoProvider(MobileAppsInfoProvider provider) {
        this.providers.remove(provider.getClass().getName());
    }
}