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