From 6c10ee2c03f8480dcd571b050b5344ba6d86d864 Mon Sep 17 00:00:00 2001 From: "Chris M. Hostetter" Date: Tue, 2 Mar 2010 00:01:30 +0000 Subject: [PATCH] SOLR-1750: SystemInfoRequestHandler added for simpler programmatic access to info currently available from registry.jsp and stats.jsp git-svn-id: https://svn.apache.org/repos/asf/lucene/solr/trunk@917812 13f79535-47bb-0310-9956-ffa450edef68 --- CHANGES.txt | 4 + .../handler/SystemInfoRequestHandler.java | 126 ++++++++++++++++++ 2 files changed, 130 insertions(+) create mode 100644 src/java/org/apache/solr/handler/SystemInfoRequestHandler.java diff --git a/CHANGES.txt b/CHANGES.txt index fa4c4f268a6..b6c216449d2 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -88,6 +88,10 @@ New Features * SOLR-1268: Incorporate FastVectorHighlighter (koji) +* SOLR-1750: SystemInfoRequestHandler added for simpler programmatic access + to info currently available from registry.jsp and stats.jsp + (ehatcher, hossman) + Optimizations ---------------------- diff --git a/src/java/org/apache/solr/handler/SystemInfoRequestHandler.java b/src/java/org/apache/solr/handler/SystemInfoRequestHandler.java new file mode 100644 index 00000000000..c71d192692a --- /dev/null +++ b/src/java/org/apache/solr/handler/SystemInfoRequestHandler.java @@ -0,0 +1,126 @@ +package org.apache.solr.handler; + +import org.apache.solr.request.SolrQueryRequest; +import org.apache.solr.core.SolrInfoMBean; +import org.apache.solr.core.SolrCore; +import org.apache.solr.common.util.NamedList; +import org.apache.solr.common.util.SimpleOrderedMap; +import org.apache.solr.response.SolrQueryResponse; + +import org.apache.lucene.LucenePackage; + +import java.net.InetAddress; +import java.net.UnknownHostException; +import java.util.Date; +import java.util.Set; +import java.util.Map; +import java.util.HashSet; +import java.util.HashMap; + +/** + * A request handler that provides System Information about the + * current running instance and all registered SolrMBeans. + */ +public class SystemInfoRequestHandler extends RequestHandlerBase { + static InetAddress addr = null; + static String hostname = "unknown"; + static { + try { + addr = InetAddress.getLocalHost(); + hostname = addr.getCanonicalHostName(); + } catch (UnknownHostException e) { + //default to unknown + } + } + + /** + * Take an array of any type and generate a Set containing the toString. + * Set is garunteed to never be null (but may be empty) + */ + private Set arrayToSet(Object[] arr) { + HashSet r = new HashSet(); + if (null == arr) return r; + for (Object o : arr) { + r.add(o.toString()); + } + return r; + } + + + public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp) throws Exception { + SolrCore core = req.getCore(); + rsp.add("core", core.getName()); + rsp.add("schema", req.getSchema().getSchemaName()); + rsp.add("start", new Date(core.getStartTime())); + rsp.add("now", new Date().toString()); + rsp.add("host", hostname); + rsp.add("cwd", System.getProperty("user.dir")); + rsp.add("instanceDir", core.getSolrConfig().getInstanceDir()); + + Package solrP = SolrCore.class.getPackage(); + Package luceneP = LucenePackage.class.getPackage(); + NamedList version = new NamedList(); + version.add("solrSpecVersion", solrP.getSpecificationVersion()); + version.add("solrImplVersion", solrP.getImplementationVersion()); + version.add("luceneSpecVersion", luceneP.getSpecificationVersion()); + version.add("luceneImplVersion", luceneP.getImplementationVersion()); + + rsp.add("version", version); + + NamedList cats = new NamedList(); + rsp.add("objects", cats); + + Set requestedCats = arrayToSet(req.getParams().getParams("cat")); + if (requestedCats.isEmpty()) { + for (SolrInfoMBean.Category cat : SolrInfoMBean.Category.values()) { + requestedCats.add(cat.name()); + } + } + for (String catName : requestedCats) { + cats.add(catName,new SimpleOrderedMap()); + } + + Set requestedKeys = arrayToSet(req.getParams().getParams("key")); + + Map reg = core.getInfoRegistry(); + for (Map.Entry entry : reg.entrySet()) { + String key = entry.getKey(); + SolrInfoMBean m = entry.getValue(); + + if ( ! ( requestedKeys.isEmpty() || requestedKeys.contains(key) ) ) continue; + + NamedList catInfo = (NamedList) cats.get(m.getCategory().name()); + if ( null == catInfo ) continue; + + NamedList mBeanInfo = new SimpleOrderedMap(); + mBeanInfo.add("class", m.getName()); + mBeanInfo.add("version", m.getVersion()); + mBeanInfo.add("description", m.getDescription()); + mBeanInfo.add("srcId", m.getSourceId()); + mBeanInfo.add("src", m.getSource()); + mBeanInfo.add("docs", arrayToSet(m.getDocs())); + + if (req.getParams().getFieldBool(key, "stats", false)) + mBeanInfo.add("stats", m.getStatistics()); + + catInfo.add(key, mBeanInfo); + } + rsp.setHttpCaching(false); // never cache, no matter what init config looks like + } + + public String getDescription() { + return "Get Solr component statistics"; + } + + public String getSourceId() { + return "$Id$"; + } + + public String getSource() { + return "$URL$"; + } + + public String getVersion() { + return "$Revision$"; + } +}