reinhard 2003/12/19 01:01:43
Modified: src/blocks/cron/java/org/apache/cocoon/components/cron
QuartzJobScheduler.java TestCronJob.java
src/blocks/cron/conf cron-category.xlog
src/blocks/cron/samples cron.js cron.xml
Added: src/blocks/cron/java/org/apache/cocoon/components/cron
AbstractPipelineCallingCronJob.java
ServiceableCronJob.java
Log:
- modify the TestCronJob --> now it extends AbstractPipelineCallingCronJob
which provides convience methods that do the work
- modify Cron web interface to call pipelines
- see also issue
http://nagoya.apache.org/bugzilla/show_bug.cgi?id=25384
Revision Changes Path
1.7 +17 -4
cocoon-2.1/src/blocks/cron/java/org/apache/cocoon/components/cron/QuartzJobScheduler.java
Index: QuartzJobScheduler.java
===================================================================
RCS file:
/home/cvs/cocoon-2.1/src/blocks/cron/java/org/apache/cocoon/components/cron/QuartzJobScheduler.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- QuartzJobScheduler.java 18 Sep 2003 08:54:18 -0000 1.6
+++ QuartzJobScheduler.java 19 Dec 2003 09:01:43 -0000 1.7
@@ -63,12 +63,16 @@
import org.apache.avalon.framework.configuration.Configurable;
import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.configuration.ConfigurationException;
+import org.apache.avalon.framework.context.Context;
+import org.apache.avalon.framework.context.ContextException;
+import org.apache.avalon.framework.context.Contextualizable;
import org.apache.avalon.framework.logger.AbstractLogEnabled;
import org.apache.avalon.framework.parameters.Parameters;
import org.apache.avalon.framework.service.ServiceException;
import org.apache.avalon.framework.service.ServiceManager;
import org.apache.avalon.framework.service.Serviceable;
import org.apache.avalon.framework.thread.ThreadSafe;
+import org.apache.cocoon.Constants;
import org.quartz.CronTrigger;
import org.quartz.Job;
import org.quartz.JobDataMap;
@@ -97,7 +101,7 @@
*/
public class QuartzJobScheduler
extends AbstractLogEnabled
-implements JobScheduler, Component, ThreadSafe, Serviceable, Configurable,
Startable, Disposable {
+implements JobScheduler, Component, ThreadSafe, Serviceable, Configurable,
Startable, Disposable, Contextualizable {
/** ThreadPool policy RUN */
private static final String POLICY_RUN = "RUN";
@@ -301,6 +305,14 @@
m_executor = null;
}
+ /* (non-Javadoc)
+ * @see
org.apache.avalon.framework.context.Contextualizable#contextualize(org.apache.avalon.framework.context.Context)
+ */
+ public void contextualize(Context context) throws ContextException {
+ org.apache.cocoon.environment.Context c =
(org.apache.cocoon.environment.Context)context.get(Constants.CONTEXT_ENVIRONMENT_CONTEXT);
+ System.out.println("context: " + c.getRealPath("/") );
+ }
+
/* (non-Javadoc)
* @see
org.apache.cocoon.components.cron.JobScheduler#fireTarget(java.lang.Object)
*/
@@ -752,7 +764,7 @@
try {
if (m_shutdownWaitTimeMs > 0) {
if
(!m_executor.awaitTerminationAfterShutdown(m_shutdownWaitTimeMs)) {
- getLogger().warn("scheduled cron jobs are not
terminating withing " + m_shutdownWaitTimeMs +
+ getLogger().warn("scheduled cron jobs are not
terminating within " + m_shutdownWaitTimeMs +
"ms, Will shut them down by
interruption");
m_executor.interruptAll();
m_executor.shutdownNow();
@@ -765,4 +777,5 @@
}
}
}
+
}
1.3 +55 -9
cocoon-2.1/src/blocks/cron/java/org/apache/cocoon/components/cron/TestCronJob.java
Index: TestCronJob.java
===================================================================
RCS file:
/home/cvs/cocoon-2.1/src/blocks/cron/java/org/apache/cocoon/components/cron/TestCronJob.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- TestCronJob.java 5 Sep 2003 10:22:39 -0000 1.2
+++ TestCronJob.java 19 Dec 2003 09:01:43 -0000 1.3
@@ -50,47 +50,66 @@
*/
package org.apache.cocoon.components.cron;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
import java.util.Date;
import java.util.Map;
-import org.apache.avalon.framework.component.Component;
+import org.apache.avalon.framework.component.ComponentException;
+import org.apache.avalon.framework.component.ComponentManager;
+import org.apache.avalon.framework.component.Composable;
import org.apache.avalon.framework.configuration.Configurable;
import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.configuration.ConfigurationException;
-import org.apache.avalon.framework.logger.AbstractLogEnabled;
import org.apache.avalon.framework.parameters.Parameters;
/**
- * A simple test CronJob.
+ * A simple test CronJob which also calls a pipeline internally.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Giacomo Pati</a>
+ * @author <a href="http://apache.org/~reinhard">Reinhard Poetz</a>
* @version CVS $Id$
*
* @since 2.1.1
*/
-public class TestCronJob
-extends AbstractLogEnabled
-implements CronJob, Configurable, Component, ConfigurableCronJob {
+public class TestCronJob extends AbstractPipelineCallingCronJob
+ implements CronJob, Configurable, ConfigurableCronJob, Composable {
+
/** Parameter key for the message */
public static final String PARAMETER_MESSAGE =
"TestCronJob.Parameter.Message";
/** Parameter key for the sleep value */
public static final String PARAMETER_SLEEP =
"TestCronJob.Parameter.Sleep";
+ /** Parameter key for the pipeline to be called */
+ public static final String PARAMETER_PIPELINE =
"TestCronJob.Parameter.Pipeline";
+
/** The configured message */
private String m_msg;
/** The configured sleep time */
private int m_sleep;
+
+ /** The pipeline to be called */
+ private String pipeline = null;
+
+ /** The service manager */
+ private ComponentManager manager;
+
+ public void compose( ComponentManager manager) throws ComponentException
{
+ this.manager = manager;
+ }
/* (non-Javadoc)
* @see
org.apache.avalon.framework.configuration.Configurable#configure(org.apache.avalon.framework.configuration.Configuration)
*/
public void configure(final Configuration config)
- throws ConfigurationException {
+ throws ConfigurationException {
m_msg = config.getChild("msg").getValue("I was not configured");
m_sleep = config.getChild("sleep").getValueAsInteger(11000);
+ pipeline =
config.getChild("pipeline").getValue("samples/hello-world/hello.xhtml");
}
/* (non-Javadoc)
@@ -99,7 +118,29 @@
public void execute(String name) {
getLogger().info("CronJob " + name + " launched at " + new Date() +
" with message '" + m_msg +
"' and sleep timeout of " + m_sleep + "ms");
-
+
+ InputStream is = null;
+ try {
+ is = process(pipeline);
+ } catch (Exception e) {
+ getLogger().error("error in execution of TestCronJob", e);
+ }
+ StringBuffer sb = new StringBuffer();
+ try {
+ InputStreamReader reader = new InputStreamReader(is);
+ sb = new StringBuffer();
+ char[] b = new char[8192];
+ int n;
+
+ while((n = reader.read(b)) > 0) {
+ sb.append(b, 0, n);
+ }
+ } catch( IOException ioe ) {
+ getLogger().error("error trying to read the InputStream returned
by the Pipeline processor");
+ }
+ getLogger().info("Cronjob " + name + " called pipeline " + pipeline
+
+ " and received following content:\n" + sb.toString() );
+
try {
Thread.sleep(m_sleep);
} catch (final InterruptedException ie) {
@@ -117,6 +158,11 @@
if (null != params) {
m_msg = params.getParameter(PARAMETER_MESSAGE, m_msg);
m_sleep = params.getParameterAsInteger(PARAMETER_SLEEP, m_sleep);
+ pipeline = params.getParameter(PARAMETER_PIPELINE, pipeline );
+
}
}
+
+
+
}
1.1
cocoon-2.1/src/blocks/cron/java/org/apache/cocoon/components/cron/AbstractPipelineCallingCronJob.java
Index: AbstractPipelineCallingCronJob.java
===================================================================
/*
============================================================================
The Apache Software License, Version 1.1
============================================================================
Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
Redistribution and use in source and binary forms, with or without modifica-
tion, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. The end-user documentation included with the redistribution, if any, must
include the following acknowledgment: "This product includes software
developed by the Apache Software Foundation (http://www.apache.org/)."
Alternately, this acknowledgment may appear in the software itself, if
and wherever such third-party acknowledgments normally appear.
4. The names "Apache Cocoon" and "Apache Software Foundation" must not be
used to endorse or promote products derived from this software without
prior written permission. For written permission, please contact
[EMAIL PROTECTED]
5. Products derived from this software may not be called "Apache", nor may
"Apache" appear in their name, without prior written permission of the
Apache Software Foundation.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
This software consists of voluntary contributions made by many individuals
on behalf of the Apache Software Foundation and was originally created by
Stefano Mazzocchi <[EMAIL PROTECTED]>. For more information on the Apache
Software Foundation, please see <http://www.apache.org/>.
*/
package org.apache.cocoon.components.cron;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;
import org.apache.avalon.framework.context.Context;
import org.apache.avalon.framework.context.ContextException;
import org.apache.avalon.framework.context.Contextualizable;
import org.apache.cocoon.Constants;
import org.apache.cocoon.Processor;
import org.apache.cocoon.caching.CachingOutputStream;
import org.apache.cocoon.environment.Environment;
import org.apache.cocoon.environment.background.BackgroundEnvironment;
import org.apache.cocoon.util.NullOutputStream;
/**
* An abstract CronJob implementation that provides a <code>process()</code>
* method which calls a Cocoon pipeline internally. It uses the
* <code>org.apache.cocoon.environment.background.BackgroundEnvironment</code>
* to avoid an external call.
*
* @author <a href="http://apache.org/~reinhard">Reinhard Poetz</a>
* @version CVS $Id: AbstractPipelineCallingCronJob.java,v 1.1 2003/12/19
09:01:43 reinhard Exp $
*
* @since 2.1.4
*/
public abstract class AbstractPipelineCallingCronJob
extends ServiceableCronJob
implements CronJob, Contextualizable {
protected org.apache.cocoon.environment.Context context = null;
/**
* Call an available pipeline and return
*
* @param uri of the pipeline
* @return The return of the pipeline call as <code>InputStream</code>
* @throws Exception - if pipeline is not available or returned
OutputStream couldn't be closed
*
*/
protected InputStream process(String uri) throws Exception {
// use the CachingOutputStream because it buffers all bytes written
CachingOutputStream os =
new CachingOutputStream(new NullOutputStream());
File c = new File(this.context.getRealPath("/"));
BackgroundEnvironment env =
new BackgroundEnvironment(uri, "", c, os, this.getLogger());
process(uri, env);
os.close();
return new ByteArrayInputStream(os.getContent());
}
/**
* Call the Cocoon processor to execute a pipeline.
*
* TODO (RP): Is this correct or too simplified? (I guess it is to simple
for
* pipelines with components using the ComponentManager ...)
*/
private boolean process(String uri, Environment env) throws Exception {
if (uri.length() > 0 && uri.charAt(0) == '/') {
uri = uri.substring(1);
} else {
uri = env.getURIPrefix() + uri;
}
Processor processor = null;
boolean result = false;
try {
processor = (Processor) this.manager.lookup(Processor.ROLE);
result = processor.process(env);
env.commitResponse();
return (result);
} catch (Exception any) {
throw (any);
} finally {
this.manager.release(processor);
}
}
public void contextualize(Context context) throws ContextException {
this.context =
(org.apache.cocoon.environment.Context) context.get(
Constants.CONTEXT_ENVIRONMENT_CONTEXT);
}
}
1.1
cocoon-2.1/src/blocks/cron/java/org/apache/cocoon/components/cron/ServiceableCronJob.java
Index: ServiceableCronJob.java
===================================================================
/*
============================================================================
The Apache Software License, Version 1.1
============================================================================
Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
Redistribution and use in source and binary forms, with or without modifica-
tion, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. The end-user documentation included with the redistribution, if any, must
include the following acknowledgment: "This product includes software
developed by the Apache Software Foundation (http://www.apache.org/)."
Alternately, this acknowledgment may appear in the software itself, if
and wherever such third-party acknowledgments normally appear.
4. The names "Apache Cocoon" and "Apache Software Foundation" must not be
used to endorse or promote products derived from this software without
prior written permission. For written permission, please contact
[EMAIL PROTECTED]
5. Products derived from this software may not be called "Apache", nor may
"Apache" appear in their name, without prior written permission of the
Apache Software Foundation.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
This software consists of voluntary contributions made by many individuals
on behalf of the Apache Software Foundation and was originally created by
Stefano Mazzocchi <[EMAIL PROTECTED]>. For more information on the Apache
Software Foundation, please see <http://www.apache.org/>.
*/
package org.apache.cocoon.components.cron;
import org.apache.avalon.framework.logger.AbstractLogEnabled;
import org.apache.avalon.framework.service.ServiceException;
import org.apache.avalon.framework.service.ServiceManager;
import org.apache.avalon.framework.service.Serviceable;
/**
* Serviceable CronJob
*
* @author <a href="http://apache.org/~reinhard">Reinhard Poetz</a>
* @version CVS $Id: ServiceableCronJob.java,v 1.1 2003/12/19 09:01:43
reinhard Exp $
*
* @since 2.1.4
*/
public abstract class ServiceableCronJob extends AbstractLogEnabled
implements CronJob, Serviceable {
/** The service manager */
protected ServiceManager manager;
public void service(ServiceManager manager) throws ServiceException {
this.manager = manager;
}
}
1.2 +1 -1 cocoon-2.1/src/blocks/cron/conf/cron-category.xlog
Index: cron-category.xlog
===================================================================
RCS file: /home/cvs/cocoon-2.1/src/blocks/cron/conf/cron-category.xlog,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- cron-category.xlog 3 Sep 2003 16:04:01 -0000 1.1
+++ cron-category.xlog 19 Dec 2003 09:01:43 -0000 1.2
@@ -2,7 +2,7 @@
<xlog xpath="/logkit/categories"
unless="[EMAIL PROTECTED]'cron']">
- <category log-level="DEBUG" name="cron">
+ <category log-level="INFO" name="cron">
<log-target id-ref="cron" />
<log-target id-ref="error"/>
</category>
1.3 +8 -0 cocoon-2.1/src/blocks/cron/samples/cron.js
Index: cron.js
===================================================================
RCS file: /home/cvs/cocoon-2.1/src/blocks/cron/samples/cron.js,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- cron.js 5 Sep 2003 10:23:17 -0000 1.2
+++ cron.js 19 Dec 2003 09:01:43 -0000 1.3
@@ -18,12 +18,14 @@
var scheduler =
cocoon.getComponent(Packages.org.apache.cocoon.components.cron.JobScheduler.ROLE);
var msg_param_key =
Packages.org.apache.cocoon.components.cron.TestCronJob.PARAMETER_MESSAGE;
var sleep_param_key =
Packages.org.apache.cocoon.components.cron.TestCronJob.PARAMETER_SLEEP;
+ var pipeline_param_key =
Packages.org.apache.cocoon.components.cron.TestCronJob.PARAMETER_PIPELINE;
var testjobrole = "org.apache.cocoon.components.cron.CronJob/test";
var logsize = 15;
var formatter = new Format();
var jobname = "";
var message = "I'm here";
var sleep = "23";
+ var pipeline = "samples/hello-world/hello.xml";
var cronexpr = "";
var intervalexpr = "";
var atexpr = "";
@@ -57,6 +59,7 @@
"jobname" : jobname,
"message" : message,
"sleep" : sleep,
+ "pipeline" : pipeline,
"cronexpr" : cronexpr,
"intervalexpr" : intervalexpr,
"atexpr" : atexpr
@@ -73,6 +76,7 @@
jobname = cocoon.request.getParameter( "jobname" );
message = cocoon.request.getParameter( "message" );
sleep = cocoon.request.getParameter( "sleep" );
+ pipeline = cocoon.request.getParameter( "pipeline" );
cronexpr = cocoon.request.getParameter( "cronexpr" );
intervalexpr = cocoon.request.getParameter( "intervalexpr" );
atexpr = cocoon.request.getParameter( "atexpr" );
@@ -84,6 +88,7 @@
params.setParameter( msg_param_key, message );
var sleepms = sleep * 1000;
params.setParameter( sleep_param_key, sleepms );
+ params.setParameter( pipeline_param_key, pipeline );
scheduler.addJob(jobname, testjobrole, cronexpr, false,
params, null);
}
scheduletype = cocoon.request.getParameter( "periodic" );
@@ -93,6 +98,7 @@
params.setParameter( msg_param_key, message );
var sleepms = sleep * 1000;
params.setParameter( sleep_param_key, sleepms );
+ params.setParameter( pipeline_param_key, pipeline );
scheduler.addPeriodicJob(jobname, testjobrole, intervalexpr,
false, params, null);
}
scheduletype = cocoon.request.getParameter( "at" );
@@ -102,6 +108,7 @@
params.setParameter( msg_param_key, message );
var sleepms = sleep * 1000;
params.setParameter( sleep_param_key, sleepms );
+ params.setParameter( pipeline_param_key, pipeline );
var date = formatter.parse( atexpr );
scheduler.fireJobAt(date, jobname, testjobrole, params, null)
}
@@ -112,6 +119,7 @@
params.setParameter( msg_param_key, message );
var sleepms = sleep * 1000;
params.setParameter( sleep_param_key, sleepms );
+ params.setParameter( pipeline_param_key, pipeline );
scheduler.fireJob(testjobrole, params, null)
}
}
1.4 +4 -0 cocoon-2.1/src/blocks/cron/samples/cron.xml
Index: cron.xml
===================================================================
RCS file: /home/cvs/cocoon-2.1/src/blocks/cron/samples/cron.xml,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- cron.xml 29 Oct 2003 13:06:22 -0000 1.3
+++ cron.xml 19 Dec 2003 09:01:43 -0000 1.4
@@ -63,6 +63,10 @@
<td><input name="sleep" size="4"
value="${sleep}"/></td>
</tr>
<tr>
+ <td align="right">Call a pipeline:</td>
+ <td><input name="pipeline" size="40"
value="${pipeline}"/></td>
+ </tr>
+ <tr>
<td align="right">
<p>Choose the type of triggering:</p>
</td>