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: * Created Aug 15, 2005
014: * @author wseyler
015: */
016:
017: package org.pentaho.ui.component;
018:
019: import java.util.Date;
020: import java.util.List;
021: import org.apache.commons.logging.Log;
022: import org.apache.commons.logging.LogFactory;
023: import org.dom4j.Document;
024: import org.dom4j.DocumentHelper;
025: import org.dom4j.Element;
026: import org.pentaho.core.ui.IPentahoUrlFactory;
027: import org.pentaho.messages.Messages;
028: import org.pentaho.plugin.quartz.QuartzSystemListener;
029: import org.pentaho.ui.XmlComponent;
030: import org.quartz.JobDetail;
031: import org.quartz.Scheduler;
032: import org.quartz.SchedulerException;
033: import org.quartz.SimpleTrigger;
034: import org.quartz.Trigger;
035:
036: /**
037: * @author wseyler
038: *
039: * TODO To change the template for this generated type comment go to Window -
040: * Preferences - Java - Code Style - Code Templates
041: */
042: public class SchedulerAdminUIComponent extends XmlComponent {
043: /**
044: *
045: */
046: private static final long serialVersionUID = 2963902264708970014L;
047:
048: private static final String JOB = "job"; //$NON-NLS-1$
049:
050: private static final String JOB_NAME = "jobName"; //$NON-NLS-1$
051:
052: private static final String RESULT = "schedulerResults"; //$NON-NLS-1$
053:
054: private static final String JOB_GROUP = "jobGroup"; //$NON-NLS-1$
055:
056: public static final String RESUME_SCHEDULER_ACTION_STR = "resumeScheduler"; //$NON-NLS-1$
057:
058: public static final String SUSPEND_SCHEDULER_ACTION_STR = "suspendScheduler"; //$NON-NLS-1$
059:
060: public static final String GET_JOB_NAMES_ACTION_STR = "getJobNames"; //$NON-NLS-1$
061:
062: public static final String GET_IS_SCHEDULER_PAUSED_ACTION_STR = "isSchedulerPaused"; //$NON-NLS-1$
063:
064: public static final String PAUSE_JOB_ACTION_STR = "pauseJob"; //$NON-NLS-1$
065:
066: public static final String RESUME_JOB_ACTION_STR = "resumeJob"; //$NON-NLS-1$
067:
068: public static final String DELETE_JOB_ACTION_STR = "deleteJob"; //$NON-NLS-1$
069:
070: public static final String SCHEDULER_ACTION_STR = "schedulerAction"; //$NON-NLS-1$
071:
072: public static final String RUN_JOB_ACTION_STR = "executeJob"; //$NON-NLS-1$
073:
074: private Scheduler sched = null;
075:
076: private static final Log logger = LogFactory
077: .getLog(SchedulerAdminUIComponent.class);
078:
079: /**
080: * @param urlFactory
081: */
082: public SchedulerAdminUIComponent(IPentahoUrlFactory urlFactory,
083: List messages) {
084: super (urlFactory, messages, null);
085: try {
086: sched = QuartzSystemListener.getSchedulerInstance();
087: } catch (Exception e) {
088: error(Messages
089: .getString(Messages
090: .getErrorString("SchedulerAdminUIComponent.ERROR_0002_NoScheduler") + e.toString())); //$NON-NLS-1$
091: e.printStackTrace();
092: }
093: setXsl("text/html", "SchedulerAdmin.xsl"); //$NON-NLS-1$ //$NON-NLS-2$
094: }
095:
096: /*
097: * (non-Javadoc)
098: *
099: * @see org.pentaho.core.system.PentahoBase#getLogger()
100: */
101: public Log getLogger() {
102: return logger;
103: }
104:
105: /*
106: * (non-Javadoc)
107: *
108: * @see org.pentaho.core.ui.component.BaseUIComponent#validate()
109: */
110: public boolean validate() {
111: return true;
112: }
113:
114: /*
115: * (non-Javadoc)
116: *
117: * @see org.pentaho.core.ui.component.BaseUIComponent#getXmlContent()
118: */
119: public Document getXmlContent() {
120: String schedulerActionStr = getParameter(SCHEDULER_ACTION_STR,
121: GET_JOB_NAMES_ACTION_STR);
122: setXslProperty(
123: "baseUrl", urlFactory.getDisplayUrlBuilder().getUrl()); //$NON-NLS-1$
124:
125: if (SUSPEND_SCHEDULER_ACTION_STR.equals(schedulerActionStr)) {
126: return doPauseAll();
127: } else if (RESUME_SCHEDULER_ACTION_STR
128: .equals(schedulerActionStr)) {
129: return doResumeAll();
130: } else if (GET_JOB_NAMES_ACTION_STR.equals(schedulerActionStr)) {
131: return doGetJobNames();
132: } else if (GET_IS_SCHEDULER_PAUSED_ACTION_STR
133: .equals(schedulerActionStr)) {
134: return doIsSchedulerPaused();
135: } else if (PAUSE_JOB_ACTION_STR
136: .equalsIgnoreCase(schedulerActionStr)) {
137: return doPauseJob();
138: } else if (RESUME_JOB_ACTION_STR
139: .equalsIgnoreCase(schedulerActionStr)) {
140: return doResumeJob();
141: } else if (DELETE_JOB_ACTION_STR
142: .equalsIgnoreCase(schedulerActionStr)) {
143: return doDeleteJob();
144: } else if (RUN_JOB_ACTION_STR
145: .equalsIgnoreCase(schedulerActionStr)) {
146: return doExecuteJobNow();
147: } else {
148: Document document = DocumentHelper.createDocument();
149: document.setName(SCHEDULER_ACTION_STR);
150:
151: return document;
152: // returns a blank document if
153: // nothing else excuted.
154: /*
155: * TODO Create some sort of document to display when the default
156: * action occurs.
157: */
158: }
159: }
160:
161: private Document doExecuteJobNow() {
162: String jobName = getParameter(JOB_NAME, ""); //$NON-NLS-1$
163: String groupName = getParameter(JOB_GROUP, ""); //$NON-NLS-1$
164: Trigger trigger = new SimpleTrigger("Immediate", "DEFAULT"); //$NON-NLS-1$ //$NON-NLS-2$
165:
166: try {
167: JobDetail jobDetail = sched
168: .getJobDetail(jobName, groupName);
169: jobDetail.setGroup("Immediate"); //$NON-NLS-1$
170: sched.scheduleJob(jobDetail, trigger);
171: } catch (SchedulerException e) {
172: // TODO Auto-generated catch block
173: e.printStackTrace();
174: }
175: return doGetJobNames();
176: }
177:
178: private Document doDeleteJob() {
179: String jobName = getParameter(JOB_NAME, ""); //$NON-NLS-1$
180: String groupName = getParameter(JOB_GROUP, ""); //$NON-NLS-1$
181: try {
182: sched.deleteJob(jobName, groupName);
183: } catch (SchedulerException e) {
184: // TODO Auto-generated catch block
185: e.printStackTrace();
186: }
187:
188: return doGetJobNames();
189: }
190:
191: private Document doResumeJob() {
192: String jobName = getParameter(JOB_NAME, ""); //$NON-NLS-1$
193: String groupName = getParameter(JOB_GROUP, ""); //$NON-NLS-1$
194: try {
195: sched.resumeJob(jobName, groupName);
196: } catch (SchedulerException e) {
197: // TODO Auto-generated catch block
198: e.printStackTrace();
199: }
200:
201: return doGetJobNames();
202: }
203:
204: private Document doPauseJob() {
205: String jobName = getParameter(JOB_NAME, ""); //$NON-NLS-1$
206: String groupName = getParameter(JOB_GROUP, ""); //$NON-NLS-1$
207: try {
208: sched.pauseJob(jobName, groupName);
209: } catch (SchedulerException e) {
210: // TODO Auto-generated catch block
211: e.printStackTrace();
212: }
213: return doGetJobNames();
214: }
215:
216: /**
217: * @return
218: */
219: private Document doIsSchedulerPaused() {
220: Document document = DocumentHelper.createDocument();
221: document.setName(SCHEDULER_ACTION_STR);
222: Element root = document.addElement(getParameter(
223: SCHEDULER_ACTION_STR, "")); //$NON-NLS-1$
224: try {
225: boolean isInStandby = sched.isInStandbyMode();
226: root
227: .addAttribute(
228: RESULT,
229: isInStandby ? Messages
230: .getString("SchedulerAdminUIComponent.USER_isPaused") : Messages.getString("SchedulerAdminUIComponent.USER_isRunning")); //$NON-NLS-1$ //$NON-NLS-2$
231: } catch (SchedulerException e) {
232: error(Messages
233: .getErrorString("SchedulerAdminUIComponent.ERROR_0001_ErrorInScheduler") + e.toString()); //$NON-NLS-1$
234: root
235: .addAttribute(
236: RESULT,
237: Messages
238: .getErrorString("SchedulerAdminUIComponent.ERROR_0001_ErrorInScheduler") + e.toString()); //$NON-NLS-1$
239: }
240: return document;
241: }
242:
243: /**
244: * @return
245: */
246: private Document doGetJobNames() {
247: Document document = DocumentHelper.createDocument();
248: document.setName(SCHEDULER_ACTION_STR);
249: Element root = document.addElement(GET_JOB_NAMES_ACTION_STR);
250: try {
251: String[] triggerGroups = sched.getTriggerGroupNames();
252: for (int i = 0; i < triggerGroups.length; i++) {
253: String[] triggerNames = sched
254: .getTriggerNames(triggerGroups[i]);
255: for (int j = 0; j < triggerNames.length; j++) {
256: Element job = root.addElement(JOB);
257: job.addAttribute("triggerName", triggerNames[j]); //$NON-NLS-1$
258: job.addAttribute("triggerGroup", triggerGroups[i]); //$NON-NLS-1$
259:
260: try {
261: job
262: .addAttribute(
263: "triggerState", Integer.toString(sched.getTriggerState(triggerNames[j], triggerGroups[i]))); //$NON-NLS-1$
264:
265: Trigger trigger = sched.getTrigger(
266: triggerNames[j], triggerGroups[i]);
267: job.addAttribute(
268: "triggerName", trigger.getName()); //$NON-NLS-1$
269: job.addAttribute(
270: "triggerGroup", trigger.getGroup()); //$NON-NLS-1$
271: Date date = trigger.getNextFireTime();
272: job
273: .addAttribute(
274: "nextFireTime", (date == null) ? Messages.getString("SchedulerAdminUIComponent.USER_NEVER") : date.toString()); //$NON-NLS-1$ //$NON-NLS-2$
275: date = trigger.getPreviousFireTime();
276: job
277: .addAttribute(
278: "prevFireTime", (date == null) ? Messages.getString("SchedulerAdminUIComponent.USER_NEVER") : date.toString()); //$NON-NLS-1$ //$NON-NLS-2$
279:
280: // get the job info
281: job
282: .addAttribute(JOB_NAME, trigger
283: .getJobName());
284: job.addAttribute(JOB_GROUP, trigger
285: .getJobGroup());
286: JobDetail jobDetail = sched.getJobDetail(
287: trigger.getJobName(), trigger
288: .getJobGroup());
289: job
290: .addElement("description").addCDATA(jobDetail.getDescription()); //$NON-NLS-1$
291: // job.addAttribute("class",
292: // jobDetail.getClass().getName()); //$NON-NLS-1$
293: } catch (Exception e) {
294: job
295: .addElement("description").addCDATA(e.getMessage()); //$NON-NLS-1$
296: job.addAttribute("triggerState", "3"); // ERROR //$NON-NLS-1$ //$NON-NLS-2$
297: }
298: }
299: }
300: } catch (SchedulerException e) {
301: error(Messages
302: .getErrorString("SchedulerAdminUIComponent.ERROR_0001_ErrorInScheduler") + e.toString()); //$NON-NLS-1$
303: root
304: .addAttribute(
305: RESULT,
306: Messages
307: .getErrorString("SchedulerAdminUIComponent.ERROR_0001_ErrorInScheduler") + e.toString()); //$NON-NLS-1$
308: }
309:
310: return document;
311: }
312:
313: /**
314: * @return
315: */
316: private Document doResumeAll() {
317: Document document = DocumentHelper.createDocument();
318: document.setName(SCHEDULER_ACTION_STR);
319: Element root = document.addElement(getParameter(
320: SCHEDULER_ACTION_STR, "")); //$NON-NLS-1$
321: try {
322: sched.resumeAll();
323: root
324: .addAttribute(
325: RESULT,
326: Messages
327: .getString("SchedulerAdminUIComponent.USER_JobsResumed")); //$NON-NLS-1$
328: } catch (SchedulerException e) {
329: error(Messages
330: .getErrorString("SchedulerAdminUIComponent.ERROR_0001_ErrorInScheduler") + e.toString()); //$NON-NLS-1$
331: root
332: .addAttribute(
333: RESULT,
334: Messages
335: .getErrorString("SchedulerAdminUIComponent.ERROR_0001_ErrorInScheduler") + e.toString()); //$NON-NLS-1$
336: }
337:
338: return document;
339: }
340:
341: /**
342: * @return
343: */
344: private Document doPauseAll() {
345: Document document = DocumentHelper.createDocument();
346: document.setName(SCHEDULER_ACTION_STR);
347: Element root = document.addElement(getParameter(
348: SCHEDULER_ACTION_STR, "")); //$NON-NLS-1$
349: try {
350: sched.pauseAll();
351: root
352: .addAttribute(
353: RESULT,
354: Messages
355: .getString("SchedulerAdminUIComponent.USER_JobsSuspended")); //$NON-NLS-1$
356: } catch (SchedulerException e) {
357: error(Messages
358: .getErrorString("SchedulerAdminUIComponent.ERROR_0001_ErrorInScheduler") + e.toString()); //$NON-NLS-1$
359: root
360: .addAttribute(
361: RESULT,
362: Messages
363: .getErrorString("SchedulerAdminUIComponent.ERROR_0001_ErrorInScheduler") + e.toString()); //$NON-NLS-1$
364: }
365:
366: return document;
367: }
368:
369: }
|