001/*
002 * (C) Copyright 2006-2008 Nuxeo SA (http://nuxeo.com/) and others.
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 *
016 * Contributors:
017 *     bstefanescu
018 *
019 * $Id$
020 */
021
022package org.nuxeo.ecm.webengine;
023
024import java.io.File;
025import java.io.IOException;
026import java.text.ParseException;
027
028import org.apache.commons.logging.Log;
029import org.apache.commons.logging.LogFactory;
030import org.nuxeo.ecm.webengine.rendering.RenderingExtensionDescriptor;
031import org.nuxeo.ecm.webengine.security.GuardDescriptor;
032import org.nuxeo.ecm.webengine.security.PermissionService;
033import org.nuxeo.runtime.RuntimeServiceException;
034import org.nuxeo.runtime.api.Framework;
035import org.nuxeo.runtime.model.ComponentContext;
036import org.nuxeo.runtime.model.ComponentInstance;
037import org.nuxeo.runtime.model.ComponentName;
038import org.nuxeo.runtime.model.DefaultComponent;
039
040/**
041 * TODO remove old WebEngine references and rename WebEngine2 to WebEngine
042 *
043 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
044 */
045public class WebEngineComponent extends DefaultComponent { // implements
046    // ConfigurationChangedListener
047    // {
048
049    public static final ComponentName NAME = new ComponentName(WebEngineComponent.class.getName());
050
051    public static final String RENDERING_EXTENSION_XP = "rendering-extension";
052
053    public static final String RESOURCE_BINDING_XP = "resource";
054
055    public static final String REQUEST_CONFIGURATION_XP = "request-configuration";
056
057    public static final String GUARD_XP = "guard"; // global guards
058
059    public static final String FORM_XP = "form";
060
061    private static final Log log = LogFactory.getLog(WebEngineComponent.class);
062
063    private WebEngine engine;
064
065    @Override
066    public void activate(ComponentContext context) {
067        super.activate(context);
068
069        String webDir = Framework.getProperty("org.nuxeo.ecm.web.root");
070        File root = null;
071        if (webDir != null) {
072            root = new File(webDir);
073        } else {
074            root = new File(Framework.getRuntime().getHome(), "web");
075        }
076        try {
077            root = root.getCanonicalFile();
078        } catch (IOException e) {
079            throw new RuntimeException(e);
080        }
081        log.info("Using web root: " + root);
082
083        engine = new WebEngine(new File(root, "root.war"));
084
085        engine.start();
086    }
087
088    @Override
089    public void deactivate(ComponentContext context) {
090        engine.stop();
091        engine = null;
092        super.deactivate(context);
093    }
094
095    public WebEngine getEngine() {
096        return engine;
097    }
098
099    @Override
100    public void registerContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
101        if (GUARD_XP.equals(extensionPoint)) {
102            GuardDescriptor gd = (GuardDescriptor) contribution;
103            try {
104                PermissionService.getInstance().registerGuard(gd.getId(), gd.getGuard());
105            } catch (ParseException e) {
106                throw new RuntimeException(e);
107            }
108        } else if (RESOURCE_BINDING_XP.equals(extensionPoint)) {
109            engine.addResourceBinding((ResourceBinding) contribution);
110        } else if (extensionPoint.equals(RENDERING_EXTENSION_XP)) {
111            RenderingExtensionDescriptor fed = (RenderingExtensionDescriptor) contribution;
112            try {
113                engine.registerRenderingExtension(fed.name, fed.newInstance());
114            } catch (ReflectiveOperationException e) {
115                throw new RuntimeServiceException(
116                        "Deployment Error. Failed to contribute freemarker template extension: " + fed.name);
117            }
118            // TODO
119            // } else if (extensionPoint.endsWith(FORM_XP)) {
120            // Form form = (Form)contribution;
121            // engine.getFormManager().registerForm(form);
122        } else if (extensionPoint.equals(REQUEST_CONFIGURATION_XP)) {
123            log.warn("Extension point " + REQUEST_CONFIGURATION_XP + " is obsolete since 8.4, transactions are always active");
124        }
125    }
126
127    @Override
128    public void unregisterContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
129        if (GUARD_XP.equals(extensionPoint)) {
130            GuardDescriptor gd = (GuardDescriptor) contribution;
131            PermissionService.getInstance().unregisterGuard(gd.getId());
132        } else if (RESOURCE_BINDING_XP.equals(extensionPoint)) {
133            engine.removeResourceBinding((ResourceBinding) contribution);
134        } else if (extensionPoint.equals(RENDERING_EXTENSION_XP)) {
135            RenderingExtensionDescriptor fed = (RenderingExtensionDescriptor) contribution;
136            engine.unregisterRenderingExtension(fed.name);
137            // TODO
138            // } else if (extensionPoint.endsWith(FORM_XP)) {
139            // Form form = (Form)contribution;
140            // engine.getFormManager().unregisterForm(form.getId());
141        }
142    }
143
144    @Override
145    public <T> T getAdapter(Class<T> adapter) {
146        if (adapter == WebEngine.class) {
147            return adapter.cast(engine);
148        }
149        return null;
150    }
151
152}