View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.jetspeed.scheduler;
18  
19  
20  /***
21   * Wrapper for a <code>JobEntry</code> to actually perform the job's action.
22   *
23   * @author <a href="mailto:mbryson@mont.mindspring.com">Dave Bryson</a>
24   * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
25   * @version $Id: WorkerThread.java 516448 2007-03-09 16:25:47Z ate $
26   */
27  public class WorkerThread
28      implements Runnable
29  {
30      /***
31       * The <code>JobEntry</code> to run.
32       */
33      private JobEntry je = null;
34  
35      /***
36       * The {@link org.apache.fulcrum.logging.Logger} facility to use.
37       */
38  
39      /***
40       * Creates a new worker to run the specified <code>JobEntry</code>.
41       *
42       * @param je The <code>JobEntry</code> to create a worker for.
43       */
44      public WorkerThread(JobEntry je)
45      {
46          this.je = je;
47      }
48  
49      /***
50       * Run the job.
51       */
52      public void run()
53      {
54          if (je == null || je.isActive())
55          {
56              return;
57          }
58  
59          try
60          {
61              if (! je.isActive())
62              {
63                  je.setActive(true);
64                  logStateChange("started");
65  
66                  // We should have a set of job packages and
67                  // search through them like the module
68                  // loader does. This right here requires the
69                  // getTask() method to return a class name.
70                  String className = je.getTask();
71  
72                  //If a FactoryService is registered, use it. Otherwise,
73                  //instantiate the ScheduledJob directly.
74                  ScheduledJob sc = (ScheduledJob)Class.forName(className).newInstance();
75                  sc.execute(je);
76              }
77          }
78          catch (Exception e)
79          {
80              //!! use the service for logging
81              //Log.error("Error in WorkerThread for sheduled job #" +
82              //             je.getPrimaryKey() + ", task: " + je.getTask(), e);
83          }
84          finally
85          {
86              if (je.isActive())
87              {
88                  je.setActive(false);
89                  logStateChange("completed");
90              }
91          }
92      }
93  
94      /***
95       * Macro to log <code>JobEntry</code> status information.
96       *
97       * @param state The new state of the <code>JobEntry</code>.
98       */
99      private final void logStateChange(String state)
100     {
101         //!! use the service to log.
102         //Log.debug("Scheduled job #" + je.getPrimaryKey() + ' ' + state +
103         //    ", task: " + je.getTask());
104     }
105 }