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 org.apache.avalon.framework.CascadingRuntimeException;
020: import org.apache.avalon.framework.configuration.Configurable;
021: import org.apache.avalon.framework.configuration.Configuration;
022: import org.apache.avalon.framework.configuration.ConfigurationException;
023: import org.apache.avalon.framework.parameters.Parameters;
024:
025: import org.apache.excalibur.source.Source;
026: import org.apache.excalibur.source.SourceResolver;
027:
028: import java.io.IOException;
029: import java.io.InputStream;
030: import java.io.InputStreamReader;
031: import java.util.Map;
032:
033: /**
034: * A simple CronJob which calls an internal cocoon:// pipeline.
035: *
036: * You must provide it with a <pipeline>pipeline/to/call</pipeline> parameter in cocoon.xconf
037: * Your supplied pipeline String will have "cocoon://" prepended to it.
038: * If you set info log enabled, this will write the output of the pipeline to the cron log
039: *
040: * @author <a href="mailto:giacomo@apache.org">Giacomo Pati</a>
041: * @author <a href="http://apache.org/~reinhard">Reinhard Poetz</a>
042: * @author <a href="http://apache.org/~jeremy/">Jeremy Quinn</a>
043: * @version $Id: CocoonPipelineCronJob.java 433543 2006-08-22 06:22:54Z crossley $
044: *
045: * @since 2.1.5
046: */
047: public class CocoonPipelineCronJob extends ServiceableCronJob implements
048: Configurable, ConfigurableCronJob {
049:
050: public static final String PIPELINE_PARAM = "pipeline";
051:
052: private String configuredPipeline;
053: private String pipeline;
054:
055: public void execute(String name) {
056:
057: if (getLogger().isDebugEnabled()) {
058: getLogger().debug(
059: "CocoonPipelineCronJob: " + name
060: + ", calling pipeline: " + pipeline);
061: }
062:
063: SourceResolver resolver = null;
064: Source src = null;
065: InputStream is = null;
066: InputStreamReader reader = null;
067: try {
068: boolean append = getLogger().isInfoEnabled();
069: resolver = (SourceResolver) this .manager
070: .lookup(SourceResolver.ROLE);
071: src = resolver.resolveURI("cocoon://" + pipeline);
072:
073: is = src.getInputStream();
074: reader = new InputStreamReader(is);
075: StringBuffer sb = new StringBuffer();
076: char[] b = new char[8192];
077: int n;
078: while ((n = reader.read(b)) > 0) {
079: if (append) {
080: sb.append(b, 0, n);
081: }
082: }
083: reader.close();
084: if (append) {
085: getLogger().info(
086: "CocoonPipelineCronJob: " + name
087: + ", called pipeline: " + pipeline
088: + ", and received following content:\n"
089: + sb.toString());
090: }
091: } catch (Exception e) {
092: throw new CascadingRuntimeException(
093: "CocoonPipelineCronJob: " + name
094: + ", raised an exception: ", e);
095: } finally {
096: try {
097: if (reader != null)
098: reader.close();
099: if (is != null)
100: is.close();
101: } catch (IOException e) {
102: throw new CascadingRuntimeException(
103: "CocoonPipelineCronJob: " + name
104: + ", raised an exception: ", e);
105: }
106: if (resolver != null) {
107: resolver.release(src);
108: this .manager.release(resolver);
109: resolver = null;
110: src = null;
111: }
112: }
113: }
114:
115: public void configure(final Configuration config)
116: throws ConfigurationException {
117: this .configuredPipeline = config.getChild(PIPELINE_PARAM)
118: .getValue(null);
119: }
120:
121: /* (non-Javadoc)
122: * @see org.apache.cocoon.components.cron.ConfigurableCronJob#setup(org.apache.avalon.framework.parameters.Parameters, java.util.Map)
123: */
124: public void setup(Parameters params, Map objects) {
125: if (params != null) {
126: pipeline = params.getParameter(PIPELINE_PARAM,
127: configuredPipeline);
128: } else {
129: pipeline = configuredPipeline;
130: }
131: }
132: }
|