001: /*
002: * Copyright 2006 Pentaho Corporation. All rights reserved.
003: * This software was developed by Pentaho Corporation and is provided under the terms
004: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
005: * this file except in compliance with the license. If you need a copy of the license,
006: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
007: * BI Platform. The Initial Developer is Pentaho Corporation.
008: *
009: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
011: * the license for the specific language governing your rights and limitations.
012: */
013: /*
014: * Created on Jul 19, 2005
015: *
016: * TODO To change the template for this generated file go to
017: * Window - Preferences - Java - Code Style - Code Templates
018: */
019: package org.pentaho.plugin.quartz;
020:
021: import java.io.IOException;
022: import java.io.OutputStream;
023: import java.text.DateFormat;
024: import java.util.Date;
025: import java.util.HashMap;
026: import java.util.Locale;
027: import org.apache.commons.logging.Log;
028: import org.apache.commons.logging.LogFactory;
029: import org.pentaho.core.repository.IContentItem;
030: import org.pentaho.core.runtime.IBackgroundExecution;
031: import org.pentaho.core.runtime.IRuntimeContext;
032: import org.pentaho.core.services.BaseRequestHandler;
033: import org.pentaho.core.session.IPentahoSession;
034: import org.pentaho.core.session.StandaloneSession;
035: import org.pentaho.core.solution.HttpSessionParameterProvider;
036: import org.pentaho.core.solution.IOutputHandler;
037: import org.pentaho.core.solution.SimpleOutputHandler;
038: import org.pentaho.core.solution.SimpleParameterProvider;
039: import org.pentaho.core.system.PentahoBase;
040: import org.pentaho.core.system.PentahoSystem;
041: import org.pentaho.core.util.UIUtil;
042: import org.pentaho.messages.Messages;
043: import org.pentaho.messages.util.LocaleHelper;
044: import org.pentaho.util.UUIDUtil;
045: import org.quartz.Job;
046: import org.quartz.JobDataMap;
047: import org.quartz.JobExecutionContext;
048:
049: /**
050: * @author James Dixon
051: *
052: * TODO To change the template for this generated type comment go to Window -
053: * Preferences - Java - Code Style - Code Templates
054: */
055: public class QuartzExecute extends PentahoBase implements Job {
056:
057: /**
058: *
059: */
060: private static final long serialVersionUID = -1897327117890535410L;
061:
062: private static final boolean debug = PentahoSystem.debug;
063:
064: private String logId;
065:
066: private static final Log logger = LogFactory
067: .getLog(QuartzExecute.class);
068:
069: public Log getLogger() {
070: return logger;
071: }
072:
073: public String getLogId() {
074: return logId;
075: }
076:
077: public void execute(JobExecutionContext context) {
078: PentahoSystem.systemEntryPoint();
079: setLoggingLevel(PentahoSystem.loggingLevel);
080: try {
081: LocaleHelper.setLocale(Locale.getDefault());
082: logId = "Schedule:" + context.getJobDetail().getName(); //$NON-NLS-1$
083:
084: Date now = new Date();
085: logger
086: .info(Messages
087: .getString(
088: "QuartzExecute.INFO_TRIGGER_TIME", context.getJobDetail().getName(), DateFormat.getDateInstance().format(now), DateFormat.getTimeInstance().format(now))); //$NON-NLS-1$
089:
090: JobDataMap dataMap = context.getJobDetail().getJobDataMap();
091:
092: // Save the user parameters for use as an agument to
093: // parameterProvider
094: HashMap parameters = new HashMap();
095: String[] keys = dataMap.getKeys();
096: for (int i = 0; i < keys.length; i++) {
097: parameters.put(keys[i], dataMap.get(keys[i]));
098: }
099:
100: // we need to generate a unique session id
101: // String sessionId = "scheduler-"+this.hashCode()+"-"+new
102: // Date().getTime(); //$NON-NLS-1$ //$NON-NLS-2$
103: String sessionId = "scheduler-" + UUIDUtil.getUUIDAsString(); //$NON-NLS-1$
104:
105: StandaloneSession executeSession = new StandaloneSession(
106: context.getJobDetail().getName(), sessionId);
107:
108: String solutionName = dataMap.getString("solution"); //$NON-NLS-1$
109: String actionPath = dataMap.getString("path"); //$NON-NLS-1$
110: String actionName = dataMap.getString("action"); //$NON-NLS-1$
111: String instanceId = null;
112: String processId = this .getClass().getName();
113: IPentahoSession userSession = null;
114: if (solutionName == null) {
115: error(Messages
116: .getErrorString("QuartzExecute.ERROR_0001_SOLUTION_NAME_MISSING")); //$NON-NLS-1$
117: return;
118: }
119: if (actionPath == null) {
120: error(Messages
121: .getErrorString("QuartzExecute.ERROR_0002_ACTION_PATH_MISSING")); //$NON-NLS-1$
122: return;
123: }
124: if (actionName == null) {
125: error(Messages
126: .getErrorString("QuartzExecute.ERROR_0003_ACTION_NAME_MISSING")); //$NON-NLS-1$
127: return;
128: }
129: if (debug) {
130: debug(Messages
131: .getString(
132: "QuartzExecute.DEBUG_EXECUTION_INFO", solutionName + "/" + actionPath + "/" + actionName)); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
133: }
134:
135: boolean backgroundExecution = "true".equals(dataMap.getString(QuartzBackgroundExecutionHelper.BACKGROUND_EXECUTION_FLAG)); //$NON-NLS-1$
136: IOutputHandler outputHandler = null;
137: SimpleParameterProvider parameterProvider = new SimpleParameterProvider(
138: parameters);
139: IBackgroundExecution backgroundExecutionHandler = PentahoSystem
140: .getBackgroundExecutionHandler(executeSession);
141: if (backgroundExecution) {
142: String location = dataMap
143: .getString(QuartzBackgroundExecutionHelper.BACKGROUND_CONTENT_LOCATION_STR);
144: String fileName = dataMap
145: .getString(QuartzBackgroundExecutionHelper.BACKGROUND_CONTENT_GUID_STR);
146: String userName = dataMap
147: .getString(QuartzBackgroundExecutionHelper.BACKGROUND_USER_NAME_STR);
148:
149: userSession = backgroundExecutionHandler
150: .getEffectiveUserSession(userName);
151: // session.setAuthenticated(userName);
152: outputHandler = backgroundExecutionHandler
153: .getContentOutputHandler(location, fileName,
154: solutionName, userSession,
155: parameterProvider);
156:
157: } else {
158: outputHandler = new SimpleOutputHandler(
159: (OutputStream) null, false);
160: // Check to see if the user was authenticated (via the portal) in the JobSchedulerComponent
161: String userName = dataMap.getString("username"); //$NON-NLS-1$
162: if (userName != null) {
163: // Well, we got a valid user name - let's try to use the
164: // background execute component to establish the user
165: userSession = backgroundExecutionHandler
166: .getEffectiveUserSession(userName);
167: } else {
168: // User wasn't authenticated when the job was scheduled - use default behavior from old...
169: userSession = executeSession;
170: }
171: }
172:
173: BaseRequestHandler requestHandler = new BaseRequestHandler(
174: userSession, null, outputHandler,
175: parameterProvider, null);
176: requestHandler.setParameterProvider(
177: HttpSessionParameterProvider.SCOPE_SESSION,
178: new HttpSessionParameterProvider(userSession));
179:
180: requestHandler.setInstanceId(instanceId);
181: requestHandler.setProcessId(processId);
182: requestHandler.setAction(actionPath, actionName);
183: requestHandler.setSolutionName(solutionName);
184:
185: IRuntimeContext rt = null;
186: try {
187: rt = requestHandler.handleActionRequest(0, 0);
188: if (backgroundExecution) {
189: if (!outputHandler.contentDone()) {
190: IContentItem outputContentItem = outputHandler
191: .getOutputContentItem(
192: IOutputHandler.RESPONSE,
193: IOutputHandler.CONTENT, rt
194: .getActionTitle(),
195: null, rt.getSolutionName(), rt
196: .getInstanceId(),
197: "text/html"); //$NON-NLS-1$
198: outputContentItem.setMimeType("text/html"); //$NON-NLS-1$
199: try {
200: if ((rt != null)
201: && (rt.getStatus() == IRuntimeContext.RUNTIME_STATUS_SUCCESS)) {
202: StringBuffer buffer = new StringBuffer();
203: UIUtil.formatSuccessMessage(
204: "text/html", rt, buffer, false); //$NON-NLS-1$
205: OutputStream os = outputContentItem
206: .getOutputStream(actionName);
207: os.write(buffer.toString().getBytes(
208: LocaleHelper
209: .getSystemEncoding()));
210: os.close();
211: } else {
212: // we need an error message...
213: StringBuffer buffer = new StringBuffer();
214: UIUtil
215: .formatFailureMessage(
216: "text/html", rt, buffer, requestHandler.getMessages()); //$NON-NLS-1$
217: OutputStream os = outputContentItem
218: .getOutputStream(actionName);
219: os.write(buffer.toString().getBytes(
220: LocaleHelper
221: .getSystemEncoding()));
222: os.close();
223: }
224: } catch (IOException ex) {
225: logger.error(ex.getLocalizedMessage());
226: }
227: }
228:
229: }
230:
231: IContentItem outputContentItem = outputHandler
232: .getOutputContentItem(IOutputHandler.RESPONSE,
233: IOutputHandler.CONTENT, rt
234: .getSolutionName(), rt
235: .getInstanceId(), "text/html"); //$NON-NLS-1$
236: if (outputContentItem != null) {
237: context
238: .put(
239: QuartzBackgroundExecutionHelper.BACKGROUND_CONTENT_GUID_STR,
240: outputContentItem.getId());
241: }
242: } finally {
243: if (rt != null) {
244: rt.dispose();
245: }
246: }
247: if (debug)
248: logger
249: .debug(Messages
250: .getString(
251: "QuartzExecute.DEBUG_FINISHED_EXECUTION", context.getJobDetail().getName())); //$NON-NLS-1$
252: } finally {
253: PentahoSystem.systemExitPoint();
254: }
255: }
256:
257: }
|