001/* 002 * Copyright 2020 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.ametys.core.resources; 017 018import java.util.Comparator; 019import java.util.Objects; 020 021import org.apache.cocoon.components.LifecycleHelper; 022import org.apache.cocoon.util.log.SLF4JLoggerAdapter; 023 024import org.ametys.core.util.LambdaUtils; 025import org.ametys.runtime.plugin.component.AbstractThreadSafeComponentExtensionPoint; 026import org.ametys.runtime.plugin.component.LogEnabled; 027 028/** 029 * Extension point for {@link ResourceHandlerProvider}. 030 */ 031public class ResourceHandlerProviderExtensionPoint extends AbstractThreadSafeComponentExtensionPoint<ResourceHandlerProvider> 032{ 033 /** Avalon Role */ 034 public static final String ROLE = ResourceHandlerProviderExtensionPoint.class.getName(); 035 036 /** 037 * Get the extension of max priority matching the provided source, based on the registered suffixes 038 * @param source The source 039 * @return The corresponding extension 040 * @throws Exception if an error occurs during ResourceHandler creation. 041 */ 042 public ResourceHandler getResourceHandler(String source) throws Exception 043 { 044 return getExtensionsIds().stream() 045 .map(this::getExtension) 046 .sorted(Comparator.comparing(ResourceHandlerProvider::getPriority).reversed()) 047 .map(LambdaUtils.wrap(provider -> provider.getResourceHandler(source))) 048 .filter(Objects::nonNull) 049 .findFirst() 050 .orElse(_getDefaultResourceHandler()); 051 } 052 053 private ResourceHandler _getDefaultResourceHandler() throws Exception 054 { 055 ResourceHandler handler = new DefaultResourceHandler(); 056 LifecycleHelper.setupComponent(handler, new SLF4JLoggerAdapter(getLogger()), _context, _cocoonManager, null); 057 if (handler instanceof LogEnabled) 058 { 059 ((LogEnabled) handler).setLogger(getLogger()); 060 } 061 062 return handler; 063 } 064}