001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.solr.handler;
017:
018: import java.net.URL;
019:
020: import org.apache.solr.core.SolrCore;
021: import org.apache.solr.core.SolrException;
022: import org.apache.solr.core.SolrInfoMBean;
023: import org.apache.solr.request.SolrParams;
024: import org.apache.solr.request.SolrQueryRequest;
025: import org.apache.solr.request.SolrQueryResponse;
026: import org.apache.solr.request.SolrRequestHandler;
027: import org.apache.solr.util.NamedList;
028: import org.apache.solr.util.SolrPluginUtils;
029: import org.apache.solr.util.SimpleOrderedMap;
030:
031: /**
032: *
033: */
034: public abstract class RequestHandlerBase implements SolrRequestHandler,
035: SolrInfoMBean {
036:
037: // statistics
038: // TODO: should we bother synchronizing these, or is an off-by-one error
039: // acceptable every million requests or so?
040: long numRequests;
041: long numErrors;
042: protected SolrParams defaults;
043: protected SolrParams appends;
044: protected SolrParams invariants;
045:
046: /** shorten the class references for utilities */
047: private static class U extends SolrPluginUtils {
048: /* :NOOP */
049: }
050:
051: public void init(NamedList args) {
052: // Copied from StandardRequestHandler
053: if (args != null) {
054: Object o = args.get("defaults");
055: if (o != null && o instanceof NamedList) {
056: defaults = SolrParams.toSolrParams((NamedList) o);
057: }
058: o = args.get("appends");
059: if (o != null && o instanceof NamedList) {
060: appends = SolrParams.toSolrParams((NamedList) o);
061: }
062: o = args.get("invariants");
063: if (o != null && o instanceof NamedList) {
064: invariants = SolrParams.toSolrParams((NamedList) o);
065: }
066: }
067: }
068:
069: public abstract void handleRequestBody(SolrQueryRequest req,
070: SolrQueryResponse rsp) throws Exception;
071:
072: public void handleRequest(SolrQueryRequest req,
073: SolrQueryResponse rsp) {
074: numRequests++;
075:
076: try {
077: U.setDefaults(req, defaults, appends, invariants);
078: handleRequestBody(req, rsp);
079: } catch (Exception e) {
080: SolrException.log(SolrCore.log, e);
081: rsp.setException(e);
082: numErrors++;
083: }
084: }
085:
086: //////////////////////// SolrInfoMBeans methods //////////////////////
087:
088: public String getName() {
089: return this .getClass().getName();
090: }
091:
092: public abstract String getDescription();
093:
094: public abstract String getSourceId();
095:
096: public abstract String getSource();
097:
098: public abstract String getVersion();
099:
100: public Category getCategory() {
101: return Category.QUERYHANDLER;
102: }
103:
104: public URL[] getDocs() {
105: return null; // this can be overridden, but not required
106: }
107:
108: public NamedList getStatistics() {
109: NamedList lst = new SimpleOrderedMap();
110: lst.add("requests", numRequests);
111: lst.add("errors", numErrors);
112: return lst;
113: }
114: }
|