View Javadoc

1   // 
2   //Copyright (c) 2003, Caltha - Gajda, Krzewski, Mach, Potempski Sp.J. 
3   //All rights reserved. 
4   //   
5   //Redistribution and use in source and binary forms, with or without modification,  
6   //are permitted provided that the following conditions are met: 
7   //   
8   //* Redistributions of source code must retain the above copyright notice,  
9   //this list of conditions and the following disclaimer. 
10  //* Redistributions in binary form must reproduce the above copyright notice,  
11  //this list of conditions and the following disclaimer in the documentation  
12  //and/or other materials provided with the distribution. 
13  //* Neither the name of the Caltha - Gajda, Krzewski, Mach, Potempski Sp.J.  
14  //nor the names of its contributors may be used to endorse or promote products  
15  //derived from this software without specific prior written permission. 
16  // 
17  //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"  
18  //AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED  
19  //WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
20  //IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,  
21  //INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,  
22  //BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
23  //OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,  
24  //WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)  
25  //ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE  
26  //POSSIBILITY OF SUCH DAMAGE. 
27  //
28  
29  package org.objectledge.scheduler.db;
30  
31  import org.objectledge.database.persistence.InputRecord;
32  import org.objectledge.database.persistence.OutputRecord;
33  import org.objectledge.database.persistence.Persistence;
34  import org.objectledge.database.persistence.PersistenceException;
35  import org.objectledge.database.persistence.Persistent;
36  import org.objectledge.scheduler.AbstractJobDescriptor;
37  import org.objectledge.scheduler.AbstractScheduler;
38  import org.objectledge.scheduler.InvalidScheduleException;
39  import org.objectledge.scheduler.JobModificationException;
40  import org.objectledge.scheduler.Schedule;
41  
42  /**
43   * Persistent scheduled job descriptor based on database.
44   * 
45   * @author <a href="mailto:pablo@caltha.pl">Pawel Potempski</a>
46   */
47  public class DBJobDescriptor extends AbstractJobDescriptor
48      implements Persistent
49  {
50      // constants /////////////////////////////////////////////////////////////
51  
52      /*** The table name. */
53      public static final String TABLE_NAME = "ledge_scheduler";
54  
55      /*** The key columns. */
56      public static final String[] KEY_COLUMNS = new String[] { "job_id" };
57  
58      // instance variables ////////////////////////////////////////////////////
59  
60      /*** The persistence. */
61      private Persistence persistence;
62      
63      /*** The scheduler */
64      private AbstractScheduler scheduler;
65  
66      /*** The job id. */
67      private long jobId = -1L;
68  
69      /***
70       * Constructor.
71       * 
72       * @param persistence the persitence component.
73       * @param scheduler the scheduler component.
74       */
75      DBJobDescriptor(Persistence persistence, AbstractScheduler scheduler)
76      {
77          this.persistence = persistence;
78          this.scheduler = scheduler;
79      }
80  
81      // Persistent interface //////////////////////////////////////////////////
82  
83      /***
84       * Returns the name of the table this type is mapped to.
85       *
86       * @return the name of the table.
87       */
88      public String getTable()
89      {
90          return TABLE_NAME;
91      }
92  
93      /***
94       * Returns the names of the key columns.
95       *
96       * @return the names of the key columns.
97       */
98      public String[] getKeyColumns()
99      {
100         return KEY_COLUMNS;
101     }
102 
103     /***
104      * {@inheritDoc}
105      */
106     public void getData(OutputRecord record) throws PersistenceException
107     {
108         record.setLong("job_id", jobId);
109         record.setString("job_name", getName());
110         record.setString("schedule_type", getSchedule().getType());
111         record.setString("schedule_config", getSchedule().getConfig());
112         record.setString("job_class_name", getJobClassName());
113         if (getArgument() != null)
114         {
115             record.setString("argument", getArgument());
116         }
117         else
118         {
119             record.setNull("argument");
120         }
121         record.setInteger("run_count", getRunCount());
122         record.setInteger("run_count_limit", getRunCountLimit());
123         if (getLastRunTime() != null)
124         {
125             record.setTimestamp("last_run_time", getLastRunTime());
126         }
127         else
128         {
129             record.setNull("last_run_time");
130         }
131         if (getTimeLimitStart() != null)
132         {
133             record.setTimestamp("run_time_limit_start", getTimeLimitStart());
134         }
135         else
136         {
137             record.setNull("run_time_limit_start");
138         }
139         if (getTimeLimitEnd() != null)
140         {
141             record.setTimestamp("run_time_limit_end", getTimeLimitEnd());
142         }
143         else
144         {
145             record.setNull("run_time_limit_end");
146         }
147         record.setBoolean("reentrant", isReentrant());
148         record.setBoolean("enabled", isEnabled());
149     }
150 
151     /***
152      * {@inheritDoc}
153      */
154     public void setData(InputRecord record) throws PersistenceException
155     {
156         jobId = record.getLong("job_id");
157         String name = record.getString("job_name");
158         String scheduleType = record.getString("schedule_type");
159         String scheduleConfig = record.getString("schedule_config");
160         String jobClassName = record.getString("job_class_name");
161         Schedule schedule = null;
162         try
163         {
164             schedule = scheduler.createSchedule(scheduleType, scheduleConfig);
165         }
166         catch (InvalidScheduleException e)
167         {
168             throw new PersistenceException("failed to create schedule", e);
169         }
170         try
171         {
172             super.init(name, schedule, jobClassName);
173             if (!record.isNull("argument"))
174             {
175                 argument = record.getString("argument");
176             }
177             if (!record.isNull("run_count"))
178             {
179                 runCount = record.getInteger("run_count");
180             }
181             if (!record.isNull("run_count_limit"))
182             {
183                 runCountLimit = record.getInteger("run_count_limit");
184             }
185             if (!record.isNull("last_run_time"))
186             {
187                 lastRunTime = record.getDate("last_run_time");
188             }
189             if (!record.isNull("run_time_limit_start"))
190             {
191                 runTimeLimitStart = record.getDate("run_time_limit_start");
192             }
193             if (!record.isNull("run_time_limit_end"))
194             {
195                 runTimeLimitEnd = record.getDate("run_time_limit_end");
196             }
197             if (!record.isNull("auto_clean"))
198             {
199                 autoClean = record.getBoolean("auto_clean");
200             }
201             if (!record.isNull("reentrant"))
202             {
203                 reentrant = record.getBoolean("reentrant");
204             }
205             if (!record.isNull("enabled"))
206             {
207                 enabled = record.getBoolean("enabled");
208             }
209         }
210         catch (Exception e)
211         {
212             throw new PersistenceException("Failed to initialize scheduled job", e);
213         }
214     }
215 
216     /***
217      * {@inheritDoc}
218      */
219     public boolean getSaved()
220     {
221         return jobId != -1L;
222     }
223 
224     /***
225      * {@inheritDoc}
226      */
227     public void setSaved(long id)
228     {
229         this.jobId = id;
230     }
231 
232     // implementation ////////////////////////////////////////////////////////
233 
234     /***
235      * {@inheritDoc}
236      */
237     protected void saveChanges() throws JobModificationException
238     {
239         try
240         {
241             persistence.save(this);
242         }
243         catch (PersistenceException e)
244         {
245             throw new JobModificationException("failed to save job state", e);
246         }
247     }
248 
249 }