KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > threading > FOPTestbed


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 /* $Id: FOPTestbed.java 426576 2006-07-28 15:44:37Z jeremias $ */
19
20 package org.apache.fop.threading;
21
22 import java.util.List JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.io.File JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.io.OutputStream JavaDoc;
27 import java.text.DecimalFormat JavaDoc;
28
29 import javax.xml.transform.Source JavaDoc;
30 import javax.xml.transform.Templates JavaDoc;
31 import javax.xml.transform.TransformerConfigurationException JavaDoc;
32 import javax.xml.transform.TransformerFactory JavaDoc;
33 import javax.xml.transform.stream.StreamSource JavaDoc;
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 JavaDoc tasks = new java.util.ArrayList JavaDoc();
51     private int threads;
52     private File JavaDoc outputDir;
53     private Configuration fopCfg;
54
55     private int counter = 0;
56     
57     public void configure(Configuration configuration) throws ConfigurationException JavaDoc {
58         this.threads = configuration.getChild("threads").getValueAsInteger(10);
59         this.outputDir = new File JavaDoc(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 JavaDoc {
70     }
71
72     public void doStressTest() {
73         getLogger().info("Starting stress test...");
74         long start = System.currentTimeMillis();
75         this.counter = 0;
76         
77         //Initialize threads
78
List JavaDoc threadList = new java.util.LinkedList JavaDoc();
79         for (int ti = 0; ti < this.threads; ti++) {
80             TaskRunner runner = new TaskRunner();
81             ContainerUtil.enableLogging(runner, getLogger());
82             Thread JavaDoc thread = new Thread JavaDoc(runner);
83             threadList.add(thread);
84         }
85         
86         //Start threads
87
Iterator JavaDoc i = threadList.iterator();
88         while (i.hasNext()) {
89             ((Thread JavaDoc)i.next()).start();
90         }
91         
92         //Wait for threads to end
93
while (threadList.size() > 0) {
94             Thread JavaDoc t = (Thread JavaDoc)threadList.get(0);
95             if (!t.isAlive()) {
96                 threadList.remove(0);
97                 continue;
98             }
99             try {
100                 Thread.sleep(100);
101             } catch (InterruptedException JavaDoc ie) {
102             }
103         }
104         getLogger().info("Stress test duration: " + (System.currentTimeMillis() - start) + "ms");
105     }
106
107     private class TaskRunner extends AbstractLogEnabled implements Runnable JavaDoc {
108         
109         public void run() {
110             try {
111                 for (int r = 0; r < repeat; r++) {
112                     Iterator JavaDoc 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 JavaDoc e) {
121                 getLogger().error("Thread ended with an exception", e);
122             }
123         }
124
125     }
126     
127     
128     public FOProcessor createFOProcessor() {
129         try {
130             Class JavaDoc 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 JavaDoc e) {
138             throw new CascadingRuntimeException("Error creating FO Processor", e);
139         }
140     }
141     
142
143     private class TaskDef {
144         private String JavaDoc fo;
145         private String JavaDoc xml;
146         private String JavaDoc xslt;
147         private Templates JavaDoc templates;
148
149         public TaskDef(String JavaDoc fo) {
150             this.fo = fo;
151         }
152
153         public TaskDef(Configuration cfg) throws ConfigurationException JavaDoc {
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 JavaDoc factory = TransformerFactory.newInstance();
159                 Source JavaDoc xsltSource = new StreamSource JavaDoc(new File JavaDoc(xslt));
160                 try {
161                     this.templates = factory.newTemplates(xsltSource);
162                 } catch (TransformerConfigurationException JavaDoc tce) {
163                     throw new ConfigurationException JavaDoc("Invalid XSLT", tce);
164                 }
165             }
166         }
167
168         public String JavaDoc getFO() {
169             return this.fo;
170         }
171
172         public String JavaDoc getXML() {
173             return this.xml;
174         }
175
176         public Templates JavaDoc getTemplates() {
177             return this.templates;
178         }
179
180         public String JavaDoc toString() {
181             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
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 JavaDoc {
208             getLogger().info("Processing: " + def);
209             FOProcessor fop = (FOProcessor)createFOProcessor();
210             DecimalFormat JavaDoc df = new DecimalFormat JavaDoc("00000");
211             File JavaDoc outfile = new File JavaDoc(outputDir, df.format(num) + ".pdf");
212             OutputStream JavaDoc out = new java.io.FileOutputStream JavaDoc(outfile);
213             try {
214                 InputStream JavaDoc in;
215                 Templates JavaDoc templates;
216                 
217                 if (def.getFO() != null) {
218                     in = new java.io.FileInputStream JavaDoc(new File JavaDoc(def.getFO()));
219                     templates = null;
220                 } else {
221                     in = new java.io.FileInputStream JavaDoc(new File JavaDoc(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