1 /**
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package org.apache.solr.handler;
19
20 import java.net.URL;
21
22 import org.apache.solr.core.SolrCore;
23 import org.apache.solr.core.SolrException;
24 import org.apache.solr.core.SolrInfoMBean;
25 import org.apache.solr.request.SolrParams;
26 import org.apache.solr.request.SolrQueryRequest;
27 import org.apache.solr.request.SolrQueryResponse;
28 import org.apache.solr.request.SolrRequestHandler;
29 import org.apache.solr.util.NamedList;
30 import org.apache.solr.util.SolrPluginUtils;
31 import org.apache.solr.util.SimpleOrderedMap;
32
33 /**
34 *
35 */
36 public abstract class RequestHandlerBase implements SolrRequestHandler, SolrInfoMBean {
37
38 // statistics
39 // TODO: should we bother synchronizing these, or is an off-by-one error
40 // acceptable every million requests or so?
41 long numRequests;
42 long numErrors;
43 protected SolrParams defaults;
44 protected SolrParams appends;
45 protected SolrParams invariants;
46
47 /** shorten the class references for utilities */
48 private static class U extends SolrPluginUtils {
49 /* :NOOP */
50 }
51
52 public void init(NamedList args) {
53 // Copied from StandardRequestHandler
54 if( args != null ) {
55 Object o = args.get("defaults");
56 if (o != null && o instanceof NamedList) {
57 defaults = SolrParams.toSolrParams((NamedList)o);
58 }
59 o = args.get("appends");
60 if (o != null && o instanceof NamedList) {
61 appends = SolrParams.toSolrParams((NamedList)o);
62 }
63 o = args.get("invariants");
64 if (o != null && o instanceof NamedList) {
65 invariants = SolrParams.toSolrParams((NamedList)o);
66 }
67 }
68 }
69
70 public abstract void handleRequestBody( SolrQueryRequest req, SolrQueryResponse rsp ) throws Exception;
71
72 public void handleRequest(SolrQueryRequest req, SolrQueryResponse rsp) {
73 numRequests++;
74
75 try {
76 U.setDefaults(req,defaults,appends,invariants);
77 handleRequestBody( req, rsp );
78 } catch (Exception e) {
79 SolrException.log(SolrCore.log,e);
80 rsp.setException(e);
81 numErrors++;
82 }
83 }
84
85
86 //////////////////////// SolrInfoMBeans methods //////////////////////
87
88 public String getName() {
89 return this.getClass().getName();
90 }
91
92 public abstract String getDescription();
93 public abstract String getSourceId();
94 public abstract String getSource();
95 public abstract String getVersion();
96
97 public Category getCategory() {
98 return Category.QUERYHANDLER;
99 }
100
101 public URL[] getDocs() {
102 return null; // this can be overridden, but not required
103 }
104
105 public NamedList getStatistics() {
106 NamedList lst = new SimpleOrderedMap();
107 lst.add("requests", numRequests);
108 lst.add("errors", numErrors);
109 return lst;
110 }
111 }
112