Changeset 718

Show
Ignore:
Timestamp:
03/08/05 17:32:03 (4 years ago)
Author:
torben
Message:

Completed the work on the Daemon stuff, works good now, and seems fairly
IPC safe on a first glance.

Files:

Legend:

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

    r716 r718  
    7171        factory.setIgnoringComments(true); 
    7272        factory.setIgnoringElementContentWhitespace(true); 
     73        factory.setCoalescing(true); 
    7374        documentBuilder = factory.newDocumentBuilder(); 
    7475         
     
    7980    /** 
    8081     * Parses and executes the request, the output is written to the configured 
    81      * output stream. 
     82     * output stream. The class will read from the socket until a line that only 
     83     * consists of </request> is encountered. In case you have this string in  
     84     * your content, you can mask it using </request_>. Be aware, that this 
     85     * must be the one and only thing standing in the last line of the request. 
     86     *  
     87     * This "workaround" is there since at least PHP cannot close only the outgoing 
     88     * half of the TCP socket (so EOF checks done in the DocumentBuilder won't help. 
    8289     */ 
    8390    public void ParseRequest() 
    8491    { 
    85         outputStream.println("<?xml version='1.0' encoding='UTF-8' ?>"); 
    86         outputStream.println("<!DOCTYPE request SYSTEM 'xml-communication-response.dtd'>"); 
    87         outputStream.println("<response>"); 
    88          
    8992        // Parse the input 
    9093        try 
    9194        { 
    92                 document = documentBuilder.parse(inputStream); 
     95            // Read until </request> is there. 
     96            StringBuffer buffer = new StringBuffer(); 
     97            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8")); 
     98            String line; 
     99            do 
     100            { 
     101                line = reader.readLine(); 
     102                if (line.indexOf("</request_>") == 0) 
     103                { 
     104                    buffer.append("</request>\n"); 
     105                } 
     106                else 
     107                { 
     108                    buffer.append(line); 
     109                    buffer.append("\n"); 
     110                } 
     111            } while (line.indexOf("</request>") == -1); 
     112             
     113                document = documentBuilder.parse(new InputSource(new StringReader(buffer.toString()))); 
    93114        } 
    94115        catch (IOException ex) 
    95116        { 
     117            PrintXMLHeader(); 
    96118            PrintError("IOException", ex.toString()); 
    97119            return; 
     
    99121        catch (SAXException ex) 
    100122        { 
     123            PrintXMLHeader(); 
    101124            PrintError("SAXException", ex.toString()); 
    102125            return; 
    103126        } 
     127        PrintXMLHeader(); 
    104128         
    105129        Element element = document.getDocumentElement(); 
     
    158182        { 
    159183            PrintError("UncaughtException", ex.toString()); 
     184            return; 
    160185        } 
    161186         
    162187        outputStream.println("</response>"); 
     188    } 
     189     
     190    /** 
     191     * Renders the XML header, including the opening response tag. 
     192     */ 
     193    protected void PrintXMLHeader() 
     194    { 
     195        outputStream.println("<?xml version='1.0' encoding='UTF-8' ?>"); 
     196        outputStream.println("<!DOCTYPE request SYSTEM 'xml-communication-response.dtd'>"); 
     197        outputStream.println("<response>"); 
    163198    } 
    164199     
     
    303338    { 
    304339        String result = node.getNodeValue(); 
    305          
    306340        if (result == null) 
    307341        { 
  • trunk/external-tools/indexer-backends/lucene/org/midgardproject/lucene/HandlerThread.java

    r716 r718  
    99import java.io.*; 
    1010import java.net.*; 
     11import java.util.logging.*; 
    1112import org.midgardproject.*; 
    1213 
     
    2526 
    2627    /** 
     28     * The ID of the request 
     29     */ 
     30    private Long requestID; 
     31     
     32    /** 
     33     * The highest request ID. 
     34     */ 
     35    private static long maxRequestID = 0; 
     36     
     37    /** 
    2738     * Create a new instance of the handler thread. 
    2839     *  
     
    3344                super(); 
    3445        this.socket = socket; 
     46        requestID = new Long(maxRequestID); 
     47        maxRequestID++; 
    3548        } 
    36          
     49     
    3750    /** 
    3851     * Runner method, takes the IO streams from the sockets and processes the  
     
    4154        public void run() 
    4255    { 
     56        Logger myLogger = Logger.getLogger("org.midgardproject.lucene.HandlerThread." + requestID.toString()); 
    4357        try 
    4458        { 
     
    4660                OutputStream out = socket.getOutputStream(); 
    4761                XmlComm comm = new XmlComm(socket.getInputStream(), socket.getOutputStream()); 
     62            myLogger.fine("Processing the request."); 
    4863            comm.ParseRequest(); 
    4964        } 
    5065        catch (Exception ex) 
    5166        { 
    52             System.err.println("Uncaught Exception while answerting to a client:"); 
    53             System.err.println("Remote Address:" + socket.getInetAddress().toString()); 
    54             System.err.print("Remote Port:"); 
    55             System.err.println(socket.getPort()); 
    56             System.err.println(ex.getClass()); 
    57             ex.printStackTrace(System.err); 
     67            myLogger.log(Level.WARNING, "Uncaught Exception while processing the request, aborting.", ex); 
    5868        } 
    5969        finally 
     
    6272            { 
    6373                socket.close(); 
     74                myLogger.fine("Finished."); 
    6475            } 
    6576            catch (IOException ex) 
    6677            { 
    67                 System.err.println("Failed to close socket after processing."); 
    68                 System.err.println(ex.getClass()); 
    69                 ex.printStackTrace(System.err); 
     78                myLogger.log(Level.WARNING, "Failed to close socket after processing.", ex); 
    7079            } 
    7180        } 
  • trunk/external-tools/indexer-backends/lucene/org/midgardproject/lucene/Main.java

    r716 r718  
    77package org.midgardproject.lucene; 
    88 
    9 import org.midgardproject.*; 
     9import java.io.*; 
     10import java.net.*; 
     11import java.util.logging.*; 
     12import java.util.Properties; 
    1013 
    1114/** 
     
    1518 *  
    1619 * Launches a thread for each incoming connection. 
     20 *  
     21 * Configuration options, definable in the config file with their 
     22 * defaults), all options are case sensitive: 
     23 *  
     24 * - logfile ("", which will log to stderr) 
     25 * - loglevel (WARNING, one of the log level constants defined in java.util.logging.Level) 
     26 * - bind (127.0.0.1) 
     27 * - port (2222) 
     28 *  
     29 * Log files are limited to 5 MB in size and are automatically rotated 4 times. 
    1730 */ 
    1831public class Main  
    1932{ 
    20  
     33     
     34    /** 
     35     * Default configuration: Log File 
     36     */ 
     37    private static String defaultLogFile = ""; 
     38     
     39    /** 
     40     * Default configuration: Log Level 
     41     */ 
     42    private static String defaultLogLevel = "WARNING"; 
     43     
     44    /** 
     45     * Default configuration: Default Bind Address 
     46     */ 
     47    private static String defaultBindAddress = "127.0.0.1"; 
     48     
     49    /** 
     50     * Default configuration: Default Bind Port 
     51     */ 
     52    private static String defaultPort = "2222"; 
     53     
     54    /** 
     55     * Application entry point. 
     56     *  
     57     * Configuration and Server Socket loop. 
     58     *  
     59     * @param args 
     60     */ 
    2161        public static void main(String[] args)  
    2262        { 
     63        // Load configuration 
     64        Properties configuration = new Properties(); 
     65        if (args.length == 1) 
     66        { 
     67            try 
     68            { 
     69                InputStream configFileStream = new FileInputStream(args[0]); 
     70                configuration.load(configFileStream); 
     71            } 
     72            catch (IOException ex) 
     73            { 
     74                System.err.println("Failed to read the config file from " + args[0] + ": " + ex.getMessage()); 
     75                System.exit(1); 
     76            } 
     77        } 
     78         
     79        // Start up logging 
     80        String logFileName = configuration.getProperty("logfile", defaultLogFile); 
     81        Handler logHandler = null; 
     82        if (logFileName == "") 
     83        { 
     84            logHandler = new ConsoleHandler(); 
     85        } 
     86        else 
     87        { 
     88            try 
     89            { 
     90                logHandler = new FileHandler(logFileName, 5000000, 4, true); 
     91            } 
     92            catch (IOException ex) 
     93            { 
     94                System.err.println("Failed to open the log file " + logFileName + "for writing: " + ex.getMessage()); 
     95                System.exit(1); 
     96            } 
     97        } 
     98         
     99        Level logLevel = null; 
    23100        try 
    24101        { 
    25             // Create a server socket 
    26             // loop infinitly 
    27             // launch worker threads 
    28              
    29             // open: shutdown function 
    30             // open: daemon threads vs. worker threads? 
     102            logLevel = Level.parse(configuration.getProperty("loglevel", defaultLogLevel)); 
    31103        } 
    32         catch (Exception ex) 
     104        catch (IllegalArgumentException ex) 
    33105        { 
    34             System.err.println(ex.toString()); 
     106            System.err.println("Failed to parse the log level: " + ex.getMessage()); 
     107            System.exit(1); 
     108        } 
     109         
     110        Logger rootLogger = Logger.getLogger(""); 
     111        // Drop all handlers first. 
     112        Handler handlers[] = rootLogger.getHandlers(); 
     113        for (int i = 0; i < handlers.length; i++) 
     114        { 
     115            rootLogger.removeHandler(handlers[i]); 
     116        } 
     117         
     118        rootLogger.addHandler(logHandler); 
     119        rootLogger.setLevel(logLevel); 
     120         
     121        Logger myLogger = Logger.getLogger("org.midgardproject.lucene.Main"); 
     122        myLogger.config("Successfully configured the logging system, system startup commencing."); 
     123         
     124        InetAddress bindTo = null; 
     125        try 
     126        { 
     127            bindTo = InetAddress.getByName(configuration.getProperty("bind", defaultBindAddress)); 
     128        } 
     129        catch (UnknownHostException ex) 
     130        { 
     131            myLogger.log(Level.SEVERE, "Failed to parse the Bind Address", ex); 
     132            myLogger.severe("Exitting..."); 
     133            System.exit(1); 
     134        } 
     135        int bindPort = 0; 
     136        try 
     137        { 
     138            String bindPortString = configuration.getProperty("port", defaultPort); 
     139            bindPort = Integer.parseInt(bindPortString); 
     140        } 
     141        catch (NumberFormatException ex) 
     142        { 
     143            myLogger.log(Level.SEVERE, "Failed to parse the Bind Port", ex); 
     144            myLogger.severe("Exitting..."); 
     145            System.exit(1); 
     146        } 
     147        myLogger.log(Level.CONFIG, "Binding a server socket to {0}:{1}.", new Object[] {bindTo, new Integer(bindPort)}); 
     148         
     149        ServerSocket server = null; 
     150        try 
     151        { 
     152            server = new ServerSocket(bindPort, 50, bindTo); 
     153        } 
     154        catch (IOException ex) 
     155        { 
     156            myLogger.log(Level.SEVERE, "Failed to bind the server socket", ex); 
     157            myLogger.severe("Exitting..."); 
     158            System.exit(1); 
     159        } 
     160         
     161         
     162        // open: shutdown function 
     163        // open: daemon threads vs. worker threads? 
     164        try 
     165        { 
     166            while (true) 
     167            { 
     168                Socket socket = server.accept(); 
     169                myLogger.log(Level.INFO, "Incoming connection from IP {0}", socket.getInetAddress()); 
     170                HandlerThread handler = new HandlerThread(socket); 
     171                handler.start(); 
     172            } 
     173        } 
     174        catch (IOException ex) 
     175        { 
     176            myLogger.log(Level.SEVERE, "Failed to accept an incoming connetion.", ex); 
    35177        } 
    36178        } 
  • trunk/external-tools/indexer-backends/lucene/org/midgardproject/lucene/QueryRequest.java

    r715 r718  
    131131                hits = searcher.search(query, filter, Sort.RELEVANCE); 
    132132            } 
    133             processor.ReleaseIndexReader(this); 
     133            PrintResult(hits, comm); 
    134134        } 
    135135        catch (IOException ex) 
    136136        { 
    137             throw new LuceneErrorException("IO Exception durin execution of the query: " + ex.getMessage(), ex); 
    138         } 
    139          
    140         PrintResult(hits, comm); 
     137            throw new LuceneErrorException("IO Exception during execution of the query: " + ex.getMessage(), ex); 
     138        } 
     139        finally 
     140        { 
     141            try 
     142            { 
     143                processor.ReleaseIndexReader(this); 
     144            } 
     145            catch (IOException ex) 
     146            { 
     147                throw new LuceneErrorException("IO Exception while releasing the IndexeReader: " + ex.getMessage(), ex); 
     148            } 
     149        } 
     150         
    141151    } 
    142152     
     
    179189            catch (IOException ex) 
    180190            { 
    181                 throw new LuceneErrorException("Failed to retrieve a document from the resultset, id was "  
    182                     + id + " IO Error was: " + ex.getMessage(), ex); 
     191                throw new LuceneErrorException("Failed to retrieve a document from the resultset, i was "  
     192                    + i + " IO Error was: " + ex.getMessage(), ex); 
    183193            } 
    184194