# HG changeset patch # User cmielack # Date 1236334106 -3600 # Node ID 06d7e8c09b11d30cff6cc11004aa2a540f51d082 # Parent a370e72f2179b4e75fe4948fb602dff3519095ec *** empty log message *** diff -r a370e72f2179 -r 06d7e8c09b11 servlet/src/digilib/servlet/DigilibPDFWorker.java --- a/servlet/src/digilib/servlet/DigilibPDFWorker.java Fri Mar 06 11:06:21 2009 +0100 +++ b/servlet/src/digilib/servlet/DigilibPDFWorker.java Fri Mar 06 11:08:26 2009 +0100 @@ -78,13 +78,14 @@ // create document object doc = new Document(PageSize.A4, 0,0,0,0); PdfWriter docwriter = null; - File output_file = new File(PDFCache.cache_directory + filename); + File output_file = new File(PDFCache.temp_directory + filename); FileOutputStream fos; - + try { fos = new FileOutputStream(output_file); } catch (FileNotFoundException e1) { // TODO Auto-generated catch block + logger.error(e1.getMessage()); e1.printStackTrace(); return; } @@ -100,16 +101,20 @@ doc.open(); - logger.debug("- doc.open()ed ("+(System.currentTimeMillis()-start_time) + "ms)"); + logger.debug("- "+filename+" doc.open()ed ("+(System.currentTimeMillis()-start_time) + "ms)"); start_time = System.currentTimeMillis(); Integer[] pgs = get_pgs(); for(Integer p: pgs){ + logger.debug(" - adding Image "+p+" to " + filename); addImage(p); + logger.debug(" - done adding Image "+p+" to " + filename); } + logger.debug(" - done adding all Images to " + filename); + } @@ -122,7 +127,7 @@ finally { if (doc!=null){ doc.close(); - logger.debug("- doc.close() ("+(System.currentTimeMillis()-start_time) + "ms)"); + logger.debug("- "+filename+" doc.close() ("+(System.currentTimeMillis()-start_time) + "ms)"); } if (docwriter!=null){ docwriter.close(); @@ -134,6 +139,7 @@ try { fos.flush(); } catch (IOException e) { + logger.error(e.getMessage()); e.printStackTrace(); error = e; } @@ -142,6 +148,7 @@ try { fos.close(); } catch (IOException e) { + logger.error(e.getMessage()); e.printStackTrace(); } } @@ -205,14 +212,19 @@ doc.add(theimg); } catch (FileOpException e) { + logger.error(e.getMessage()); e.printStackTrace(); } catch (IOException e) { + logger.error(e.getMessage()); e.printStackTrace(); } catch (ImageOpException e) { + logger.error(e.getMessage()); e.printStackTrace(); } catch (BadElementException e) { + logger.error(e.getMessage()); e.printStackTrace(); } catch (DocumentException e) { + logger.error(e.getMessage()); e.printStackTrace(); } } diff -r a370e72f2179 -r 06d7e8c09b11 servlet/src/digilib/servlet/PDFCache.java --- a/servlet/src/digilib/servlet/PDFCache.java Fri Mar 06 11:06:21 2009 +0100 +++ b/servlet/src/digilib/servlet/PDFCache.java Fri Mar 06 11:08:26 2009 +0100 @@ -7,6 +7,7 @@ import java.io.InputStream; import java.util.HashMap; +import javax.servlet.RequestDispatcher; import javax.servlet.ServletConfig; import javax.servlet.ServletContext; import javax.servlet.ServletException; @@ -18,12 +19,17 @@ private DigilibConfiguration dlConfig = null; + + public static String cache_directory = "cache/"; // TODO set using dlConfig - public static String cache_hash_id = "digilib.servlet.PDFCache"; + public static String temp_directory = "pdf_temp/"; + + - HashMap cache_hash = null; + private static String JSP_WIP = "/pdf/wip.jsp"; + private static String JSP_ERROR = "/pdf/error.jsp"; public static Integer STATUS_DONE = 0; // document exists in cache @@ -40,6 +46,7 @@ // TODO use JSPs for automatically refreshing waiting-pages and download-pages // TODO register the PDFCache instance globally and implement getters for cache_dir + private ServletContext context = null; public void init(ServletConfig config) throws ServletException{ @@ -50,7 +57,7 @@ logger.info("initialized PDFCache v."+version); // create and register hashtable - ServletContext context = config.getServletContext(); + context = config.getServletContext(); dlConfig = (DigilibConfiguration) context.getAttribute("digilib.servlet.configuration"); @@ -59,38 +66,30 @@ throw new ServletException("No Configuration!"); } - context.setAttribute(cache_hash_id, new HashMap()); + //context.setAttribute(cache_hash_id, new HashMap()); - cache_hash = (HashMap) context.getAttribute(cache_hash_id); + //cache_hash = (HashMap) context.getAttribute(cache_hash_id); - if (cache_hash==null){ + /*if (cache_hash==null){ cache_hash = new HashMap(); context.setAttribute(cache_hash_id, cache_hash); - } + }*/ // scan the directory - scanCacheDirectory(); - + // scanCacheDirectory(); + emptyTempDirectory(); } - public void scanCacheDirectory(){ + public void emptyTempDirectory(){ // search the cache-directory for existing files and fill them into the Hashtable as STATUS_DONE - ServletContext context = this.getServletContext(); - HashMap cache_hash = (HashMap) context.getAttribute(cache_hash_id); - - File cache_dir = new File(cache_directory); - String[] cached_files = cache_dir.list(); + File temp_dir = new File(temp_directory); + String[] cached_files = temp_dir.list(); for (String file: cached_files){ - String docid = file.substring(0,file.length()-4); - logger.debug("docid = "+docid); - if (file.endsWith(".pdf") && !cache_hash.containsKey(docid)){ - logger.debug("PDFCache reads in "+file); - cache_hash.put(file, STATUS_DONE); - } + new File(temp_directory,file).delete(); } } @@ -107,6 +106,11 @@ String docid = pdfji.getDocumentId(); + // if some invalid data has been entered ... + if(!pdfji.checkValidity()) { + notifyUser(STATUS_ERROR, docid, request, response); + return; + } int status = getStatus(docid); @@ -114,7 +118,7 @@ if(status == STATUS_NONEXISTENT){ createNewPdfDocument(pdfji, docid); - informUser(status, docid, response); + notifyUser(status, docid, request, response); } else if (status == STATUS_DONE){ try { @@ -125,53 +129,73 @@ } } else { - informUser(status, docid, response); + notifyUser(status, docid, request, response); } } - public void informUser(int status, String documentid, HttpServletResponse response){ + public void notifyUser(int status, String documentid, HttpServletRequest request, HttpServletResponse response){ // depending on the documents status, redirect the user to an appropriate waiting- or download-site // TODO + String jsp=null; + if(status == STATUS_NONEXISTENT){ // tell the user that the document has to be created before he/she can download it logger.debug("PDFCache: "+documentid+" has STATUS_NONEXISTENT."); + jsp = JSP_WIP; } else if(status == STATUS_WIP){ logger.debug("PDFCache: "+documentid+" has STATUS_WIP."); + jsp = JSP_WIP; // estimate remaining work time // tell the user he/she has to wait } else if(status == STATUS_DONE){ logger.debug("PDFCache: "+documentid+" has STATUS_DONE."); - - // do nothing or refresh } else { logger.debug("PDFCache: "+documentid+" has STATUS_ERROR."); + jsp = JSP_ERROR; + } - // status == STATUS_ERROR - // an error must have occured; show error page + RequestDispatcher dispatch = context.getRequestDispatcher(jsp); + + + try { + dispatch.forward(request, response); + } catch (ServletException e) { + logger.debug(e.getMessage()); + e.printStackTrace(); + } catch (IOException e) { + logger.debug(e.getMessage()); + e.printStackTrace(); } - + } /** check the status of the document corresponding to the documentid */ public Integer getStatus(String documentid){ - // rescan directory? might be useful if more than one instance uses the same cache directory ; Problem: wip-files occur in the list - if(cache_hash.containsKey(documentid)) - return cache_hash.get(documentid); - else + // looks into the cache and temp directory in order to find out the status of the document + File cached = new File(cache_directory + documentid); + File wip = new File(temp_directory + documentid); + if(cached.exists()){ + return STATUS_DONE; + } + else if (wip.exists()){ + return STATUS_WIP; + } + else { return STATUS_NONEXISTENT; + } } public void createNewPdfDocument(PDFJobInformation pdfji, String filename){ // start new worker - PDFMaker pdf_maker = new PDFMaker(dlConfig, cache_hash, pdfji,filename); + PDFMaker pdf_maker = new PDFMaker(dlConfig, pdfji,filename); new Thread(pdf_maker, "PDFMaker").start(); } diff -r a370e72f2179 -r 06d7e8c09b11 servlet/src/digilib/servlet/PDFJobInformation.java --- a/servlet/src/digilib/servlet/PDFJobInformation.java Fri Mar 06 11:06:21 2009 +0100 +++ b/servlet/src/digilib/servlet/PDFJobInformation.java Fri Mar 06 11:08:26 2009 +0100 @@ -6,6 +6,8 @@ import javax.servlet.http.HttpServletRequest; +import org.apache.log4j.Logger; + /** * A container class for storing a set of instructional parameters @@ -29,6 +31,9 @@ ImageJobInformation image_info = null; DigilibConfiguration dlConfig = null; + /** gengeral logger for this class */ + protected static Logger logger = Logger.getLogger("digilib.servlet"); + public PDFJobInformation(DigilibConfiguration dlcfg) { super(30); @@ -176,4 +181,40 @@ return numarray; } + + public boolean checkValidity(){ + String pgs = getAsString("pgs"); + try{ + String[] intervals = null; + if(pgs.indexOf(",")>0){ + intervals = pgs.split(","); + } + else{ + intervals = new String[1]; + intervals[0]=pgs; + } + for(String interval:intervals){ + if(interval.indexOf("-")>=0){ + String[] intrvl = interval.split("-"); + int a = Integer.valueOf(intrvl[0]); + int b = Integer.valueOf(intrvl[1]); + if(a<=0 || b) context.getAttribute(PDFCache.cache_hash_id); - }*/ + /** gengeral logger for this class */ + protected static Logger logger = Logger.getLogger("digilib.servlet"); - public PDFMaker(DigilibConfiguration dlConfig, HashMap cache_hash, PDFJobInformation pdfji, String filename){ - this.cache_hash = cache_hash; + + public PDFMaker(DigilibConfiguration dlConfig, PDFJobInformation pdfji, String filename){ this.job_info = pdfji; this.filename = filename; this.dlConfig = dlConfig; @@ -35,24 +35,34 @@ public void run() { if (! DigilibWorker.canRun()) { - // logger.error("Servlet overloaded!"); - cache_hash.put(filename, PDFCache.STATUS_ERROR); + // TODO include the logger + logger.error("Servlet overloaded!"); + return; } - cache_hash.put(filename, PDFCache.STATUS_WIP); - // create PDFWorker DigilibPDFWorker pdf_worker = new DigilibPDFWorker(dlConfig, job_info, filename); // run PDFWorker pdf_worker.run(); - + + File document = new File(PDFCache.temp_directory + filename); + if(pdf_worker.hasError()){ - cache_hash.put(filename, PDFCache.STATUS_ERROR); + // raise error, write to logger + logger.error("@@@@ "+pdf_worker.getError().getMessage()); + document.delete(); return; } - cache_hash.put(filename, PDFCache.STATUS_DONE); + else{ + boolean success = document.renameTo(new File(PDFCache.cache_directory, filename)); + if(!success){ + // TODO raise error + + } + } + }