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.JellyTagException;
20  import org.apache.commons.jelly.XMLOutput;
21  import org.apache.commons.jelly.MissingAttributeException;
22  
23  import org.quartz.Scheduler;
24  import org.quartz.SchedulerException;
25  import org.quartz.JobDetail;
26  import org.quartz.JobDataMap;
27  
28  /*** Defines a schedulable job.
29   *
30   *  @author <a href="mailto:bob@eng.werken.com">bob mcwhirter</a>
31   */
32  public class JobTag extends QuartzTagSupport
33  {
34      // ------------------------------------------------------------
35      //     Instance members
36      // ------------------------------------------------------------
37  
38      /*** Group of the job. */
39      private String group;
40  
41      /*** Name of the job. */
42      private String name;
43  
44      // ------------------------------------------------------------
45      //     Constructors
46      // ------------------------------------------------------------
47  
48      /*** Construct.
49       */
50      public JobTag()
51      {
52          // intentionally left blank.
53      }
54  
55      // ------------------------------------------------------------
56      //     Instance methods
57      // ------------------------------------------------------------
58  
59      /*** Set the name of this job.
60       *
61       *  @param name The name of this job.
62       */
63      public void setName(String name)
64      {
65          this.name = name;
66      }
67  
68      /*** Retrieve the name of this job.
69       *
70       *  @return The name of this job.
71       */
72      public String getName()
73      {
74          return this.name;
75      }
76  
77      /*** Set the group of this job.
78       *
79       *  @param group The group of this job.
80       */
81      public void setGroup(String group)
82      {
83          this.group = group;
84      }
85  
86      /*** Retrieve the group of this job.
87       *
88       *  @return The group of this job.
89       */
90      public String getGroup()
91      {
92          return this.group;
93      }
94  
95      // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
96      //     org.apache.commons.jelly.Tag
97      // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
98  
99      /*** Perform this tag.
100      *
101      *  @param output Output sink.
102      *
103      *  @throws Exception If an error occurs.
104      */
105     public void doTag(XMLOutput output) throws MissingAttributeException, JellyTagException
106     {
107         if ( getName() == null )
108         {
109             throw new MissingAttributeException( "name" );
110         }
111 
112         if ( getGroup() == null )
113         {
114             throw new MissingAttributeException( "group" );
115         }
116 
117         JobDetail detail = new JobDetail( getName(),
118                                           getGroup(),
119                                           JellyJob.class );
120 
121         detail.setDurability( true );
122 
123         JobDataMap data = new JobDataMap();
124 
125         data.put( "jelly.output",
126                   output );
127 
128         data.put( "jelly.context",
129                   getContext() );
130 
131         data.put( "jelly.script",
132                   getBody() );
133 
134         detail.setJobDataMap( data );
135 
136         try {
137             Scheduler sched = getScheduler();
138             sched.addJob( detail,
139                           true );
140         }
141         catch (SchedulerException e) {
142             throw new JellyTagException(e);
143         }
144     }
145 }
146