| 1 16 package org.outerj.daisy.books.publisher.impl.publicationprocess; 17 18 import xslfo.FODocument; 19 20 import java.io.InputStream ; 21 import java.io.OutputStream ; 22 import java.util.Properties ; 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 public class MakePdfTask implements PublicationProcessTask { 36 private final String input; 37 private final String output; 38 private static FIFOSemaphore semaphore = new FIFOSemaphore(1); 39 40 static { 41 try { 42 InputStream 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 properties = new Properties (); 47 properties.load(is); 48 String 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 e) { 58 System.err.println("Error in ibex key loading: " + e.toString()); 59 } 60 } 61 62 public MakePdfTask(String input, String output) { 63 this.input = input; 64 this.output = output; 65 } 66 67 public void run(PublicationContext context) throws Exception { 68 InputStream is = null; 69 OutputStream 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 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 t) { 85 throw new Exception ("Error calling Ibex.", t); 86 } finally { 87 semaphore.release(); 88 Logger.getLogger().clearHandlers(); 89 if (is != null) 90 try { is.close(); } catch (Throwable e) { context.getPublicationLog().error("Error closing fo is.", e); } 91 if (os != null) 92 try { os.close(); } catch (Throwable 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 |