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