001: package org.apache.turbine.modules;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import java.util.List;
023:
024: import org.apache.commons.logging.Log;
025: import org.apache.commons.logging.LogFactory;
026:
027: import org.apache.turbine.Turbine;
028: import org.apache.turbine.TurbineConstants;
029: import org.apache.turbine.services.assemblerbroker.AssemblerBrokerService;
030: import org.apache.turbine.services.assemblerbroker.TurbineAssemblerBroker;
031: import org.apache.turbine.services.schedule.JobEntry;
032: import org.apache.turbine.util.ObjectUtils;
033: import org.apache.turbine.util.RunData;
034:
035: /**
036: * ScheduledJobs loader class.
037: *
038: * @author <a href="mailto:mbryson@mindspring.com">Dave Bryson</a>
039: * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
040: * @version $Id: ScheduledJobLoader.java 534527 2007-05-02 16:10:59Z tv $
041: */
042: public class ScheduledJobLoader extends GenericLoader {
043: /** Serial Version UID */
044: private static final long serialVersionUID = 7207944483452185019L;
045:
046: /** Logging */
047: private static Log log = LogFactory
048: .getLog(ScheduledJobLoader.class);
049:
050: /** The single instance of this class. */
051: private static ScheduledJobLoader instance = new ScheduledJobLoader(
052: Turbine.getConfiguration().getInt(
053: TurbineConstants.SCHEDULED_JOB_CACHE_SIZE_KEY,
054: TurbineConstants.SCHEDULED_JOB_CACHE_SIZE_DEFAULT));
055:
056: /** The Assembler Broker Service */
057: private static AssemblerBrokerService ab = TurbineAssemblerBroker
058: .getService();
059:
060: /**
061: * These ctor's are private to force clients to use getInstance()
062: * to access this class.
063: */
064: private ScheduledJobLoader() {
065: super ();
066: }
067:
068: /**
069: * These ctor's are private to force clients to use getInstance()
070: * to access this class.
071: */
072: private ScheduledJobLoader(int i) {
073: super (i);
074: }
075:
076: /**
077: * Adds an instance of an object into the hashtable.
078: *
079: * @param name Name of object.
080: * @param job Job to be associated with name.
081: */
082: private void addInstance(String name, ScheduledJob job) {
083: if (cache()) {
084: this .put(name, (ScheduledJob) job);
085: }
086: }
087:
088: /**
089: * Attempts to load and execute the external ScheduledJob.
090: *
091: * @param job The JobEntry.
092: * @param name Name of object that will execute the job.
093: * @exception Exception a generic exception.
094: */
095: public void exec(JobEntry job, String name) throws Exception {
096: // Execute job
097: getInstance(name).run(job);
098: }
099:
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) throws Exception {
115: throw new Exception(
116: "RunData objects not accepted for Scheduled jobs");
117: }
118:
119: /**
120: * Pulls out an instance of the object by name. Name is just the
121: * single name of the object.
122: *
123: * @param name Name of object instance.
124: * @return An ScheduledJob with the specified name, or null.
125: * @exception Exception a generic exception.
126: */
127: public ScheduledJob getInstance(String name) throws Exception {
128: ScheduledJob job = null;
129:
130: // Check if the screen is already in the cache
131: if (cache() && this .containsKey(name)) {
132: job = (ScheduledJob) this .get(name);
133: log.debug("Found Job " + name + " in the cache!");
134: } else {
135: log.debug("Loading Job " + name
136: + " from the Assembler Broker");
137:
138: try {
139: if (ab != null) {
140: // Attempt to load the job
141: job = (ScheduledJob) ab.getAssembler(
142: AssemblerBrokerService.SCHEDULEDJOB_TYPE,
143: name);
144: }
145: } catch (ClassCastException cce) {
146: // This can alternatively let this exception be thrown
147: // So that the ClassCastException is shown in the
148: // browser window. Like this it shows "Screen not Found"
149: job = null;
150: }
151:
152: if (job == null) {
153: // If we did not find a screen we should try and give
154: // the user a reason for that...
155: // FIX ME: The AssemblerFactories should each add it's
156: // own string here...
157: List packages = Turbine.getConfiguration().getList(
158: TurbineConstants.MODULE_PACKAGES);
159:
160: ObjectUtils.addOnce(packages, GenericLoader
161: .getBasePackage());
162:
163: throw new ClassNotFoundException(
164: "\n\n\tRequested ScheduledJob not found: "
165: + name
166: + "\n\tTurbine looked in the following "
167: + "modules.packages path: \n\t"
168: + packages.toString() + "\n");
169: } else if (cache()) {
170: // The new instance is added to the cache
171: addInstance(name, job);
172: }
173: }
174: return job;
175: }
176:
177: /**
178: * The method through which this class is accessed.
179: *
180: * @return The single instance of this class.
181: */
182: public static ScheduledJobLoader getInstance() {
183: return instance;
184: }
185: }
|