View Javadoc

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