PGBuildManagerAdapterFactory.java 7.83 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.granite.crypto.CryptoSupport
 *  com.day.cq.contentsync.ContentSyncManager
 *  com.day.cq.wcm.webservicesupport.ConfigurationManager
 *  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.Property
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.http.osgi.services.HttpClientBuilderFactory
 *  org.apache.sling.api.adapter.AdapterManager
 *  org.apache.sling.api.resource.LoginException
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.resource.ResourceResolverFactory
 *  org.apache.sling.commons.osgi.PropertiesUtil
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.cq.mobile.phonegap.impl.build.service;

import com.adobe.cq.mobile.phonegap.impl.build.PGBuildManager;
import com.adobe.cq.mobile.phonegap.impl.build.service.PGBuildManagerImpl;
import com.adobe.granite.crypto.CryptoSupport;
import com.day.cq.contentsync.ContentSyncManager;
import com.day.cq.wcm.webservicesupport.ConfigurationManager;
import java.util.HashMap;
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.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.http.osgi.services.HttpClientBuilderFactory;
import org.apache.sling.api.adapter.AdapterManager;
import org.apache.sling.api.resource.LoginException;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.commons.osgi.PropertiesUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(metatype=1, immediate=1, label="%mobile.phonegap.pgBuildManager.adapterfactory.service.name", description="%mobile.phonegap.pgBuildManager.adapterfactory.service.description")
@Service(value={PGBuildManagerAdapterFactory.class})
public class PGBuildManagerAdapterFactory {
    private static Logger log = LoggerFactory.getLogger(PGBuildManagerAdapterFactory.class);
    @Property(name="adapters", propertyPrivate=1)
    private static final String[] ADAPTER_CLASSES = new String[]{PGBuildManager.class.getName()};
    @Property(name="adaptables", propertyPrivate=1)
    private static final String[] ADAPTABLE_CLASSES = new String[]{Session.class.getName(), ResourceResolver.class.getName()};
    @Property(label="PhoneGap Build Host", description="PhoneGap Build Host", value={"build.phonegap.com"})
    private static String PHONEGAP_BUILD_HOST = "phonegap.build.host";
    @Property(label="PhoneGap Build Port", description="PhoneGap Build Port", value={"443"})
    private static final String PHONEGAP_BUILD_PORT = "phonegap.build.port";
    @Property(label="Use SSL", description="Use SSL", value={"true"})
    private static final String USE_SSL = "phonegap.build.useSsl";
    private static int phonegapBuildPort = 443;
    private static String phonegapBuildHost = "build.phonegap.com";
    private static boolean useSsl = true;
    @Reference
    private AdapterManager adapterManagerService;
    @Reference
    private ContentSyncManager contentSyncManagerService;
    @Reference
    private CryptoSupport cryptoSupportService;
    @Reference
    private ResourceResolverFactory resourceResolverFactory;
    @Reference
    private HttpClientBuilderFactory clientBuilderFactory;

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

    public PGBuildManager getPGBuildManager(Session userSession) {
        if (log.isDebugEnabled()) {
            log.debug("Creating PGBuildManager for user: " + (userSession == null ? "no user" : userSession.getUserID()));
        }
        HashMap<String, Session> authInfo = new HashMap<String, Session>();
        authInfo.put("user.jcr.session", userSession);
        ResourceResolver resolver = null;
        try {
            resolver = this.resourceResolverFactory.getResourceResolver(authInfo);
        }
        catch (LoginException e) {
            log.error("Unable to obtain the resource resolver", (Object)0);
            return null;
        }
        ConfigurationManager configurationManagerService = (ConfigurationManager)resolver.adaptTo(ConfigurationManager.class);
        return new PGBuildManagerImpl(userSession, this.contentSyncManagerService, this.adapterManagerService, configurationManagerService, this.cryptoSupportService, this.clientBuilderFactory, phonegapBuildPort, phonegapBuildHost, useSsl);
    }

    @Activate
    public void activate(Map<String, Object> properties) {
        this.modified(properties);
    }

    @Modified
    protected void modified(Map<String, Object> props) {
        Object host = props.get(PHONEGAP_BUILD_HOST);
        phonegapBuildHost = PropertiesUtil.toString((Object)host, (String)phonegapBuildHost);
        Object port = props.get("phonegap.build.port");
        phonegapBuildPort = PropertiesUtil.toInteger((Object)port, (int)phonegapBuildPort);
        Object propertyUseSsl = props.get("phonegap.build.useSsl");
        useSsl = PropertiesUtil.toBoolean((Object)propertyUseSsl, (boolean)useSsl);
    }

    protected void bindAdapterManagerService(AdapterManager adapterManager) {
        this.adapterManagerService = adapterManager;
    }

    protected void unbindAdapterManagerService(AdapterManager adapterManager) {
        if (this.adapterManagerService == adapterManager) {
            this.adapterManagerService = null;
        }
    }

    protected void bindContentSyncManagerService(ContentSyncManager contentSyncManager) {
        this.contentSyncManagerService = contentSyncManager;
    }

    protected void unbindContentSyncManagerService(ContentSyncManager contentSyncManager) {
        if (this.contentSyncManagerService == contentSyncManager) {
            this.contentSyncManagerService = null;
        }
    }

    protected void bindCryptoSupportService(CryptoSupport cryptoSupport) {
        this.cryptoSupportService = cryptoSupport;
    }

    protected void unbindCryptoSupportService(CryptoSupport cryptoSupport) {
        if (this.cryptoSupportService == cryptoSupport) {
            this.cryptoSupportService = null;
        }
    }

    protected void bindResourceResolverFactory(ResourceResolverFactory resourceResolverFactory) {
        this.resourceResolverFactory = resourceResolverFactory;
    }

    protected void unbindResourceResolverFactory(ResourceResolverFactory resourceResolverFactory) {
        if (this.resourceResolverFactory == resourceResolverFactory) {
            this.resourceResolverFactory = null;
        }
    }

    protected void bindClientBuilderFactory(HttpClientBuilderFactory httpClientBuilderFactory) {
        this.clientBuilderFactory = httpClientBuilderFactory;
    }

    protected void unbindClientBuilderFactory(HttpClientBuilderFactory httpClientBuilderFactory) {
        if (this.clientBuilderFactory == httpClientBuilderFactory) {
            this.clientBuilderFactory = null;
        }
    }
}