KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > books > publisher > impl > publicationprocess > MakePdfTask


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
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 package org.outerj.daisy.books.publisher.impl.publicationprocess;
17
18 import xslfo.FODocument;
19
20 import java.io.InputStream JavaDoc;
21 import java.io.OutputStream JavaDoc;
22 import java.util.Properties JavaDoc;
23
24 import org.outerj.daisy.books.publisher.impl.BookInstanceLayout;
25 import org.outerj.daisy.books.publisher.impl.util.PublicationLog;
26 import xmlpdf.logging.Logger;
27 import xmlpdf.logging.Level;
28 import xmlpdf.logging.Handler;
29 import xmlpdf.logging.LogRecord;
30 import EDU.oswego.cs.dl.util.concurrent.FIFOSemaphore;
31
32 // VERY IMPORTANT: this class assumes it is the only one in the Java VM (or at least in the classloader
33
// from which IBEX was loaded) that uses IBEX.
34
// This class synchronises all IBEX access within its classloader.
35
public class MakePdfTask implements PublicationProcessTask {
36     private final String JavaDoc input;
37     private final String JavaDoc output;
38     private static FIFOSemaphore semaphore = new FIFOSemaphore(1);
39
40     static {
41         try {
42             InputStream JavaDoc is = null;
43             try {
44                 is = MakePdfTask.class.getClassLoader().getResourceAsStream("org/outerj/daisy/books/publisher/impl/publicationprocess/daisy-ibex.properties");
45                 if (is != null) {
46                     Properties JavaDoc properties = new Properties JavaDoc();
47                     properties.load(is);
48                     String JavaDoc runtimekey = properties.getProperty("ibex.runtimekey");
49                     if (runtimekey != null) {
50                         xmlpdf.licensing.Generator.setRuntimeKey(runtimekey.trim());
51                     }
52                 }
53             } finally {
54                 if (is != null)
55                     is.close();
56             }
57         } catch (Throwable JavaDoc e) {
58             System.err.println("Error in ibex key loading: " + e.toString());
59         }
60     }
61
62     public MakePdfTask(String JavaDoc input, String JavaDoc output) {
63         this.input = input;
64         this.output = output;
65     }
66
67     public void run(PublicationContext context) throws Exception JavaDoc {
68         InputStream JavaDoc is = null;
69         OutputStream JavaDoc os = null;
70         context.getPublicationLog().info("Will wait in the queue to produce a PDF...");
71         long beforeAcquire = System.currentTimeMillis();
72         semaphore.acquire();
73         try {
74             context.getPublicationLog().info("Will begin PDF production, had to wait " + (System.currentTimeMillis() - beforeAcquire) + " ms before starting.");
75             Logger.getLogger().setLevel(Level.FINEST);
76             Logger.getLogger().clearHandlers();
77             Logger.getLogger().addHandler(new MyLogHandler(context.getPublicationLog()));
78             String JavaDoc pubPath = BookInstanceLayout.getPublicationOutputPath(context.getPublicationOutputName());
79             FODocument foDocument = new FODocument();
80             is = context.getBookInstance().getResource(pubPath + input);
81             os = context.getBookInstance().getResourceOutputStream(pubPath + output);
82             foDocument.generate(is, os, true);
83             context.getPublicationLog().info("Produced a PDF containg " + foDocument.getPageCount() + " pages.");
84         } catch (Throwable JavaDoc t) {
85             throw new Exception JavaDoc("Error calling Ibex.", t);
86         } finally {
87             semaphore.release();
88             Logger.getLogger().clearHandlers();
89             if (is != null)
90                 try { is.close(); } catch (Throwable JavaDoc e) { context.getPublicationLog().error("Error closing fo is.", e); }
91             if (os != null)
92                 try { os.close(); } catch (Throwable JavaDoc e) { context.getPublicationLog().error("Error closing pdf os.", e); }
93         }
94     }
95
96     static class MyLogHandler extends Handler {
97         private final PublicationLog publicationLog;
98
99         public MyLogHandler(PublicationLog publicationLog) {
100             this.publicationLog = publicationLog;
101         }
102
103         public void publish(LogRecord logRecord) {
104             publicationLog.info("IBEX: " + logRecord.getMessage());
105         }
106
107         public void close() {
108         }
109     }
110 }
111
Popular Tags