View Javadoc

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