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.query;
28

    
29
import java.io.IOException;
30
import java.io.InputStream;
31
import java.util.Set;
32

    
33
import javax.xml.parsers.ParserConfigurationException;
34

    
35
import org.apache.solr.client.solrj.SolrServer;
36
import org.apache.solr.client.solrj.SolrServerException;
37
import org.apache.solr.client.solrj.embedded.EmbeddedSolrServer;
38
import org.apache.solr.client.solrj.impl.CommonsHttpSolrServer;
39
import org.apache.solr.common.params.SolrParams;
40
import org.apache.solr.core.CoreContainer;
41
import org.dataone.service.exceptions.NotFound;
42
import org.dataone.service.exceptions.NotImplemented;
43
import org.dataone.service.exceptions.UnsupportedType;
44
import org.dataone.service.types.v1.Subject;
45
import org.xml.sax.SAXException;
46

    
47
import edu.ucsb.nceas.metacat.common.SolrServerFactory;
48

    
49
/**
50
 * A class to handle the solr query. It will call the SolrServerFactory to generate a SolrServer and
51
 * determine the type - the embedded or http solr server.
52
 * @author tao
53
 *
54
 */
55
public class SolrQueryServiceController {
56
    private static SolrQueryServiceController controller = null;
57
    private boolean isEmbeddedSolrServer = false;
58
    private EmbeddedSolrQueryService embeddedQueryService = null;
59
    private HttpSolrQueryService httpQueryService = null;
60
    
61
    /*
62
     * Private consctructor
63
     */
64
    private SolrQueryServiceController() throws UnsupportedType, ParserConfigurationException, IOException, SAXException, NotFound {
65
        SolrServer solrServer = SolrServerFactory.createSolrServer();
66
        if(solrServer instanceof EmbeddedSolrServer) {
67
            isEmbeddedSolrServer = true;
68
            EmbeddedSolrServer embeddedServer = (EmbeddedSolrServer) solrServer;
69
            CoreContainer coreContainer = SolrServerFactory.getCoreContainer();
70
            String collectionName = SolrServerFactory.getCollectionName();
71
            embeddedQueryService = new EmbeddedSolrQueryService(embeddedServer, coreContainer, collectionName);
72
        } else {
73
            isEmbeddedSolrServer = false;
74
            CommonsHttpSolrServer httpServer = (CommonsHttpSolrServer)solrServer;
75
            httpQueryService = new HttpSolrQueryService(httpServer);
76
        }
77
    }
78
    
79
    /**
80
     * Get the controller
81
     * @return
82
     * @throws UnsupportedType
83
     * @throws NotFound
84
     * @throws ParserConfigurationException
85
     * @throws IOException
86
     * @throws SAXException
87
     */
88
    public static SolrQueryServiceController getInstance() throws UnsupportedType, NotFound, ParserConfigurationException, IOException, SAXException {
89
        if(controller == null) {
90
            controller = new SolrQueryServiceController();
91
        }
92
        return controller;
93
    }
94
    
95
   
96
    
97
    /**
98
     * Query the Solr server with specified query string or SolrParams and the user's identity. 
99
     * @param query  query string. It is for the HttpSolrQueryService.
100
     * @param params the SolrParam. It is for the EmbeddedSolrQueryService.
101
     * @param subjects
102
     * @return
103
     * @throws NotImplemented
104
     * @throws NotFound
105
     * @throws IOException
106
     * @throws SolrServerException 
107
     * @throws SAXException 
108
     * @throws ParserConfigurationException 
109
     * @throws UnsupportedType 
110
     */
111
    public InputStream query(String query, SolrParams params,Set<Subject>subjects) throws NotImplemented, NotFound, IOException, UnsupportedType, ParserConfigurationException, SAXException, SolrServerException  {
112
        if(isEmbeddedSolrServer) {
113
            return embeddedQueryService.query(params, subjects);
114
        } else {
115
            return httpQueryService.query(query, subjects);
116
        }
117
      
118
    }
119

    
120
    
121
}
(4-4/4)