KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.avalon.framework.CascadingRuntimeException;
19 import org.apache.avalon.framework.configuration.Configurable;
20 import org.apache.avalon.framework.configuration.Configuration;
21 import org.apache.avalon.framework.configuration.ConfigurationException;
22 import org.apache.avalon.framework.parameters.Parameters;
23
24 import org.apache.excalibur.source.Source;
25 import org.apache.excalibur.source.SourceResolver;
26
27 import java.io.InputStream JavaDoc;
28 import java.io.InputStreamReader JavaDoc;
29 import java.util.Map JavaDoc;
30
31 /**
32  * A simple CronJob which calls an internal cocoon:// pipeline.
33  *
34  * You must provide it with a <pipeline>pipeline/to/call</pipeline> parameter in cocoon.xconf
35  * Your supplied pipeline String will have "cocoon://" prepended to it.
36  * If you set info log enabled, this will write the output of the pipeline to the cron log
37  *
38  * @author <a HREF="mailto:giacomo@apache.org">Giacomo Pati</a>
39  * @author <a HREF="http://apache.org/~reinhard">Reinhard Poetz</a>
40  * @author <a HREF="http://apache.org/~jeremy/">Jeremy Quinn</a>
41  * @version CVS $Id: CocoonPipelineCronJob.java 291649 2005-09-26 16:05:33Z sylvain $
42  *
43  * @since 2.1.5
44  */

45 public class CocoonPipelineCronJob extends ServiceableCronJob
46                                    implements Configurable, ConfigurableCronJob {
47
48     public static final String JavaDoc PIPELINE_PARAM = "pipeline";
49
50     private String JavaDoc configuredPipeline;
51     private String JavaDoc pipeline;
52
53     public void execute(String JavaDoc name) {
54
55         if (getLogger ().isDebugEnabled ()) {
56             getLogger().debug ("CocoonPipelineCronJob: " + name + ", calling pipeline: " + pipeline);
57         }
58
59         SourceResolver resolver = null;
60         Source src = null;
61         try {
62             resolver = (SourceResolver)this.manager.lookup (SourceResolver.ROLE);
63             src = resolver.resolveURI ("cocoon://" + pipeline);
64
65             InputStream JavaDoc is = src.getInputStream();
66             InputStreamReader JavaDoc reader = new InputStreamReader JavaDoc (is);
67             StringBuffer JavaDoc sb = new StringBuffer JavaDoc ();
68             char[] b = new char[8192];
69             int n;
70             while((n = reader.read (b)) > 0) {
71                 sb.append (b, 0, n);
72             }
73             reader.close ();
74             if (getLogger ().isInfoEnabled ()) {
75                 getLogger ().info ("CocoonPipelineCronJob: " + name + ", called pipeline: " +
76                                    pipeline + ", and received following content:\n" + sb.toString() );
77             }
78         } catch(Exception JavaDoc e) {
79             throw new CascadingRuntimeException ("CocoonPipelineCronJob: " + name + ", raised an exception: ", e);
80         } finally {
81             if (resolver != null) {
82                 resolver.release (src);
83                 this.manager.release (resolver);
84                 resolver = null;
85                 src = null;
86             }
87         }
88     }
89
90     public void configure(final Configuration config) throws ConfigurationException {
91         this.configuredPipeline = config.getChild(PIPELINE_PARAM).getValue(null);
92     }
93
94     /* (non-Javadoc)
95      * @see org.apache.cocoon.components.cron.ConfigurableCronJob#setup(org.apache.avalon.framework.parameters.Parameters, java.util.Map)
96      */

97     public void setup(Parameters params, Map JavaDoc objects) {
98         if (null != params) {
99             pipeline = params.getParameter(PIPELINE_PARAM, configuredPipeline);
100         } else {
101              pipeline = configuredPipeline;
102         }
103     }
104 }
105
Popular Tags