001/*
002 * (C) Copyright 2006-2008 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     bstefanescu
016 *
017 * $Id$
018 */
019
020package org.nuxeo.ecm.webengine;
021
022import java.io.File;
023import java.io.IOException;
024import java.text.ParseException;
025
026import org.apache.commons.logging.Log;
027import org.apache.commons.logging.LogFactory;
028import org.nuxeo.ecm.webengine.rendering.RenderingExtensionDescriptor;
029import org.nuxeo.ecm.webengine.security.GuardDescriptor;
030import org.nuxeo.ecm.webengine.security.PermissionService;
031import org.nuxeo.runtime.RuntimeServiceException;
032import org.nuxeo.runtime.api.Framework;
033import org.nuxeo.runtime.model.ComponentContext;
034import org.nuxeo.runtime.model.ComponentInstance;
035import org.nuxeo.runtime.model.ComponentName;
036import org.nuxeo.runtime.model.DefaultComponent;
037
038/**
039 * TODO remove old WebEngine references and rename WebEngine2 to WebEngine
040 *
041 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
042 */
043public class WebEngineComponent extends DefaultComponent { // implements
044    // ConfigurationChangedListener
045    // {
046
047    public static final ComponentName NAME = new ComponentName(WebEngineComponent.class.getName());
048
049    public static final String RENDERING_EXTENSION_XP = "rendering-extension";
050
051    public static final String RESOURCE_BINDING_XP = "resource";
052
053    public static final String REQUEST_CONFIGURATION_XP = "request-configuration";
054
055    public static final String GUARD_XP = "guard"; // global guards
056
057    public static final String FORM_XP = "form";
058
059    private static final Log log = LogFactory.getLog(WebEngineComponent.class);
060
061    private WebEngine engine;
062
063    @Override
064    public void activate(ComponentContext context) {
065        super.activate(context);
066
067        String webDir = Framework.getProperty("org.nuxeo.ecm.web.root");
068        File root = null;
069        if (webDir != null) {
070            root = new File(webDir);
071        } else {
072            root = new File(Framework.getRuntime().getHome(), "web");
073        }
074        try {
075            root = root.getCanonicalFile();
076        } catch (IOException e) {
077            throw new RuntimeException(e);
078        }
079        log.info("Using web root: " + root);
080
081        engine = new WebEngine(new File(root, "root.war"));
082
083        engine.start();
084    }
085
086    @Override
087    public void deactivate(ComponentContext context) {
088        engine.stop();
089        engine = null;
090        super.deactivate(context);
091    }
092
093    public WebEngine getEngine() {
094        return engine;
095    }
096
097    @Override
098    public void registerContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
099        if (GUARD_XP.equals(extensionPoint)) {
100            GuardDescriptor gd = (GuardDescriptor) contribution;
101            try {
102                PermissionService.getInstance().registerGuard(gd.getId(), gd.getGuard());
103            } catch (ParseException e) {
104                throw new RuntimeException(e);
105            }
106        } else if (RESOURCE_BINDING_XP.equals(extensionPoint)) {
107            engine.addResourceBinding((ResourceBinding) contribution);
108        } else if (extensionPoint.equals(RENDERING_EXTENSION_XP)) {
109            RenderingExtensionDescriptor fed = (RenderingExtensionDescriptor) contribution;
110            try {
111                engine.registerRenderingExtension(fed.name, fed.newInstance());
112            } catch (ReflectiveOperationException e) {
113                throw new RuntimeServiceException(
114                        "Deployment Error. Failed to contribute freemarker template extension: " + fed.name);
115            }
116            // TODO
117            // } else if (extensionPoint.endsWith(FORM_XP)) {
118            // Form form = (Form)contribution;
119            // engine.getFormManager().registerForm(form);
120        } else if (extensionPoint.equals(REQUEST_CONFIGURATION_XP)) {
121            PathDescriptor pd = (PathDescriptor) contribution;
122            engine.getRequestConfiguration().addPathDescriptor(pd);
123        }
124    }
125
126    @Override
127    public void unregisterContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
128        if (GUARD_XP.equals(extensionPoint)) {
129            GuardDescriptor gd = (GuardDescriptor) contribution;
130            PermissionService.getInstance().unregisterGuard(gd.getId());
131        } else if (RESOURCE_BINDING_XP.equals(extensionPoint)) {
132            engine.removeResourceBinding((ResourceBinding) contribution);
133        } else if (extensionPoint.equals(RENDERING_EXTENSION_XP)) {
134            RenderingExtensionDescriptor fed = (RenderingExtensionDescriptor) contribution;
135            engine.unregisterRenderingExtension(fed.name);
136            // TODO
137            // } else if (extensionPoint.endsWith(FORM_XP)) {
138            // Form form = (Form)contribution;
139            // engine.getFormManager().unregisterForm(form.getId());
140        } else if (extensionPoint.equals(REQUEST_CONFIGURATION_XP)) {
141            PathDescriptor pd = (PathDescriptor) contribution;
142            engine.getRequestConfiguration().removePathDescriptor(pd);
143        }
144    }
145
146    @Override
147    public <T> T getAdapter(Class<T> adapter) {
148        if (adapter == WebEngine.class) {
149            return adapter.cast(engine);
150        }
151        return null;
152    }
153
154}