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.impl.schedule;
017
018import java.io.IOException;
019import java.util.Locale;
020import java.util.Map;
021
022import javax.script.ScriptException;
023
024import org.apache.avalon.framework.service.ServiceException;
025import org.apache.avalon.framework.service.ServiceManager;
026import org.quartz.JobDataMap;
027import org.quartz.JobExecutionContext;
028import org.slf4j.Logger;
029
030import org.ametys.core.schedule.Schedulable;
031import org.ametys.core.user.UserIdentity;
032import org.ametys.plugins.core.schedule.Scheduler;
033import org.ametys.plugins.core.ui.script.AsyncScriptHandler;
034import org.ametys.plugins.core.ui.script.ScriptHandler;
035
036import jakarta.mail.MessagingException;
037
038/**
039 * A {@link Schedulable} job for executing scripts.
040 */
041public class ScriptSchedulable extends AbstractStaticSchedulable
042{
043    /** The key for the script (as string) to execute */
044    public static final String SCRIPT_KEY = "script";
045    /** The key for the recipient of the report mail */
046    public static final String RECIPIENT_KEY = "recipient";
047    /** The key for the workspace */
048    public static final String WORKSPACE_KEY = "workspace";
049    
050    private static final String __JOBDATAMAP_SCRIPT_KEY = Scheduler.PARAM_VALUES_PREFIX + SCRIPT_KEY;
051    private static final String __JOBDATAMAP_RECIPIENT_KEY = Scheduler.PARAM_VALUES_PREFIX + RECIPIENT_KEY;
052    private static final String __JOBDATAMAP_WORKSPACE_KEY = Scheduler.PARAM_VALUES_PREFIX + WORKSPACE_KEY;
053    
054    /** The script handler */
055    protected ScriptHandler _scriptHandler;
056    /** The async script handler */
057    protected AsyncScriptHandler _asyncScriptHandler;
058    
059    @Override
060    public void service(ServiceManager manager) throws ServiceException
061    {
062        super.service(manager);
063        _scriptHandler = (ScriptHandler) manager.lookup(ScriptHandler.ROLE);
064        
065        if (manager.hasService(AsyncScriptHandler.COMPONENT_ROLE))
066        {
067            _asyncScriptHandler = (AsyncScriptHandler) manager.lookup(AsyncScriptHandler.COMPONENT_ROLE);
068            _scriptHandler = _asyncScriptHandler;
069        }
070    }
071    
072    @Override
073    public void execute(JobExecutionContext context) throws Exception
074    {
075        JobDataMap jobDataMap = context.getJobDetail().getJobDataMap();
076        String script = (String) jobDataMap.get(__JOBDATAMAP_SCRIPT_KEY);
077        String recipient = (String) jobDataMap.get(__JOBDATAMAP_RECIPIENT_KEY);
078        String workspace = (String) jobDataMap.get(__JOBDATAMAP_WORKSPACE_KEY);
079        UserIdentity userIdentity = UserIdentity.stringToUserIdentity(jobDataMap.getString(Scheduler.KEY_RUNNABLE_USERIDENTITY));
080        
081        Map<String, Object> scriptResults = _executeScript(script, workspace);
082        _sendMail(scriptResults, recipient, userIdentity);
083    }
084    
085    private Map<String, Object> _executeScript(String script, String workspace) throws ScriptException
086    {
087        return _scriptHandler.executeScript("function main() { \n " + script + " \n }", null, workspace);
088    }
089    
090    private void _sendMail(Map<String, Object> scriptResults, String recipient, UserIdentity userIdentity) throws MessagingException, IOException
091    {
092        Locale locale = Locale.getDefault(); // FIXME cannot have the user locale at this time
093        Logger logger = getLogger();
094        
095        if (_asyncScriptHandler != null)
096        {
097            _asyncScriptHandler.sendReportMail(scriptResults, userIdentity, recipient, locale, logger);
098        }
099    }
100}