001/*
002 *  Copyright 2012 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.runtime.plugin.component;
017
018import org.apache.avalon.framework.component.Component;
019import org.apache.avalon.framework.component.ComponentException;
020import org.apache.avalon.framework.component.ComponentSelector;
021
022/**
023 * Bridge between the plugin stuff and Cocoon ComponentSelectors.<br>
024 * The aim is to provide a way to declare InputModules, DataSources, SourceFactories, ... at the plugin level.<br>
025 * Selectable components are declared as extensions to this extension point and may be used as normal Cocoon components.
026 * @param <T> the type of the managed extensions. Must extends Component to be compatible with the ComponentSelector contract
027 */
028public abstract class AbstractSelectorExtensionPoint<T extends Component> extends AbstractThreadSafeComponentExtensionPoint<T> implements ParentAware, ComponentSelector
029{
030    private ComponentSelector _parentSelector;
031    
032    public Component select(Object hint) throws ComponentException
033    {
034        if (!(hint instanceof String))
035        {
036            throw new IllegalArgumentException("This kind of selector only accepts Strings as hint values.");
037        }
038        
039        String id = (String) hint;
040        
041        T extension = getExtension(id);
042        
043        if (extension != null)
044        {
045            return extension;
046        }
047        else if (_parentSelector != null)
048        {
049            return _parentSelector.select(id);
050        }
051        
052        throw new ComponentException(id, "Could not find component");
053    }
054    
055    public boolean hasComponent(Object hint)
056    {
057        boolean exists = hasExtension((String) hint);
058       
059        if (!exists && _parentSelector != null) 
060        {
061            exists = _parentSelector.hasComponent(hint);
062        }
063        
064        return exists;
065    }
066
067    @SuppressWarnings("unchecked")
068    public void release(Component component)
069    {
070        if (!_manager.hasComponent((T) component) && _parentSelector != null)
071        {
072            _parentSelector.release(component);
073        } 
074    }
075
076    public void setParent(Object parentComponent)
077    {
078        _parentSelector = (ComponentSelector) parentComponent;
079    }
080}