View Javadoc
1   /*
2    * This file is part of ***  M y C o R e  ***
3    * See http://www.mycore.de/ for details.
4    *
5    * MyCoRe 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 3 of the License, or
8    * (at your option) any later version.
9    *
10   * MyCoRe 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 MyCoRe.  If not, see <http://www.gnu.org/licenses/>.
17   */
18  
19  package org.mycore.solr.index.handlers;
20  
21  import java.io.IOException;
22  import java.util.Collections;
23  import java.util.List;
24  
25  import org.apache.solr.client.solrj.SolrClient;
26  import org.apache.solr.client.solrj.SolrServerException;
27  import org.apache.solr.client.solrj.request.UpdateRequest;
28  import org.mycore.solr.MCRSolrClientFactory;
29  import org.mycore.solr.index.MCRSolrIndexHandler;
30  
31  public abstract class MCRSolrAbstractIndexHandler implements MCRSolrIndexHandler {
32  
33      protected SolrClient solrClient;
34  
35      protected int commitWithin;
36  
37      public MCRSolrAbstractIndexHandler() {
38          this(null);
39      }
40  
41      public MCRSolrAbstractIndexHandler(SolrClient solrClient) {
42          this.solrClient = solrClient != null ? solrClient : MCRSolrClientFactory.getMainSolrClient();
43          this.commitWithin = -1;
44      }
45  
46      public SolrClient getSolrClient() {
47          return this.solrClient;
48      }
49  
50      public abstract void index() throws IOException, SolrServerException;
51  
52      @Override
53      public List<MCRSolrIndexHandler> getSubHandlers() {
54          return Collections.emptyList();
55      }
56  
57      /**
58       * Time in milliseconds solr should index the stream. -1 by default,
59       * says that solr decide when to commit.
60       */
61      public void setCommitWithin(int commitWithin) {
62          this.commitWithin = commitWithin;
63      }
64  
65      public int getCommitWithin() {
66          return commitWithin;
67      }
68  
69      @Override
70      public void setSolrServer(SolrClient solrClient) {
71          this.solrClient = solrClient;
72      }
73  
74      @Override
75      public int getDocuments() {
76          return 1;
77      }
78  
79      protected UpdateRequest getUpdateRequest(String path) {
80          UpdateRequest req = path != null ? new UpdateRequest(path) : new UpdateRequest();
81          req.setCommitWithin(getCommitWithin());
82          return req;
83      }
84  
85  }