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