1 17 18 19 20 package org.apache.fop.threading; 21 22 import java.util.List ; 23 import java.util.Iterator ; 24 import java.io.File ; 25 import java.io.InputStream ; 26 import java.io.OutputStream ; 27 import java.text.DecimalFormat ; 28 29 import javax.xml.transform.Source ; 30 import javax.xml.transform.Templates ; 31 import javax.xml.transform.TransformerConfigurationException ; 32 import javax.xml.transform.TransformerFactory ; 33 import javax.xml.transform.stream.StreamSource ; 34 35 import org.apache.avalon.framework.CascadingRuntimeException; 36 import org.apache.avalon.framework.activity.Executable; 37 import org.apache.avalon.framework.activity.Initializable; 38 import org.apache.avalon.framework.configuration.Configurable; 39 import org.apache.avalon.framework.configuration.Configuration; 40 import org.apache.avalon.framework.configuration.ConfigurationException; 41 import org.apache.avalon.framework.container.ContainerUtil; 42 import org.apache.avalon.framework.logger.AbstractLogEnabled; 43 import org.apache.avalon.framework.logger.Logger; 44 import org.apache.commons.io.IOUtils; 45 46 public class FOPTestbed extends AbstractLogEnabled 47 implements Configurable, Initializable { 48 49 private int repeat; 50 private List tasks = new java.util.ArrayList (); 51 private int threads; 52 private File outputDir; 53 private Configuration fopCfg; 54 55 private int counter = 0; 56 57 public void configure(Configuration configuration) throws ConfigurationException { 58 this.threads = configuration.getChild("threads").getValueAsInteger(10); 59 this.outputDir = new File (configuration.getChild("output-dir").getValue()); 60 Configuration tasks = configuration.getChild("tasks"); 61 this.repeat = tasks.getAttributeAsInteger("repeat", 1); 62 Configuration[] entries = tasks.getChildren("task"); 63 for (int i=0; i<entries.length; i++) { 64 this.tasks.add(new TaskDef(entries[i])); 65 } 66 this.fopCfg = configuration.getChild("foprocessor"); 67 } 68 69 public void initialize() throws Exception { 70 } 71 72 public void doStressTest() { 73 getLogger().info("Starting stress test..."); 74 long start = System.currentTimeMillis(); 75 this.counter = 0; 76 77 List threadList = new java.util.LinkedList (); 79 for (int ti = 0; ti < this.threads; ti++) { 80 TaskRunner runner = new TaskRunner(); 81 ContainerUtil.enableLogging(runner, getLogger()); 82 Thread thread = new Thread (runner); 83 threadList.add(thread); 84 } 85 86 Iterator i = threadList.iterator(); 88 while (i.hasNext()) { 89 ((Thread )i.next()).start(); 90 } 91 92 while (threadList.size() > 0) { 94 Thread t = (Thread )threadList.get(0); 95 if (!t.isAlive()) { 96 threadList.remove(0); 97 continue; 98 } 99 try { 100 Thread.sleep(100); 101 } catch (InterruptedException ie) { 102 } 103 } 104 getLogger().info("Stress test duration: " + (System.currentTimeMillis() - start) + "ms"); 105 } 106 107 private class TaskRunner extends AbstractLogEnabled implements Runnable { 108 109 public void run() { 110 try { 111 for (int r = 0; r < repeat; r++) { 112 Iterator i = tasks.iterator(); 113 while (i.hasNext()) { 114 TaskDef def = (TaskDef)i.next(); 115 final Task task = new Task(def, counter++); 116 ContainerUtil.enableLogging(task, getLogger()); 117 task.execute(); 118 } 119 } 120 } catch (Exception e) { 121 getLogger().error("Thread ended with an exception", e); 122 } 123 } 124 125 } 126 127 128 public FOProcessor createFOProcessor() { 129 try { 130 Class clazz = Class.forName(this.fopCfg.getAttribute("class", 131 "org.apache.fop.threading.FOProcessorImpl")); 132 FOProcessor fop = (FOProcessor)clazz.newInstance(); 133 ContainerUtil.enableLogging(fop, getLogger()); 134 ContainerUtil.configure(fop, this.fopCfg); 135 ContainerUtil.initialize(fop); 136 return fop; 137 } catch (Exception e) { 138 throw new CascadingRuntimeException("Error creating FO Processor", e); 139 } 140 } 141 142 143 private class TaskDef { 144 private String fo; 145 private String xml; 146 private String xslt; 147 private Templates templates; 148 149 public TaskDef(String fo) { 150 this.fo = fo; 151 } 152 153 public TaskDef(Configuration cfg) throws ConfigurationException { 154 this.fo = cfg.getAttribute("fo", null); 155 if (this.fo == null) { 156 this.xml = cfg.getAttribute("xml"); 157 this.xslt = cfg.getAttribute("xslt"); 158 TransformerFactory factory = TransformerFactory.newInstance(); 159 Source xsltSource = new StreamSource (new File (xslt)); 160 try { 161 this.templates = factory.newTemplates(xsltSource); 162 } catch (TransformerConfigurationException tce) { 163 throw new ConfigurationException ("Invalid XSLT", tce); 164 } 165 } 166 } 167 168 public String getFO() { 169 return this.fo; 170 } 171 172 public String getXML() { 173 return this.xml; 174 } 175 176 public Templates getTemplates() { 177 return this.templates; 178 } 179 180 public String toString() { 181 StringBuffer sb = new StringBuffer (); 182 if (this.fo != null) { 183 sb.append("fo="); 184 sb.append(this.fo); 185 } else { 186 sb.append("xml="); 187 sb.append(this.xml); 188 sb.append(" xslt="); 189 sb.append(this.xslt); 190 } 191 return sb.toString(); 192 } 193 } 194 195 196 private class Task extends AbstractLogEnabled implements Executable { 197 198 private TaskDef def; 199 private int num; 200 201 public Task(TaskDef def, int num) { 202 this.def = def; 203 this.num = num; 204 } 205 206 207 public void execute() throws Exception { 208 getLogger().info("Processing: " + def); 209 FOProcessor fop = (FOProcessor)createFOProcessor(); 210 DecimalFormat df = new DecimalFormat ("00000"); 211 File outfile = new File (outputDir, df.format(num) + ".pdf"); 212 OutputStream out = new java.io.FileOutputStream (outfile); 213 try { 214 InputStream in; 215 Templates templates; 216 217 if (def.getFO() != null) { 218 in = new java.io.FileInputStream (new File (def.getFO())); 219 templates = null; 220 } else { 221 in = new java.io.FileInputStream (new File (def.getXML())); 222 templates = def.getTemplates(); 223 } 224 try { 225 fop.process(in, templates, out); 226 } finally { 227 IOUtils.closeQuietly(in); 228 } 229 } finally { 230 IOUtils.closeQuietly(out); 231 } 232 } 233 } 234 235 } | Popular Tags |