Changeset 718
- Timestamp:
- 03/08/05 17:32:03 (4 years ago)
- Files:
-
- trunk/external-tools/indexer-backends/lucene/org/midgardproject/XmlComm.java (modified) (5 diffs)
- trunk/external-tools/indexer-backends/lucene/org/midgardproject/lucene/HandlerThread.java (modified) (6 diffs)
- trunk/external-tools/indexer-backends/lucene/org/midgardproject/lucene/Main.java (modified) (2 diffs)
- trunk/external-tools/indexer-backends/lucene/org/midgardproject/lucene/QueryRequest.java (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/external-tools/indexer-backends/lucene/org/midgardproject/XmlComm.java
r716 r718 71 71 factory.setIgnoringComments(true); 72 72 factory.setIgnoringElementContentWhitespace(true); 73 factory.setCoalescing(true); 73 74 documentBuilder = factory.newDocumentBuilder(); 74 75 … … 79 80 /** 80 81 * 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. 82 89 */ 83 90 public void ParseRequest() 84 91 { 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 89 92 // Parse the input 90 93 try 91 94 { 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()))); 93 114 } 94 115 catch (IOException ex) 95 116 { 117 PrintXMLHeader(); 96 118 PrintError("IOException", ex.toString()); 97 119 return; … … 99 121 catch (SAXException ex) 100 122 { 123 PrintXMLHeader(); 101 124 PrintError("SAXException", ex.toString()); 102 125 return; 103 126 } 127 PrintXMLHeader(); 104 128 105 129 Element element = document.getDocumentElement(); … … 158 182 { 159 183 PrintError("UncaughtException", ex.toString()); 184 return; 160 185 } 161 186 162 187 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>"); 163 198 } 164 199 … … 303 338 { 304 339 String result = node.getNodeValue(); 305 306 340 if (result == null) 307 341 { trunk/external-tools/indexer-backends/lucene/org/midgardproject/lucene/HandlerThread.java
r716 r718 9 9 import java.io.*; 10 10 import java.net.*; 11 import java.util.logging.*; 11 12 import org.midgardproject.*; 12 13 … … 25 26 26 27 /** 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 /** 27 38 * Create a new instance of the handler thread. 28 39 * … … 33 44 super(); 34 45 this.socket = socket; 46 requestID = new Long(maxRequestID); 47 maxRequestID++; 35 48 } 36 49 37 50 /** 38 51 * Runner method, takes the IO streams from the sockets and processes the … … 41 54 public void run() 42 55 { 56 Logger myLogger = Logger.getLogger("org.midgardproject.lucene.HandlerThread." + requestID.toString()); 43 57 try 44 58 { … … 46 60 OutputStream out = socket.getOutputStream(); 47 61 XmlComm comm = new XmlComm(socket.getInputStream(), socket.getOutputStream()); 62 myLogger.fine("Processing the request."); 48 63 comm.ParseRequest(); 49 64 } 50 65 catch (Exception ex) 51 66 { 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); 58 68 } 59 69 finally … … 62 72 { 63 73 socket.close(); 74 myLogger.fine("Finished."); 64 75 } 65 76 catch (IOException ex) 66 77 { 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); 70 79 } 71 80 } trunk/external-tools/indexer-backends/lucene/org/midgardproject/lucene/Main.java
r716 r718 7 7 package org.midgardproject.lucene; 8 8 9 import org.midgardproject.*; 9 import java.io.*; 10 import java.net.*; 11 import java.util.logging.*; 12 import java.util.Properties; 10 13 11 14 /** … … 15 18 * 16 19 * 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. 17 30 */ 18 31 public class Main 19 32 { 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 */ 21 61 public static void main(String[] args) 22 62 { 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; 23 100 try 24 101 { 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)); 31 103 } 32 catch ( Exception ex)104 catch (IllegalArgumentException ex) 33 105 { 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); 35 177 } 36 178 } trunk/external-tools/indexer-backends/lucene/org/midgardproject/lucene/QueryRequest.java
r715 r718 131 131 hits = searcher.search(query, filter, Sort.RELEVANCE); 132 132 } 133 processor.ReleaseIndexReader(this);133 PrintResult(hits, comm); 134 134 } 135 135 catch (IOException ex) 136 136 { 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 141 151 } 142 152 … … 179 189 catch (IOException ex) 180 190 { 181 throw new LuceneErrorException("Failed to retrieve a document from the resultset, i dwas "182 + i d+ " 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); 183 193 } 184 194
