1
|
/**
|
2
|
* Copyright: 2013 Regents of the University of California and the
|
3
|
* National Center for Ecological Analysis and Synthesis
|
4
|
*
|
5
|
* This program is free software; you can redistribute it and/or modify
|
6
|
* it under the terms of the GNU General Public License as published by
|
7
|
* the Free Software Foundation; either version 2 of the License, or
|
8
|
* (at your option) any later version.
|
9
|
*
|
10
|
* This program is distributed in the hope that it will be useful,
|
11
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13
|
* GNU General Public License for more details.
|
14
|
*
|
15
|
* You should have received a copy of the GNU General Public License
|
16
|
* along with this program; if not, write to the Free Software
|
17
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
18
|
*/
|
19
|
package edu.ucsb.nceas.metacat.common.query;
|
20
|
|
21
|
import java.io.IOException;
|
22
|
import java.io.InputStream;
|
23
|
import java.net.MalformedURLException;
|
24
|
import java.util.List;
|
25
|
import java.util.Map;
|
26
|
import java.util.Set;
|
27
|
|
28
|
import javax.xml.parsers.ParserConfigurationException;
|
29
|
|
30
|
import org.apache.solr.client.solrj.SolrServer;
|
31
|
import org.apache.solr.client.solrj.SolrServerException;
|
32
|
import org.apache.solr.client.solrj.embedded.EmbeddedSolrServer;
|
33
|
import org.apache.solr.client.solrj.impl.CommonsHttpSolrServer;
|
34
|
import org.apache.solr.client.solrj.response.QueryResponse;
|
35
|
import org.apache.solr.common.params.SolrParams;
|
36
|
import org.apache.solr.core.CoreContainer;
|
37
|
import org.apache.solr.schema.IndexSchema;
|
38
|
import org.apache.solr.schema.SchemaField;
|
39
|
import org.dataone.service.exceptions.NotFound;
|
40
|
import org.dataone.service.exceptions.NotImplemented;
|
41
|
import org.dataone.service.exceptions.UnsupportedType;
|
42
|
import org.dataone.service.types.v1.Identifier;
|
43
|
import org.dataone.service.types.v1.Subject;
|
44
|
import org.xml.sax.SAXException;
|
45
|
|
46
|
import edu.ucsb.nceas.metacat.common.SolrServerFactory;
|
47
|
|
48
|
/**
|
49
|
* A class to handle the solr query. It will call the SolrServerFactory to generate a SolrServer and
|
50
|
* determine the type - the embedded or http solr server.
|
51
|
* @author tao
|
52
|
*
|
53
|
*/
|
54
|
public class SolrQueryServiceController {
|
55
|
private static SolrQueryServiceController controller = null;
|
56
|
private boolean isEmbeddedSolrServer = false;
|
57
|
private EmbeddedSolrQueryService embeddedQueryService = null;
|
58
|
private HttpSolrQueryService httpQueryService = null;
|
59
|
|
60
|
/*
|
61
|
* Private consctructor
|
62
|
*/
|
63
|
private SolrQueryServiceController() throws UnsupportedType, ParserConfigurationException, IOException, SAXException, NotFound {
|
64
|
SolrServer solrServer = SolrServerFactory.createSolrServer();
|
65
|
if(solrServer instanceof EmbeddedSolrServer) {
|
66
|
isEmbeddedSolrServer = true;
|
67
|
EmbeddedSolrServer embeddedServer = (EmbeddedSolrServer) solrServer;
|
68
|
CoreContainer coreContainer = SolrServerFactory.getCoreContainer();
|
69
|
String collectionName = SolrServerFactory.getCollectionName();
|
70
|
embeddedQueryService = new EmbeddedSolrQueryService(embeddedServer, coreContainer, collectionName);
|
71
|
} else {
|
72
|
isEmbeddedSolrServer = false;
|
73
|
CommonsHttpSolrServer httpServer = (CommonsHttpSolrServer)solrServer;
|
74
|
httpQueryService = new HttpSolrQueryService(httpServer);
|
75
|
}
|
76
|
}
|
77
|
|
78
|
/**
|
79
|
* Get the controller
|
80
|
* @return
|
81
|
* @throws UnsupportedType
|
82
|
* @throws NotFound
|
83
|
* @throws ParserConfigurationException
|
84
|
* @throws IOException
|
85
|
* @throws SAXException
|
86
|
*/
|
87
|
public static SolrQueryServiceController getInstance() throws UnsupportedType, NotFound, ParserConfigurationException, IOException, SAXException {
|
88
|
if(controller == null) {
|
89
|
controller = new SolrQueryServiceController();
|
90
|
}
|
91
|
return controller;
|
92
|
}
|
93
|
|
94
|
|
95
|
|
96
|
/**
|
97
|
* Query the Solr server with specified query string or SolrParams and the user's identity.
|
98
|
* @param query query string. It is for the HttpSolrQueryService.
|
99
|
* @param params the SolrParam. It is for the EmbeddedSolrQueryService.
|
100
|
* @param subjects
|
101
|
* @return
|
102
|
* @throws NotImplemented
|
103
|
* @throws NotFound
|
104
|
* @throws IOException
|
105
|
* @throws SolrServerException
|
106
|
* @throws SAXException
|
107
|
* @throws ParserConfigurationException
|
108
|
* @throws UnsupportedType
|
109
|
*/
|
110
|
public InputStream query(SolrParams params,Set<Subject>subjects) throws NotImplemented, NotFound, IOException, UnsupportedType, ParserConfigurationException, SAXException, SolrServerException {
|
111
|
if(isEmbeddedSolrServer) {
|
112
|
return embeddedQueryService.query(params, subjects);
|
113
|
} else {
|
114
|
return httpQueryService.query(params, subjects);
|
115
|
}
|
116
|
|
117
|
}
|
118
|
|
119
|
/**
|
120
|
* Get the spec version of the solr server
|
121
|
* @return
|
122
|
*/
|
123
|
public String getSolrSpecVersion() {
|
124
|
if(isEmbeddedSolrServer) {
|
125
|
return embeddedQueryService.getSolrServerVersion();
|
126
|
} else {
|
127
|
return httpQueryService.getSolrServerVersion();
|
128
|
}
|
129
|
}
|
130
|
|
131
|
/**
|
132
|
* Get the fields list of the index schema
|
133
|
* @return
|
134
|
* @throws SAXException
|
135
|
* @throws IOException
|
136
|
* @throws ParserConfigurationException
|
137
|
* @throws MalformedURLException
|
138
|
* @throws Exception
|
139
|
*/
|
140
|
public Map<String, SchemaField> getIndexSchemaFields() throws MalformedURLException, ParserConfigurationException, IOException, SAXException {
|
141
|
if(isEmbeddedSolrServer) {
|
142
|
return embeddedQueryService.getIndexSchemaFields();
|
143
|
} else {
|
144
|
return httpQueryService.getIndexSchemaFields();
|
145
|
}
|
146
|
}
|
147
|
|
148
|
/**
|
149
|
* Access the SOLR index schema
|
150
|
* @return
|
151
|
* @throws SAXException
|
152
|
* @throws IOException
|
153
|
* @throws ParserConfigurationException
|
154
|
* @throws MalformedURLException
|
155
|
*/
|
156
|
public IndexSchema getSchema() throws MalformedURLException, ParserConfigurationException, IOException, SAXException {
|
157
|
if(isEmbeddedSolrServer) {
|
158
|
return embeddedQueryService.getSchema();
|
159
|
} else{
|
160
|
return httpQueryService.getSchema();
|
161
|
}
|
162
|
|
163
|
}
|
164
|
|
165
|
/**
|
166
|
* If the solr client was configured as an embedded solr server.
|
167
|
* @return true if it is; false otherwise.
|
168
|
*/
|
169
|
public boolean isEmbeddedSolrServer() {
|
170
|
return isEmbeddedSolrServer;
|
171
|
}
|
172
|
|
173
|
|
174
|
/**
|
175
|
* If the solr server has at least one solr doc for the given id.
|
176
|
* @param id
|
177
|
* @return true if the server has at lease one solr doc; false otherwise.
|
178
|
*/
|
179
|
public boolean hasSolrDoc(Identifier id) throws ParserConfigurationException, SolrServerException, IOException, SAXException{
|
180
|
if(isEmbeddedSolrServer) {
|
181
|
return embeddedQueryService.hasSolrDoc(id);
|
182
|
} else{
|
183
|
return httpQueryService.hasSolrDoc(id);
|
184
|
}
|
185
|
}
|
186
|
|
187
|
|
188
|
}
|