📄 commonsquerylogging.java
字号:
/* -*- mode:java; indent-tabs-mode:nil; c-basic-offset:2 -*- * $RCSFile$ $Revision: 1.4 $ $Date: 2006/02/01 00:17:24 $ * Copyright (c) 2004 Autonomy Corp. All Rights Reserved. */import java.io.*;import java.util.*;import java.util.logging.*;import com.ultraseek.xpa.search.*;import com.ultraseek.xpa.server.*;/** * Logging queries. * <p> * This sample requires JDK 1.4 or higher. * <p> * This application demonstrates how to log queries * via the Jakarta Commons Logging component. * <p> * To use this application, edit the file "Sample.properties" * to point to your Ultraseek server. * * @see QueryLog * @see CommonsQueryLog * @since XPA2.2 * @requires JDK 1.4 */public class CommonsQueryLogging { /** * Loads parameters from the file Sample.properties */ static String getString(String key, String def) { try { String resourceName = System.getProperty("Sample"); if (resourceName==null) resourceName = "Sample"; ResourceBundle settings = ResourceBundle.getBundle( resourceName ); return settings.getString(key); } catch (MissingResourceException e) { return def; } catch (Exception e) { System.out.println( "Property file problem: " + e ); return def; } } static final String serverName = getString("UltraseekServer.host", "software-demo.ultraseek.com"); static final int serverPort = Integer.valueOf(getString("UltraseekServer.port", "80")).intValue(); static final String serverProto = getString("UltraseekServer.protocol", "http"); static final String colName = getString("UltraseekCollection.id", "loc"); static UltraseekServer server = new UltraseekServer(serverName, serverPort, serverProto); static UltraseekCollection collection = null; // initialize later static CommonsQueryLog queryLog = new CommonsQueryLog(); public static String LOGFILE = "Query.log"; /** * Show the results of the query, and log it. */ static void demoLogging(String query) throws IOException { Date queryStartTime = new Date(); System.out.println(); System.out.println("Query processing begins at " + queryStartTime); Query q = Query.parse(query); try { SearchResultList srl = collection.search(q); // Show summary of search results. System.out.println(); System.out.println( "" + srl.getResultCount() + " document(s) match your query."); if (!srl.isEmpty()) { int nShowResults = Math.min(10, srl.size()); System.out.println(); System.out.println("Here are the URLs of results 1.." + nShowResults); Iterator it = srl.iterator(); try { for (int i = 0; i < nShowResults && it.hasNext(); i++) { SearchResult searchResult = (SearchResult)it.next(); System.out.println(searchResult.getURL()); } } catch (NoSuchElementException exc) { /* No more results from server, not an error. */ } } queryLog.logQuery(q, srl, collection.getID(), queryStartTime); System.out.println(); System.out.println("Logged the query to " + LOGFILE); } catch (QueryNotSupportedException e) { System.out.println( "Query problem: " + e ); } } static public void main(String args[]) throws IOException { System.out.println("Query Logging Demo"); System.out.println("Using Ultraseek at " + server ); System.out.println(" (version " + server.getVersionString() +")" ); collection = (UltraseekCollection)server.getSearchCollection(colName); if (collection == null) { /* pick the first collection if the one in the property doesn't exist */ Iterator it = server.getSearchCollections().iterator(); if (it.hasNext()) { collection = (UltraseekCollection)it.next(); } else { System.out.println("No collections exist on " + server); return; } } System.out.println("Searching collection \"" + collection.getID() + "\""); /* Initialize logging */ // the CommonsQueryLog class generates the log messages Logger logger = Logger.getLogger("CommonsQueryLog"); // log to file "Query.log" FileHandler handler = new FileHandler(LOGFILE); // use simple text format instead of the verbose XML file format handler.setFormatter(new SimpleFormatter()); // register handler logger.addHandler(handler); // the level query messages are generated at logger.setLevel(Level.FINE); // don't also send these messages to more generic handlers logger.setUseParentHandlers(false); System.out.println("Logging queries to " + LOGFILE); InputStreamReader inputStreamReader = new InputStreamReader(System.in); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); while (true) { System.out.print("\n\nEnter a query (EOF to end): "); String line = bufferedReader.readLine(); if (line==null) break; try { demoLogging( line ); } catch (IOException e) { System.out.println( "Problem communicating with server: " + e ); } catch (XPARuntimeException e) { System.out.println( "Problem communicating with server: " + e.getCause() ); } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -