KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pentaho > plugin > quartz > QuartzExecute


1 /*
2  * Copyright 2006 Pentaho Corporation. All rights reserved.
3  * This software was developed by Pentaho Corporation and is provided under the terms
4  * of the Mozilla Public License, Version 1.1, or any later version. You may not use
5  * this file except in compliance with the license. If you need a copy of the license,
6  * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
7  * BI Platform. The Initial Developer is Pentaho Corporation.
8  *
9  * Software distributed under the Mozilla Public License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
11  * the license for the specific language governing your rights and limitations.
12 */

13 /*
14  * Created on Jul 19, 2005
15  *
16  * TODO To change the template for this generated file go to
17  * Window - Preferences - Java - Code Style - Code Templates
18  */

19 package org.pentaho.plugin.quartz;
20
21 import java.io.IOException JavaDoc;
22 import java.io.OutputStream JavaDoc;
23 import java.text.DateFormat JavaDoc;
24 import java.util.Date JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Locale JavaDoc;
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 /**
50  * @author James Dixon
51  *
52  * TODO To change the template for this generated type comment go to Window -
53  * Preferences - Java - Code Style - Code Templates
54  */

55 public class QuartzExecute extends PentahoBase implements Job {
56
57     /**
58      *
59      */

60     private static final long serialVersionUID = -1897327117890535410L;
61
62     private static final boolean debug = PentahoSystem.debug;
63
64     private String JavaDoc 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 JavaDoc 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(); //$NON-NLS-1$
81

82             Date JavaDoc now = new Date JavaDoc();
83             logger.info(Messages.getString("QuartzExecute.INFO_TRIGGER_TIME", context.getJobDetail().getName(), DateFormat.getDateInstance().format(now), DateFormat.getTimeInstance().format(now))); //$NON-NLS-1$
84

85             JobDataMap dataMap = context.getJobDetail().getJobDataMap();
86
87             // Save the user parameters for use as an agument to
88
// parameterProvider
89
HashMap JavaDoc parameters = new HashMap JavaDoc();
90             String JavaDoc[] keys = dataMap.getKeys();
91             for (int i = 0; i < keys.length; i++) {
92                 parameters.put(keys[i], dataMap.get(keys[i]));
93             }
94
95             // we need to generate a unique session id
96
// String sessionId = "scheduler-"+this.hashCode()+"-"+new
97
// Date().getTime(); //$NON-NLS-1$ //$NON-NLS-2$
98
String JavaDoc sessionId = "scheduler-" + UUIDUtil.getUUIDAsString(); //$NON-NLS-1$
99

100             StandaloneSession executeSession = new StandaloneSession(context.getJobDetail().getName(), sessionId);
101
102             String JavaDoc solutionName = dataMap.getString("solution"); //$NON-NLS-1$
103
String JavaDoc actionPath = dataMap.getString("path"); //$NON-NLS-1$
104
String JavaDoc actionName = dataMap.getString("action"); //$NON-NLS-1$
105
String JavaDoc instanceId = null;
106             String JavaDoc processId = this.getClass().getName();
107             IPentahoSession userSession = null;
108             if (solutionName == null) {
109                 error(Messages.getErrorString("QuartzExecute.ERROR_0001_SOLUTION_NAME_MISSING")); //$NON-NLS-1$
110
return;
111             }
112             if (actionPath == null) {
113                 error(Messages.getErrorString("QuartzExecute.ERROR_0002_ACTION_PATH_MISSING")); //$NON-NLS-1$
114
return;
115             }
116             if (actionName == null) {
117                 error(Messages.getErrorString("QuartzExecute.ERROR_0003_ACTION_NAME_MISSING")); //$NON-NLS-1$
118
return;
119             }
120             if (debug) {
121                 if (debug)
122                     debug(Messages.getString("QuartzExecute.DEBUG_EXECUTION_INFO", solutionName + "/" + actionPath + "/" + actionName)); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
123
}
124
125             boolean backgroundExecution = "true".equals(dataMap.getString(QuartzBackgroundExecutionHelper.BACKGROUND_EXECUTION_FLAG)); //$NON-NLS-1$
126
IOutputHandler outputHandler = null;
127             SimpleParameterProvider parameterProvider = new SimpleParameterProvider(parameters);
128             IBackgroundExecution backgroundExecutionHandler = PentahoSystem.getBackgroundExecutionHandler(executeSession);
129             if (backgroundExecution) {
130               String JavaDoc location = dataMap.getString(QuartzBackgroundExecutionHelper.BACKGROUND_CONTENT_LOCATION_STR);
131               String JavaDoc fileName = dataMap.getString(QuartzBackgroundExecutionHelper.BACKGROUND_CONTENT_GUID_STR);
132               String JavaDoc userName = dataMap.getString(QuartzBackgroundExecutionHelper.BACKGROUND_USER_NAME_STR);
133
134               userSession = backgroundExecutionHandler.getEffectiveUserSession(userName);
135               // session.setAuthenticated(userName);
136
outputHandler = backgroundExecutionHandler.getContentOutputHandler(location, fileName, solutionName, userSession, parameterProvider);
137               
138             } else {
139               outputHandler = new SimpleOutputHandler(null, false);
140               // Check to see if the user was authenticated (via the portal) in the JobSchedulerComponent
141
String JavaDoc userName = dataMap.getString("username"); //$NON-NLS-1$
142
if (userName != null) {
143                 // Well, we got a valid user name - let's try to use the
144
// background execute component to establish the user
145
userSession = backgroundExecutionHandler.getEffectiveUserSession(userName);
146               } else {
147                 // User wasn't authenticated when the job was scheduled - use default behavior from old...
148
userSession = executeSession;
149               }
150             }
151
152             BaseRequestHandler requestHandler = new BaseRequestHandler(userSession, null, outputHandler, parameterProvider, null);
153             requestHandler.setParameterProvider("session", new HttpSessionParameterProvider(userSession)); //$NON-NLS-1$
154

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"); //$NON-NLS-1$
167
try {
168                   if ((rt != null) && (rt.getStatus() == IRuntimeContext.RUNTIME_STATUS_SUCCESS)) {
169                       StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
170                       UIUtil.formatSuccessMessage("text/html", rt, buffer, false); //$NON-NLS-1$
171
OutputStream JavaDoc os = outputContentItem.getOutputStream(actionName);
172                       os.write(buffer.toString().getBytes(LocaleHelper.getSystemEncoding()));
173                       os.close();
174                   } else {
175                       // we need an error message...
176
StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
177                       UIUtil.formatFailureMessage("text/html", rt, buffer, requestHandler.getMessages()); //$NON-NLS-1$
178
OutputStream JavaDoc os = outputContentItem.getOutputStream(actionName);
179                       os.write(buffer.toString().getBytes(LocaleHelper.getSystemEncoding()));
180                       os.close();
181                   }
182                 } catch (IOException JavaDoc 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())); //$NON-NLS-1$
200
} finally {
201             PentahoSystem.systemExitPoint();
202         }
203     }
204
205 }
206
Popular Tags