Changeset 769

Show
Ignore:
Timestamp:
03/14/05 17:56:22 (4 years ago)
Author:
torben
Message:

Rewrote the exception handling.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/external-tools/indexer-backends/lucene/org/midgardproject/lucene/HandlerThread.java

    r718 r769  
    3636     
    3737    /** 
     38     * Logging daemon 
     39     */ 
     40    private Logger myLogger; 
     41     
     42    /** 
    3843     * Create a new instance of the handler thread. 
    3944     *  
     
    4651        requestID = new Long(maxRequestID); 
    4752        maxRequestID++; 
     53        myLogger = Logger.getLogger("org.midgardproject.lucene.HandlerThread." + requestID.toString()); 
    4854        } 
    4955     
     
    5460        public void run() 
    5561    { 
    56         Logger myLogger = Logger.getLogger("org.midgardproject.lucene.HandlerThread." + requestID.toString()); 
    5762        try 
    5863        { 
     
    6671        { 
    6772            myLogger.log(Level.WARNING, "Uncaught Exception while processing the request, aborting.", ex); 
    68         } 
    69         finally 
    70         { 
     73            myLogger.warning(ex.toString()); 
    7174            try 
    7275            { 
    73                 socket.close(); 
    74                 myLogger.fine("Finished."); 
     76                PrintExceptionToClient(socket.getOutputStream(), ex); 
    7577            } 
    76             catch (IOException ex
     78            catch (IOException ex2
    7779            { 
    78                 myLogger.log(Level.WARNING, "Failed to close socket after processing.", ex); 
     80                myLogger.warning("Failed to write Exception to the client: " + ex2.toString()); 
    7981            } 
    8082        } 
    81                  
     83        try 
     84        { 
     85                socket.close(); 
     86        } 
     87        catch (IOException ex) 
     88        { 
     89            myLogger.log(Level.WARNING, "Failed to close socket after processing.", ex); 
     90        } 
     91        myLogger.fine("Finished."); 
    8292    } 
     93 
     94     
     95    /** 
     96     * Prints an Exception to the XML Stream to the client, this is used for 
     97     * uncaught exceptions during processing. It assumes an empty output stream, 
     98     * you cannot really determine anything else. 
     99     *  
     100     * @param out OutputStream 
     101     * @param ex Exception thrown. 
     102     */ 
     103    private void PrintExceptionToClient (OutputStream out, Exception exception) 
     104    { 
     105        PrintStream outputStream; 
     106        try 
     107        { 
     108            outputStream = new PrintStream(out, true, "UTF-8"); 
     109        } 
     110        catch (Exception ex) 
     111        { 
     112            myLogger.log(Level.WARNING, "Failed to write an Uncaught Exception Message to the client: " + ex.toString()); 
     113            return; 
     114        } 
     115        outputStream.println("<?xml version='1.0' encoding='UTF-8' ?>"); 
     116        outputStream.println("<!DOCTYPE request SYSTEM 'xml-communication-response.dtd'>"); 
     117        outputStream.println("<response>"); 
     118        outputStream.print("<error id='UNCAUGHT_EXCEPTION'><![CDATA["); 
     119        outputStream.print(exception.toString().replaceAll("]]>", "]]&gt;")); 
     120        exception.printStackTrace(outputStream); 
     121        outputStream.println("]]></error>"); 
     122        outputStream.println("</response>"); 
     123    } 
     124     
     125    /** 
     126     * Encodes an XML Textfor use in CDATA sections. The result is not surrounded by 
     127     * CDATA Tags.  
     128     *  
     129     * @param arg The argument string to encode. 
     130     * @return The encoded argument. 
     131     */ 
     132    public String EncodeCData(String arg) 
     133    { 
     134        return arg.replaceAll("]]>", "]]&gt;"); 
     135    } 
     136 
    83137}