001    /**
002     * Copyright (c) 2000-2011 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.bean.ClassLoaderBeanHandler;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.messaging.Destination;
021    import com.liferay.portal.kernel.messaging.DestinationNames;
022    import com.liferay.portal.kernel.messaging.InvokerMessageListener;
023    import com.liferay.portal.kernel.messaging.Message;
024    import com.liferay.portal.kernel.messaging.MessageBus;
025    import com.liferay.portal.kernel.messaging.MessageBusUtil;
026    import com.liferay.portal.kernel.messaging.MessageListener;
027    import com.liferay.portal.kernel.scheduler.messaging.SchedulerEventMessageListenerWrapper;
028    import com.liferay.portal.kernel.scheduler.messaging.SchedulerResponse;
029    import com.liferay.portal.kernel.util.ObjectValuePair;
030    import com.liferay.portal.kernel.util.ProxyUtil;
031    import com.liferay.portal.kernel.util.StringPool;
032    
033    import java.util.ArrayList;
034    import java.util.Date;
035    import java.util.List;
036    import java.util.Set;
037    
038    /**
039     * @author Bruno Farache
040     * @author Shuyang Zhou
041     * @author Tina Tian
042     */
043    public class SchedulerEngineUtil {
044    
045            public static void addScriptingJob(
046                            Trigger trigger, StorageType storageType, String description,
047                            String language, String script, int exceptionsMaxSize)
048                    throws SchedulerException {
049    
050                    _instance._addScriptingJob(
051                            trigger, storageType, description, language, script,
052                            exceptionsMaxSize);
053            }
054    
055            public static void delete(
056                            SchedulerEntry schedulerEntry, StorageType storageType)
057                    throws SchedulerException {
058    
059                    _instance._delete(schedulerEntry, storageType);
060            }
061    
062            public static void delete(String groupName, StorageType storageType)
063                    throws SchedulerException {
064    
065                    _instance._delete(groupName, storageType);
066            }
067    
068            public static void delete(
069                            String jobName, String groupName, StorageType storageType)
070                    throws SchedulerException {
071    
072                    _instance._delete(jobName, groupName, storageType);
073            }
074    
075            public static Date getEndTime(SchedulerResponse schedulerResponse) {
076                    return _instance._getEndTime(schedulerResponse);
077            }
078    
079            public static Date getEndTime(
080                            String jobName, String groupName, StorageType storageType)
081                    throws SchedulerException {
082    
083                    return _instance._getEndTime(jobName, groupName, storageType);
084            }
085    
086            public static Date getFinalFireTime(SchedulerResponse schedulerResponse) {
087                    return _instance._getFinalFireTime(schedulerResponse);
088            }
089    
090            public static Date getFinalFireTime(
091                            String jobName, String groupName, StorageType storageType)
092                    throws SchedulerException {
093    
094                    return _instance._getFinalFireTime(jobName, groupName, storageType);
095            }
096    
097            public static ObjectValuePair<Exception, Date>[] getJobExceptions(
098                    SchedulerResponse schedulerResponse) {
099    
100                    return _instance._getJobExceptions(schedulerResponse);
101            }
102    
103            public static ObjectValuePair<Exception, Date>[] getJobExceptions(
104                            String jobName, String groupName, StorageType storageType)
105                    throws SchedulerException {
106    
107                    return _instance._getJobExceptions(jobName, groupName, storageType);
108            }
109    
110            public static TriggerState getJobState(
111                    SchedulerResponse schedulerResponse) {
112    
113                    return _instance._getJobState(schedulerResponse);
114            }
115    
116            public static TriggerState getJobState(
117                            String jobName, String groupName, StorageType storageType)
118                    throws SchedulerException {
119    
120                    return _instance._getJobState(jobName, groupName, storageType);
121            }
122    
123            public static Date getNextFireTime(SchedulerResponse schedulerResponse) {
124                    return _instance._getNextFireTime(schedulerResponse);
125            }
126    
127            public static Date getNextFireTime(
128                            String jobName, String groupName, StorageType storageType)
129                    throws SchedulerException {
130    
131                    return _instance._getNextFireTime(jobName, groupName, storageType);
132            }
133    
134            public static Date getPreviousFireTime(
135                    SchedulerResponse schedulerResponse) {
136    
137                    return _instance._getPreviousFireTime(schedulerResponse);
138            }
139    
140            public static Date getPreviousFireTime(
141                            String jobName, String groupName, StorageType storageType)
142                    throws SchedulerException {
143    
144                    return _instance._getPreviousFireTime(jobName, groupName, storageType);
145            }
146    
147            public static SchedulerResponse getScheduledJob(
148                            String jobName, String groupName, StorageType storageType)
149                    throws SchedulerException {
150    
151                    return _instance._getScheduledJob(jobName, groupName, storageType);
152            }
153    
154            public static List<SchedulerResponse> getScheduledJobs()
155                    throws SchedulerException {
156    
157                    return _instance._getScheduledJobs();
158            }
159    
160            public static List<SchedulerResponse> getScheduledJobs(
161                            StorageType storageType)
162                    throws SchedulerException {
163    
164                    return _instance._getScheduledJobs(storageType);
165            }
166    
167            public static List<SchedulerResponse> getScheduledJobs(
168                            String groupName, StorageType storageType)
169                    throws SchedulerException {
170    
171                    return _instance._getScheduledJobs(groupName, storageType);
172            }
173    
174            public static Date getStartTime(SchedulerResponse schedulerResponse) {
175                    return _instance._getStartTime(schedulerResponse);
176            }
177    
178            public static Date getStartTime(
179                            String jobName, String groupName, StorageType storageType)
180                    throws SchedulerException {
181    
182                    return _instance._getStartTime(jobName, groupName, storageType);
183            }
184    
185            public static String namespaceGroupName(
186                    String groupName, StorageType storageType) {
187    
188                    return _instance._namespaceGroupName(groupName, storageType);
189            }
190    
191            public static void pause(String groupName, StorageType storageType)
192                    throws SchedulerException {
193    
194                    _instance._pause(groupName, storageType);
195            }
196    
197            public static void pause(
198                            String jobName, String groupName, StorageType storageType)
199                    throws SchedulerException {
200    
201                    _instance._pause(jobName, groupName, storageType);
202            }
203    
204            public static void resume(String groupName, StorageType storageType)
205                    throws SchedulerException {
206    
207                    _instance._resume(groupName, storageType);
208            }
209    
210            public static void resume(
211                            String jobName, String groupName, StorageType storageType)
212                    throws SchedulerException {
213    
214                    _instance._resume(jobName, groupName, storageType);
215            }
216    
217            public static void schedule(
218                            SchedulerEntry schedulerEntry, StorageType storageType,
219                            ClassLoader classLoader, int exceptionsMaxSize)
220                    throws SchedulerException {
221    
222                    _instance._schedule(
223                            schedulerEntry, storageType, classLoader, exceptionsMaxSize);
224            }
225    
226            public static void schedule(
227                            Trigger trigger, StorageType storageType, String description,
228                            String destinationName, Message message, int exceptionsMaxSize)
229                    throws SchedulerException {
230    
231                    _instance._schedule(
232                            trigger, storageType, description, destinationName, message,
233                            exceptionsMaxSize);
234            }
235    
236            public static void schedule(
237                            Trigger trigger, StorageType storageType, String description,
238                            String destinationName, Object payload, int exceptionsMaxSize)
239                    throws SchedulerException {
240    
241                    _instance._schedule(
242                            trigger, storageType, description, destinationName, payload,
243                            exceptionsMaxSize);
244            }
245    
246            public static void shutdown() throws SchedulerException {
247                    _instance._shutdown();
248            }
249    
250            public static void start() throws SchedulerException {
251                    _instance._start();
252            }
253    
254            public static void suppressError(
255                            String jobName, String groupName, StorageType storageType)
256                    throws SchedulerException {
257    
258                    _instance._suppressError(jobName, groupName, storageType);
259            }
260    
261            public static void unschedule(
262                            SchedulerEntry schedulerEntry, StorageType storageType)
263                    throws SchedulerException {
264    
265                    _instance._unschedule(schedulerEntry, storageType);
266            }
267    
268            public static void unschedule(String groupName, StorageType storageType)
269                    throws SchedulerException {
270    
271                    _instance._unschedule(groupName, storageType);
272            }
273    
274            public static void unschedule(
275                            String jobName, String groupName, StorageType storageType)
276                    throws SchedulerException {
277    
278                    _instance._unschedule(jobName, groupName, storageType);
279            }
280    
281            public static void update(
282                            String jobName, String groupName, StorageType storageType,
283                            String description, String language, String script,
284                            int exceptionsMaxSize)
285                    throws SchedulerException {
286    
287                    _instance._update(
288                            jobName, groupName, storageType, description, language, script,
289                            exceptionsMaxSize);
290            }
291    
292            public static void update(Trigger trigger, StorageType storageType)
293                    throws SchedulerException {
294    
295                    _instance._update(trigger, storageType);
296            }
297    
298            public static void updateMemorySchedulerClusterMaster()
299                    throws SchedulerException {
300    
301                    _instance._updateMemorySchedulerClusterMaster();
302            }
303    
304            public void setSchedulerEngine(SchedulerEngine schedulerEngine) {
305                    _instance._schedulerEngine = schedulerEngine;
306    
307                    if (schedulerEngine instanceof SchedulerEngineClusterManager) {
308                            _instance._schedulerEngineClusterManager =
309                                    (SchedulerEngineClusterManager)schedulerEngine;
310                    }
311            }
312    
313            private void _addScriptingJob(
314                            Trigger trigger, StorageType storageType, String description,
315                            String language, String script, int exceptionsMaxSize)
316                    throws SchedulerException {
317    
318                    Message message = new Message();
319    
320                    message.put(SchedulerEngine.LANGUAGE, language);
321                    message.put(SchedulerEngine.SCRIPT, script);
322    
323                    _schedule(
324                            trigger, storageType, description,
325                            DestinationNames.SCHEDULER_SCRIPTING, message, exceptionsMaxSize);
326            }
327    
328            private void _delete(SchedulerEntry schedulerEntry, StorageType storageType)
329                    throws SchedulerException {
330    
331                    Trigger trigger = schedulerEntry.getTrigger();
332    
333                    _delete(trigger.getJobName(), trigger.getGroupName(), storageType);
334            }
335    
336            private void _delete(String groupName, StorageType storageType)
337                    throws SchedulerException {
338    
339                    _unregisterMessageListener(groupName, storageType);
340    
341                    _schedulerEngine.delete(_namespaceGroupName(groupName, storageType));
342            }
343    
344            private void _delete(
345                            String jobName, String groupName, StorageType storageType)
346                    throws SchedulerException {
347    
348                    _unregisterMessageListener(jobName, groupName, storageType);
349    
350                    _schedulerEngine.delete(
351                            jobName, _namespaceGroupName(groupName, storageType));
352            }
353    
354            private Date _getEndTime(SchedulerResponse schedulerResponse) {
355                    Message message = schedulerResponse.getMessage();
356    
357                    JobState jobState = (JobState)message.get(SchedulerEngine.JOB_STATE);
358    
359                    TriggerState triggerState = jobState.getTriggerState();
360    
361                    if ((triggerState.equals(TriggerState.NORMAL)) ||
362                            (triggerState.equals(TriggerState.PAUSED))) {
363    
364                            return (Date)message.get(SchedulerEngine.END_TIME);
365                    }
366                    else {
367                            return jobState.getTriggerDate(SchedulerEngine.END_TIME);
368                    }
369            }
370    
371            private Date _getEndTime(
372                            String jobName, String groupName, StorageType storageType)
373                    throws SchedulerException {
374    
375                    SchedulerResponse schedulerResponse = _getScheduledJob(
376                            jobName, groupName, storageType);
377    
378                    if (schedulerResponse != null) {
379                            return _getEndTime(schedulerResponse);
380                    }
381    
382                    return null;
383            }
384    
385            private Date _getFinalFireTime(SchedulerResponse schedulerResponse) {
386                    Message message = schedulerResponse.getMessage();
387    
388                    JobState jobState = (JobState)message.get(SchedulerEngine.JOB_STATE);
389    
390                    TriggerState triggerState = jobState.getTriggerState();
391    
392                    if ((triggerState.equals(TriggerState.NORMAL)) ||
393                            (triggerState.equals(TriggerState.PAUSED))) {
394    
395                            return (Date)message.get(SchedulerEngine.FINAL_FIRE_TIME);
396                    }
397                    else {
398                            return jobState.getTriggerDate(SchedulerEngine.FINAL_FIRE_TIME);
399                    }
400            }
401    
402            private Date _getFinalFireTime(
403                            String jobName, String groupName, StorageType storageType)
404                    throws SchedulerException {
405    
406                    SchedulerResponse schedulerResponse = _getScheduledJob(
407                            jobName, groupName, storageType);
408    
409                    if (schedulerResponse != null) {
410                            return _getFinalFireTime(schedulerResponse);
411                    }
412    
413                    return null;
414            }
415    
416            private ObjectValuePair<Exception, Date>[] _getJobExceptions(
417                    SchedulerResponse schedulerResponse) {
418    
419                    Message message = schedulerResponse.getMessage();
420    
421                    JobState jobState = (JobState)message.get(SchedulerEngine.JOB_STATE);
422    
423                    return jobState.getExceptions();
424            }
425    
426            private ObjectValuePair<Exception, Date>[] _getJobExceptions(
427                            String jobName, String groupName, StorageType storageType)
428                    throws SchedulerException {
429    
430                    SchedulerResponse schedulerResponse = _getScheduledJob(
431                            jobName, groupName, storageType);
432    
433                    if (schedulerResponse != null) {
434                            return _getJobExceptions(schedulerResponse);
435                    }
436    
437                    return null;
438            }
439    
440            private TriggerState _getJobState(SchedulerResponse schedulerResponse) {
441                    Message message = schedulerResponse.getMessage();
442    
443                    JobState jobState = (JobState)message.get(SchedulerEngine.JOB_STATE);
444    
445                    return jobState.getTriggerState();
446            }
447    
448            private TriggerState _getJobState(
449                            String jobName, String groupName, StorageType storageType)
450                    throws SchedulerException {
451    
452                    SchedulerResponse schedulerResponse = _getScheduledJob(
453                            jobName, groupName, storageType);
454    
455                    if (schedulerResponse != null) {
456                            return _getJobState(schedulerResponse);
457                    }
458    
459                    return null;
460            }
461    
462            private Date _getNextFireTime(SchedulerResponse schedulerResponse) {
463                    Message message = schedulerResponse.getMessage();
464    
465                    JobState jobState = (JobState)message.get(SchedulerEngine.JOB_STATE);
466    
467                    TriggerState triggerState = jobState.getTriggerState();
468    
469                    if ((triggerState.equals(TriggerState.NORMAL)) ||
470                            (triggerState.equals(TriggerState.PAUSED))) {
471    
472                            return (Date)message.get(SchedulerEngine.NEXT_FIRE_TIME);
473                    }
474                    else {
475                            return jobState.getTriggerDate(SchedulerEngine.NEXT_FIRE_TIME);
476                    }
477            }
478    
479            private Date _getNextFireTime(
480                            String jobName, String groupName, StorageType storageType)
481                    throws SchedulerException {
482    
483                    SchedulerResponse schedulerResponse = _getScheduledJob(
484                            jobName, groupName, storageType);
485    
486                    if (schedulerResponse != null) {
487                            return _getNextFireTime(schedulerResponse);
488                    }
489    
490                    return null;
491            }
492    
493            private Date _getPreviousFireTime(SchedulerResponse schedulerResponse) {
494                    Message message = schedulerResponse.getMessage();
495    
496                    JobState jobState = (JobState)message.get(SchedulerEngine.JOB_STATE);
497    
498                    TriggerState triggerState = jobState.getTriggerState();
499    
500                    if ((triggerState.equals(TriggerState.NORMAL)) ||
501                            (triggerState.equals(TriggerState.PAUSED))) {
502    
503                            return (Date)message.get(SchedulerEngine.PREVIOUS_FIRE_TIME);
504                    }
505                    else {
506                            return jobState.getTriggerDate(SchedulerEngine.PREVIOUS_FIRE_TIME);
507                    }
508            }
509    
510            private Date _getPreviousFireTime(
511                            String jobName, String groupName, StorageType storageType)
512                    throws SchedulerException {
513    
514                    SchedulerResponse schedulerResponse = _getScheduledJob(
515                            jobName, groupName, storageType);
516    
517                    if (schedulerResponse != null) {
518                            return _getPreviousFireTime(schedulerResponse);
519                    }
520    
521                    return null;
522            }
523    
524            private SchedulerResponse _getScheduledJob(
525                            String jobName, String groupName, StorageType storageType)
526                    throws SchedulerException {
527    
528                    return _schedulerEngine.getScheduledJob(
529                            jobName, _namespaceGroupName(groupName, storageType));
530            }
531    
532            private List<SchedulerResponse> _getScheduledJobs()
533                    throws SchedulerException {
534    
535                    return _schedulerEngine.getScheduledJobs();
536            }
537    
538            private List<SchedulerResponse> _getScheduledJobs(StorageType storageType)
539                    throws SchedulerException {
540    
541                    List<SchedulerResponse> schedulerResponses =
542                            new ArrayList<SchedulerResponse>();
543    
544                    for (SchedulerResponse schedulerResponse :
545                                    _schedulerEngine.getScheduledJobs()) {
546    
547                            if (storageType.equals(schedulerResponse.getStorageType())) {
548                                    schedulerResponses.add(schedulerResponse);
549                            }
550                    }
551    
552                    return schedulerResponses;
553            }
554    
555            private List<SchedulerResponse> _getScheduledJobs(
556                            String groupName, StorageType storageType)
557                    throws SchedulerException {
558    
559                    return _schedulerEngine.getScheduledJobs(
560                            _namespaceGroupName(groupName, storageType));
561            }
562    
563            private MessageListener _getSchedulerEventListener(
564                            SchedulerEntry schedulerEntry, ClassLoader classLoader)
565                    throws SchedulerException {
566    
567                    try {
568                            MessageListener schedulerEventListener =
569                                    (MessageListener)classLoader.loadClass(
570                                            schedulerEntry.getEventListenerClass()).newInstance();
571    
572                            return (MessageListener)ProxyUtil.newProxyInstance(
573                                    classLoader, new Class[] {MessageListener.class},
574                                    new ClassLoaderBeanHandler(
575                                            schedulerEventListener, classLoader));
576                    }
577                    catch (Exception e) {
578                            throw new SchedulerException(e);
579                    }
580            }
581    
582            private Date _getStartTime(SchedulerResponse schedulerResponse) {
583                    Message message = schedulerResponse.getMessage();
584    
585                    JobState jobState = (JobState)message.get(SchedulerEngine.JOB_STATE);
586    
587                    TriggerState triggerState = jobState.getTriggerState();
588    
589                    if ((triggerState.equals(TriggerState.NORMAL)) ||
590                            (triggerState.equals(TriggerState.PAUSED))) {
591    
592                            return (Date)message.get(SchedulerEngine.START_TIME);
593                    }
594                    else {
595                            return jobState.getTriggerDate(SchedulerEngine.START_TIME);
596                    }
597            }
598    
599            private Date _getStartTime(
600                            String jobName, String groupName, StorageType storageType)
601                    throws SchedulerException {
602    
603                    SchedulerResponse schedulerResponse = _getScheduledJob(
604                            jobName, groupName, storageType);
605    
606                    if (schedulerResponse != null) {
607                            return _getStartTime(schedulerResponse);
608                    }
609    
610                    return null;
611            }
612    
613            private String _namespaceGroupName(
614                    String groupName, StorageType storageType) {
615    
616                    return storageType.toString().concat(StringPool.POUND).concat(
617                            groupName);
618            }
619    
620            private void _pause(String groupName, StorageType storageType)
621                    throws SchedulerException {
622    
623                    _schedulerEngine.pause(_namespaceGroupName(groupName, storageType));
624            }
625    
626            private void _pause(
627                            String jobName, String groupName, StorageType storageType)
628                    throws SchedulerException {
629    
630                    _schedulerEngine.pause(
631                            jobName, _namespaceGroupName(groupName, storageType));
632            }
633    
634            private void _resume(String groupName, StorageType storageType)
635                    throws SchedulerException {
636    
637                    _schedulerEngine.resume(_namespaceGroupName(groupName, storageType));
638            }
639    
640            private void _resume(
641                            String jobName, String groupName, StorageType storageType)
642                    throws SchedulerException {
643    
644                    _schedulerEngine.resume(
645                            jobName, _namespaceGroupName(groupName, storageType));
646            }
647    
648            private void _schedule(
649                            SchedulerEntry schedulerEntry, StorageType storageType,
650                            ClassLoader classLoader, int exceptionsMaxSize)
651                    throws SchedulerException {
652    
653                    SchedulerEventMessageListenerWrapper schedulerEventListenerWrapper =
654                            new SchedulerEventMessageListenerWrapper();
655    
656                    schedulerEventListenerWrapper.setClassName(
657                            schedulerEntry.getEventListenerClass());
658                    schedulerEventListenerWrapper.setMessageListener(
659                            _getSchedulerEventListener(schedulerEntry, classLoader));
660    
661                    schedulerEventListenerWrapper.afterPropertiesSet();
662    
663                    schedulerEntry.setEventListener(schedulerEventListenerWrapper);
664    
665                    MessageBusUtil.registerMessageListener(
666                            DestinationNames.SCHEDULER_DISPATCH, schedulerEventListenerWrapper);
667    
668                    Message message = new Message();
669    
670                    message.put(
671                            SchedulerEngine.CONTEXT_PATH, schedulerEntry.getContextPath());
672                    message.put(
673                            SchedulerEngine.MESSAGE_LISTENER_UUID,
674                            schedulerEventListenerWrapper.getMessageListenerUUID());
675    
676                    _schedule(
677                            schedulerEntry.getTrigger(), storageType,
678                            schedulerEntry.getDescription(),
679                            DestinationNames.SCHEDULER_DISPATCH, message, exceptionsMaxSize);
680            }
681    
682            private void _schedule(
683                            Trigger trigger, StorageType storageType, String description,
684                            String destinationName, Message message, int exceptionsMaxSize)
685                    throws SchedulerException {
686    
687                    if (message == null) {
688                            message = new Message();
689                    }
690    
691                    message.put(SchedulerEngine.EXCEPTIONS_MAX_SIZE, exceptionsMaxSize);
692    
693                    trigger = TriggerFactoryUtil.buildTrigger(
694                            trigger.getTriggerType(), trigger.getJobName(),
695                            _namespaceGroupName(trigger.getGroupName(), storageType),
696                            trigger.getStartDate(), trigger.getEndDate(),
697                            trigger.getTriggerContent());
698    
699                    _schedulerEngine.schedule(
700                            trigger, description, destinationName, message);
701            }
702    
703            private void _schedule(
704                            Trigger trigger, StorageType storageType, String description,
705                            String destinationName, Object payload, int exceptionsMaxSize)
706                    throws SchedulerException {
707    
708                    Message message = new Message();
709    
710                    message.setPayload(payload);
711    
712                    _schedule(
713                            trigger, storageType, description, destinationName, message,
714                            exceptionsMaxSize);
715            }
716    
717            private void _shutdown() throws SchedulerException {
718                    _schedulerEngine.shutdown();
719            }
720    
721            private void _start() throws SchedulerException {
722                    _schedulerEngine.start();
723            }
724    
725            private void _suppressError(
726                            String jobName, String groupName, StorageType storageType)
727                    throws SchedulerException {
728    
729                    _schedulerEngine.suppressError(
730                            jobName, _namespaceGroupName(groupName, storageType));
731            }
732    
733            private void _unregisterMessageListener(
734                    SchedulerResponse schedulerResponse) {
735    
736                    if (schedulerResponse == null) {
737                            return;
738                    }
739    
740                    String destinationName = schedulerResponse.getDestinationName();
741    
742                    if (!destinationName.equals(DestinationNames.SCHEDULER_DISPATCH)) {
743                            return;
744                    }
745    
746                    Message message = schedulerResponse.getMessage();
747    
748                    String messageListenerUUID = message.getString(
749                            SchedulerEngine.MESSAGE_LISTENER_UUID);
750    
751                    if (messageListenerUUID == null) {
752                            return;
753                    }
754    
755                    MessageBus messageBus = MessageBusUtil.getMessageBus();
756    
757                    Destination destination = messageBus.getDestination(
758                            DestinationNames.SCHEDULER_DISPATCH);
759    
760                    Set<MessageListener> messageListeners =
761                            destination.getMessageListeners();
762    
763                    for (MessageListener messageListener : messageListeners) {
764                            InvokerMessageListener invokerMessageListener =
765                                    (InvokerMessageListener)messageListener;
766    
767                            SchedulerEventMessageListenerWrapper schedulerMessageListener =
768                                    (SchedulerEventMessageListenerWrapper)
769                                            invokerMessageListener.getMessageListener();
770    
771                            if (messageListenerUUID.equals(
772                                            schedulerMessageListener.getMessageListenerUUID())) {
773    
774                                    MessageBusUtil.unregisterMessageListener(
775                                            DestinationNames.SCHEDULER_DISPATCH,
776                                            schedulerMessageListener);
777    
778                                    return;
779                            }
780                    }
781            }
782    
783            private void _unregisterMessageListener(
784                            String groupName, StorageType storageType)
785                    throws SchedulerException {
786    
787                    List<SchedulerResponse> schedulerResponses = _getScheduledJobs(
788                            groupName, storageType);
789    
790                    for (SchedulerResponse schedulerResponse : schedulerResponses) {
791                            _unregisterMessageListener(schedulerResponse);
792                    }
793            }
794    
795            private void _unregisterMessageListener(
796                            String jobName, String groupName, StorageType storageType)
797                    throws SchedulerException {
798    
799                    SchedulerResponse schedulerResponse = _getScheduledJob(
800                            jobName, groupName, storageType);
801    
802                    _unregisterMessageListener(schedulerResponse);
803            }
804    
805            private void _unschedule(
806                            SchedulerEntry schedulerEntry, StorageType storageType)
807                    throws SchedulerException {
808    
809                    Trigger trigger = schedulerEntry.getTrigger();
810    
811                    _unschedule(trigger.getJobName(), trigger.getGroupName(), storageType);
812            }
813    
814            private void _unschedule(String groupName, StorageType storageType)
815                    throws SchedulerException {
816    
817                    _unregisterMessageListener(groupName, storageType);
818    
819                    _schedulerEngine.unschedule(
820                            _namespaceGroupName(groupName, storageType));
821            }
822    
823            private void _unschedule(
824                            String jobName, String groupName, StorageType storageType)
825                    throws SchedulerException {
826    
827                    _unregisterMessageListener(jobName, groupName, storageType);
828    
829                    _schedulerEngine.unschedule(
830                            jobName, _namespaceGroupName(groupName, storageType));
831            }
832    
833            private void _update(
834                            String jobName, String groupName, StorageType storageType,
835                            String description, String language, String script,
836                            int exceptionsMaxSize)
837                    throws SchedulerException {
838    
839                    SchedulerResponse schedulerResponse = _getScheduledJob(
840                            jobName, groupName, storageType);
841    
842                    if (schedulerResponse == null) {
843                            return;
844                    }
845    
846                    Trigger trigger = schedulerResponse.getTrigger();
847    
848                    if (trigger == null) {
849                            return;
850                    }
851    
852                    Message message = schedulerResponse.getMessage();
853    
854                    if (message == null) {
855                            return;
856                    }
857    
858                    _unregisterMessageListener(schedulerResponse);
859    
860                    _addScriptingJob(
861                            trigger, storageType, description, language, script,
862                            exceptionsMaxSize);
863            }
864    
865            private void _update(Trigger trigger, StorageType storageType)
866                    throws SchedulerException {
867    
868                    trigger = TriggerFactoryUtil.buildTrigger(
869                            trigger.getTriggerType(), trigger.getJobName(),
870                            _namespaceGroupName(trigger.getGroupName(), storageType),
871                            trigger.getStartDate(), trigger.getEndDate(),
872                            trigger.getTriggerContent());
873    
874                    _schedulerEngine.update(trigger);
875            }
876    
877            private void _updateMemorySchedulerClusterMaster()
878                    throws SchedulerException {
879    
880                    if (_schedulerEngineClusterManager == null) {
881                            _log.error(
882                                    "Unable to update memory scheduler cluster master because " +
883                                            "the portal is not using a clustered scheduler engine");
884    
885                            return;
886                    }
887    
888                    _schedulerEngineClusterManager.updateMemorySchedulerClusterMaster();
889            }
890    
891            private static Log _log = LogFactoryUtil.getLog(SchedulerEngineUtil.class);
892    
893            private static SchedulerEngineUtil _instance = new SchedulerEngineUtil();
894    
895            private SchedulerEngine _schedulerEngine;
896            private SchedulerEngineClusterManager _schedulerEngineClusterManager;
897    
898    }