001/*
002 * (C) Copyright 2015-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 *     Thierry Delprat <tdelprat@nuxeo.com>
018 *     Stephane Lacoin <slacoin@nuxeo.com>
019 *     Vladimir Pasquier <vpasquier@nuxeo.com>
020 */
021package org.nuxeo.automation.scripting.internals;
022
023import org.apache.commons.logging.Log;
024import org.apache.commons.logging.LogFactory;
025import org.nuxeo.automation.scripting.api.AutomationScriptingService;
026import org.nuxeo.ecm.automation.AutomationService;
027import org.nuxeo.runtime.api.Framework;
028import org.nuxeo.runtime.model.ComponentContext;
029import org.nuxeo.runtime.model.ComponentInstance;
030import org.nuxeo.runtime.model.DefaultComponent;
031import org.nuxeo.runtime.services.config.ConfigurationService;
032
033/**
034 * @since 7.2
035 */
036public class AutomationScriptingComponent extends DefaultComponent {
037
038    private static final Log log = LogFactory.getLog(AutomationScriptingComponent.class);
039
040    protected final AutomationScriptingServiceImpl service = new AutomationScriptingServiceImpl();
041
042    protected final AutomationScriptingRegistry registry = new AutomationScriptingRegistry();
043
044    @Override
045    public void activate(ComponentContext context) {
046        registry.automation = Framework.getService(AutomationService.class);
047        registry.scripting = service;
048    }
049
050    @Override
051    public void start(ComponentContext context) {
052        boolean inlinedContext = Framework.getService(ConfigurationService.class)
053                                          .isBooleanPropertyTrue("nuxeo.automation.scripting.inline-context-in-params");
054
055        service.paramsInjector = AutomationScriptingParamsInjector.newInstance(inlinedContext);
056    }
057
058    @Override
059    public void registerContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
060        if (contribution instanceof ScriptingOperationDescriptor) {
061            registry.addContribution((ScriptingOperationDescriptor) contribution);
062        } else {
063            log.error("Unknown extension point " + extensionPoint);
064        }
065    }
066
067    @Override
068    public void unregisterContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
069        if (contribution instanceof ScriptingOperationDescriptor) {
070            registry.removeContribution((ScriptingOperationDescriptor) contribution);
071        } else {
072            log.error("Unknown extension point " + extensionPoint);
073        }
074    }
075
076    @Override
077    public <T> T getAdapter(Class<T> adapter) {
078        if (adapter.isAssignableFrom(AutomationScriptingService.class)) {
079            return adapter.cast(service);
080        }
081        return super.getAdapter(adapter);
082    }
083
084}