View Javadoc

1   package org.apache.turbine.modules;
2   
3   /*
4    * Copyright 2001-2005 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 java.util.List;
20  
21  import org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  
24  import org.apache.turbine.Turbine;
25  import org.apache.turbine.TurbineConstants;
26  import org.apache.turbine.services.assemblerbroker.AssemblerBrokerService;
27  import org.apache.turbine.services.assemblerbroker.TurbineAssemblerBroker;
28  import org.apache.turbine.services.schedule.JobEntry;
29  import org.apache.turbine.util.ObjectUtils;
30  import org.apache.turbine.util.RunData;
31  
32  /***
33   * ScheduledJobs loader class.
34   *
35   * @author <a href="mailto:mbryson@mindspring.com">Dave Bryson</a>
36   * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
37   * @version $Id: ScheduledJobLoader.java 264148 2005-08-29 14:21:04Z henning $
38   */
39  public class ScheduledJobLoader
40      extends GenericLoader
41  {
42      /*** Serial Version UID */
43      private static final long serialVersionUID = 7207944483452185019L;
44  
45      /*** Logging */
46      private static Log log = LogFactory.getLog(ScheduledJobLoader.class);
47  
48      /*** The single instance of this class. */
49      private static ScheduledJobLoader instance =
50          new ScheduledJobLoader(Turbine.getConfiguration()
51              .getInt(TurbineConstants.SCHEDULED_JOB_CACHE_SIZE_KEY,
52                  TurbineConstants.SCHEDULED_JOB_CACHE_SIZE_DEFAULT));
53  
54      /*** The Assembler Broker Service */
55      private static AssemblerBrokerService ab = TurbineAssemblerBroker.getService();
56  
57      /***
58       * These ctor's are private to force clients to use getInstance()
59       * to access this class.
60       */
61      private ScheduledJobLoader()
62      {
63          super();
64      }
65  
66      /***
67       * These ctor's are private to force clients to use getInstance()
68       * to access this class.
69       */
70      private ScheduledJobLoader(int i)
71      {
72          super(i);
73      }
74  
75      /***
76       * Adds an instance of an object into the hashtable.
77       *
78       * @param name Name of object.
79       * @param job Job to be associated with name.
80       */
81      private void addInstance(String name, ScheduledJob job)
82      {
83          if (cache())
84          {
85              this.put(name, (ScheduledJob) job);
86          }
87      }
88  
89      /***
90       * Attempts to load and execute the external ScheduledJob.
91       *
92       * @param job The JobEntry.
93       * @param name Name of object that will execute the job.
94       * @exception Exception a generic exception.
95       */
96      public void exec(JobEntry job, String name)
97              throws Exception
98      {
99          // Execute job
100         getInstance(name).run(job);
101     }
102 
103     /***
104      * Attempts to load and execute the external ScheduledJob.
105      *
106      * HELP! - THIS IS UGLY!
107      *
108      * I want the cache stuff from GenericLoader, BUT, I don't think
109      * the scheduler needs the Rundata object.  The scheduler runs
110      * independently of an HTTP request.  This should not extend
111      * GenericLoader!  Thoughts??
112      *
113      * @param data Turbine information.
114      * @param name Name of object that will execute the job.
115      * @exception Exception a generic exception.
116      */
117     public void exec(RunData data, String name)
118             throws Exception
119     {
120         throw new Exception("RunData objects not accepted for Scheduled jobs");
121     }
122 
123     /***
124      * Pulls out an instance of the object by name.  Name is just the
125      * single name of the object.
126      *
127      * @param name Name of object instance.
128      * @return An ScheduledJob with the specified name, or null.
129      * @exception Exception a generic exception.
130      */
131     public ScheduledJob getInstance(String name)
132             throws Exception
133     {
134         ScheduledJob job = null;
135 
136         // Check if the screen is already in the cache
137         if (cache() && this.containsKey(name))
138         {
139             job = (ScheduledJob) this.get(name);
140             log.debug("Found Job " + name + " in the cache!");
141         }
142         else
143         {
144             log.debug("Loading Job " + name + " from the Assembler Broker");
145 
146             try
147             {
148                 if (ab != null)
149                 {
150                     // Attempt to load the job
151                     job = (ScheduledJob) ab.getAssembler(
152                         AssemblerBrokerService.SCHEDULEDJOB_TYPE, name);
153                 }
154             }
155             catch (ClassCastException cce)
156             {
157                 // This can alternatively let this exception be thrown
158                 // So that the ClassCastException is shown in the
159                 // browser window.  Like this it shows "Screen not Found"
160                 job = null;
161             }
162 
163             if (job == null)
164             {
165                 // If we did not find a screen we should try and give
166                 // the user a reason for that...
167                 // FIX ME: The AssemblerFactories should each add it's
168                 // own string here...
169                 List packages = Turbine.getConfiguration()
170                     .getList(TurbineConstants.MODULE_PACKAGES);
171 
172                 ObjectUtils.addOnce(packages, GenericLoader.getBasePackage());
173 
174                 throw new ClassNotFoundException(
175                         "\n\n\tRequested ScheduledJob not found: " + name +
176                         "\n\tTurbine looked in the following " +
177                         "modules.packages path: \n\t" + packages.toString() + "\n");
178             }
179             else if (cache())
180             {
181                 // The new instance is added to the cache
182                 addInstance(name, job);
183             }
184         }
185         return job;
186     }
187 
188     /***
189      * The method through which this class is accessed.
190      *
191      * @return The single instance of this class.
192      */
193     public static ScheduledJobLoader getInstance()
194     {
195         return instance;
196     }
197 }