001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.kernel.scheduler;
016    
017    import com.liferay.portal.kernel.audit.AuditMessage;
018    import com.liferay.portal.kernel.audit.AuditRouterUtil;
019    import com.liferay.portal.kernel.cal.DayAndPosition;
020    import com.liferay.portal.kernel.cal.Duration;
021    import com.liferay.portal.kernel.cal.Recurrence;
022    import com.liferay.portal.kernel.cal.RecurrenceSerializer;
023    import com.liferay.portal.kernel.json.JSONFactoryUtil;
024    import com.liferay.portal.kernel.log.Log;
025    import com.liferay.portal.kernel.log.LogFactoryUtil;
026    import com.liferay.portal.kernel.messaging.DestinationNames;
027    import com.liferay.portal.kernel.messaging.Message;
028    import com.liferay.portal.kernel.scheduler.messaging.SchedulerResponse;
029    import com.liferay.portal.kernel.security.pacl.permission.PortalRuntimePermission;
030    import com.liferay.portal.kernel.util.CalendarFactoryUtil;
031    import com.liferay.portal.kernel.util.InetAddressUtil;
032    import com.liferay.portal.kernel.util.ObjectValuePair;
033    import com.liferay.portal.kernel.util.ParamUtil;
034    import com.liferay.portal.kernel.util.PortalLifecycle;
035    import com.liferay.portal.kernel.util.StringPool;
036    import com.liferay.portal.model.CompanyConstants;
037    import com.liferay.portal.util.PortalUtil;
038    
039    import java.util.ArrayList;
040    import java.util.Calendar;
041    import java.util.Date;
042    import java.util.List;
043    
044    import javax.portlet.PortletRequest;
045    
046    /**
047     * @author Bruno Farache
048     * @author Shuyang Zhou
049     * @author Tina Tian
050     * @author Raymond Augé
051     */
052    public class SchedulerEngineUtil {
053    
054            public static void addJob(
055                            Trigger trigger, StorageType storageType, String description,
056                            String destinationName, Message message,
057                            String messageListenerClassName, String portletId,
058                            int exceptionsMaxSize)
059                    throws SchedulerException {
060    
061                    getInstance()._addJob(
062                            trigger, storageType, description, destinationName, message,
063                            messageListenerClassName, portletId, exceptionsMaxSize);
064            }
065    
066            public static void addJob(
067                            Trigger trigger, StorageType storageType, String description,
068                            String destinationName, Object payload,
069                            String messageListenerClassName, String portletId,
070                            int exceptionsMaxSize)
071                    throws SchedulerException {
072    
073                    getInstance()._addJob(
074                            trigger, storageType, description, destinationName, payload,
075                            messageListenerClassName, portletId, exceptionsMaxSize);
076            }
077    
078            public static void addScriptingJob(
079                            Trigger trigger, StorageType storageType, String description,
080                            String language, String script, int exceptionsMaxSize)
081                    throws SchedulerException {
082    
083                    getInstance()._addScriptingJob(
084                            trigger, storageType, description, language, script,
085                            exceptionsMaxSize);
086            }
087    
088            public static void auditSchedulerJobs(
089                            Message message, TriggerState triggerState)
090                    throws SchedulerException {
091    
092                    getInstance()._auditSchedulerJobs(message, triggerState);
093            }
094    
095            public static void delete(
096                            SchedulerEntry schedulerEntry, StorageType storageType)
097                    throws SchedulerException {
098    
099                    getInstance()._delete(schedulerEntry, storageType);
100            }
101    
102            public static void delete(String groupName, StorageType storageType)
103                    throws SchedulerException {
104    
105                    getInstance()._delete(groupName, storageType);
106            }
107    
108            public static void delete(
109                            String jobName, String groupName, StorageType storageType)
110                    throws SchedulerException {
111    
112                    getInstance()._delete(jobName, groupName, storageType);
113            }
114    
115            public static String getCronText(
116                    Calendar calendar, boolean timeZoneSensitive) {
117    
118                    return getInstance()._getCronText(calendar, timeZoneSensitive);
119            }
120    
121            public static String getCronText(
122                    PortletRequest portletRequest, Calendar calendar,
123                    boolean timeZoneSensitive, int recurrenceType) {
124    
125                    return getInstance()._getCronText(
126                            portletRequest, calendar, timeZoneSensitive, recurrenceType);
127            }
128    
129            public static Date getEndTime(SchedulerResponse schedulerResponse) {
130                    return getInstance()._getEndTime(schedulerResponse);
131            }
132    
133            public static Date getEndTime(
134                            String jobName, String groupName, StorageType storageType)
135                    throws SchedulerException {
136    
137                    return getInstance()._getEndTime(jobName, groupName, storageType);
138            }
139    
140            public static Date getFinalFireTime(SchedulerResponse schedulerResponse) {
141                    return getInstance()._getFinalFireTime(schedulerResponse);
142            }
143    
144            public static Date getFinalFireTime(
145                            String jobName, String groupName, StorageType storageType)
146                    throws SchedulerException {
147    
148                    return getInstance()._getFinalFireTime(jobName, groupName, storageType);
149            }
150    
151            public static SchedulerEngineUtil getInstance() {
152                    PortalRuntimePermission.checkGetBeanProperty(SchedulerEngineUtil.class);
153    
154                    return _instance;
155            }
156    
157            public static ObjectValuePair<Exception, Date>[] getJobExceptions(
158                    SchedulerResponse schedulerResponse) {
159    
160                    return getInstance()._getJobExceptions(schedulerResponse);
161            }
162    
163            public static ObjectValuePair<Exception, Date>[] getJobExceptions(
164                            String jobName, String groupName, StorageType storageType)
165                    throws SchedulerException {
166    
167                    return getInstance()._getJobExceptions(jobName, groupName, storageType);
168            }
169    
170            public static TriggerState getJobState(
171                    SchedulerResponse schedulerResponse) {
172    
173                    return getInstance()._getJobState(schedulerResponse);
174            }
175    
176            public static TriggerState getJobState(
177                            String jobName, String groupName, StorageType storageType)
178                    throws SchedulerException {
179    
180                    return getInstance()._getJobState(jobName, groupName, storageType);
181            }
182    
183            public static Date getNextFireTime(SchedulerResponse schedulerResponse) {
184                    return getInstance()._getNextFireTime(schedulerResponse);
185            }
186    
187            public static Date getNextFireTime(
188                            String jobName, String groupName, StorageType storageType)
189                    throws SchedulerException {
190    
191                    return getInstance()._getNextFireTime(jobName, groupName, storageType);
192            }
193    
194            public static Date getPreviousFireTime(
195                    SchedulerResponse schedulerResponse) {
196    
197                    return getInstance()._getPreviousFireTime(schedulerResponse);
198            }
199    
200            public static Date getPreviousFireTime(
201                            String jobName, String groupName, StorageType storageType)
202                    throws SchedulerException {
203    
204                    return getInstance()._getPreviousFireTime(
205                            jobName, groupName, storageType);
206            }
207    
208            public static SchedulerResponse getScheduledJob(
209                            String jobName, String groupName, StorageType storageType)
210                    throws SchedulerException {
211    
212                    return getInstance()._getScheduledJob(jobName, groupName, storageType);
213            }
214    
215            public static List<SchedulerResponse> getScheduledJobs()
216                    throws SchedulerException {
217    
218                    return getInstance()._getScheduledJobs();
219            }
220    
221            public static List<SchedulerResponse> getScheduledJobs(
222                            StorageType storageType)
223                    throws SchedulerException {
224    
225                    return getInstance()._getScheduledJobs(storageType);
226            }
227    
228            public static List<SchedulerResponse> getScheduledJobs(
229                            String groupName, StorageType storageType)
230                    throws SchedulerException {
231    
232                    return getInstance()._getScheduledJobs(groupName, storageType);
233            }
234    
235            public static Date getStartTime(SchedulerResponse schedulerResponse) {
236                    return getInstance()._getStartTime(schedulerResponse);
237            }
238    
239            public static Date getStartTime(
240                            String jobName, String groupName, StorageType storageType)
241                    throws SchedulerException {
242    
243                    return getInstance()._getStartTime(jobName, groupName, storageType);
244            }
245    
246            public static void initialize() throws SchedulerException {
247                    getInstance()._initialize();
248    
249                    SchedulerLifecycle schedulerLifecycle = new SchedulerLifecycle();
250    
251                    schedulerLifecycle.registerPortalLifecycle(PortalLifecycle.METHOD_INIT);
252            }
253    
254            public static String namespaceGroupName(
255                    String groupName, StorageType storageType) {
256    
257                    return getInstance()._namespaceGroupName(groupName, storageType);
258            }
259    
260            public static void pause(String groupName, StorageType storageType)
261                    throws SchedulerException {
262    
263                    getInstance()._pause(groupName, storageType);
264            }
265    
266            public static void pause(
267                            String jobName, String groupName, StorageType storageType)
268                    throws SchedulerException {
269    
270                    getInstance()._pause(jobName, groupName, storageType);
271            }
272    
273            public static void resume(String groupName, StorageType storageType)
274                    throws SchedulerException {
275    
276                    getInstance()._resume(groupName, storageType);
277            }
278    
279            public static void resume(
280                            String jobName, String groupName, StorageType storageType)
281                    throws SchedulerException {
282    
283                    getInstance()._resume(jobName, groupName, storageType);
284            }
285    
286            public static void schedule(
287                            SchedulerEntry schedulerEntry, StorageType storageType,
288                            String portletId, int exceptionsMaxSize)
289                    throws SchedulerException {
290    
291                    getInstance()._schedule(
292                            schedulerEntry, storageType, portletId, exceptionsMaxSize);
293            }
294    
295            public static void schedule(
296                            Trigger trigger, StorageType storageType, String description,
297                            String destinationName, Message message, int exceptionsMaxSize)
298                    throws SchedulerException {
299    
300                    getInstance()._schedule(
301                            trigger, storageType, description, destinationName, message,
302                            exceptionsMaxSize);
303            }
304    
305            public static void schedule(
306                            Trigger trigger, StorageType storageType, String description,
307                            String destinationName, Object payload, int exceptionsMaxSize)
308                    throws SchedulerException {
309    
310                    getInstance()._schedule(
311                            trigger, storageType, description, destinationName, payload,
312                            exceptionsMaxSize);
313            }
314    
315            public static void shutdown() throws SchedulerException {
316                    getInstance()._shutdown();
317            }
318    
319            public static void start() throws SchedulerException {
320                    getInstance()._start();
321            }
322    
323            public static void suppressError(
324                            String jobName, String groupName, StorageType storageType)
325                    throws SchedulerException {
326    
327                    getInstance()._suppressError(jobName, groupName, storageType);
328            }
329    
330            public static void unschedule(
331                            SchedulerEntry schedulerEntry, StorageType storageType)
332                    throws SchedulerException {
333    
334                    getInstance()._unschedule(schedulerEntry, storageType);
335            }
336    
337            public static void unschedule(String groupName, StorageType storageType)
338                    throws SchedulerException {
339    
340                    getInstance()._unschedule(groupName, storageType);
341            }
342    
343            public static void unschedule(
344                            String jobName, String groupName, StorageType storageType)
345                    throws SchedulerException {
346    
347                    getInstance()._unschedule(jobName, groupName, storageType);
348            }
349    
350            public static void update(
351                            String jobName, String groupName, StorageType storageType,
352                            String description, String language, String script,
353                            int exceptionsMaxSize)
354                    throws SchedulerException {
355    
356                    getInstance()._update(
357                            jobName, groupName, storageType, description, language, script,
358                            exceptionsMaxSize);
359            }
360    
361            public static void update(Trigger trigger, StorageType storageType)
362                    throws SchedulerException {
363    
364                    getInstance()._update(trigger, storageType);
365            }
366    
367            public static void updateMemorySchedulerClusterMaster()
368                    throws SchedulerException {
369    
370                    getInstance()._updateMemorySchedulerClusterMaster();
371            }
372    
373            public void setSchedulerEngine(SchedulerEngine schedulerEngine) {
374                    PortalRuntimePermission.checkSetBeanProperty(getClass());
375    
376                    _instance._schedulerEngine = schedulerEngine;
377    
378                    if (schedulerEngine instanceof SchedulerEngineClusterManager) {
379                            _instance._schedulerEngineClusterManager =
380                                    (SchedulerEngineClusterManager)schedulerEngine;
381                    }
382            }
383    
384            private void _addJob(
385                            Trigger trigger, StorageType storageType, String description,
386                            String destinationName, Message message,
387                            String messageListenerClassName, String portletId,
388                            int exceptionsMaxSize)
389                    throws SchedulerException {
390    
391                    if (message == null) {
392                            message = new Message();
393                    }
394    
395                    message.put(
396                            SchedulerEngine.MESSAGE_LISTENER_CLASS_NAME,
397                            messageListenerClassName);
398                    message.put(SchedulerEngine.PORTLET_ID, portletId);
399    
400                    _schedule(
401                            trigger, storageType, description, destinationName, message,
402                            exceptionsMaxSize);
403            }
404    
405            private void _addJob(
406                            Trigger trigger, StorageType storageType, String description,
407                            String destinationName, Object payload,
408                            String messageListenerClassName, String portletId,
409                            int exceptionsMaxSize)
410                    throws SchedulerException {
411    
412                    Message message = new Message();
413    
414                    message.setPayload(payload);
415    
416                    _addJob(
417                            trigger, storageType, description, destinationName, message,
418                            messageListenerClassName, portletId, exceptionsMaxSize);
419            }
420    
421            private void _addScriptingJob(
422                            Trigger trigger, StorageType storageType, String description,
423                            String language, String script, int exceptionsMaxSize)
424                    throws SchedulerException {
425    
426                    Message message = new Message();
427    
428                    message.put(SchedulerEngine.LANGUAGE, language);
429                    message.put(SchedulerEngine.SCRIPT, script);
430    
431                    _schedule(
432                            trigger, storageType, description,
433                            DestinationNames.SCHEDULER_SCRIPTING, message, exceptionsMaxSize);
434            }
435    
436            private void _addWeeklyDayPos(
437                    PortletRequest portletRequest, List<DayAndPosition> list, int day) {
438    
439                    if (ParamUtil.getBoolean(portletRequest, "weeklyDayPos" + day)) {
440                            list.add(new DayAndPosition(day, 0));
441                    }
442            }
443    
444            private void _auditSchedulerJobs(Message message, TriggerState triggerState)
445                    throws SchedulerException {
446    
447                    try {
448                            AuditMessage auditMessage = new AuditMessage(
449                                    SchedulerEngine.AUDIT_ACTION, CompanyConstants.SYSTEM, 0,
450                                    StringPool.BLANK, SchedulerEngine.class.getName(), "0",
451                                    triggerState.toString(), new Date(),
452                                    JSONFactoryUtil.createJSONObject(
453                                            JSONFactoryUtil.serialize(message)));
454    
455                            auditMessage.setServerName(InetAddressUtil.getLocalHostName());
456                            auditMessage.setServerPort(PortalUtil.getPortalPort(false));
457    
458                            AuditRouterUtil.route(auditMessage);
459                    }
460                    catch (Exception e) {
461                            throw new SchedulerException(e);
462                    }
463            }
464    
465            private void _delete(SchedulerEntry schedulerEntry, StorageType storageType)
466                    throws SchedulerException {
467    
468                    Trigger trigger = schedulerEntry.getTrigger();
469    
470                    _delete(trigger.getJobName(), trigger.getGroupName(), storageType);
471            }
472    
473            private void _delete(String groupName, StorageType storageType)
474                    throws SchedulerException {
475    
476                    _schedulerEngine.delete(_namespaceGroupName(groupName, storageType));
477            }
478    
479            private void _delete(
480                            String jobName, String groupName, StorageType storageType)
481                    throws SchedulerException {
482    
483                    _schedulerEngine.delete(
484                            jobName, _namespaceGroupName(groupName, storageType));
485            }
486    
487            private String _getCronText(Calendar calendar, boolean timeZoneSensitive) {
488                    return _getCronText(
489                            null, calendar, timeZoneSensitive, Recurrence.NO_RECURRENCE);
490            }
491    
492            private String _getCronText(
493                    PortletRequest portletRequest, Calendar calendar,
494                    boolean timeZoneSensitive, int recurrenceType) {
495    
496                    Calendar recurrenceCalendar = null;
497    
498                    if (timeZoneSensitive) {
499                            recurrenceCalendar = CalendarFactoryUtil.getCalendar();
500    
501                            recurrenceCalendar.setTime(calendar.getTime());
502                    }
503                    else {
504                            recurrenceCalendar = (Calendar)calendar.clone();
505                    }
506    
507                    Recurrence recurrence = new Recurrence(
508                            recurrenceCalendar, new Duration(1, 0, 0, 0), recurrenceType);
509    
510                    recurrence.setWeekStart(Calendar.SUNDAY);
511    
512                    if (recurrenceType == Recurrence.DAILY) {
513                            int dailyType = ParamUtil.getInteger(portletRequest, "dailyType");
514    
515                            if (dailyType == 0) {
516                                    int dailyInterval = ParamUtil.getInteger(
517                                            portletRequest, "dailyInterval", 1);
518    
519                                    recurrence.setInterval(dailyInterval);
520                            }
521                            else {
522                                    DayAndPosition[] dayPos = {
523                                            new DayAndPosition(Calendar.MONDAY, 0),
524                                            new DayAndPosition(Calendar.TUESDAY, 0),
525                                            new DayAndPosition(Calendar.WEDNESDAY, 0),
526                                            new DayAndPosition(Calendar.THURSDAY, 0),
527                                            new DayAndPosition(Calendar.FRIDAY, 0)};
528    
529                                    recurrence.setByDay(dayPos);
530                            }
531                    }
532                    else if (recurrenceType == Recurrence.WEEKLY) {
533                            int weeklyInterval = ParamUtil.getInteger(
534                                    portletRequest, "weeklyInterval", 1);
535    
536                            recurrence.setInterval(weeklyInterval);
537    
538                            List<DayAndPosition> dayPos = new ArrayList<DayAndPosition>();
539    
540                            _addWeeklyDayPos(portletRequest, dayPos, Calendar.SUNDAY);
541                            _addWeeklyDayPos(portletRequest, dayPos, Calendar.MONDAY);
542                            _addWeeklyDayPos(portletRequest, dayPos, Calendar.TUESDAY);
543                            _addWeeklyDayPos(portletRequest, dayPos, Calendar.WEDNESDAY);
544                            _addWeeklyDayPos(portletRequest, dayPos, Calendar.THURSDAY);
545                            _addWeeklyDayPos(portletRequest, dayPos, Calendar.FRIDAY);
546                            _addWeeklyDayPos(portletRequest, dayPos, Calendar.SATURDAY);
547    
548                            if (dayPos.size() == 0) {
549                                    dayPos.add(new DayAndPosition(Calendar.MONDAY, 0));
550                            }
551    
552                            recurrence.setByDay(dayPos.toArray(new DayAndPosition[0]));
553                    }
554                    else if (recurrenceType == Recurrence.MONTHLY) {
555                            int monthlyType = ParamUtil.getInteger(
556                                    portletRequest, "monthlyType");
557    
558                            if (monthlyType == 0) {
559                                    int monthlyDay = ParamUtil.getInteger(
560                                            portletRequest, "monthlyDay0", 1);
561    
562                                    recurrence.setByMonthDay(new int[] {monthlyDay});
563    
564                                    int monthlyInterval = ParamUtil.getInteger(
565                                            portletRequest, "monthlyInterval0", 1);
566    
567                                    recurrence.setInterval(monthlyInterval);
568                            }
569                            else {
570                                    int monthlyPos = ParamUtil.getInteger(
571                                            portletRequest, "monthlyPos");
572                                    int monthlyDay = ParamUtil.getInteger(
573                                            portletRequest, "monthlyDay1");
574    
575                                    DayAndPosition[] dayPos = {
576                                            new DayAndPosition(monthlyDay, monthlyPos)};
577    
578                                    recurrence.setByDay(dayPos);
579    
580                                    int monthlyInterval = ParamUtil.getInteger(
581                                            portletRequest, "monthlyInterval1", 1);
582    
583                                    recurrence.setInterval(monthlyInterval);
584                            }
585                    }
586                    else if (recurrenceType == Recurrence.YEARLY) {
587                            int yearlyType = ParamUtil.getInteger(portletRequest, "yearlyType");
588    
589                            if (yearlyType == 0) {
590                                    int yearlyMonth = ParamUtil.getInteger(
591                                            portletRequest, "yearlyMonth0");
592                                    int yearlyDay = ParamUtil.getInteger(
593                                            portletRequest, "yearlyDay0", 1);
594    
595                                    recurrence.setByMonth(new int[] {yearlyMonth});
596                                    recurrence.setByMonthDay(new int[] {yearlyDay});
597    
598                                    int yearlyInterval = ParamUtil.getInteger(
599                                            portletRequest, "yearlyInterval0", 1);
600    
601                                    recurrence.setInterval(yearlyInterval);
602                            }
603                            else {
604                                    int yearlyPos = ParamUtil.getInteger(
605                                            portletRequest, "yearlyPos");
606                                    int yearlyDay = ParamUtil.getInteger(
607                                            portletRequest, "yearlyDay1");
608                                    int yearlyMonth = ParamUtil.getInteger(
609                                            portletRequest, "yearlyMonth1");
610    
611                                    DayAndPosition[] dayPos = {
612                                            new DayAndPosition(yearlyDay, yearlyPos)};
613    
614                                    recurrence.setByDay(dayPos);
615    
616                                    recurrence.setByMonth(new int[] {yearlyMonth});
617    
618                                    int yearlyInterval = ParamUtil.getInteger(
619                                            portletRequest, "yearlyInterval1", 1);
620    
621                                    recurrence.setInterval(yearlyInterval);
622                            }
623                    }
624    
625                    return RecurrenceSerializer.toCronText(recurrence);
626            }
627    
628            private Date _getEndTime(SchedulerResponse schedulerResponse) {
629                    Message message = schedulerResponse.getMessage();
630    
631                    JobState jobState = (JobState)message.get(SchedulerEngine.JOB_STATE);
632    
633                    TriggerState triggerState = jobState.getTriggerState();
634    
635                    if (triggerState.equals(TriggerState.NORMAL) ||
636                            triggerState.equals(TriggerState.PAUSED)) {
637    
638                            return (Date)message.get(SchedulerEngine.END_TIME);
639                    }
640                    else {
641                            return jobState.getTriggerDate(SchedulerEngine.END_TIME);
642                    }
643            }
644    
645            private Date _getEndTime(
646                            String jobName, String groupName, StorageType storageType)
647                    throws SchedulerException {
648    
649                    SchedulerResponse schedulerResponse = _getScheduledJob(
650                            jobName, groupName, storageType);
651    
652                    if (schedulerResponse != null) {
653                            return _getEndTime(schedulerResponse);
654                    }
655    
656                    return null;
657            }
658    
659            private Date _getFinalFireTime(SchedulerResponse schedulerResponse) {
660                    Message message = schedulerResponse.getMessage();
661    
662                    JobState jobState = (JobState)message.get(SchedulerEngine.JOB_STATE);
663    
664                    TriggerState triggerState = jobState.getTriggerState();
665    
666                    if (triggerState.equals(TriggerState.NORMAL) ||
667                            triggerState.equals(TriggerState.PAUSED)) {
668    
669                            return (Date)message.get(SchedulerEngine.FINAL_FIRE_TIME);
670                    }
671                    else {
672                            return jobState.getTriggerDate(SchedulerEngine.FINAL_FIRE_TIME);
673                    }
674            }
675    
676            private Date _getFinalFireTime(
677                            String jobName, String groupName, StorageType storageType)
678                    throws SchedulerException {
679    
680                    SchedulerResponse schedulerResponse = _getScheduledJob(
681                            jobName, groupName, storageType);
682    
683                    if (schedulerResponse != null) {
684                            return _getFinalFireTime(schedulerResponse);
685                    }
686    
687                    return null;
688            }
689    
690            private ObjectValuePair<Exception, Date>[] _getJobExceptions(
691                    SchedulerResponse schedulerResponse) {
692    
693                    Message message = schedulerResponse.getMessage();
694    
695                    JobState jobState = (JobState)message.get(SchedulerEngine.JOB_STATE);
696    
697                    return jobState.getExceptions();
698            }
699    
700            private ObjectValuePair<Exception, Date>[] _getJobExceptions(
701                            String jobName, String groupName, StorageType storageType)
702                    throws SchedulerException {
703    
704                    SchedulerResponse schedulerResponse = _getScheduledJob(
705                            jobName, groupName, storageType);
706    
707                    if (schedulerResponse != null) {
708                            return _getJobExceptions(schedulerResponse);
709                    }
710    
711                    return null;
712            }
713    
714            private TriggerState _getJobState(SchedulerResponse schedulerResponse) {
715                    Message message = schedulerResponse.getMessage();
716    
717                    JobState jobState = (JobState)message.get(SchedulerEngine.JOB_STATE);
718    
719                    return jobState.getTriggerState();
720            }
721    
722            private TriggerState _getJobState(
723                            String jobName, String groupName, StorageType storageType)
724                    throws SchedulerException {
725    
726                    SchedulerResponse schedulerResponse = _getScheduledJob(
727                            jobName, groupName, storageType);
728    
729                    if (schedulerResponse != null) {
730                            return _getJobState(schedulerResponse);
731                    }
732    
733                    return null;
734            }
735    
736            private Date _getNextFireTime(SchedulerResponse schedulerResponse) {
737                    Message message = schedulerResponse.getMessage();
738    
739                    JobState jobState = (JobState)message.get(SchedulerEngine.JOB_STATE);
740    
741                    TriggerState triggerState = jobState.getTriggerState();
742    
743                    if (triggerState.equals(TriggerState.NORMAL) ||
744                            triggerState.equals(TriggerState.PAUSED)) {
745    
746                            return (Date)message.get(SchedulerEngine.NEXT_FIRE_TIME);
747                    }
748                    else {
749                            return jobState.getTriggerDate(SchedulerEngine.NEXT_FIRE_TIME);
750                    }
751            }
752    
753            private Date _getNextFireTime(
754                            String jobName, String groupName, StorageType storageType)
755                    throws SchedulerException {
756    
757                    SchedulerResponse schedulerResponse = _getScheduledJob(
758                            jobName, groupName, storageType);
759    
760                    if (schedulerResponse != null) {
761                            return _getNextFireTime(schedulerResponse);
762                    }
763    
764                    return null;
765            }
766    
767            private Date _getPreviousFireTime(SchedulerResponse schedulerResponse) {
768                    Message message = schedulerResponse.getMessage();
769    
770                    JobState jobState = (JobState)message.get(SchedulerEngine.JOB_STATE);
771    
772                    TriggerState triggerState = jobState.getTriggerState();
773    
774                    if (triggerState.equals(TriggerState.NORMAL) ||
775                            triggerState.equals(TriggerState.PAUSED)) {
776    
777                            return (Date)message.get(SchedulerEngine.PREVIOUS_FIRE_TIME);
778                    }
779                    else {
780                            return jobState.getTriggerDate(SchedulerEngine.PREVIOUS_FIRE_TIME);
781                    }
782            }
783    
784            private Date _getPreviousFireTime(
785                            String jobName, String groupName, StorageType storageType)
786                    throws SchedulerException {
787    
788                    SchedulerResponse schedulerResponse = _getScheduledJob(
789                            jobName, groupName, storageType);
790    
791                    if (schedulerResponse != null) {
792                            return _getPreviousFireTime(schedulerResponse);
793                    }
794    
795                    return null;
796            }
797    
798            private SchedulerResponse _getScheduledJob(
799                            String jobName, String groupName, StorageType storageType)
800                    throws SchedulerException {
801    
802                    return _schedulerEngine.getScheduledJob(
803                            jobName, _namespaceGroupName(groupName, storageType));
804            }
805    
806            private List<SchedulerResponse> _getScheduledJobs()
807                    throws SchedulerException {
808    
809                    return _schedulerEngine.getScheduledJobs();
810            }
811    
812            private List<SchedulerResponse> _getScheduledJobs(StorageType storageType)
813                    throws SchedulerException {
814    
815                    List<SchedulerResponse> schedulerResponses =
816                            new ArrayList<SchedulerResponse>();
817    
818                    for (SchedulerResponse schedulerResponse :
819                                    _schedulerEngine.getScheduledJobs()) {
820    
821                            if (storageType.equals(schedulerResponse.getStorageType())) {
822                                    schedulerResponses.add(schedulerResponse);
823                            }
824                    }
825    
826                    return schedulerResponses;
827            }
828    
829            private List<SchedulerResponse> _getScheduledJobs(
830                            String groupName, StorageType storageType)
831                    throws SchedulerException {
832    
833                    return _schedulerEngine.getScheduledJobs(
834                            _namespaceGroupName(groupName, storageType));
835            }
836    
837            private Date _getStartTime(SchedulerResponse schedulerResponse) {
838                    Message message = schedulerResponse.getMessage();
839    
840                    JobState jobState = (JobState)message.get(SchedulerEngine.JOB_STATE);
841    
842                    TriggerState triggerState = jobState.getTriggerState();
843    
844                    if (triggerState.equals(TriggerState.NORMAL) ||
845                            triggerState.equals(TriggerState.PAUSED)) {
846    
847                            return (Date)message.get(SchedulerEngine.START_TIME);
848                    }
849                    else {
850                            return jobState.getTriggerDate(SchedulerEngine.START_TIME);
851                    }
852            }
853    
854            private Date _getStartTime(
855                            String jobName, String groupName, StorageType storageType)
856                    throws SchedulerException {
857    
858                    SchedulerResponse schedulerResponse = _getScheduledJob(
859                            jobName, groupName, storageType);
860    
861                    if (schedulerResponse != null) {
862                            return _getStartTime(schedulerResponse);
863                    }
864    
865                    return null;
866            }
867    
868            private void _initialize() throws SchedulerException {
869                    if (_schedulerEngineClusterManager != null) {
870                            _schedulerEngineClusterManager.initialize();
871                    }
872            }
873    
874            private String _namespaceGroupName(
875                    String groupName, StorageType storageType) {
876    
877                    return storageType.toString().concat(StringPool.POUND).concat(
878                            groupName);
879            }
880    
881            private void _pause(String groupName, StorageType storageType)
882                    throws SchedulerException {
883    
884                    _schedulerEngine.pause(_namespaceGroupName(groupName, storageType));
885            }
886    
887            private void _pause(
888                            String jobName, String groupName, StorageType storageType)
889                    throws SchedulerException {
890    
891                    _schedulerEngine.pause(
892                            jobName, _namespaceGroupName(groupName, storageType));
893            }
894    
895            private void _resume(String groupName, StorageType storageType)
896                    throws SchedulerException {
897    
898                    _schedulerEngine.resume(_namespaceGroupName(groupName, storageType));
899            }
900    
901            private void _resume(
902                            String jobName, String groupName, StorageType storageType)
903                    throws SchedulerException {
904    
905                    _schedulerEngine.resume(
906                            jobName, _namespaceGroupName(groupName, storageType));
907            }
908    
909            private void _schedule(
910                            SchedulerEntry schedulerEntry, StorageType storageType,
911                            String portletId, int exceptionsMaxSize)
912                    throws SchedulerException {
913    
914                    Message message = new Message();
915    
916                    message.put(
917                            SchedulerEngine.CONTEXT_PATH, schedulerEntry.getContextPath());
918                    message.put(
919                            SchedulerEngine.MESSAGE_LISTENER_CLASS_NAME,
920                            schedulerEntry.getEventListenerClass());
921                    message.put(SchedulerEngine.PORTLET_ID, portletId);
922    
923                    _schedule(
924                            schedulerEntry.getTrigger(), storageType,
925                            schedulerEntry.getDescription(),
926                            DestinationNames.SCHEDULER_DISPATCH, message, exceptionsMaxSize);
927            }
928    
929            private void _schedule(
930                            Trigger trigger, StorageType storageType, String description,
931                            String destinationName, Message message, int exceptionsMaxSize)
932                    throws SchedulerException {
933    
934                    if (message == null) {
935                            message = new Message();
936                    }
937    
938                    message.put(SchedulerEngine.EXCEPTIONS_MAX_SIZE, exceptionsMaxSize);
939    
940                    trigger = TriggerFactoryUtil.buildTrigger(
941                            trigger.getTriggerType(), trigger.getJobName(),
942                            _namespaceGroupName(trigger.getGroupName(), storageType),
943                            trigger.getStartDate(), trigger.getEndDate(),
944                            trigger.getTriggerContent());
945    
946                    _schedulerEngine.schedule(
947                            trigger, description, destinationName, message);
948            }
949    
950            private void _schedule(
951                            Trigger trigger, StorageType storageType, String description,
952                            String destinationName, Object payload, int exceptionsMaxSize)
953                    throws SchedulerException {
954    
955                    Message message = new Message();
956    
957                    message.setPayload(payload);
958    
959                    _schedule(
960                            trigger, storageType, description, destinationName, message,
961                            exceptionsMaxSize);
962            }
963    
964            private void _shutdown() throws SchedulerException {
965                    _schedulerEngine.shutdown();
966            }
967    
968            private void _start() throws SchedulerException {
969                    _schedulerEngine.start();
970            }
971    
972            private void _suppressError(
973                            String jobName, String groupName, StorageType storageType)
974                    throws SchedulerException {
975    
976                    _schedulerEngine.suppressError(
977                            jobName, _namespaceGroupName(groupName, storageType));
978            }
979    
980            private void _unschedule(
981                            SchedulerEntry schedulerEntry, StorageType storageType)
982                    throws SchedulerException {
983    
984                    Trigger trigger = schedulerEntry.getTrigger();
985    
986                    _unschedule(trigger.getJobName(), trigger.getGroupName(), storageType);
987            }
988    
989            private void _unschedule(String groupName, StorageType storageType)
990                    throws SchedulerException {
991    
992                    _schedulerEngine.unschedule(
993                            _namespaceGroupName(groupName, storageType));
994            }
995    
996            private void _unschedule(
997                            String jobName, String groupName, StorageType storageType)
998                    throws SchedulerException {
999    
1000                    _schedulerEngine.unschedule(
1001                            jobName, _namespaceGroupName(groupName, storageType));
1002            }
1003    
1004            private void _update(
1005                            String jobName, String groupName, StorageType storageType,
1006                            String description, String language, String script,
1007                            int exceptionsMaxSize)
1008                    throws SchedulerException {
1009    
1010                    SchedulerResponse schedulerResponse = _getScheduledJob(
1011                            jobName, groupName, storageType);
1012    
1013                    if (schedulerResponse == null) {
1014                            return;
1015                    }
1016    
1017                    Trigger trigger = schedulerResponse.getTrigger();
1018    
1019                    if (trigger == null) {
1020                            return;
1021                    }
1022    
1023                    Message message = schedulerResponse.getMessage();
1024    
1025                    if (message == null) {
1026                            return;
1027                    }
1028    
1029                    _addScriptingJob(
1030                            trigger, storageType, description, language, script,
1031                            exceptionsMaxSize);
1032            }
1033    
1034            private void _update(Trigger trigger, StorageType storageType)
1035                    throws SchedulerException {
1036    
1037                    trigger = TriggerFactoryUtil.buildTrigger(
1038                            trigger.getTriggerType(), trigger.getJobName(),
1039                            _namespaceGroupName(trigger.getGroupName(), storageType),
1040                            trigger.getStartDate(), trigger.getEndDate(),
1041                            trigger.getTriggerContent());
1042    
1043                    _schedulerEngine.update(trigger);
1044            }
1045    
1046            private void _updateMemorySchedulerClusterMaster()
1047                    throws SchedulerException {
1048    
1049                    if (_schedulerEngineClusterManager == null) {
1050                            _log.error(
1051                                    "Unable to update memory scheduler cluster master because " +
1052                                            "the portal is not using a clustered scheduler engine");
1053    
1054                            return;
1055                    }
1056    
1057                    _schedulerEngineClusterManager.updateMemorySchedulerClusterMaster();
1058            }
1059    
1060            private static Log _log = LogFactoryUtil.getLog(SchedulerEngineUtil.class);
1061    
1062            private static SchedulerEngineUtil _instance = new SchedulerEngineUtil();
1063    
1064            private SchedulerEngine _schedulerEngine;
1065            private SchedulerEngineClusterManager _schedulerEngineClusterManager;
1066    
1067    }