LicenseProvider.java 6.86 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  org.apache.felix.scr.annotations.Activate
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.sling.settings.SlingSettingsService
 *  org.osgi.framework.BundleContext
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.granite.license.impl;

import com.adobe.granite.license.License;
import com.adobe.granite.license.impl.LicenseImpl;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.settings.SlingSettingsService;
import org.osgi.framework.BundleContext;
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
@Service(value={LicenseProvider.class})
public class LicenseProvider {
    private final Logger log = LoggerFactory.getLogger(LicenseProvider.class);
    private static final String LICENSE_PROPERTIES = "license.properties";
    private static final String PROP_GRANITE_LICENSE_URL = "granite.license.url";
    @Reference
    private SlingSettingsService slingSettings;
    private License license;

    public License getLicense() {
        return this.license;
    }

    @Activate
    protected void activate(BundleContext bundleContext) {
        if (!this.checkLicenseUrl(bundleContext)) {
            this.readLicenseFile();
        }
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    private void readLicenseFile() {
        block14 : {
            File licenseFile = this.getLicenseFile();
            if (licenseFile.exists()) {
                InputStream in = null;
                try {
                    if (licenseFile.canRead()) {
                        in = new FileInputStream(licenseFile);
                        Properties licenseProps = new Properties();
                        licenseProps.load(in);
                        this.setLicense(LicenseImpl.createLicense(this.getMap(licenseProps)));
                        break block14;
                    }
                    this.log.error("Could not validate license. Please check " + licenseFile.getAbsolutePath() + ". ");
                }
                catch (IOException e) {
                    this.log.error("Could not validate license. Please check " + licenseFile.getAbsolutePath() + ". ", (Throwable)e);
                }
                finally {
                    if (in != null) {
                        try {
                            in.close();
                        }
                        catch (IOException ignore) {}
                    }
                }
            }
        }
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    private boolean checkLicenseUrl(BundleContext bundleContext) {
        String urlValue = bundleContext.getProperty("granite.license.url");
        if (urlValue != null) {
            InputStream is = null;
            try {
                URL url = LicenseProvider.getLicenseUrl(this.slingSettings.getSlingHomePath(), urlValue);
                is = url.openStream();
                Properties licenseProps = new Properties();
                licenseProps.load(is);
                this.setLicense(LicenseImpl.createLicense(this.getMap(licenseProps)));
                boolean bl = true;
                return bl;
            }
            catch (IOException ioe) {
                this.log.warn("Unable to read license properties from {}. Trying default location.", (Object)urlValue);
            }
            finally {
                if (is != null) {
                    try {
                        is.close();
                    }
                    catch (IOException ignore) {}
                }
            }
        }
        return false;
    }

    public static URL getLicenseUrl(String slingHome, String urlValue) throws IOException {
        URL baseUrl = slingHome.endsWith("/") ? new URL(slingHome) : new URL(slingHome + "/");
        return new URL(baseUrl, urlValue);
    }

    public void saveLicense(License license) throws IOException {
        File file = this.getLicenseFile();
        file.getParentFile().mkdirs();
        FileOutputStream out = null;
        try {
            out = new FileOutputStream(file);
            this.getProperties(license).store(out, "Adobe Granite License Properties");
        }
        catch (IOException e) {
            this.log.error("Error while saving license properties.", (Throwable)e);
            throw e;
        }
        finally {
            if (out != null) {
                try {
                    out.close();
                }
                catch (IOException e) {}
            }
        }
        this.setLicense(license);
    }

    private File getLicenseFile() {
        String slingHome = this.slingSettings.getSlingHomePath();
        File slingHomeDir = new File(slingHome);
        try {
            slingHomeDir = slingHomeDir.getCanonicalFile();
        }
        catch (IOException ioe) {
            // empty catch block
        }
        File outsideDir = slingHomeDir.getParentFile();
        return new File(outsideDir, "license.properties");
    }

    private Map<String, String> getMap(Properties props) {
        HashMap<String, String> map = new HashMap<String, String>();
        for (Map.Entry entry : props.entrySet()) {
            map.put(entry.getKey().toString(), entry.getValue().toString());
        }
        return map;
    }

    private Properties getProperties(Map<String, String> map) {
        Properties props = new Properties();
        for (Map.Entry<String, String> entry : map.entrySet()) {
            props.put(entry.getKey(), entry.getValue());
        }
        return props;
    }

    private void setLicense(License license) {
        this.license = license;
        if (license != null) {
            this.log.info(license.getProductName() + " " + license.getProductVersion() + " licensed by " + license.getCustomerName());
            this.log.info("DownloadID:" + license.getDownloadId());
        }
    }

    protected void bindSlingSettings(SlingSettingsService slingSettingsService) {
        this.slingSettings = slingSettingsService;
    }

    protected void unbindSlingSettings(SlingSettingsService slingSettingsService) {
        if (this.slingSettings == slingSettingsService) {
            this.slingSettings = null;
        }
    }
}