001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
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.PortalLifecycle;
031    import com.liferay.portal.kernel.util.ProxyUtil;
032    import com.liferay.portal.kernel.util.StringPool;
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 void initialize() throws SchedulerException {
187                    _instance._initialize();
188    
189                    SchedulerLifecycle schedulerLifecycle = new SchedulerLifecycle();
190    
191                    schedulerLifecycle.registerPortalLifecycle(PortalLifecycle.METHOD_INIT);
192            }
193    
194            public static String namespaceGroupName(
195                    String groupName, StorageType storageType) {
196    
197                    return _instance._namespaceGroupName(groupName, storageType);
198            }
199    
200            public static void pause(String groupName, StorageType storageType)
201                    throws SchedulerException {
202    
203                    _instance._pause(groupName, storageType);
204            }
205    
206            public static void pause(
207                            String jobName, String groupName, StorageType storageType)
208                    throws SchedulerException {
209    
210                    _instance._pause(jobName, groupName, storageType);
211            }
212    
213            public static void resume(String groupName, StorageType storageType)
214                    throws SchedulerException {
215    
216                    _instance._resume(groupName, storageType);
217            }
218    
219            public static void resume(
220                            String jobName, String groupName, StorageType storageType)
221                    throws SchedulerException {
222    
223                    _instance._resume(jobName, groupName, storageType);
224            }
225    
226            public static void schedule(
227                            SchedulerEntry schedulerEntry, StorageType storageType,
228                            ClassLoader classLoader, int exceptionsMaxSize)
229                    throws SchedulerException {
230    
231                    _instance._schedule(
232                            schedulerEntry, storageType, classLoader, exceptionsMaxSize);
233            }
234    
235            public static void schedule(
236                            Trigger trigger, StorageType storageType, String description,
237                            String destinationName, Message message, int exceptionsMaxSize)
238                    throws SchedulerException {
239    
240                    _instance._schedule(
241                            trigger, storageType, description, destinationName, message,
242                            exceptionsMaxSize);
243            }
244    
245            public static void schedule(
246                            Trigger trigger, StorageType storageType, String description,
247                            String destinationName, Object payload, int exceptionsMaxSize)
248                    throws SchedulerException {
249    
250                    _instance._schedule(
251                            trigger, storageType, description, destinationName, payload,
252                            exceptionsMaxSize);
253            }
254    
255            public static void shutdown() throws SchedulerException {
256                    _instance._shutdown();
257            }
258    
259            public static void start() throws SchedulerException {
260                    _instance._start();
261            }
262    
263            public static void suppressError(
264                            String jobName, String groupName, StorageType storageType)
265                    throws SchedulerException {
266    
267                    _instance._suppressError(jobName, groupName, storageType);
268            }
269    
270            public static void unschedule(
271                            SchedulerEntry schedulerEntry, StorageType storageType)
272                    throws SchedulerException {
273    
274                    _instance._unschedule(schedulerEntry, storageType);
275            }
276    
277            public static void unschedule(String groupName, StorageType storageType)
278                    throws SchedulerException {
279    
280                    _instance._unschedule(groupName, storageType);
281            }
282    
283            public static void unschedule(
284                            String jobName, String groupName, StorageType storageType)
285                    throws SchedulerException {
286    
287                    _instance._unschedule(jobName, groupName, storageType);
288            }
289    
290            public static void update(
291                            String jobName, String groupName, StorageType storageType,
292                            String description, String language, String script,
293                            int exceptionsMaxSize)
294                    throws SchedulerException {
295    
296                    _instance._update(
297                            jobName, groupName, storageType, description, language, script,
298                            exceptionsMaxSize);
299            }
300    
301            public static void update(Trigger trigger, StorageType storageType)
302                    throws SchedulerException {
303    
304                    _instance._update(trigger, storageType);
305            }
306    
307            public static void updateMemorySchedulerClusterMaster()
308                    throws SchedulerException {
309    
310                    _instance._updateMemorySchedulerClusterMaster();
311            }
312    
313            public void setSchedulerEngine(SchedulerEngine schedulerEngine) {
314                    _instance._schedulerEngine = schedulerEngine;
315    
316                    if (schedulerEngine instanceof SchedulerEngineClusterManager) {
317                            _instance._schedulerEngineClusterManager =
318                                    (SchedulerEngineClusterManager)schedulerEngine;
319                    }
320            }
321    
322            private void _addScriptingJob(
323                            Trigger trigger, StorageType storageType, String description,
324                            String language, String script, int exceptionsMaxSize)
325                    throws SchedulerException {
326    
327                    Message message = new Message();
328    
329                    message.put(SchedulerEngine.LANGUAGE, language);
330                    message.put(SchedulerEngine.SCRIPT, script);
331    
332                    _schedule(
333                            trigger, storageType, description,
334                            DestinationNames.SCHEDULER_SCRIPTING, message, exceptionsMaxSize);
335            }
336    
337            private void _delete(SchedulerEntry schedulerEntry, StorageType storageType)
338                    throws SchedulerException {
339    
340                    Trigger trigger = schedulerEntry.getTrigger();
341    
342                    _delete(trigger.getJobName(), trigger.getGroupName(), storageType);
343            }
344    
345            private void _delete(String groupName, StorageType storageType)
346                    throws SchedulerException {
347    
348                    _unregisterMessageListener(groupName, storageType);
349    
350                    _schedulerEngine.delete(_namespaceGroupName(groupName, storageType));
351            }
352    
353            private void _delete(
354                            String jobName, String groupName, StorageType storageType)
355                    throws SchedulerException {
356    
357                    _unregisterMessageListener(jobName, groupName, storageType);
358    
359                    _schedulerEngine.delete(
360                            jobName, _namespaceGroupName(groupName, storageType));
361            }
362    
363            private Date _getEndTime(SchedulerResponse schedulerResponse) {
364                    Message message = schedulerResponse.getMessage();
365    
366                    JobState jobState = (JobState)message.get(SchedulerEngine.JOB_STATE);
367    
368                    TriggerState triggerState = jobState.getTriggerState();
369    
370                    if ((triggerState.equals(TriggerState.NORMAL)) ||
371                            (triggerState.equals(TriggerState.PAUSED))) {
372    
373                            return (Date)message.get(SchedulerEngine.END_TIME);
374                    }
375                    else {
376                            return jobState.getTriggerDate(SchedulerEngine.END_TIME);
377                    }
378            }
379    
380            private Date _getEndTime(
381                            String jobName, String groupName, StorageType storageType)
382                    throws SchedulerException {
383    
384                    SchedulerResponse schedulerResponse = _getScheduledJob(
385                            jobName, groupName, storageType);
386    
387                    if (schedulerResponse != null) {
388                            return _getEndTime(schedulerResponse);
389                    }
390    
391                    return null;
392            }
393    
394            private Date _getFinalFireTime(SchedulerResponse schedulerResponse) {
395                    Message message = schedulerResponse.getMessage();
396    
397                    JobState jobState = (JobState)message.get(SchedulerEngine.JOB_STATE);
398    
399                    TriggerState triggerState = jobState.getTriggerState();
400    
401                    if ((triggerState.equals(TriggerState.NORMAL)) ||
402                            (triggerState.equals(TriggerState.PAUSED))) {
403    
404                            return (Date)message.get(SchedulerEngine.FINAL_FIRE_TIME);
405                    }
406                    else {
407                            return jobState.getTriggerDate(SchedulerEngine.FINAL_FIRE_TIME);
408                    }
409            }
410    
411            private Date _getFinalFireTime(
412                            String jobName, String groupName, StorageType storageType)
413                    throws SchedulerException {
414    
415                    SchedulerResponse schedulerResponse = _getScheduledJob(
416                            jobName, groupName, storageType);
417    
418                    if (schedulerResponse != null) {
419                            return _getFinalFireTime(schedulerResponse);
420                    }
421    
422                    return null;
423            }
424    
425            private ObjectValuePair<Exception, Date>[] _getJobExceptions(
426                    SchedulerResponse schedulerResponse) {
427    
428                    Message message = schedulerResponse.getMessage();
429    
430                    JobState jobState = (JobState)message.get(SchedulerEngine.JOB_STATE);
431    
432                    return jobState.getExceptions();
433            }
434    
435            private ObjectValuePair<Exception, Date>[] _getJobExceptions(
436                            String jobName, String groupName, StorageType storageType)
437                    throws SchedulerException {
438    
439                    SchedulerResponse schedulerResponse = _getScheduledJob(
440                            jobName, groupName, storageType);
441    
442                    if (schedulerResponse != null) {
443                            return _getJobExceptions(schedulerResponse);
444                    }
445    
446                    return null;
447            }
448    
449            private TriggerState _getJobState(SchedulerResponse schedulerResponse) {
450                    Message message = schedulerResponse.getMessage();
451    
452                    JobState jobState = (JobState)message.get(SchedulerEngine.JOB_STATE);
453    
454                    return jobState.getTriggerState();
455            }
456    
457            private TriggerState _getJobState(
458                            String jobName, String groupName, StorageType storageType)
459                    throws SchedulerException {
460    
461                    SchedulerResponse schedulerResponse = _getScheduledJob(
462                            jobName, groupName, storageType);
463    
464                    if (schedulerResponse != null) {
465                            return _getJobState(schedulerResponse);
466                    }
467    
468                    return null;
469            }
470    
471            private Date _getNextFireTime(SchedulerResponse schedulerResponse) {
472                    Message message = schedulerResponse.getMessage();
473    
474                    JobState jobState = (JobState)message.get(SchedulerEngine.JOB_STATE);
475    
476                    TriggerState triggerState = jobState.getTriggerState();
477    
478                    if ((triggerState.equals(TriggerState.NORMAL)) ||
479                            (triggerState.equals(TriggerState.PAUSED))) {
480    
481                            return (Date)message.get(SchedulerEngine.NEXT_FIRE_TIME);
482                    }
483                    else {
484                            return jobState.getTriggerDate(SchedulerEngine.NEXT_FIRE_TIME);
485                    }
486            }
487    
488            private Date _getNextFireTime(
489                            String jobName, String groupName, StorageType storageType)
490                    throws SchedulerException {
491    
492                    SchedulerResponse schedulerResponse = _getScheduledJob(
493                            jobName, groupName, storageType);
494    
495                    if (schedulerResponse != null) {
496                            return _getNextFireTime(schedulerResponse);
497                    }
498    
499                    return null;
500            }
501    
502            private Date _getPreviousFireTime(SchedulerResponse schedulerResponse) {
503                    Message message = schedulerResponse.getMessage();
504    
505                    JobState jobState = (JobState)message.get(SchedulerEngine.JOB_STATE);
506    
507                    TriggerState triggerState = jobState.getTriggerState();
508    
509                    if ((triggerState.equals(TriggerState.NORMAL)) ||
510                            (triggerState.equals(TriggerState.PAUSED))) {
511    
512                            return (Date)message.get(SchedulerEngine.PREVIOUS_FIRE_TIME);
513                    }
514                    else {
515                            return jobState.getTriggerDate(SchedulerEngine.PREVIOUS_FIRE_TIME);
516                    }
517            }
518    
519            private Date _getPreviousFireTime(
520                            String jobName, String groupName, StorageType storageType)
521                    throws SchedulerException {
522    
523                    SchedulerResponse schedulerResponse = _getScheduledJob(
524                            jobName, groupName, storageType);
525    
526                    if (schedulerResponse != null) {
527                            return _getPreviousFireTime(schedulerResponse);
528                    }
529    
530                    return null;
531            }
532    
533            private SchedulerResponse _getScheduledJob(
534                            String jobName, String groupName, StorageType storageType)
535                    throws SchedulerException {
536    
537                    return _schedulerEngine.getScheduledJob(
538                            jobName, _namespaceGroupName(groupName, storageType));
539            }
540    
541            private List<SchedulerResponse> _getScheduledJobs()
542                    throws SchedulerException {
543    
544                    return _schedulerEngine.getScheduledJobs();
545            }
546    
547            private List<SchedulerResponse> _getScheduledJobs(StorageType storageType)
548                    throws SchedulerException {
549    
550                    List<SchedulerResponse> schedulerResponses =
551                            new ArrayList<SchedulerResponse>();
552    
553                    for (SchedulerResponse schedulerResponse :
554                                    _schedulerEngine.getScheduledJobs()) {
555    
556                            if (storageType.equals(schedulerResponse.getStorageType())) {
557                                    schedulerResponses.add(schedulerResponse);
558                            }
559                    }
560    
561                    return schedulerResponses;
562            }
563    
564            private List<SchedulerResponse> _getScheduledJobs(
565                            String groupName, StorageType storageType)
566                    throws SchedulerException {
567    
568                    return _schedulerEngine.getScheduledJobs(
569                            _namespaceGroupName(groupName, storageType));
570            }
571    
572            private MessageListener _getSchedulerEventListener(
573                            SchedulerEntry schedulerEntry, ClassLoader classLoader)
574                    throws SchedulerException {
575    
576                    try {
577                            MessageListener schedulerEventListener =
578                                    (MessageListener)classLoader.loadClass(
579                                            schedulerEntry.getEventListenerClass()).newInstance();
580    
581                            return (MessageListener)ProxyUtil.newProxyInstance(
582                                    classLoader, new Class[] {MessageListener.class},
583                                    new ClassLoaderBeanHandler(
584                                            schedulerEventListener, classLoader));
585                    }
586                    catch (Exception e) {
587                            throw new SchedulerException(e);
588                    }
589            }
590    
591            private Date _getStartTime(SchedulerResponse schedulerResponse) {
592                    Message message = schedulerResponse.getMessage();
593    
594                    JobState jobState = (JobState)message.get(SchedulerEngine.JOB_STATE);
595    
596                    TriggerState triggerState = jobState.getTriggerState();
597    
598                    if ((triggerState.equals(TriggerState.NORMAL)) ||
599                            (triggerState.equals(TriggerState.PAUSED))) {
600    
601                            return (Date)message.get(SchedulerEngine.START_TIME);
602                    }
603                    else {
604                            return jobState.getTriggerDate(SchedulerEngine.START_TIME);
605                    }
606            }
607    
608            private Date _getStartTime(
609                            String jobName, String groupName, StorageType storageType)
610                    throws SchedulerException {
611    
612                    SchedulerResponse schedulerResponse = _getScheduledJob(
613                            jobName, groupName, storageType);
614    
615                    if (schedulerResponse != null) {
616                            return _getStartTime(schedulerResponse);
617                    }
618    
619                    return null;
620            }
621    
622            private void _initialize() throws SchedulerException {
623                    if (_schedulerEngineClusterManager != null) {
624                            _schedulerEngineClusterManager.initialize();
625                    }
626            }
627    
628            private String _namespaceGroupName(
629                    String groupName, StorageType storageType) {
630    
631                    return storageType.toString().concat(StringPool.POUND).concat(
632                            groupName);
633            }
634    
635            private void _pause(String groupName, StorageType storageType)
636                    throws SchedulerException {
637    
638                    _schedulerEngine.pause(_namespaceGroupName(groupName, storageType));
639            }
640    
641            private void _pause(
642                            String jobName, String groupName, StorageType storageType)
643                    throws SchedulerException {
644    
645                    _schedulerEngine.pause(
646                            jobName, _namespaceGroupName(groupName, storageType));
647            }
648    
649            private void _resume(String groupName, StorageType storageType)
650                    throws SchedulerException {
651    
652                    _schedulerEngine.resume(_namespaceGroupName(groupName, storageType));
653            }
654    
655            private void _resume(
656                            String jobName, String groupName, StorageType storageType)
657                    throws SchedulerException {
658    
659                    _schedulerEngine.resume(
660                            jobName, _namespaceGroupName(groupName, storageType));
661            }
662    
663            private void _schedule(
664                            SchedulerEntry schedulerEntry, StorageType storageType,
665                            ClassLoader classLoader, int exceptionsMaxSize)
666                    throws SchedulerException {
667    
668                    SchedulerEventMessageListenerWrapper schedulerEventListenerWrapper =
669                            new SchedulerEventMessageListenerWrapper();
670    
671                    schedulerEventListenerWrapper.setClassName(
672                            schedulerEntry.getEventListenerClass());
673                    schedulerEventListenerWrapper.setMessageListener(
674                            _getSchedulerEventListener(schedulerEntry, classLoader));
675    
676                    schedulerEventListenerWrapper.afterPropertiesSet();
677    
678                    schedulerEntry.setEventListener(schedulerEventListenerWrapper);
679    
680                    MessageBusUtil.registerMessageListener(
681                            DestinationNames.SCHEDULER_DISPATCH, schedulerEventListenerWrapper);
682    
683                    Message message = new Message();
684    
685                    message.put(
686                            SchedulerEngine.CONTEXT_PATH, schedulerEntry.getContextPath());
687                    message.put(
688                            SchedulerEngine.MESSAGE_LISTENER_UUID,
689                            schedulerEventListenerWrapper.getMessageListenerUUID());
690    
691                    _schedule(
692                            schedulerEntry.getTrigger(), storageType,
693                            schedulerEntry.getDescription(),
694                            DestinationNames.SCHEDULER_DISPATCH, message, exceptionsMaxSize);
695            }
696    
697            private void _schedule(
698                            Trigger trigger, StorageType storageType, String description,
699                            String destinationName, Message message, int exceptionsMaxSize)
700                    throws SchedulerException {
701    
702                    if (message == null) {
703                            message = new Message();
704                    }
705    
706                    message.put(SchedulerEngine.EXCEPTIONS_MAX_SIZE, exceptionsMaxSize);
707    
708                    trigger = TriggerFactoryUtil.buildTrigger(
709                            trigger.getTriggerType(), trigger.getJobName(),
710                            _namespaceGroupName(trigger.getGroupName(), storageType),
711                            trigger.getStartDate(), trigger.getEndDate(),
712                            trigger.getTriggerContent());
713    
714                    _schedulerEngine.schedule(
715                            trigger, description, destinationName, message);
716            }
717    
718            private void _schedule(
719                            Trigger trigger, StorageType storageType, String description,
720                            String destinationName, Object payload, int exceptionsMaxSize)
721                    throws SchedulerException {
722    
723                    Message message = new Message();
724    
725                    message.setPayload(payload);
726    
727                    _schedule(
728                            trigger, storageType, description, destinationName, message,
729                            exceptionsMaxSize);
730            }
731    
732            private void _shutdown() throws SchedulerException {
733                    _schedulerEngine.shutdown();
734            }
735    
736            private void _start() throws SchedulerException {
737                    _schedulerEngine.start();
738            }
739    
740            private void _suppressError(
741                            String jobName, String groupName, StorageType storageType)
742                    throws SchedulerException {
743    
744                    _schedulerEngine.suppressError(
745                            jobName, _namespaceGroupName(groupName, storageType));
746            }
747    
748            private void _unregisterMessageListener(
749                    SchedulerResponse schedulerResponse) {
750    
751                    if (schedulerResponse == null) {
752                            return;
753                    }
754    
755                    String destinationName = schedulerResponse.getDestinationName();
756    
757                    if (!destinationName.equals(DestinationNames.SCHEDULER_DISPATCH)) {
758                            return;
759                    }
760    
761                    Message message = schedulerResponse.getMessage();
762    
763                    String messageListenerUUID = message.getString(
764                            SchedulerEngine.MESSAGE_LISTENER_UUID);
765    
766                    if (messageListenerUUID == null) {
767                            return;
768                    }
769    
770                    MessageBus messageBus = MessageBusUtil.getMessageBus();
771    
772                    Destination destination = messageBus.getDestination(
773                            DestinationNames.SCHEDULER_DISPATCH);
774    
775                    Set<MessageListener> messageListeners =
776                            destination.getMessageListeners();
777    
778                    for (MessageListener messageListener : messageListeners) {
779                            InvokerMessageListener invokerMessageListener =
780                                    (InvokerMessageListener)messageListener;
781    
782                            SchedulerEventMessageListenerWrapper schedulerMessageListener =
783                                    (SchedulerEventMessageListenerWrapper)
784                                            invokerMessageListener.getMessageListener();
785    
786                            if (messageListenerUUID.equals(
787                                            schedulerMessageListener.getMessageListenerUUID())) {
788    
789                                    MessageBusUtil.unregisterMessageListener(
790                                            DestinationNames.SCHEDULER_DISPATCH,
791                                            schedulerMessageListener);
792    
793                                    return;
794                            }
795                    }
796            }
797    
798            private void _unregisterMessageListener(
799                            String groupName, StorageType storageType)
800                    throws SchedulerException {
801    
802                    List<SchedulerResponse> schedulerResponses = _getScheduledJobs(
803                            groupName, storageType);
804    
805                    for (SchedulerResponse schedulerResponse : schedulerResponses) {
806                            _unregisterMessageListener(schedulerResponse);
807                    }
808            }
809    
810            private void _unregisterMessageListener(
811                            String jobName, String groupName, StorageType storageType)
812                    throws SchedulerException {
813    
814                    SchedulerResponse schedulerResponse = _getScheduledJob(
815                            jobName, groupName, storageType);
816    
817                    _unregisterMessageListener(schedulerResponse);
818            }
819    
820            private void _unschedule(
821                            SchedulerEntry schedulerEntry, StorageType storageType)
822                    throws SchedulerException {
823    
824                    Trigger trigger = schedulerEntry.getTrigger();
825    
826                    _unschedule(trigger.getJobName(), trigger.getGroupName(), storageType);
827            }
828    
829            private void _unschedule(String groupName, StorageType storageType)
830                    throws SchedulerException {
831    
832                    _unregisterMessageListener(groupName, storageType);
833    
834                    _schedulerEngine.unschedule(
835                            _namespaceGroupName(groupName, storageType));
836            }
837    
838            private void _unschedule(
839                            String jobName, String groupName, StorageType storageType)
840                    throws SchedulerException {
841    
842                    _unregisterMessageListener(jobName, groupName, storageType);
843    
844                    _schedulerEngine.unschedule(
845                            jobName, _namespaceGroupName(groupName, storageType));
846            }
847    
848            private void _update(
849                            String jobName, String groupName, StorageType storageType,
850                            String description, String language, String script,
851                            int exceptionsMaxSize)
852                    throws SchedulerException {
853    
854                    SchedulerResponse schedulerResponse = _getScheduledJob(
855                            jobName, groupName, storageType);
856    
857                    if (schedulerResponse == null) {
858                            return;
859                    }
860    
861                    Trigger trigger = schedulerResponse.getTrigger();
862    
863                    if (trigger == null) {
864                            return;
865                    }
866    
867                    Message message = schedulerResponse.getMessage();
868    
869                    if (message == null) {
870                            return;
871                    }
872    
873                    _unregisterMessageListener(schedulerResponse);
874    
875                    _addScriptingJob(
876                            trigger, storageType, description, language, script,
877                            exceptionsMaxSize);
878            }
879    
880            private void _update(Trigger trigger, StorageType storageType)
881                    throws SchedulerException {
882    
883                    trigger = TriggerFactoryUtil.buildTrigger(
884                            trigger.getTriggerType(), trigger.getJobName(),
885                            _namespaceGroupName(trigger.getGroupName(), storageType),
886                            trigger.getStartDate(), trigger.getEndDate(),
887                            trigger.getTriggerContent());
888    
889                    _schedulerEngine.update(trigger);
890            }
891    
892            private void _updateMemorySchedulerClusterMaster()
893                    throws SchedulerException {
894    
895                    if (_schedulerEngineClusterManager == null) {
896                            _log.error(
897                                    "Unable to update memory scheduler cluster master because " +
898                                            "the portal is not using a clustered scheduler engine");
899    
900                            return;
901                    }
902    
903                    _schedulerEngineClusterManager.updateMemorySchedulerClusterMaster();
904            }
905    
906            private static Log _log = LogFactoryUtil.getLog(SchedulerEngineUtil.class);
907    
908            private static SchedulerEngineUtil _instance = new SchedulerEngineUtil();
909    
910            private SchedulerEngine _schedulerEngine;
911            private SchedulerEngineClusterManager _schedulerEngineClusterManager;
912    
913    }