diff --git a/source/org/thdl/tib/scanner/CachedSyllableListTree.java b/source/org/thdl/tib/scanner/CachedSyllableListTree.java index bf28a95..42f5fbe 100644 --- a/source/org/thdl/tib/scanner/CachedSyllableListTree.java +++ b/source/org/thdl/tib/scanner/CachedSyllableListTree.java @@ -35,15 +35,20 @@ import org.thdl.util.*; public class CachedSyllableListTree implements SyllableListTree { SyllableListTree syllables[]; - + public CachedSyllableListTree(String archivo) throws Exception + { + this (archivo, true); + } + + public CachedSyllableListTree(String archivo, boolean backwardCompatible) throws Exception { String sil; long pos, defSources[]; DictionarySource sourceDef; int i; - FileSyllableListTree.openFiles(archivo); + FileSyllableListTree.openFiles(archivo, backwardCompatible); SimplifiedLinkedList syllables = new SimplifiedLinkedList(); do diff --git a/source/org/thdl/tib/scanner/FileSyllableListTree.java b/source/org/thdl/tib/scanner/FileSyllableListTree.java index 98013ef..9ccd510 100644 --- a/source/org/thdl/tib/scanner/FileSyllableListTree.java +++ b/source/org/thdl/tib/scanner/FileSyllableListTree.java @@ -82,8 +82,26 @@ public class FileSyllableListTree implements SyllableListTree { return this.defSourcesWanted; } - + public static void openFiles(String archivo) throws Exception + { + openFiles(archivo, true); + } + + public static void closeFiles() + { + try + { + wordRaf.close(); + defRaf.close(); + } + catch (Exception e) + { + e.printStackTrace(); + } + } + + public static void openFiles(String archivo, boolean backwardCompatible) throws Exception { long fileSize; int pos; @@ -109,17 +127,34 @@ public class FileSyllableListTree implements SyllableListTree // Updates the dictionary for backward compatibility. try { - wordRaf.close(); - wordRaf = new RandomAccessFile(archivo + ".wrd", "rw"); - wordRaf.seek(fileSize); - wordRaf.writeShort(-1); - wordRaf.writeByte(-1); - - // Because it didn't have a version number, must be version 2. - versionNumber = 2; - wordRaf.writeByte(versionNumber); - wordRaf.close(); - wordRaf = new RandomAccessFile(archivo + ".wrd", "r"); + if (backwardCompatible) + { + wordRaf.close(); + wordRaf = new RandomAccessFile(archivo + ".wrd", "rw"); + wordRaf.seek(fileSize); + wordRaf.writeShort(-1); + wordRaf.writeByte(-1); + + // Because it didn't have a version number, must be version 2. + versionNumber = 2; + wordRaf.writeByte(versionNumber); + wordRaf.close(); + wordRaf = new RandomAccessFile(archivo + ".wrd", "r"); + } + else + { + // something is wrong + ScannerLogger sl = new ScannerLogger(); + sl.writeLog("Crash\tFileSyllableListTree\t" + "size: " + fileSize + "; bytes: " + Integer.toHexString(pos)); + + // try to open again, but not corrupting the file + wordRaf = new RandomAccessFile(archivo + ".wrd", "r"); + + fileSize = wordRaf.length(); + wordRaf.seek(fileSize-8L); + pos = wordRaf.readInt(); + versionNumber = 3; + } } catch (Exception e) { diff --git a/source/org/thdl/tib/scanner/LocalTibetanScanner.java b/source/org/thdl/tib/scanner/LocalTibetanScanner.java index 7d48030..bb5c1b8 100644 --- a/source/org/thdl/tib/scanner/LocalTibetanScanner.java +++ b/source/org/thdl/tib/scanner/LocalTibetanScanner.java @@ -47,15 +47,19 @@ public class LocalTibetanScanner extends TibetanScanner { return raiz.getDictionarySourcesWanted(); } - - + public LocalTibetanScanner(String arch) throws Exception + { + this (arch, true); + } + + public LocalTibetanScanner(String arch, boolean backwardCompatible) throws Exception { super(); archivo = arch; // raiz = new MemorySyllableListTree(archivo); // raiz = new FileSyllableListTree(archivo); - raiz = new CachedSyllableListTree(archivo); + raiz = new CachedSyllableListTree(archivo, backwardCompatible); floatingSil = new Vector(); resetAll(); } @@ -409,4 +413,9 @@ outAHere: return null; } } + public void destroy() + { + FileSyllableListTree.closeFiles(); + } + } \ No newline at end of file diff --git a/source/org/thdl/tib/scanner/OnLineScannerFilter.java b/source/org/thdl/tib/scanner/OnLineScannerFilter.java index 48df7ba..d2ffe37 100644 --- a/source/org/thdl/tib/scanner/OnLineScannerFilter.java +++ b/source/org/thdl/tib/scanner/OnLineScannerFilter.java @@ -48,16 +48,29 @@ public class OnLineScannerFilter extends HttpServlet ResourceBundle rb; private TibetanScanner scanner; private String dictionaries[]; + private ScannerLogger sl; - public OnLineScannerFilter() throws Exception + public OnLineScannerFilter() //throws Exception { rb = ResourceBundle.getBundle(propertyFile); - scanner = new LocalTibetanScanner(rb.getString(dictNameProperty)); + sl = new ScannerLogger(); + + try + { + scanner = new LocalTibetanScanner(rb.getString(dictNameProperty), false); + } + catch (Exception e) + { + sl.writeLog("Crash\tOnLineScannerFilter"); + sl.writeException(e); + } + dictionaries = scanner.getDictionaryDescriptions(); + sl.writeLog("Creation\tOnLineScannerFilter"); } synchronized public void doGet(HttpServletRequest request, - HttpServletResponse response) throws IOException, ServletException + HttpServletResponse response) //throws IOException, ServletException { String answer, parrafo = null, checkboxName; @@ -66,7 +79,19 @@ public class OnLineScannerFilter extends HttpServlet ThdlOptions.setUserPreference("thdl.rely.on.system.tm.fonts", true); response.setContentType("text/html"); - PrintWriter out = response.getWriter(); + PrintWriter out; + sl.setUserIP(request.getRemoteAddr()); + + try + { + out = response.getWriter(); + } + catch (Exception e) + { + sl.writeLog("Crash\tOnLineScannerFilter"); + sl.writeException(e); + return; + } BitDictionarySource ds=null; boolean checkedDicts[], allUnchecked, wantsTibetan, useTHDLBanner = (request.getParameter("thdlBanner")!=null); @@ -260,8 +285,12 @@ public class OnLineScannerFilter extends HttpServlet out.println(""); if (parrafo != null) + { + sl.writeLog("Translation\tOnLineScannerFilter"); if (ds!=null && !ds.isEmpty()) desglosar(parrafo, out, wantsTibetan); + } + else sl.writeLog("Invocation\tOnLineScannerFilter"); out.println(TibetanScanner.copyrightHTML); if (useTHDLBanner) out.println(""); @@ -271,7 +300,7 @@ public class OnLineScannerFilter extends HttpServlet public void doPost(HttpServletRequest request, HttpServletResponse response) - throws IOException, ServletException + //throws IOException, ServletException { doGet(request, response); } @@ -408,4 +437,13 @@ public class OnLineScannerFilter extends HttpServlet } pw.println(""); } + + public void destroy() + { + super.destroy(); + sl.setUserIP(null); + sl.writeLog("Shutdown\tOnLineScannerFilter"); + scanner.destroy(); + } + } diff --git a/source/org/thdl/tib/scanner/PocketWindowScannerFilter.java b/source/org/thdl/tib/scanner/PocketWindowScannerFilter.java index 8041a39..a53f943 100644 --- a/source/org/thdl/tib/scanner/PocketWindowScannerFilter.java +++ b/source/org/thdl/tib/scanner/PocketWindowScannerFilter.java @@ -22,7 +22,6 @@ import java.awt.*; import java.util.*; import java.io.*; import java.awt.event.*; -import java.applet.Applet; import java.awt.*; import java.awt.datatransfer.*; import org.thdl.util.*; diff --git a/source/org/thdl/tib/scanner/RemoteScannerFilter.java b/source/org/thdl/tib/scanner/RemoteScannerFilter.java index b0116e9..9a0c105 100644 --- a/source/org/thdl/tib/scanner/RemoteScannerFilter.java +++ b/source/org/thdl/tib/scanner/RemoteScannerFilter.java @@ -36,30 +36,47 @@ public class RemoteScannerFilter extends GenericServlet { private TibetanScanner scanner; private BitDictionarySource ds; + private ScannerLogger sl; - public RemoteScannerFilter() throws Exception + public RemoteScannerFilter() { ResourceBundle rb = ResourceBundle.getBundle("dictionary"); - scanner = new LocalTibetanScanner(rb.getString("onlinescannerfilter.dict-file-name")); - ds = scanner.getDictionarySource(); + sl = new ScannerLogger(); - - String fileName = rb.getString("remotescannerfilter.log-file-name"); - Calendar rightNow = Calendar.getInstance(); - - PrintStream pw = new PrintStream(new FileOutputStream(fileName, true)); - pw.println("Testing: " + rightNow.toString()); - pw.flush(); - pw.close(); + try + { + scanner = new LocalTibetanScanner(rb.getString("onlinescannerfilter.dict-file-name"),false); + } + catch (Exception e) + { + sl.writeLog("Crash\tRemoteScannerFilter"); + sl.writeException(e); + } + ds = scanner.getDictionarySource(); + sl.writeLog("Creation\tRemoteScannerFilter"); } - public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException + public void service(ServletRequest req, ServletResponse res) //throws ServletException, IOException { BufferedReader br; res.setContentType ("text/plain"); + sl.setUserIP(req.getRemoteAddr()); + Token token[] = null; Word word = null; - PrintWriter out = res.getWriter(); + PrintWriter out; + + try + { + out = res.getWriter(); + } + catch (Exception e) + { + sl.writeLog("Crash\tRemoteScannerFilter"); + sl.writeException(e); + return; + } + int i; String linea, dicts = req.getParameter("dicts"), dicDescrip[]; @@ -67,6 +84,7 @@ public class RemoteScannerFilter extends GenericServlet { if (dicts.equals("names")) { + sl.writeLog("Invocation\tRemoteScannerFilter"); dicDescrip = scanner.getDictionaryDescriptions(); if (dicDescrip==null) { @@ -86,11 +104,24 @@ public class RemoteScannerFilter extends GenericServlet ds.setDicts(Integer.parseInt(dicts)); } } - br = new BufferedReader(new InputStreamReader(req.getInputStream())); + + try + { + br = new BufferedReader(new InputStreamReader(req.getInputStream())); + } + catch (Exception e) + { + sl.writeLog("Crash\tRemoteScannerFilter"); + sl.writeException(e); + return; + } + /* FIXME: sometimes getDef returns raises a NullPointerException. In the meantime, I'll just keep it from crashing */ + sl.writeLog("Translation\tRemoteScannerFilter"); + try { while((linea = br.readLine())!= null) @@ -112,29 +143,19 @@ public class RemoteScannerFilter extends GenericServlet } catch (Exception e) { - ResourceBundle rb = ResourceBundle.getBundle("dictionary"); - String fileName = rb.getString("remotescannerfilter.log-file-name"); - Calendar rightNow = Calendar.getInstance(); - - PrintStream pw = new PrintStream(new FileOutputStream(fileName, true)); - pw.println("Translation tool crashed on: " + rightNow.toString()); - e.printStackTrace(pw); - pw.println(); - pw.println("Word that crashed: " + word.getWylie()); - pw.println(); - pw.println("All words:"); - for (i=0; i