KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > cron > TestCronJob


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.components.cron;
17
18 import java.io.InputStreamReader JavaDoc;
19 import java.util.Date JavaDoc;
20 import java.util.Map JavaDoc;
21
22 import org.apache.avalon.framework.CascadingRuntimeException;
23 import org.apache.avalon.framework.configuration.Configurable;
24 import org.apache.avalon.framework.configuration.Configuration;
25 import org.apache.avalon.framework.configuration.ConfigurationException;
26 import org.apache.avalon.framework.parameters.Parameters;
27 import org.apache.excalibur.source.Source;
28 import org.apache.excalibur.source.SourceResolver;
29
30 /**
31  * A simple test CronJob which also calls a pipeline internally.
32  *
33  * @author <a HREF="mailto:giacomo@apache.org">Giacomo Pati</a>
34  * @author <a HREF="http://apache.org/~reinhard">Reinhard Poetz</a>
35  * @version CVS $Id: TestCronJob.java 47062 2004-09-22 16:37:29Z unico $
36  * @since 2.1.1
37  */

38 public class TestCronJob extends ServiceableCronJob
39                          implements Configurable, ConfigurableCronJob {
40
41     /** Parameter key for the message */
42     public static final String JavaDoc PARAMETER_MESSAGE = "TestCronJob.Parameter.Message";
43
44     /** Parameter key for the sleep value */
45     public static final String JavaDoc PARAMETER_SLEEP = "TestCronJob.Parameter.Sleep";
46
47     /** Parameter key for the pipeline to be called */
48     public static final String JavaDoc PARAMETER_PIPELINE = "TestCronJob.Parameter.Pipeline";
49
50     /** The configured message */
51     private String JavaDoc m_msg;
52
53     /** The configured sleep time */
54     private int m_sleep;
55
56     /** The pipeline to be called */
57     private String JavaDoc pipeline;
58
59     /* (non-Javadoc)
60      * @see org.apache.avalon.framework.configuration.Configurable#configure(org.apache.avalon.framework.configuration.Configuration)
61      */

62     public void configure(final Configuration config)
63     throws ConfigurationException {
64         m_msg = config.getChild("msg").getValue("I was not configured");
65         m_sleep = config.getChild("sleep").getValueAsInteger(11000);
66         pipeline = config.getChild("pipeline").getValue("samples/hello-world/hello.xhtml");
67     }
68
69     /* (non-Javadoc)
70      * @see org.apache.cocoon.components.cron.CronJob#execute(java.lang.String)
71      */

72     public void execute(String JavaDoc name) {
73         getLogger().info("CronJob " + name + " launched at " + new Date JavaDoc() + " with message '" + m_msg +
74                          "' and sleep timeout of " + m_sleep + "ms");
75
76         SourceResolver resolver = null;
77         Source src = null;
78         try {
79             resolver = (SourceResolver) this.manager.lookup(SourceResolver.ROLE);
80             src = resolver.resolveURI("cocoon://" + this.pipeline);
81
82             InputStreamReader JavaDoc r = new InputStreamReader JavaDoc(src.getInputStream());
83             try {
84                 StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
85                 char[] b = new char[8192];
86                 int n;
87
88                 while((n = r.read(b)) > 0) {
89                     sb.append(b, 0, n);
90                 }
91
92                 getLogger().info("CronJob " + name + " called pipeline " + pipeline +
93                                  " and received following content:\n" + sb.toString());
94             } finally {
95                 r.close();
96             }
97
98         } catch(Exception JavaDoc e) {
99             throw new CascadingRuntimeException("CronJob " + name + " raised an exception", e);
100         } finally {
101             if (resolver != null) {
102                 resolver.release(src);
103                 this.manager.release(resolver);
104                 resolver = null;
105                 src = null;
106             }
107         }
108
109         try {
110             Thread.sleep(m_sleep);
111         } catch (final InterruptedException JavaDoc ie) {
112             //getLogger().error("CronJob " + name + " interrupted", ie);
113
}
114
115         getLogger().info("CronJob " + name + " finished at " + new Date JavaDoc() + " with message '" + m_msg +
116                          "' and sleep timeout of " + m_sleep + "ms");
117     }
118
119     /* (non-Javadoc)
120      * @see org.apache.cocoon.components.cron.ConfigurableCronJob#setup(org.apache.avalon.framework.parameters.Parameters, java.util.Map)
121      */

122     public void setup(Parameters params, Map JavaDoc objects) {
123         if (null != params) {
124             m_msg = params.getParameter(PARAMETER_MESSAGE, m_msg);
125             m_sleep = params.getParameterAsInteger(PARAMETER_SLEEP, m_sleep);
126             pipeline = params.getParameter(PARAMETER_PIPELINE, pipeline );
127
128         }
129     }
130 }
131
Popular Tags