1 16 package org.apache.cocoon.components.cron; 17 18 import java.io.InputStreamReader ; 19 import java.util.Date ; 20 import java.util.Map ; 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 38 public class TestCronJob extends ServiceableCronJob 39 implements Configurable, ConfigurableCronJob { 40 41 42 public static final String PARAMETER_MESSAGE = "TestCronJob.Parameter.Message"; 43 44 45 public static final String PARAMETER_SLEEP = "TestCronJob.Parameter.Sleep"; 46 47 48 public static final String PARAMETER_PIPELINE = "TestCronJob.Parameter.Pipeline"; 49 50 51 private String m_msg; 52 53 54 private int m_sleep; 55 56 57 private String pipeline; 58 59 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 72 public void execute(String name) { 73 getLogger().info("CronJob " + name + " launched at " + new Date () + " 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 r = new InputStreamReader (src.getInputStream()); 83 try { 84 StringBuffer sb = new StringBuffer (); 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 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 ie) { 112 } 114 115 getLogger().info("CronJob " + name + " finished at " + new Date () + " with message '" + m_msg + 116 "' and sleep timeout of " + m_sleep + "ms"); 117 } 118 119 122 public void setup(Parameters params, Map 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 |