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;
|
20
|
|
21
|
import java.io.File;
|
22
|
import java.io.IOException;
|
23
|
|
24
|
import javax.xml.parsers.ParserConfigurationException;
|
25
|
|
26
|
import org.apache.commons.logging.Log;
|
27
|
import org.apache.commons.logging.LogFactory;
|
28
|
import org.apache.solr.client.solrj.SolrServer;
|
29
|
import org.apache.solr.client.solrj.embedded.EmbeddedSolrServer;
|
30
|
import org.apache.solr.client.solrj.impl.CommonsHttpSolrServer;
|
31
|
import org.apache.solr.core.CoreContainer;
|
32
|
import org.dataone.configuration.Settings;
|
33
|
import org.dataone.service.exceptions.UnsupportedType;
|
34
|
import org.xml.sax.SAXException;
|
35
|
|
36
|
/**
|
37
|
* A factory for creating SolrServer implementations based on
|
38
|
* configuration in Settings.java
|
39
|
* Options are as follows.
|
40
|
*
|
41
|
* Embedded (default):
|
42
|
* solr.server.classname=org.apache.solr.client.solrj.embedded.EmbeddedSolrServer
|
43
|
* solr.homeDir=/path/to/solr/home
|
44
|
*
|
45
|
* HTTP:
|
46
|
* solr.server.classname=org.apache.solr.client.solrj.impl.CommonsHttpSolrServer
|
47
|
* solr.endpoint=http://endpoint/to/solr/service
|
48
|
*
|
49
|
* @author leinfelder + tao
|
50
|
*
|
51
|
*/
|
52
|
public class SolrServerFactory {
|
53
|
|
54
|
public static final String SOLR_HOME_PROPERTY_NAME = "solr.homeDir";
|
55
|
public static final String SOLR_CONFIG_FILE_NAME_PROPERTY_NAME = "solr.configFileName";
|
56
|
public static final String SOLR_COLLECTION_NAME_PROPERTY_NAME = "solr.collectionName";
|
57
|
public static final String SOLR_SERVER_CLASSNAME_PROPERTY_NAME = "solr.server.classname";
|
58
|
public static final String SOLR_ENPOINT_PROPERTY_NAME = "solr.endpoint";
|
59
|
private static final String EMBEDDEDSERVERCLASS = "org.apache.solr.client.solrj.embedded.EmbeddedSolrServer";
|
60
|
private static final String HTTPSERVERCLASS = "org.apache.solr.client.solrj.impl.CommonsHttpSolrServer";
|
61
|
|
62
|
public static Log log = LogFactory.getLog(SolrServerFactory.class);
|
63
|
|
64
|
private static CoreContainer coreContainer = null;
|
65
|
private static String collectionName = null;
|
66
|
private static SolrServer solrServer = null;
|
67
|
//private static String solrServerBaseURL = null;
|
68
|
|
69
|
public static SolrServer createSolrServer() throws ParserConfigurationException, IOException, SAXException, UnsupportedType {
|
70
|
if(solrServer == null) {
|
71
|
String className = Settings.getConfiguration().getString(SOLR_SERVER_CLASSNAME_PROPERTY_NAME);
|
72
|
if (className != null && className.equals(EMBEDDEDSERVERCLASS)) {
|
73
|
generateEmbeddedServer();
|
74
|
} else if (className != null && className.equals(HTTPSERVERCLASS)) {
|
75
|
String solrServerBaseURL = Settings.getConfiguration().getString(SOLR_ENPOINT_PROPERTY_NAME);
|
76
|
solrServer = new CommonsHttpSolrServer(solrServerBaseURL);
|
77
|
} else {
|
78
|
throw new UnsupportedType("0000","SolrServerFactory.createSolrServer - MetacatIndex doesn't support this solr server type: "+className);
|
79
|
}
|
80
|
}
|
81
|
return solrServer;
|
82
|
}
|
83
|
|
84
|
private static void generateEmbeddedServer() throws ParserConfigurationException, IOException, SAXException {
|
85
|
String solrHomeDir = Settings.getConfiguration().getString(SOLR_HOME_PROPERTY_NAME);
|
86
|
log.info("The configured solr home from properties is " + solrHomeDir);
|
87
|
String configFileName = Settings.getConfiguration().getString(SOLR_CONFIG_FILE_NAME_PROPERTY_NAME);
|
88
|
File configFile = new File(solrHomeDir, configFileName);
|
89
|
coreContainer = new CoreContainer(solrHomeDir, configFile);
|
90
|
coreContainer.load(solrHomeDir, configFile);
|
91
|
collectionName = getCollectionName();
|
92
|
solrServer = new EmbeddedSolrServer(coreContainer, collectionName);
|
93
|
}
|
94
|
|
95
|
/**
|
96
|
* Get the the CoreContainer of the generated EmbeddedSolrServer.
|
97
|
* @return it may return null if the solr is configured as the SolrHttpServer
|
98
|
* @throws SAXException
|
99
|
* @throws IOException
|
100
|
* @throws ParserConfigurationException
|
101
|
* @throws UnsupportedType
|
102
|
* @throws Exception
|
103
|
*/
|
104
|
public static CoreContainer getCoreContainer() throws UnsupportedType, ParserConfigurationException, IOException, SAXException {
|
105
|
if(coreContainer == null) {
|
106
|
createSolrServer();
|
107
|
}
|
108
|
return coreContainer;
|
109
|
}
|
110
|
|
111
|
/**
|
112
|
* Get the CollectionName of the generated EmbeddedSolrServer. It can be null if the solr is configured as a SolrHttpServer
|
113
|
* @return
|
114
|
*/
|
115
|
public static String getCollectionName() {
|
116
|
if(collectionName == null) {
|
117
|
collectionName = Settings.getConfiguration().getString(SOLR_COLLECTION_NAME_PROPERTY_NAME);
|
118
|
}
|
119
|
return collectionName;
|
120
|
}
|
121
|
|
122
|
|
123
|
}
|