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