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.io.InputStreamReader;
020: import java.util.Date;
021: import java.util.Map;
022:
023: import org.apache.avalon.framework.CascadingRuntimeException;
024: import org.apache.avalon.framework.configuration.Configurable;
025: import org.apache.avalon.framework.configuration.Configuration;
026: import org.apache.avalon.framework.configuration.ConfigurationException;
027: import org.apache.avalon.framework.parameters.Parameters;
028: import org.apache.excalibur.source.Source;
029: import org.apache.excalibur.source.SourceResolver;
030:
031: /**
032: * A simple test CronJob which also calls a pipeline internally.
033: *
034: * @author <a href="mailto:giacomo@apache.org">Giacomo Pati</a>
035: * @author <a href="http://apache.org/~reinhard">Reinhard Poetz</a>
036: * @version CVS $Id: TestCronJob.java 433543 2006-08-22 06:22:54Z crossley $
037: * @since 2.1.1
038: */
039: public class TestCronJob extends ServiceableCronJob implements
040: Configurable, ConfigurableCronJob {
041:
042: /** Parameter key for the message */
043: public static final String PARAMETER_MESSAGE = "TestCronJob.Parameter.Message";
044:
045: /** Parameter key for the sleep value */
046: public static final String PARAMETER_SLEEP = "TestCronJob.Parameter.Sleep";
047:
048: /** Parameter key for the pipeline to be called */
049: public static final String PARAMETER_PIPELINE = "TestCronJob.Parameter.Pipeline";
050:
051: /** The configured message. */
052: private String msg;
053:
054: /** The configured sleep time. */
055: private int sleep;
056:
057: /** The pipeline to be called. */
058: private String pipeline;
059:
060: /**
061: * @see org.apache.avalon.framework.configuration.Configurable#configure(org.apache.avalon.framework.configuration.Configuration)
062: */
063: public void configure(final Configuration config)
064: throws ConfigurationException {
065: msg = config.getChild("msg").getValue("I was not configured");
066: sleep = config.getChild("sleep").getValueAsInteger(11000);
067: pipeline = config.getChild("pipeline").getValue(
068: "samples/hello-world/hello.xhtml");
069: }
070:
071: /**
072: * @see org.apache.cocoon.components.cron.CronJob#execute(java.lang.String)
073: */
074: public void execute(String name) {
075: if (this .getLogger().isInfoEnabled()) {
076: getLogger().info(
077: "CronJob " + name + " launched at " + new Date()
078: + " with message '" + msg
079: + "' and sleep timeout of " + sleep + "ms");
080: }
081:
082: SourceResolver resolver = null;
083: Source src = null;
084: try {
085: resolver = (SourceResolver) this .manager
086: .lookup(SourceResolver.ROLE);
087: src = resolver.resolveURI("cocoon://" + this .pipeline);
088:
089: final InputStreamReader r = new InputStreamReader(src
090: .getInputStream());
091: try {
092: boolean append = this .getLogger().isInfoEnabled();
093: StringBuffer sb = new StringBuffer();
094: char[] b = new char[8192];
095: int n;
096:
097: while ((n = r.read(b)) > 0) {
098: if (append) {
099: sb.append(b, 0, n);
100: }
101: }
102:
103: if (append) {
104: getLogger()
105: .info(
106: "CronJob "
107: + name
108: + " called pipeline "
109: + pipeline
110: + " and received following content:\n"
111: + sb.toString());
112: }
113: } finally {
114: r.close();
115: }
116:
117: } catch (Exception e) {
118: throw new CascadingRuntimeException("CronJob " + name
119: + " raised an exception.", e);
120: } finally {
121: if (resolver != null) {
122: resolver.release(src);
123: this .manager.release(resolver);
124: resolver = null;
125: src = null;
126: }
127: }
128:
129: try {
130: Thread.sleep(sleep);
131: } catch (final InterruptedException ie) {
132: //getLogger().error("CronJob " + name + " interrupted", ie);
133: }
134:
135: if (this .getLogger().isInfoEnabled()) {
136: getLogger().info(
137: "CronJob " + name + " finished at " + new Date()
138: + " with message '" + msg
139: + "' and sleep timeout of " + sleep + "ms");
140: }
141: }
142:
143: /**
144: * @see org.apache.cocoon.components.cron.ConfigurableCronJob#setup(org.apache.avalon.framework.parameters.Parameters, java.util.Map)
145: */
146: public void setup(Parameters params, Map objects) {
147: if (null != params) {
148: msg = params.getParameter(PARAMETER_MESSAGE, msg);
149: sleep = params
150: .getParameterAsInteger(PARAMETER_SLEEP, sleep);
151: pipeline = params
152: .getParameter(PARAMETER_PIPELINE, pipeline);
153:
154: }
155: }
156: }
|