001/*
002 *  Copyright 2011 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.renderingcontext;
017
018import java.util.Collections;
019import java.util.Iterator;
020import java.util.Map;
021
022import org.apache.avalon.framework.configuration.Configuration;
023import org.apache.avalon.framework.configuration.ConfigurationException;
024import org.apache.avalon.framework.logger.AbstractLogEnabled;
025import org.apache.avalon.framework.service.ServiceException;
026import org.apache.avalon.framework.service.ServiceManager;
027import org.apache.avalon.framework.service.Serviceable;
028import org.apache.avalon.framework.thread.ThreadSafe;
029import org.apache.cocoon.components.modules.input.InputModule;
030
031/**
032 * Input module for getting the current rendering context.<p>
033 * Possible values are:
034 * <dl>
035 *  <dt>back
036 *  <dd>page rendering is done for the back-office
037 *  <dt>preview
038 *  <dd>preview of page rendering for the front-office
039 *  <dt>site
040 *  <dd>page rendering for the front-office
041 * </dl>
042 */
043public class RenderingContextInputModule extends AbstractLogEnabled implements InputModule, ThreadSafe, Serviceable
044{
045    private RenderingContextHandler _handler;
046    
047    @Override
048    public void service(ServiceManager manager) throws ServiceException
049    {
050        _handler = (RenderingContextHandler) manager.lookup(RenderingContextHandler.ROLE);
051    }
052    
053    @Override
054    public Object getAttribute(String name, Configuration modeConf, Map objectModel) throws ConfigurationException
055    {
056        RenderingContext context = _handler.getRenderingContext();
057
058        if (context != null)
059        {
060            return context;
061        }
062        
063        return RenderingContext.BACK;
064    }
065
066    @Override
067    public Iterator getAttributeNames(Configuration modeConf, Map objectModel) throws ConfigurationException
068    {
069        return Collections.singleton("context").iterator();
070    }
071
072    @Override
073    public Object[] getAttributeValues(String name, Configuration modeConf, Map objectModel) throws ConfigurationException
074    {
075        return new Object[] {getAttribute(name, modeConf, objectModel)};
076    }
077}