EmulatorServiceImpl.java 3.47 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.Deactivate
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.sling.api.resource.Resource
 *  org.osgi.framework.BundleContext
 *  org.osgi.framework.InvalidSyntaxException
 *  org.osgi.framework.ServiceReference
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.wcm.emulator.impl;

import com.day.cq.wcm.emulator.Emulator;
import com.day.cq.wcm.emulator.EmulatorGroup;
import com.day.cq.wcm.emulator.EmulatorProvider;
import com.day.cq.wcm.emulator.EmulatorService;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Deactivate;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.resource.Resource;
import org.osgi.framework.BundleContext;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceReference;
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=0)
@Service
public class EmulatorServiceImpl
implements EmulatorService {
    private Logger log = LoggerFactory.getLogger(EmulatorServiceImpl.class);
    private BundleContext bundleContext = null;

    @Override
    public List<Emulator> getEmulators(Resource resource) {
        LinkedList<Emulator> emulators = new LinkedList<Emulator>();
        List<EmulatorProvider> providers = this.getProviders(resource);
        for (EmulatorProvider provider : providers) {
            emulators.addAll(provider.getEmulators(resource));
        }
        return emulators;
    }

    @Override
    public synchronized List<EmulatorGroup> getEmulatorGroups(Resource resource) {
        LinkedList<EmulatorGroup> emulatorGroups = new LinkedList<EmulatorGroup>();
        List<EmulatorProvider> providers = this.getProviders(resource);
        for (EmulatorProvider emulatorProvider : providers) {
            emulatorGroups.addAll(emulatorProvider.getEmulatorGroups(resource));
        }
        return emulatorGroups;
    }

    private List<EmulatorProvider> getProviders(Resource resource) {
        if (this.bundleContext == null) {
            throw new IllegalStateException("Service not activated, bundle context is null");
        }
        ArrayList<EmulatorProvider> providers = new ArrayList<EmulatorProvider>();
        try {
            ServiceReference[] refs;
            for (ServiceReference ref : refs = this.bundleContext.getAllServiceReferences(EmulatorProvider.class.getName(), null)) {
                EmulatorProvider provider;
                Object service = this.bundleContext.getService(ref);
                if (!(service instanceof EmulatorProvider) || !(provider = (EmulatorProvider)service).handles(resource)) continue;
                providers.add(provider);
            }
        }
        catch (InvalidSyntaxException e) {
            this.log.error("Failed to get service reference: ", (Throwable)e);
        }
        return providers;
    }

    @Activate
    private void activate(BundleContext context) {
        this.bundleContext = context;
    }

    @Deactivate
    private void deactivate() {
        this.bundleContext = null;
    }
}