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 12, 2005
014: * @author wseyler
015: */
016:
017: package org.pentaho.plugin.quartz;
018:
019: import java.util.ArrayList;
020:
021: import org.apache.commons.logging.Log;
022: import org.apache.commons.logging.LogFactory;
023: import org.pentaho.actionsequence.dom.actions.ActionDefinition;
024: import org.pentaho.actionsequence.dom.actions.ListSchedJobsAction;
025: import org.pentaho.actionsequence.dom.actions.ResumeSchedulerAction;
026: import org.pentaho.actionsequence.dom.actions.SchedulerStatusAction;
027: import org.pentaho.actionsequence.dom.actions.SuspendSchedulerAction;
028: import org.pentaho.messages.Messages;
029: import org.pentaho.plugin.ComponentBase;
030: import org.quartz.Scheduler;
031: import org.quartz.SchedulerException;
032:
033: /**
034: * @author wseyler
035: *
036: * TODO To change the template for this generated type comment go to Window -
037: * Preferences - Java - Code Style - Code Templates
038: */
039: public class SchedulerAdminComponent extends ComponentBase {
040:
041: /**
042: *
043: */
044: private static final long serialVersionUID = 5003948074206255569L;
045: private static final String SCHEDULER_ACTION_STR = "schedulerAction"; //$NON-NLS-1$
046:
047: private Scheduler sched = null;
048:
049: /*
050: * (non-Javadoc)
051: *
052: * @see org.pentaho.component.IComponent#init()
053: */
054: public boolean init() {
055: try {
056: sched = QuartzSystemListener.getSchedulerInstance();
057: } catch (Exception e) {
058: error(
059: Messages
060: .getErrorString("JobSchedulerComponent.ERROR_0001_NoScheduler"), e); //$NON-NLS-1$
061: return false;
062: }
063: return true;
064: }
065:
066: /*
067: * (non-Javadoc)
068: *
069: * @see org.pentaho.component.ComponentBase#validateAction()
070: */
071: protected boolean validateAction() {
072: return isDefinedInput(SCHEDULER_ACTION_STR);
073: }
074:
075: /*
076: * (non-Javadoc)
077: *
078: * @see org.pentaho.component.ComponentBase#validateSystemSettings()
079: */
080: protected boolean validateSystemSettings() {
081: return true;
082: }
083:
084: /*
085: * (non-Javadoc)
086: *
087: * @see org.pentaho.component.ComponentBase#done()
088: */
089: public void done() {
090: sched = null;
091: }
092:
093: /*
094: * (non-Javadoc)
095: *
096: * @see org.pentaho.component.ComponentBase#executeAction()
097: */
098: protected boolean executeAction() {
099:
100: ActionDefinition actionDefinition = (ActionDefinition) getActionDefinition();
101:
102: if (actionDefinition instanceof ListSchedJobsAction) {
103: return doGetJobNames();
104: } else if (actionDefinition instanceof SuspendSchedulerAction) {
105: return doPauseAll();
106: } else if (actionDefinition instanceof ResumeSchedulerAction) {
107: return doResumeAll();
108: } else if (actionDefinition instanceof SchedulerStatusAction) {
109: return doIsSchedulerPaused();
110: } else {
111: return false;
112: }
113: }
114:
115: /**
116: * @return
117: */
118: private boolean doIsSchedulerPaused() {
119: if (feedbackAllowed()) {
120: try {
121: String resultBool = sched.isInStandbyMode() ? Messages
122: .getString("SchedulerAdminComponent.CODE_true") : Messages.getString("SchedulerAdminComponent.CODE_false"); //$NON-NLS-1$ //$NON-NLS-2$
123: createFeedbackParameter(
124: "isPaused", Messages.getString("SchedulerAdminComponent.USER_IS_PAUSED"), "", resultBool, true); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
125: return true;
126: } catch (SchedulerException e) {
127: error(
128: Messages
129: .getErrorString("SchedulerAdminComponent.ERROR_0001_SchedulerError"), e); //$NON-NLS-1$
130: return false;
131: }
132: }
133: return false;
134: }
135:
136: /**
137: * @return
138: */
139: private boolean doGetJobNames() {
140: if (feedbackAllowed()) {
141: try {
142: String[] jobNames = sched
143: .getJobNames(Scheduler.DEFAULT_GROUP);
144: ArrayList values = new ArrayList();
145: for (int i = 0; i < jobNames.length; i++) {
146: values.add(jobNames[i]);
147: }
148: createFeedbackParameter(
149: "jobNames", Messages.getString("SchedulerAdminComponent.USER_JOB_NAMES"), "", null, values, null, null); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
150: return true;
151: } catch (SchedulerException e) {
152: error(
153: Messages
154: .getErrorString("SchedulerAdminComponent.ERROR_0001_SchedulerError"), e); //$NON-NLS-1$
155: return false;
156: }
157: }
158: return false;
159: }
160:
161: /**
162: * @return
163: */
164: private boolean doResumeAll() {
165: try {
166: sched.resumeAll();
167: } catch (SchedulerException e) {
168: error(
169: Messages
170: .getErrorString("SchedulerAdminComponent.ERROR_0001_SchedulerError"), e); //$NON-NLS-1$
171: return false;
172: }
173: return true;
174: }
175:
176: /**
177: * @return
178: */
179: private boolean doPauseAll() {
180: try {
181: sched.pauseAll();
182: } catch (SchedulerException e) {
183: error(
184: Messages
185: .getErrorString("SchedulerAdminComponent.ERROR_0001_SchedulerError"), e); //$NON-NLS-1$
186: return false;
187: }
188: return true;
189: }
190:
191: /*
192: * (non-Javadoc)
193: *
194: * @see org.pentaho.core.system.PentahoBase#getLogger()
195: */
196: public Log getLogger() {
197: return LogFactory.getLog(SchedulerAdminComponent.class);
198: }
199:
200: }
|