001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.components.cron;
018:
019: import java.net.MalformedURLException;
020: import java.util.Map;
021:
022: import org.apache.avalon.framework.component.WrapperComponentManager;
023: import org.apache.avalon.framework.context.ContextException;
024: import org.apache.avalon.framework.parameters.Parameters;
025: import org.apache.avalon.framework.service.ServiceException;
026: import org.apache.cocoon.Constants;
027: import org.apache.cocoon.Processor;
028: import org.apache.cocoon.components.CocoonComponentManager;
029: import org.apache.cocoon.environment.ObjectModelHelper;
030: import org.apache.cocoon.environment.Request;
031: import org.apache.cocoon.environment.background.BackgroundEnvironment;
032: import org.quartz.JobDataMap;
033: import org.quartz.JobExecutionException;
034:
035: /**
036: * This component is resposible to launch a {@link CronJob}s in a Quart Scheduler.
037: *
038: * @author <a href="mailto:giacomo@apache.org">Giacomo Pati</a>
039: * @version CVS $Id: CocoonQuartzJobExecutor.java 433543 2006-08-22 06:22:54Z crossley $
040: *
041: * @since 2.1.1
042: */
043: public class CocoonQuartzJobExecutor extends QuartzJobExecutor {
044:
045: private Object m_key;
046: private BackgroundEnvironment m_env;
047: private Processor m_processor;
048:
049: protected void setup(JobDataMap data) throws JobExecutionException {
050: super .setup(data);
051: org.apache.cocoon.environment.Context envContext;
052: try {
053: envContext = (org.apache.cocoon.environment.Context) m_context
054: .get(Constants.CONTEXT_ENVIRONMENT_CONTEXT);
055: } catch (ContextException e) {
056: throw new JobExecutionException(e);
057: }
058:
059: try {
060: m_env = new BackgroundEnvironment(m_logger, envContext);
061: } catch (MalformedURLException e) {
062: // Unlikely to happen
063: throw new JobExecutionException(e);
064: }
065:
066: Request req = ObjectModelHelper.getRequest(m_env
067: .getObjectModel());
068: Map objects = (Map) data
069: .get(QuartzJobScheduler.DATA_MAP_OBJECTMAP);
070: if (objects != null) {
071: req.setAttribute("cron-objectmap", objects);
072: }
073:
074: Parameters params = (Parameters) data
075: .get(QuartzJobScheduler.DATA_MAP_PARAMETERS);
076: if (params != null) {
077: req.setAttribute("cron-parameters", params);
078: }
079:
080: try {
081: m_processor = (Processor) m_manager.lookup(Processor.ROLE);
082: } catch (ServiceException e) {
083: throw new JobExecutionException(e);
084: }
085:
086: m_key = CocoonComponentManager.startProcessing(m_env);
087: CocoonComponentManager.enterEnvironment(m_env,
088: new WrapperComponentManager(m_manager), m_processor);
089: }
090:
091: protected void release(JobDataMap data) {
092: super.release(data);
093: CocoonComponentManager.leaveEnvironment();
094: CocoonComponentManager.endProcessing(m_env, m_key);
095: if (m_manager != null) {
096: m_manager.release(m_processor);
097: }
098: }
099:
100: }
|