Project

General

Profile

1
/**
2
 *  Copyright: 2013 Regents of the University of California and the
3
 *             National Center for Ecological Analysis and Synthesis
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation; either version 2 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
 */
19
package edu.ucsb.nceas.metacat.common.query;
20

    
21
import java.io.IOException;
22
import java.io.InputStream;
23
import java.net.MalformedURLException;
24
import java.util.List;
25
import java.util.Map;
26
import java.util.Set;
27

    
28
import javax.xml.parsers.ParserConfigurationException;
29

    
30
import org.apache.solr.client.solrj.SolrServer;
31
import org.apache.solr.client.solrj.SolrServerException;
32
import org.apache.solr.client.solrj.embedded.EmbeddedSolrServer;
33
import org.apache.solr.client.solrj.impl.CommonsHttpSolrServer;
34
import org.apache.solr.common.params.SolrParams;
35
import org.apache.solr.core.CoreContainer;
36
import org.apache.solr.schema.SchemaField;
37
import org.dataone.service.exceptions.NotFound;
38
import org.dataone.service.exceptions.NotImplemented;
39
import org.dataone.service.exceptions.UnsupportedType;
40
import org.dataone.service.types.v1.Subject;
41
import org.xml.sax.SAXException;
42

    
43
import edu.ucsb.nceas.metacat.common.SolrServerFactory;
44

    
45
/**
46
 * A class to handle the solr query. It will call the SolrServerFactory to generate a SolrServer and
47
 * determine the type - the embedded or http solr server.
48
 * @author tao
49
 *
50
 */
51
public class SolrQueryServiceController {
52
    private static SolrQueryServiceController controller = null;
53
    private boolean isEmbeddedSolrServer = false;
54
    private EmbeddedSolrQueryService embeddedQueryService = null;
55
    private HttpSolrQueryService httpQueryService = null;
56
    
57
    /*
58
     * Private consctructor
59
     */
60
    private SolrQueryServiceController() throws UnsupportedType, ParserConfigurationException, IOException, SAXException, NotFound {
61
        SolrServer solrServer = SolrServerFactory.createSolrServer();
62
        if(solrServer instanceof EmbeddedSolrServer) {
63
            isEmbeddedSolrServer = true;
64
            EmbeddedSolrServer embeddedServer = (EmbeddedSolrServer) solrServer;
65
            CoreContainer coreContainer = SolrServerFactory.getCoreContainer();
66
            String collectionName = SolrServerFactory.getCollectionName();
67
            embeddedQueryService = new EmbeddedSolrQueryService(embeddedServer, coreContainer, collectionName);
68
        } else {
69
            isEmbeddedSolrServer = false;
70
            CommonsHttpSolrServer httpServer = (CommonsHttpSolrServer)solrServer;
71
            httpQueryService = new HttpSolrQueryService(httpServer);
72
        }
73
    }
74
    
75
    /**
76
     * Get the controller
77
     * @return
78
     * @throws UnsupportedType
79
     * @throws NotFound
80
     * @throws ParserConfigurationException
81
     * @throws IOException
82
     * @throws SAXException
83
     */
84
    public static SolrQueryServiceController getInstance() throws UnsupportedType, NotFound, ParserConfigurationException, IOException, SAXException {
85
        if(controller == null) {
86
            controller = new SolrQueryServiceController();
87
        }
88
        return controller;
89
    }
90
    
91
   
92
    
93
    /**
94
     * Query the Solr server with specified query string or SolrParams and the user's identity. 
95
     * @param query  query string. It is for the HttpSolrQueryService.
96
     * @param params the SolrParam. It is for the EmbeddedSolrQueryService.
97
     * @param subjects
98
     * @return
99
     * @throws NotImplemented
100
     * @throws NotFound
101
     * @throws IOException
102
     * @throws SolrServerException 
103
     * @throws SAXException 
104
     * @throws ParserConfigurationException 
105
     * @throws UnsupportedType 
106
     */
107
    public InputStream query(SolrParams params,Set<Subject>subjects) throws NotImplemented, NotFound, IOException, UnsupportedType, ParserConfigurationException, SAXException, SolrServerException  {
108
        if(isEmbeddedSolrServer) {
109
            return embeddedQueryService.query(params, subjects);
110
        } else {
111
            return httpQueryService.query(params, subjects);
112
        }
113
      
114
    }
115
    
116
    /**
117
     * Get the spec version of the solr server
118
     * @return
119
     */
120
    public String getSolrSpecVersion() {
121
        if(isEmbeddedSolrServer) {
122
            return embeddedQueryService.getSolrServerVersion();
123
        } else {
124
            return httpQueryService.getSolrServerVersion();
125
        }
126
    }
127
    
128
    /**
129
     * Get the fields list of the index schema
130
     * @return
131
     * @throws SAXException 
132
     * @throws IOException 
133
     * @throws ParserConfigurationException 
134
     * @throws MalformedURLException 
135
     * @throws Exception
136
     */
137
    public  Map<String, SchemaField> getIndexSchemaFields() throws MalformedURLException, ParserConfigurationException, IOException, SAXException  {
138
        if(isEmbeddedSolrServer) {
139
            return embeddedQueryService.getIndexSchemaFields();
140
        } else {
141
            return httpQueryService.getIndexSchemaFields();
142
        }
143
    }
144
    
145
   
146
    
147
    /**
148
     * Get the list of the valid field name (moved the fields names of the CopyFieldTarget).
149
     * @return
150
     * @throws SAXException 
151
     * @throws IOException 
152
     * @throws ParserConfigurationException 
153
     * @throws MalformedURLException 
154
     */
155
    public List<String> getValidSchemaFields() throws MalformedURLException, ParserConfigurationException, IOException, SAXException {
156
        if(isEmbeddedSolrServer) {
157
            return embeddedQueryService.getValidSchemaFields();
158
        } else{
159
            return httpQueryService.getValidSchemaField();
160
        }
161
       
162
    }
163

    
164
    
165
}
(7-7/7)