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.cocoon;
017
018import org.apache.avalon.framework.component.ComponentException;
019import org.apache.avalon.framework.component.ComponentManager;
020import org.apache.cocoon.Constants;
021import org.apache.cocoon.environment.Context;
022import org.apache.commons.lang.BooleanUtils;
023
024import org.ametys.runtime.plugin.PluginsManager;
025import org.ametys.runtime.plugin.component.PluginsComponentManager;
026import org.ametys.runtime.servlet.RuntimeConfig;
027import org.ametys.runtime.workspace.WorkspaceManager;
028
029/**
030 * Own TreeProcessor implementation used to initialize plugin stuff.<br>
031 * This allows plugins to access ComponentManager and sitemaps components to access plugins.
032 */
033public class TreeProcessor extends org.apache.cocoon.components.treeprocessor.TreeProcessor
034{
035    private PluginsComponentManager _pluginCM;
036    
037    @Override
038    public void compose(ComponentManager componentManager) throws ComponentException
039    {
040        try
041        {
042            Context ctx = (Context) context.get(Constants.CONTEXT_ENVIRONMENT_CONTEXT);
043            String contextPath = ctx.getRealPath("/");
044            
045            // WorkspaceManager loading
046            WorkspaceManager.getInstance().init(RuntimeConfig.getInstance().getExcludedWorkspaces(), contextPath);
047            
048            boolean forceSafeMode = BooleanUtils.toBoolean((Boolean) ctx.getAttribute("org.ametys.runtime.forceSafeMode"));
049            PluginsComponentManager pluginCM = PluginsManager.getInstance().init(componentManager, context, contextPath, forceSafeMode);
050            ctx.removeAttribute("org.ametys.runtime.forceSafeMode");
051            
052            // Effective substitution of the Cocoon CM
053            super.compose(pluginCM);
054            _pluginCM = pluginCM;
055            
056            // Set the new ComponentManager in the servlet context so that it can be retrieved bt the Servlet 
057            ctx.setAttribute("PluginsComponentManager", pluginCM);
058        }
059        catch (ComponentException | RuntimeException e)
060        {
061            throw e;
062        }
063        catch (Exception e)
064        {
065            throw new ComponentException(ROLE, "Unable to initialize the ComponentManager", e);
066        }
067    }
068    
069    @Override
070    public void dispose()
071    {
072        super.dispose();
073        
074        if (_pluginCM != null)
075        {
076            _pluginCM.dispose();
077            _pluginCM = null;
078        }
079    }
080}