View Javadoc

1   package org.apache.commons.jelly.tags.quartz;
2   
3   /*
4    * Copyright 2002,2004 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import org.apache.commons.jelly.TagSupport;
20  import org.quartz.Scheduler;
21  import org.quartz.SchedulerException;
22  import org.quartz.impl.StdSchedulerFactory;
23  
24  /*** Basic support for all tags requiring a Quartz scheduler.
25   *
26   *  @author <a href="mailto:bob@eng.werken.com">bob mcwhirter</a>
27   */
28  public abstract class QuartzTagSupport extends TagSupport
29  {
30      /*** The scheduler variable name in the JellyContext. */
31      public static final String SCHED_VAR_NAME = "org.apache.commons.jelly.quartz.Scheduler";
32  
33  
34      /*** Retrieve or create a scheduler.
35       *
36       *  <p>
37       *  If a scheduler has already been created an installed
38       *  in the variable {@link #SCHED_VAR_NAME}, then that scheduler
39       *  will be returned.  Otherwise, a new StdScheduler will be
40       *  created, started, and installed.  Additionally, a runtime
41       *  shutdown hook will be added to cleanly shutdown the scheduler.
42       *
43       *  @return The scheduler.
44       *
45       *  @throws SchedulerException If there is an error creating the
46       *          scheduler.
47       */
48      public Scheduler getScheduler() throws SchedulerException
49      {
50          Scheduler sched = (Scheduler) getContext().getVariable( SCHED_VAR_NAME );
51  
52          if ( sched == null )
53          {
54              StdSchedulerFactory factory = new StdSchedulerFactory();
55  
56              final Scheduler newSched = factory.getScheduler();
57  
58              sched = newSched;
59  
60              getContext().setVariable( SCHED_VAR_NAME,
61                                        newSched );
62  
63              Runtime.getRuntime().addShutdownHook(
64                  new Thread() {
65                      public void run()
66                      {
67                          try
68                          {
69                              if ( ! newSched.isShutdown() )
70                              {
71                                  newSched.shutdown();
72                              }
73                          }
74                          catch (SchedulerException e)
75                          {
76                              e.printStackTrace();
77                          }
78                      }
79                  }
80                  );
81              newSched.start();
82          }
83  
84  
85          return sched;
86      }
87  }
88