001/*
002 *  Copyright 2010 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.web;
017
018import java.util.Iterator;
019import java.util.Map;
020
021import org.apache.avalon.framework.configuration.Configuration;
022import org.apache.avalon.framework.configuration.ConfigurationException;
023import org.apache.avalon.framework.logger.AbstractLogEnabled;
024import org.apache.avalon.framework.service.ServiceException;
025import org.apache.avalon.framework.service.ServiceManager;
026import org.apache.avalon.framework.service.Serviceable;
027import org.apache.avalon.framework.thread.ThreadSafe;
028import org.apache.cocoon.components.modules.input.InputModule;
029
030import org.ametys.plugins.repository.UnknownAmetysObjectException;
031import org.ametys.web.repository.site.Site;
032import org.ametys.web.repository.site.SiteManager;
033
034/**
035 * Input module for getting site configuration values.
036 */
037public class SiteConfigurationInputModule extends AbstractLogEnabled implements InputModule, ThreadSafe, Serviceable
038{
039    private SiteManager _siteManager;
040    
041    @Override
042    public void service(ServiceManager smanager) throws ServiceException
043    {
044        _siteManager = (SiteManager) smanager.lookup(SiteManager.ROLE);
045    }
046    
047    @Override
048    public Object getAttribute(String name, Configuration modeConf, Map objectModel) throws ConfigurationException
049    {
050        int index = name.indexOf(':');
051        if (index == -1)
052        {
053            return null;
054        }
055
056        String siteName = name.substring(0, index);
057        try
058        {
059            Site site = _siteManager.getSite(siteName);
060            String metadataName = name.substring(index + 1);
061            if ("url".equals(metadataName))
062            {
063                return site.getUrl();
064            }
065            else
066            {
067                return site.getValue(name.substring(index + 1));
068            }
069        }
070        catch (UnknownAmetysObjectException e)
071        {
072            String message = "'" + name + "' was asked to input module, but the site '" + siteName + "' cannot be found.";
073            getLogger().error(message, e);
074            throw new IllegalStateException(message);
075        }
076    }
077
078    @Override
079    public Iterator getAttributeNames(Configuration modeConf, Map objectModel) throws ConfigurationException
080    {
081        return null;
082    }
083
084    @Override
085    public Object[] getAttributeValues(String name, Configuration modeConf, Map objectModel) throws ConfigurationException
086    {
087        return new Object[] {getAttribute(name, modeConf, objectModel)};
088    }
089}