/*
 * #%L
 * wcm.io
 * %%
 * Copyright (C) 2016 wcm.io
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */
package io.wcm.caconfig.application.impl;

import java.util.Map;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

import org.apache.sling.api.resource.Resource;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.component.annotations.ReferenceCardinality;
import org.osgi.service.component.annotations.ReferencePolicy;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;

import io.wcm.caconfig.application.ApplicationFinder;
import io.wcm.caconfig.application.ApplicationInfo;
import io.wcm.caconfig.application.spi.ApplicationProvider;
import io.wcm.sling.commons.osgi.RankedServices;

/**
 * Default implementation of {@link ApplicationFinder}.
 */
@Component(immediate = true, service = ApplicationFinder.class)
public final class ApplicationFinderImpl implements ApplicationFinder {

  private static final ApplicationInfo APPLICATION_NOT_FOUND = new ApplicationInfo("APPLICATION_NOT_FOUND", null);

  @Reference(service = ApplicationProvider.class, cardinality = ReferenceCardinality.MULTIPLE, policy = ReferencePolicy.DYNAMIC,
      bind = "bindApplicationProvider", unbind = "unbindApplicationProvider")
  private final RankedServices<ApplicationProvider> applicationProviders = new RankedServices<>();

  // apply a simple cache mechanism for looking up application per resource path
  private final Cache<String, ApplicationInfo> applicationFindCache = CacheBuilder.newBuilder()
      .maximumSize(10000)
      .expireAfterWrite(10, TimeUnit.SECONDS)
      .build();

  @Override
  public ApplicationInfo find(final Resource resource) {
    try {
      ApplicationInfo result = applicationFindCache.get(resource.getPath(), new Callable<ApplicationInfo>() {
        @Override
        public ApplicationInfo call() {
          for (ApplicationProvider provider : applicationProviders) {
            if (provider.matches(resource)) {
              return new ApplicationInfo(provider.getApplicationId(), provider.getLabel());
            }
          }
          return APPLICATION_NOT_FOUND;
        }
      });
      if (result == APPLICATION_NOT_FOUND) {
        return null;
      }
      else {
        return result;
      }
    }
    catch (ExecutionException ex) {
      throw new RuntimeException("Error finding application.", ex.getCause());
    }
  }

  @Override
  public Set<ApplicationInfo> getAll() {
    SortedSet<ApplicationInfo> allApps = new TreeSet<>();
    for (ApplicationProvider provider : applicationProviders) {
      allApps.add(new ApplicationInfo(provider.getApplicationId(), provider.getLabel()));
    }
    return allApps;
  }

  void bindApplicationProvider(ApplicationProvider service, Map<String, Object> props) {
    applicationProviders.bind(service, props);
  }

  void unbindApplicationProvider(ApplicationProvider service, Map<String, Object> props) {
    applicationProviders.unbind(service, props);
  }

}

@@ -1610,212 +1610,69 @@
 lass
-)%0Apublic final class ApplicationFinderImpl implements ApplicationFinder
+, reference =
  %7B%0A
-%0A
   
-private static final ApplicationInfo APPLICATION_NOT_FOUND = new ApplicationInfo(%22APPLICATION_NOT_FOUND%22, null);%0A%0A  @Reference(
+  @Reference(name = %22applicationProvider%22, 
 serv
@@ -1703,16 +1703,24 @@
 r.class,
+%0A       
  cardina
@@ -1797,16 +1797,18 @@
 ,%0A      
+  
 bind = %22
@@ -1871,16 +1871,208 @@
 vider%22)%0A
+%7D)%0Apublic final class ApplicationFinderImpl implements ApplicationFinder %7B%0A%0A  private static final ApplicationInfo APPLICATION_NOT_FOUND = new ApplicationInfo(%22APPLICATION_NOT_FOUND%22, null);%0A%0A
   privat

fix problem with AEM 6.1