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 org.apache.avalon.framework.context.Context;
019import org.apache.avalon.framework.context.ContextException;
020import org.apache.avalon.framework.context.Contextualizable;
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023import org.apache.avalon.framework.service.Serviceable;
024import org.apache.cocoon.components.LifecycleHelper;
025import org.apache.cocoon.util.log.SLF4JLoggerAdapter;
026
027import org.ametys.runtime.plugin.component.AbstractLogEnabled;
028import org.ametys.runtime.plugin.component.LogEnabled;
029
030/**
031 * Abstract superclass for {@link ResourceHandlerProvider} for helping {@link ResourceHandler} creation.
032 */
033public abstract class AbstractResourceHandlerProvider extends AbstractLogEnabled implements ResourceHandlerProvider, Serviceable, Contextualizable
034{
035    /** The application context */
036    protected Context _context;
037    
038    /** The service manager */
039    protected ServiceManager _manager;
040    
041    public void contextualize(Context context) throws ContextException
042    {
043        _context = context;
044    }
045    
046    public void service(ServiceManager manager) throws ServiceException
047    {
048        _manager = manager;
049    }
050    
051    /**
052     * Setup the handler right after its creation
053     * @param handler the handler.
054     * @return the initialized {@link ResourceHandler}.
055     * @throws Exception if something goes wrong during initialization
056     */
057    protected ResourceHandler setup(ResourceHandler handler) throws Exception
058    {
059        LifecycleHelper.setupComponent(handler, new SLF4JLoggerAdapter(getLogger()), _context, _manager, null);
060        if (handler instanceof LogEnabled)
061        {
062            ((LogEnabled) handler).setLogger(getLogger());
063        }
064        
065        return handler;
066    }
067}