Project

General

Profile

1 7628 tao
/**
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 7712 tao
import java.io.IOException;
31 7628 tao
32 7712 tao
import javax.xml.parsers.ParserConfigurationException;
33
34 7628 tao
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 7712 tao
import org.dataone.service.exceptions.UnsupportedType;
42
import org.xml.sax.SAXException;
43 7628 tao
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 7657 tao
	private static String collectionName = null;
74 7628 tao
	private static SolrServer solrServer = null;
75 7663 tao
	//private static String solrServerBaseURL = null;
76 7628 tao
77 7712 tao
	public static SolrServer createSolrServer() throws ParserConfigurationException, IOException, SAXException, UnsupportedType   {
78 7657 tao
	    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 7663 tao
	            String solrServerBaseURL = Settings.getConfiguration().getString(SOLR_ENPOINT_PROPERTY_NAME);
84
	            solrServer = new CommonsHttpSolrServer(solrServerBaseURL);
85 7657 tao
	        } else {
86 7712 tao
	            throw new UnsupportedType("0000","SolrServerFactory.createSolrServer - MetacatIndex doesn't support this solr server type: "+className);
87 7657 tao
	        }
88
	    }
89 7628 tao
        return solrServer;
90
	}
91
92 7712 tao
	private static void generateEmbeddedServer() throws ParserConfigurationException, IOException, SAXException  {
93 7628 tao
	    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 7657 tao
        collectionName = getCollectionName();
100
        solrServer = new EmbeddedSolrServer(coreContainer, collectionName);
101 7628 tao
	}
102
103 7657 tao
	/**
104 7663 tao
	 * Get the the CoreContainer of the generated EmbeddedSolrServer.
105 7657 tao
	 * @return it may return null if the solr is configured as the SolrHttpServer
106 7712 tao
	 * @throws SAXException
107
	 * @throws IOException
108
	 * @throws ParserConfigurationException
109
	 * @throws UnsupportedType
110 7657 tao
	 * @throws Exception
111
	 */
112 7712 tao
	public static CoreContainer getCoreContainer() throws UnsupportedType, ParserConfigurationException, IOException, SAXException  {
113 7657 tao
	    if(coreContainer == null) {
114
	        createSolrServer();
115
	    }
116 7628 tao
	    return coreContainer;
117
	}
118 7657 tao
119 7663 tao
	/**
120
	 * Get the CollectionName of the generated EmbeddedSolrServer. It can be null if the solr is configured as a SolrHttpServer
121
	 * @return
122
	 */
123 7657 tao
	public static String getCollectionName() {
124
	    if(collectionName == null) {
125 7659 tao
	        collectionName = Settings.getConfiguration().getString(SOLR_COLLECTION_NAME_PROPERTY_NAME);
126 7657 tao
	    }
127
	    return collectionName;
128
	}
129 7663 tao
130
131 7628 tao
}