1 13 19 package org.pentaho.plugin.quartz; 20 21 import java.io.IOException ; 22 import java.io.OutputStream ; 23 import java.text.DateFormat ; 24 import java.util.Date ; 25 import java.util.HashMap ; 26 import java.util.Locale ; 27 import org.apache.commons.logging.Log; 28 import org.apache.commons.logging.LogFactory; 29 import org.pentaho.core.repository.IContentItem; 30 import org.pentaho.core.runtime.IBackgroundExecution; 31 import org.pentaho.core.runtime.IRuntimeContext; 32 import org.pentaho.core.services.BaseRequestHandler; 33 import org.pentaho.core.session.IPentahoSession; 34 import org.pentaho.core.session.StandaloneSession; 35 import org.pentaho.core.solution.HttpSessionParameterProvider; 36 import org.pentaho.core.solution.IOutputHandler; 37 import org.pentaho.core.solution.SimpleOutputHandler; 38 import org.pentaho.core.solution.SimpleParameterProvider; 39 import org.pentaho.core.system.PentahoBase; 40 import org.pentaho.core.system.PentahoSystem; 41 import org.pentaho.core.util.UIUtil; 42 import org.pentaho.messages.Messages; 43 import org.pentaho.messages.util.LocaleHelper; 44 import org.pentaho.util.UUIDUtil; 45 import org.quartz.Job; 46 import org.quartz.JobDataMap; 47 import org.quartz.JobExecutionContext; 48 49 55 public class QuartzExecute extends PentahoBase implements Job { 56 57 60 private static final long serialVersionUID = -1897327117890535410L; 61 62 private static final boolean debug = PentahoSystem.debug; 63 64 private String logId; 65 66 private static final Log logger = LogFactory.getLog(QuartzExecute.class); 67 68 public Log getLogger() { 69 return logger; 70 } 71 72 public String getLogId() { 73 return logId; 74 } 75 76 public void execute(JobExecutionContext context) { 77 PentahoSystem.systemEntryPoint(); 78 try { 79 LocaleHelper.setLocale(Locale.getDefault()); 80 logId = "Schedule:" + context.getJobDetail().getName(); 82 Date now = new Date (); 83 logger.info(Messages.getString("QuartzExecute.INFO_TRIGGER_TIME", context.getJobDetail().getName(), DateFormat.getDateInstance().format(now), DateFormat.getTimeInstance().format(now))); 85 JobDataMap dataMap = context.getJobDetail().getJobDataMap(); 86 87 HashMap parameters = new HashMap (); 90 String [] keys = dataMap.getKeys(); 91 for (int i = 0; i < keys.length; i++) { 92 parameters.put(keys[i], dataMap.get(keys[i])); 93 } 94 95 String sessionId = "scheduler-" + UUIDUtil.getUUIDAsString(); 100 StandaloneSession executeSession = new StandaloneSession(context.getJobDetail().getName(), sessionId); 101 102 String solutionName = dataMap.getString("solution"); String actionPath = dataMap.getString("path"); String actionName = dataMap.getString("action"); String instanceId = null; 106 String processId = this.getClass().getName(); 107 IPentahoSession userSession = null; 108 if (solutionName == null) { 109 error(Messages.getErrorString("QuartzExecute.ERROR_0001_SOLUTION_NAME_MISSING")); return; 111 } 112 if (actionPath == null) { 113 error(Messages.getErrorString("QuartzExecute.ERROR_0002_ACTION_PATH_MISSING")); return; 115 } 116 if (actionName == null) { 117 error(Messages.getErrorString("QuartzExecute.ERROR_0003_ACTION_NAME_MISSING")); return; 119 } 120 if (debug) { 121 if (debug) 122 debug(Messages.getString("QuartzExecute.DEBUG_EXECUTION_INFO", solutionName + "/" + actionPath + "/" + actionName)); } 124 125 boolean backgroundExecution = "true".equals(dataMap.getString(QuartzBackgroundExecutionHelper.BACKGROUND_EXECUTION_FLAG)); IOutputHandler outputHandler = null; 127 SimpleParameterProvider parameterProvider = new SimpleParameterProvider(parameters); 128 IBackgroundExecution backgroundExecutionHandler = PentahoSystem.getBackgroundExecutionHandler(executeSession); 129 if (backgroundExecution) { 130 String location = dataMap.getString(QuartzBackgroundExecutionHelper.BACKGROUND_CONTENT_LOCATION_STR); 131 String fileName = dataMap.getString(QuartzBackgroundExecutionHelper.BACKGROUND_CONTENT_GUID_STR); 132 String userName = dataMap.getString(QuartzBackgroundExecutionHelper.BACKGROUND_USER_NAME_STR); 133 134 userSession = backgroundExecutionHandler.getEffectiveUserSession(userName); 135 outputHandler = backgroundExecutionHandler.getContentOutputHandler(location, fileName, solutionName, userSession, parameterProvider); 137 138 } else { 139 outputHandler = new SimpleOutputHandler(null, false); 140 String userName = dataMap.getString("username"); if (userName != null) { 143 userSession = backgroundExecutionHandler.getEffectiveUserSession(userName); 146 } else { 147 userSession = executeSession; 149 } 150 } 151 152 BaseRequestHandler requestHandler = new BaseRequestHandler(userSession, null, outputHandler, parameterProvider, null); 153 requestHandler.setParameterProvider("session", new HttpSessionParameterProvider(userSession)); 155 requestHandler.setInstanceId(instanceId); 156 requestHandler.setProcessId(processId); 157 requestHandler.setAction(actionPath, actionName); 158 requestHandler.setSolutionName(solutionName); 159 160 IRuntimeContext rt = null; 161 try { 162 rt = requestHandler.handleActionRequest(0, 0); 163 if (backgroundExecution) { 164 if (!outputHandler.contentDone()) { 165 IContentItem outputContentItem = outputHandler.getOutputContentItem(IOutputHandler.RESPONSE, IOutputHandler.CONTENT, rt.getActionTitle(), null); 166 outputContentItem.setMimeType("text/html"); try { 168 if ((rt != null) && (rt.getStatus() == IRuntimeContext.RUNTIME_STATUS_SUCCESS)) { 169 StringBuffer buffer = new StringBuffer (); 170 UIUtil.formatSuccessMessage("text/html", rt, buffer, false); OutputStream os = outputContentItem.getOutputStream(actionName); 172 os.write(buffer.toString().getBytes(LocaleHelper.getSystemEncoding())); 173 os.close(); 174 } else { 175 StringBuffer buffer = new StringBuffer (); 177 UIUtil.formatFailureMessage("text/html", rt, buffer, requestHandler.getMessages()); OutputStream os = outputContentItem.getOutputStream(actionName); 179 os.write(buffer.toString().getBytes(LocaleHelper.getSystemEncoding())); 180 os.close(); 181 } 182 } catch (IOException ex) { 183 error(ex.getLocalizedMessage()); 184 } 185 } 186 187 } 188 189 IContentItem outputContentItem = outputHandler.getOutputContentItem(IOutputHandler.RESPONSE, IOutputHandler.CONTENT); 190 if (outputContentItem != null) { 191 context.put(QuartzBackgroundExecutionHelper.BACKGROUND_CONTENT_GUID_STR, outputContentItem.getId() ); 192 } 193 } finally { 194 if (rt != null) { 195 rt.dispose(); 196 } 197 } 198 if (debug) 199 logger.debug(Messages.getString("QuartzExecute.DEBUG_FINISHED_EXECUTION", context.getJobDetail().getName())); } finally { 201 PentahoSystem.systemExitPoint(); 202 } 203 } 204 205 } 206 | Popular Tags |