Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

FOPTestbed.java 7.5KB

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