001/*
002 *  Copyright 2016 Anyware Services
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 */
016package org.ametys.plugins.core.ui.script;
017
018import java.util.HashMap;
019import java.util.Map;
020
021import org.apache.avalon.framework.context.Context;
022import org.apache.avalon.framework.context.ContextException;
023import org.apache.avalon.framework.context.Contextualizable;
024import org.apache.avalon.framework.service.ServiceException;
025import org.apache.avalon.framework.service.ServiceManager;
026
027import org.ametys.runtime.i18n.I18nizableText;
028
029import com.google.common.collect.ImmutableMap;
030
031/**
032 * Runtime default script binding, provides default variables to the script tool, and a configuration file for the functions. 
033 */
034public class RuntimeScriptBinding extends StaticConfigurableScriptBinding implements Contextualizable
035{
036    private ServiceManager _manager;
037    private Context _context;
038
039    public void contextualize(Context context) throws ContextException
040    {
041        _context = context;
042    }
043    
044    @Override
045    public void service(ServiceManager manager) throws ServiceException
046    {
047        super.service(manager);
048
049        _manager = manager;
050    }
051    
052    @Override
053    public Map<String, Object> getVariables()
054    {
055        Map<String, Object> variables = new HashMap<>();
056        variables.put("serviceManager", _manager);
057        variables.put("sourceResolver", _sourceResolver);
058        variables.put("avalonContext", _context);
059        return variables;
060    }
061    
062    @Override
063    public Map<String, I18nizableText> getVariablesDescriptions()
064    {
065        return ImmutableMap.of("serviceManager", new I18nizableText("plugin.core-ui", "PLUGINS_CORE_UI_SCRIPT_VAR_SERVICEMANAGER"),
066                               "sourceResolver", new I18nizableText("plugin.core-ui", "PLUGINS_CORE_UI_SCRIPT_VAR_RESOLVER"),
067                               "avalonContext", new I18nizableText("plugin.core-ui", "PLUGINS_CORE_UI_SCRIPT_VAR_AVALONCONTEXT"));
068    }
069
070}