/*
 * #%L
 * wcm.io
 * %%
 * Copyright (C) 2014 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.config.core.impl;

import io.wcm.config.api.Application;
import io.wcm.config.api.management.ApplicationFinder;
import io.wcm.config.core.util.AdaptableUtil;

import org.apache.commons.lang3.StringUtils;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.models.spi.ImplementationPicker;
import org.osgi.framework.Constants;

/**
 * Sling Models {@link ImplementationPicker} implementation that checks if an application is associated
 * with the given adaptable, and then chooses the implementation that is annotated for this application.
 * If not application is associated with the adaptable, the first implementation without @Application
 * annotation is used.
 */
@Component
@Service(ImplementationPicker.class)
@Property(name = Constants.SERVICE_RANKING, intValue = 1000)
public class ApplicationImplementationPicker implements ImplementationPicker {

  @Reference
  private ApplicationFinder applicationFinder;

  @Override
  public Class<?> pick(Class<?> adapterType, Class<?>[] implementationsTypes, Object adaptable) {
    Class<?> classMatchingApplication = pickMatchingApplication(implementationsTypes, adaptable);
    if (classMatchingApplication != null) {
      return classMatchingApplication;
    }
    return pickFirstWithoutApplication(implementationsTypes);
  }

  private Class<?> pickMatchingApplication(Class<?>[] implementationsTypes, Object adaptable) {
    String applicationId = getApplicationId(adaptable);
    if (applicationId != null) {
      for (Class<?> clazz : implementationsTypes) {
        io.wcm.config.api.annotations.Application applicationAnnotation =
            clazz.getAnnotation(io.wcm.config.api.annotations.Application.class);
        if (applicationAnnotation != null
            && StringUtils.equals(applicationId, applicationAnnotation.value())) {
          return clazz;
        }
      }
    }
    return null;
  }

  private String getApplicationId(Object adaptable) {
    Resource resource = AdaptableUtil.getResource(adaptable);
    if (resource != null) {
      Application application = applicationFinder.find(resource);
      if (application != null) {
        return application.getApplicationId();
      }
    }
    return null;
  }

  private Class<?> pickFirstWithoutApplication(Class<?>[] implementationsTypes) {
    for (Class<?> clazz : implementationsTypes) {
      if (!clazz.isAnnotationPresent(io.wcm.config.api.annotations.Application.class)) {
        return clazz;
      }
    }
    return null;
  }

}

@@ -1400,16 +1400,23 @@
 on.%0A
+ * %3Cp%3E%0A
  * If no
 t ap
@@ -1411,17 +1411,16 @@
  * If no
-t
  applica
@@ -1458,19 +1458,80 @@
 ptable, 
-the
+or no implementation for the detected application exists, the%0A *
  first i
@@ -1564,19 +1564,16 @@
 lication
-%0A *
  annotat
@@ -1585,16 +1585,24 @@
 s used.%0A
+ * %3C/p%3E%0A
  */%0A@Com

add support for applications