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
import java.io.IOException;
31

    
32
import javax.xml.parsers.ParserConfigurationException;
33

    
34
import org.apache.commons.logging.Log;
35
import org.apache.commons.logging.LogFactory;
36
import org.apache.solr.client.solrj.SolrServer;
37
import org.apache.solr.client.solrj.embedded.EmbeddedSolrServer;
38
import org.apache.solr.client.solrj.impl.CommonsHttpSolrServer;
39
import org.apache.solr.core.CoreContainer;
40
import org.dataone.configuration.Settings;
41
import org.dataone.service.exceptions.UnsupportedType;
42
import org.xml.sax.SAXException;
43

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

    
70
	public static Log log = LogFactory.getLog(SolrServerFactory.class);
71
	
72
	private static CoreContainer coreContainer = null;
73
	private static String collectionName = null;
74
	private static SolrServer solrServer = null;
75
	//private static String solrServerBaseURL = null;
76

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