Project

General

Profile

« Previous | Next » 

Revision 7591

naive version of SolrServerFactory - no reflection. It returns an EmbeddedSolrServer implementation based on the solr.homeDir set in Settings class. https://projects.ecoinformatics.org/ecoinfo/issues/5883

View differences:

metacat-index/src/test/java/edu/ucsb/nceas/metacat/index/SolrIndexIT.java
1 1
package edu.ucsb.nceas.metacat.index;
2 2

  
3
import static org.junit.Assert.assertEquals;
4 3
import static org.junit.Assert.assertTrue;
5 4

  
6 5
import java.io.File;
7 6
import java.io.FileInputStream;
8
import java.io.IOException;
9 7
import java.io.InputStream;
10 8
import java.util.List;
11 9

  
12
import javax.xml.parsers.ParserConfigurationException;
13

  
14

  
15

  
16 10
import org.apache.solr.client.solrj.SolrServer;
17 11
import org.apache.solr.client.solrj.SolrServerException;
18
import org.apache.solr.client.solrj.embedded.EmbeddedSolrServer;
19 12
import org.apache.solr.client.solrj.response.QueryResponse;
20 13
import org.apache.solr.common.params.SolrParams;
21
import org.apache.solr.core.CoreContainer;
22 14
import org.apache.solr.servlet.SolrRequestParsers;
23
import org.dataone.configuration.Settings;
24 15
import org.dataone.service.types.v1.SystemMetadata;
25 16
import org.dataone.service.util.TypeMarshaller;
26 17
import org.junit.Before;
27 18
import org.junit.Test;
28
import org.xml.sax.SAXException;
29 19

  
30 20
public class SolrIndexIT  {
31 21
    
......
35 25
    private static final String EMLUPDATEFILEPATH = "src/test/resources/eml-updating-example.xml";
36 26
    private static final String id = "urn:uuid:606a19dd-b531-4bf4-b5a5-6d06c3d39098";
37 27
    private static final String newId = "urn:uuid:606a19dd-b531-4bf4-b5a5-6d06c3d39099";
38
    private static String SOLRTESTHOMEPATH = null;
39 28
    
40
    /**
41
     * 
42
     */
29
	private SolrIndex solrIndex = null;
30
    
31
    
43 32
    @Before
44
    public void setUp() {
45
    	SOLRTESTHOMEPATH = Settings.getConfiguration().getString("solr.homeDir");
33
    public void setUp() throws Exception {
34
            solrIndex  = generateSolrIndex();
46 35
    }
47 36
    
37
    private SolrIndex generateSolrIndex() throws Exception {
38
        ApplicationController controller = new ApplicationController();
39
        List<SolrIndex> list = controller.getSolrIndexes();
40
        SolrIndex[] solrIndexesarray = list.toArray(new SolrIndex[list.size()]);
41
        SolrIndex index = solrIndexesarray[0];
42
        SolrServer solrServer = SolrServerFactory.createSolrServer();
43
        index.setSolrServer(solrServer);
44
        return index;
45
    }
46
    
48 47
    /**
49 48
     * Test building index for an insert.
50 49
     */
51 50
    @Test
52 51
    public void testInsert() throws Exception {
53
    	try {
54
        SolrIndex solrIndex = generateSolrIndex();
55 52
    	
53
    	
56 54
       //InputStream systemInputStream = new FileInputStream(new File(SYSTEMMETAFILEPATH));
57 55
       SystemMetadata systemMetadata = TypeMarshaller.unmarshalTypeFromFile(SystemMetadata.class, SYSTEMMETAFILEPATH);
58 56
       InputStream emlInputStream = new FileInputStream(new File(EMLFILEPATH));    
59 57
       solrIndex.insert(id, systemMetadata, emlInputStream);
60 58
       String result = doQuery(solrIndex.getSolrServer());
61 59
       assertTrue(result.contains("version1"));
62
    	} catch (Exception e) {
63
    		e.printStackTrace();
64
    	}
60
    	
65 61
    }
66 62
    
67 63
    /**
......
69 65
     */
70 66
    @Test
71 67
    public void testUpdate() throws Exception {
72
        SolrIndex solrIndex = generateSolrIndex();
73 68
       //InputStream systemInputStream = new FileInputStream(new File(SYSTEMMETAFILEPATH));
74 69
       SystemMetadata systemMetadata = TypeMarshaller.unmarshalTypeFromFile(SystemMetadata.class, SYSTEMMETAUPDATEFILEPATH);
75 70
       InputStream emlInputStream = new FileInputStream(new File(EMLUPDATEFILEPATH));    
......
90 85
       //assertTrue(!result.contains("version2"));
91 86
    }*/
92 87
    
93
   
94 88
    
95 89
    /*
96 90
     * Do query
......
125 119
        }
126 120
    }*/
127 121
    
128
    private SolrIndex generateSolrIndex() throws Exception {
129
        ApplicationController controller = new ApplicationController();
130
        List<SolrIndex> list = controller.getSolrIndexes();
131
        SolrIndex[] solrIndexesarray = list.toArray(new SolrIndex[list.size()]);
132
        SolrIndex index = solrIndexesarray[0];
133
        index.setSolrServer(generateSolrServer());
134
        return index;
135
    }
136 122
    
137
    /*
138
     * Generate a test solr server
139
     */
140
    private SolrServer generateSolrServer() throws IOException, ParserConfigurationException, SAXException {
141
        //createSolrHome();
142
        File configFile = new File(SOLRTESTHOMEPATH, "solr.xml");
143
        CoreContainer c = new CoreContainer(SOLRTESTHOMEPATH, configFile);
144
        c.load(SOLRTESTHOMEPATH, configFile);
145
        SolrServer solrServer = new EmbeddedSolrServer(c, "collection1");
146
        return solrServer;
147
    }
148
    
149 123
}
150 124

  
metacat-index/src/main/java/edu/ucsb/nceas/metacat/index/ApplicationController.java
73 73
        context = getContext();
74 74
        solrIndexes = (List<SolrIndex>) context.getBean(SOLRINDEXES);
75 75
        
76
        // TODO: use factory to create the correct impl
76
        // use factory to create the correct impl
77 77
    	SolrServer solrServer = null;
78
		try {
79
			solrServer = SolrServerFactory.createSolrServer();
80
		} catch (Exception e) {
81
			log.error("Could not create SolrServer form factory", e);
82
		}
78 83

  
79 84
        // start the SystemMetadata listener[s] (only expect there to be one)
80 85
        for (SolrIndex solrIndex: solrIndexes) {
metacat-index/src/main/java/edu/ucsb/nceas/metacat/index/SolrIndex.java
27 27
package edu.ucsb.nceas.metacat.index;
28 28

  
29 29
import java.io.ByteArrayInputStream;
30
import java.io.File;
31 30
import java.io.IOException;
32 31
import java.io.InputStream;
33 32
import java.util.ArrayList;
......
50 49
import org.apache.commons.logging.LogFactory;
51 50
import org.apache.solr.client.solrj.SolrServer;
52 51
import org.apache.solr.client.solrj.SolrServerException;
53
import org.apache.solr.client.solrj.embedded.EmbeddedSolrServer;
54
//import org.apache.solr.client.solrj.impl.HttpSolrServer;
55 52
import org.apache.solr.client.solrj.response.UpdateResponse;
56 53
import org.apache.solr.common.SolrInputDocument;
57
import org.apache.solr.core.CoreContainer;
58 54
import org.dataone.cn.indexer.XMLNamespaceConfig;
59
import org.dataone.cn.indexer.XPathDocumentParser;
60 55
import org.dataone.cn.indexer.parser.IDocumentSubprocessor;
61 56
import org.dataone.cn.indexer.parser.SolrField;
62 57
import org.dataone.cn.indexer.solrhttp.SolrDoc;
63
import org.dataone.cn.indexer.solrhttp.SolrElementAdd;
64 58
import org.dataone.cn.indexer.solrhttp.SolrElementField;
65
import org.dataone.configuration.Settings;
66 59
import org.dataone.service.types.v1.Identifier;
67 60
import org.dataone.service.types.v1.SystemMetadata;
68 61
import org.dataone.service.util.TypeMarshaller;
......
76 69
 *
77 70
 */
78 71
public class SolrIndex {
79
    
80
    public static final String SOLRHOMEPROPERTYNAME = "solr.homeDir";
81
    
82
    //private static final String DEFAULTSOLRHOMEPATH = "/Users/tao/Downloads/apache-solr-3.4.0/example/solr";
83
    
72
            
84 73
    private List<IDocumentSubprocessor> subprocessors = null;
85 74
    private SolrServer solrServer = null;
86 75
    private XMLNamespaceConfig xmlNamespaceConfig = null;
......
114 103
                    throws XPathExpressionException, ParserConfigurationException, IOException, SAXException {
115 104
         this.xmlNamespaceConfig = xmlNamespaceConfig;
116 105
         this.sysmetaSolrFields = sysmetaSolrFields;
117
         initSolrServer();
118 106
         init();
119 107
    }
120 108
    
121
    private void initSolrServer() throws IOException, ParserConfigurationException, SAXException {
122
        String solrHomeDir = null;
123
        solrHomeDir = Settings.getConfiguration().getString(SOLRHOMEPROPERTYNAME);
124
        log.info("========================= the solr home from the metacat.properties is "+solrHomeDir);
125
        File configFile = new File(solrHomeDir, "solr.xml");
126
        CoreContainer c = new CoreContainer(solrHomeDir, configFile);
127
        c.load(solrHomeDir, configFile);
128
        solrServer = new EmbeddedSolrServer(c, "collection1");
129
    }
130
    
131 109
    private void init() throws ParserConfigurationException, XPathExpressionException {
132 110
        xpath.setNamespaceContext(xmlNamespaceConfig);
133 111
        initExpressions();
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.core.CoreContainer;
36
import org.dataone.configuration.Settings;
37

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

  
58
	public static Log log = LogFactory.getLog(SolrServerFactory.class);
59

  
60
	public static SolrServer createSolrServer() throws Exception {
61
		SolrServer solrServer = null;
62
		
63
		String solrHomeDir = Settings.getConfiguration().getString(SOLR_HOME_PROPERTY_NAME);
64
        log.info("The configured solr home from properties is " + solrHomeDir);
65
        File configFile = new File(solrHomeDir, "solr.xml");
66
        CoreContainer c = new CoreContainer(solrHomeDir, configFile);
67
        c.load(solrHomeDir, configFile);
68
        solrServer = new EmbeddedSolrServer(c, "collection1");
69
        
70
        return solrServer;
71
	}
72
}
0 73

  

Also available in: Unified diff