Project

General

Profile

« Previous | Next » 

Revision 7630

Added by Jing Tao over 11 years ago

Move this class to the metacat-common.

View differences:

metacat-index/src/main/java/edu/ucsb/nceas/metacat/index/SolrServerFactory.java
1
/**
2
 *  '$RCSfile$'
3
 *    Purpose: A class that gets Accession Number, check for uniqueness
4
 *             and register it into db
5
 *  Copyright: 2000 Regents of the University of California and the
6
 *             National Center for Ecological Analysis and Synthesis
7
 *    Authors: Jivka Bojilova, Matt Jones
8
 *
9
 *   '$Author$'
10
 *     '$Date$'
11
 * '$Revision$'
12
 *
13
 * This program is free software; you can redistribute it and/or modify
14
 * it under the terms of the GNU General Public License as published by
15
 * the Free Software Foundation; either version 2 of the License, or
16
 * (at your option) any later version.
17
 *
18
 * This program is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
 * GNU General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU General Public License
24
 * along with this program; if not, write to the Free Software
25
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26
 */
27
package edu.ucsb.nceas.metacat.index;
28

  
29
import java.io.File;
30

  
31
import org.apache.commons.logging.Log;
32
import org.apache.commons.logging.LogFactory;
33
import org.apache.solr.client.solrj.SolrServer;
34
import org.apache.solr.client.solrj.embedded.EmbeddedSolrServer;
35
import org.apache.solr.client.solrj.impl.CommonsHttpSolrServer;
36
import org.apache.solr.core.CoreContainer;
37
import org.dataone.configuration.Settings;
38

  
39
/**
40
 * A factory for creating SolrServer implementations based on
41
 * configuration in Settings.java
42
 * Options are as follows.
43
 * 
44
 * Embedded (default):
45
 * solr.server.classname=org.apache.solr.client.solrj.embedded.EmbeddedSolrServer
46
 * solr.homeDir=/path/to/solr/home
47
 * 
48
 * HTTP:
49
 * solr.server.classname=org.apache.solr.client.solrj.impl.CommonsHttpSolrServer
50
 * solr.endpoint=http://endpoint/to/solr/service
51
 * 
52
 * @author leinfelder + tao
53
 *
54
 */
55
public class SolrServerFactory {
56
	
57
    public static final String SOLR_HOME_PROPERTY_NAME = "solr.homeDir";
58
    public static final String SOLR_CONFIG_FILE_NAME_PROPERTY_NAME = "solr.configFileName";
59
    public static final String SOLR_COLLECTION_NAME_PROPERTY_NAME = "solr.collectionName";
60
    public static final String SOLR_SERVER_CLASSNAME_PROPERTY_NAME = "solr.server.classname";
61
    public static final String SOLR_ENPOINT_PROPERTY_NAME = "solr.endpoint";
62
    private static final String EMBEDDEDSERVERCLASS = "org.apache.solr.client.solrj.embedded.EmbeddedSolrServer";
63
    private static final String HTTPSERVERCLASS = "org.apache.solr.client.solrj.impl.CommonsHttpSolrServer";
64

  
65
	public static Log log = LogFactory.getLog(SolrServerFactory.class);
66
	
67
	private static CoreContainer coreContainer = null;
68
	private static SolrServer solrServer = null;
69

  
70
	public static SolrServer createSolrServer() throws Exception {
71
		
72
		String className = Settings.getConfiguration().getString(SOLR_SERVER_CLASSNAME_PROPERTY_NAME);
73
		if (className != null && className.equals(EMBEDDEDSERVERCLASS)) {
74
		    generateEmbeddedServer();
75
		} else if (className != null && className.equals(HTTPSERVERCLASS)) {
76
		    String solrServerUrl = Settings.getConfiguration().getString(SOLR_ENPOINT_PROPERTY_NAME);
77
		    solrServer = new CommonsHttpSolrServer(solrServerUrl);
78
		} else {
79
		    throw new Exception("SolrServerFactory.createSolrServer - MetacatIndex doesn't support this solr server type: "+className);
80
		}
81

  
82
        
83
        return solrServer;
84
	}
85
	
86
	private static void generateEmbeddedServer() throws Exception {
87
	    String solrHomeDir = Settings.getConfiguration().getString(SOLR_HOME_PROPERTY_NAME);
88
        log.info("The configured solr home from properties is " + solrHomeDir);
89
        String configFileName = Settings.getConfiguration().getString(SOLR_CONFIG_FILE_NAME_PROPERTY_NAME);
90
        File configFile = new File(solrHomeDir, configFileName);
91
        coreContainer = new CoreContainer(solrHomeDir, configFile);
92
        coreContainer.load(solrHomeDir, configFile);
93
        String collectioname = Settings.getConfiguration().getString(SOLR_COLLECTION_NAME_PROPERTY_NAME);
94
        solrServer = new EmbeddedSolrServer(coreContainer, collectioname);
95
	}
96
	
97
	public static CoreContainer getCoreContainer() {
98
	    return coreContainer;
99
	}
100
}
101 0

  

Also available in: Unified diff