001/*
002 * (C) Copyright 2015 Nuxeo SA (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-2.1.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 *      Vladimir Pasquier <vpasquier@nuxeo.com>
016 */
017package org.nuxeo.ecm.automation.context;
018
019import java.util.Arrays;
020import java.util.Map;
021
022import org.apache.commons.logging.Log;
023import org.apache.commons.logging.LogFactory;
024import org.nuxeo.runtime.model.SimpleContributionRegistry;
025
026/**
027 * Registry for {@link ContextHelperDescriptor} descriptors.
028 *
029 * @since 7.3
030 */
031public class ContextHelperRegistry extends SimpleContributionRegistry<ContextHelperDescriptor> {
032
033    private static final Log log = LogFactory.getLog(ContextHelperRegistry.class);
034
035    public static final String[] RESERVED_VAR_NAMES = { "CurrentDate", "Context", "ctx", "This", "Session",
036            "CurrentUser", "currentUser", "Env", "Document", "currentDocument", "Documents", "params", "input" };
037
038    @Override
039    public synchronized void addContribution(ContextHelperDescriptor contextHelperDescriptor) {
040        String id = contextHelperDescriptor.getId();
041        ContextHelper contextHelper = contextHelperDescriptor.getContextHelper();
042        if (currentContribs.keySet().contains(id)) {
043            log.warn("The context helper id/alias '" + id + " is overridden by the following helper: "
044                    + contextHelper.toString());
045        }
046        if (Arrays.asList(RESERVED_VAR_NAMES).contains(id)) {
047            log.warn("The context helper '" + contextHelper.toString() + "' cannot be registered:'" + id
048                    + "' is reserved. Please use another one. The Nuxeo reserved " + "aliases are"
049                    + RESERVED_VAR_NAMES.toString());
050            return;
051        }
052        super.addContribution(contextHelperDescriptor);
053    }
054
055    @Override
056    public String getContributionId(ContextHelperDescriptor metadataMappingDescriptor) {
057        return metadataMappingDescriptor.getId();
058    }
059
060    public Map<String, ContextHelperDescriptor> getContextHelperDescriptors() {
061        return currentContribs;
062    }
063
064}