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
	//private static String solrServerBaseURL = null;
71

    
72
	public static SolrServer createSolrServer() throws Exception {
73
	    if(solrServer == null) {
74
	        String className = Settings.getConfiguration().getString(SOLR_SERVER_CLASSNAME_PROPERTY_NAME);
75
	        if (className != null && className.equals(EMBEDDEDSERVERCLASS)) {
76
	            generateEmbeddedServer();
77
	        } else if (className != null && className.equals(HTTPSERVERCLASS)) {
78
	            String solrServerBaseURL = Settings.getConfiguration().getString(SOLR_ENPOINT_PROPERTY_NAME);
79
	            solrServer = new CommonsHttpSolrServer(solrServerBaseURL);
80
	        } else {
81
	            throw new Exception("SolrServerFactory.createSolrServer - MetacatIndex doesn't support this solr server type: "+className);
82
	        }
83
	    }
84
        return solrServer;
85
	}
86
	
87
	private static void generateEmbeddedServer() throws Exception {
88
	    String solrHomeDir = Settings.getConfiguration().getString(SOLR_HOME_PROPERTY_NAME);
89
        log.info("The configured solr home from properties is " + solrHomeDir);
90
        String configFileName = Settings.getConfiguration().getString(SOLR_CONFIG_FILE_NAME_PROPERTY_NAME);
91
        File configFile = new File(solrHomeDir, configFileName);
92
        coreContainer = new CoreContainer(solrHomeDir, configFile);
93
        coreContainer.load(solrHomeDir, configFile);
94
        collectionName = getCollectionName();
95
        solrServer = new EmbeddedSolrServer(coreContainer, collectionName);
96
	}
97
	
98
	/**
99
	 * Get the the CoreContainer of the generated EmbeddedSolrServer.
100
	 * @return it may return null if the solr is configured as the SolrHttpServer
101
	 * @throws Exception 
102
	 */
103
	public static CoreContainer getCoreContainer() throws Exception {
104
	    if(coreContainer == null) {
105
	        createSolrServer();
106
	    }
107
	    return coreContainer;
108
	}
109
	
110
	/**
111
	 * Get the CollectionName of the generated EmbeddedSolrServer. It can be null if the solr is configured as a SolrHttpServer
112
	 * @return
113
	 */
114
	public static String getCollectionName() {
115
	    if(collectionName == null) {
116
	        collectionName = Settings.getConfiguration().getString(SOLR_COLLECTION_NAME_PROPERTY_NAME);
117
	    }
118
	    return collectionName;
119
	}
120
	
121
	
122
}
(3-3/3)