Project

General

Profile

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: tao $'
10
 *     '$Date: 2013-04-19 17:47:14 -0700 (Fri, 19 Apr 2013) $'
11
 * '$Revision: 7595 $'
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.common;
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 String collectionName = null;
69
	private static SolrServer solrServer = null;
70

    
71
	public static SolrServer createSolrServer() throws Exception {
72
	    if(solrServer == null) {
73
	        String className = Settings.getConfiguration().getString(SOLR_SERVER_CLASSNAME_PROPERTY_NAME);
74
	        if (className != null && className.equals(EMBEDDEDSERVERCLASS)) {
75
	            generateEmbeddedServer();
76
	        } else if (className != null && className.equals(HTTPSERVERCLASS)) {
77
	            String solrServerUrl = Settings.getConfiguration().getString(SOLR_ENPOINT_PROPERTY_NAME);
78
	            solrServer = new CommonsHttpSolrServer(solrServerUrl);
79
	        } else {
80
	            throw new Exception("SolrServerFactory.createSolrServer - MetacatIndex doesn't support this solr server type: "+className);
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
        collectionName = getCollectionName();
94
        solrServer = new EmbeddedSolrServer(coreContainer, collectionName);
95
	}
96
	
97
	/**
98
	 * Get the the CoreContainer
99
	 * @return it may return null if the solr is configured as the SolrHttpServer
100
	 * @throws Exception 
101
	 */
102
	public static CoreContainer getCoreContainer() throws Exception {
103
	    if(coreContainer == null) {
104
	        createSolrServer();
105
	    }
106
	    return coreContainer;
107
	}
108
	
109
	public static String getCollectionName() {
110
	    if(collectionName == null) {
111
	        Settings.getConfiguration().getString(SOLR_COLLECTION_NAME_PROPERTY_NAME);
112
	    }
113
	    return collectionName;
114
	}
115
}
(2-2/2)