KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.pentaho.plugin.quartz;
14
15 import java.text.SimpleDateFormat JavaDoc;
16 import java.util.ArrayList JavaDoc;
17 import java.util.Date JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.List JavaDoc;
20
21 import org.pentaho.core.repository.IContentItem;
22 import org.pentaho.core.repository.IContentRepository;
23 import org.pentaho.core.repository.ISolutionRepository;
24 import org.pentaho.core.repository.content.ContentRepositoryOutputHandler;
25 import org.pentaho.core.runtime.IBackgroundExecution;
26 import org.pentaho.core.session.IPentahoSession;
27 import org.pentaho.core.session.UserSession;
28 import org.pentaho.core.solution.IActionSequence;
29 import org.pentaho.core.solution.IOutputHandler;
30 import org.pentaho.core.solution.IParameterProvider;
31 import org.pentaho.core.system.PentahoSystem;
32 import org.pentaho.messages.Messages;
33 import org.pentaho.repository.HibernateUtil;
34 import org.pentaho.repository.content.ContentRepository;
35 import org.pentaho.util.UUIDUtil;
36 import org.pentaho.util.logging.Logger;
37 import org.quartz.JobDataMap;
38 import org.quartz.JobDetail;
39 import org.quartz.JobExecutionContext;
40 import org.quartz.JobExecutionException;
41 import org.quartz.JobListener;
42 import org.quartz.Scheduler;
43 import org.quartz.SimpleTrigger;
44 import org.quartz.Trigger;
45
46 public class QuartzBackgroundExecutionHelper implements IBackgroundExecution {
47
48   public static final String JavaDoc DEFAULT_JOB_NAME = "bgExecution"; //$NON-NLS-1$
49
public static final String JavaDoc DEFAULT_TRIGGER_NAME = "bgTrigger"; //$NON-NLS-1$
50
public static final String JavaDoc DEFAULT_BACKGROUND_LOCATION = "background"; //$NON-NLS-1$
51
public static final String JavaDoc BACKGROUND_USER_NAME_STR = "background_user_name"; //$NON-NLS-1$
52
public static final String JavaDoc BACKGROUND_CONTENT_GUID_STR = "background_output_content_guid"; //$NON-NLS-1$
53
public static final String JavaDoc BACKGROUND_CONTENT_LOCATION_STR = "background_output_location"; //$NON-NLS-1$
54
public static final String JavaDoc BACKGROUND_CONTENT_COOKIE_PREFIX = "pentaho_background_content"; //$NON-NLS-1$
55
public static final String JavaDoc BACKGROUND_EXECUTION_FLAG = "backgroundExecution"; //$NON-NLS-1$
56

57
58   /*
59    *****************************
60    * Methods from the Interface
61    *****************************
62    */

63
64   public String JavaDoc backgroundExecuteAction(IPentahoSession userSession, IParameterProvider parameterProvider) {
65     try {
66       Scheduler sched = QuartzSystemListener.getSchedulerInstance();
67
68       String JavaDoc solutionName = parameterProvider.getStringParameter("solution", null); //$NON-NLS-1$
69
String JavaDoc actionPath = parameterProvider.getStringParameter("path", null); //$NON-NLS-1$
70
String JavaDoc actionName = parameterProvider.getStringParameter("action", null); //$NON-NLS-1$
71
String JavaDoc userName = userSession.isAuthenticated() ? userSession.getName() : IBackgroundExecution.DEFAULT_USER_NAME;
72       String JavaDoc jobGroup = userName;
73       
74       String JavaDoc outputContentGUID = UUIDUtil.getUUIDAsString();
75
76       JobDetail jobDetail = createDetailFromParameterProvider(parameterProvider,
77           userSession,
78           outputContentGUID,
79           solutionName,
80           actionPath,
81           actionName,
82           jobGroup);
83       
84       trackBackgroundExecution(userSession, outputContentGUID);
85
86       BackgroundExecuteListener listener = new BackgroundExecuteListener(userSession, outputContentGUID, sched, jobDetail.getName());
87       sched.addJobListener(listener);
88       jobDetail.addJobListener(listener.getName());
89       
90       Trigger bgTrigger = new SimpleTrigger(outputContentGUID, jobGroup, new Date JavaDoc());
91       // bgTrigger.setPriority(someValue);
92

93       sched.scheduleJob(jobDetail, bgTrigger);
94       // TODO: Fix with properly formatted HTML template for this status message
95
return Messages.getString("BackgroundExecuteHelper.USER_JOB_SUBMITTED", "UserContent", "if(window.opener) {window.opener.location.href='UserContent'; window.close() } else { return true; }" ); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
96
} catch (Exception JavaDoc ex) {
97       return ex.getLocalizedMessage();
98     }
99   }
100   
101   public void trackBackgroundExecution(IPentahoSession userSession, String JavaDoc GUID) {
102       IContentRepository repo = ContentRepository.getInstance(userSession);
103       repo.newBackgroundExecutedContentId(userSession, GUID);
104   }
105
106   public IContentItem getBackgroundContent(String JavaDoc contentGUID, IPentahoSession userSession) {
107     IContentRepository repo = ContentRepository.getInstance(userSession);
108     try {
109       IContentItem item = repo.getContentItemById(contentGUID);
110       return item;
111     } catch (Exception JavaDoc ex) {
112       Logger.error(this.getClass().getName(), ex.getLocalizedMessage(), ex);
113     }
114     return null;
115   }
116
117   public List JavaDoc getScheduledAndExecutingBackgroundJobs(IPentahoSession userSession) {
118     try {
119       Scheduler sched = QuartzSystemListener.getSchedulerInstance();
120       String JavaDoc userName = userSession.isAuthenticated() ? userSession.getName() : IBackgroundExecution.DEFAULT_USER_NAME;
121       String JavaDoc[] jobNames = sched.getJobNames(userName);
122       List JavaDoc rtn = new ArrayList JavaDoc();
123       if (jobNames != null) {
124         for (int i=0; i<jobNames.length; i++) {
125           JobDetail jobDetail = sched.getJobDetail( jobNames[i], userName );
126           rtn.add(jobDetail);
127         }
128       }
129       return rtn;
130     } catch (Exception JavaDoc ex) {
131       // TODO: Handle exception properly
132
ex.printStackTrace();
133     }
134     return new ArrayList JavaDoc();
135   }
136
137  
138   public void removeBackgroundExecutedContentForID(String JavaDoc contentGUID, IPentahoSession userSession) {
139
140     // First, remove content item from the repo with that GUID.
141
IContentRepository repo = ContentRepository.getInstance(userSession);
142     try {
143         IContentItem item = repo.getContentItemById(contentGUID);
144         if (item != null) {
145           item.makeTransient();
146         } else {
147           return;
148         }
149     } finally {
150         HibernateUtil.commitTransaction();
151     }
152     repo.removeBackgroundExecutedContentId(userSession, contentGUID);
153   }
154
155   public List JavaDoc getBackgroundExecutedContentList(IPentahoSession userSession) {
156     IContentRepository repo = ContentRepository.getInstance(userSession);
157     ArrayList JavaDoc idList = new ArrayList JavaDoc();
158     List JavaDoc idObjectList = repo.getBackgroundExecutedContentItemsForUser(userSession);
159     if (idObjectList != null) {
160       IContentItem contentItem = null;
161       for (int i=0; i<idObjectList.size(); i++) {
162         contentItem = (IContentItem)idObjectList.get(i);
163         idList.add(contentItem);
164       }
165     }
166     return idList;
167   }
168   
169   // Helper Utility Methods
170
protected JobDetail createDetailFromParameterProvider(IParameterProvider parameterProvider,
171       IPentahoSession userSession,
172       String JavaDoc outputContentGUID,
173       String JavaDoc solutionName,
174       String JavaDoc actionPath,
175       String JavaDoc actionName,
176       String JavaDoc jobGroup) {
177     String JavaDoc userName = userSession.isAuthenticated() ? userSession.getName() : IBackgroundExecution.DEFAULT_USER_NAME;
178
179   SimpleDateFormat JavaDoc fmt = new SimpleDateFormat JavaDoc();
180     String JavaDoc completeAction = solutionName + "/" + actionPath + "/" + actionName; //$NON-NLS-1$ //$NON-NLS-2$
181
String JavaDoc jobName = completeAction + "/" + outputContentGUID; //$NON-NLS-1$
182
JobDetail jobDetail = new JobDetail(jobName, jobGroup, QuartzExecute.class);
183     JobDataMap data = jobDetail.getJobDataMap();
184     Iterator JavaDoc inputNamesIterator = parameterProvider.getParameterNames();
185     String JavaDoc outputLocationGUID = UUIDUtil.getUUIDAsString();
186     while (inputNamesIterator.hasNext()) {
187         String JavaDoc inputName = (String JavaDoc) inputNamesIterator.next();
188         Object JavaDoc inputValue = parameterProvider.getParameter(inputName);
189         data.put(inputName, inputValue);
190     }
191     ISolutionRepository repo = PentahoSystem.getSolutionRepository( userSession );
192     IActionSequence action = repo.getActionSequence( solutionName, actionPath, actionName, repo.getLoggingLevel(), ISolutionRepository.ACTION_EXECUTE );
193     data.put( BACKGROUND_ACTION_NAME_STR, action.getTitle() );
194     data.put("processId", this.getClass().getName()); //$NON-NLS-1$
195
data.put(BACKGROUND_USER_NAME_STR, userName);
196     data.put(BACKGROUND_CONTENT_GUID_STR, outputContentGUID);
197     data.put(BACKGROUND_CONTENT_LOCATION_STR, DEFAULT_BACKGROUND_LOCATION + "/" + outputLocationGUID); //$NON-NLS-1$
198
data.put(BACKGROUND_SUBMITTED, fmt.format( new Date JavaDoc() ) );
199
200     // This tells our execution component (QuartzExecute) that we're running a background job instead of
201
// a standard quartz execution.
202
data.put( BACKGROUND_EXECUTION_FLAG, "true" ); //$NON-NLS-1$
203

204     return jobDetail;
205     
206   }
207   
208   public IPentahoSession getEffectiveUserSession(String JavaDoc user) {
209     UserSession us = new UserSession(user, null, true);
210     return us;
211   }
212
213   public static class BackgroundExecuteListener implements JobListener {
214
215     private IPentahoSession userSession;
216     private String JavaDoc contentGUID;
217     private Scheduler sched;
218     private String JavaDoc jobName;
219     
220     public BackgroundExecuteListener(IPentahoSession session, String JavaDoc contentGUID, Scheduler scheduler, String JavaDoc jobName) {
221       userSession = session;
222       this.contentGUID = contentGUID;
223       this.jobName = jobName;
224     }
225     
226     public String JavaDoc getName() {
227       return contentGUID;
228     }
229
230     public void jobExecutionVetoed(JobExecutionContext context) {
231       // TODO Auto-generated method stub
232

233     }
234
235     public void jobToBeExecuted(JobExecutionContext context) {
236       // TODO Auto-generated method stub
237

238     }
239
240     public void jobWasExecuted(JobExecutionContext context, JobExecutionException exception) {
241       // TODO Auto-generated method stub
242
// Update the userSession with the updated content item.
243
JobDetail ctxDetail = context.getJobDetail();
244       if ( (ctxDetail != null) && (ctxDetail.getName().equals(this.jobName) ) ) { // Only do if it's for our job...
245
Object JavaDoc contentItemGUID = context.get(BACKGROUND_CONTENT_GUID_STR);
246         if (contentItemGUID != null && userSession != null) {
247           userSession.setBackgroundExecutionAlert(); // Toggle the alert status
248
} else {
249           Logger.warn(this.getClass().getName(), Messages.getString("BackgroundExecuteHelper.WARN_CONTENT_ITEM_NOT_CREATED" )); //$NON-NLS-1$
250
}
251         this.userSession = null; // Make sure nothing keeps a handle to the user session.
252
try {
253           sched.removeJobListener(this.getName());
254         } catch (Exception JavaDoc ex) {
255           // TODO: Do something here
256
}
257       }
258     }
259     
260   }
261   
262   public IOutputHandler getContentOutputHandler(String JavaDoc location, String JavaDoc fileName, String JavaDoc solutionName, IPentahoSession userSession, IParameterProvider parameterProvider) {
263       return new ContentRepositoryOutputHandler(location, fileName, solutionName, userSession);
264   }
265   
266 }
267
Popular Tags