View Javadoc

1   package org.kuali.ole.docstore.discovery.service;
2   
3   import java.io.File;
4   import java.io.IOException;
5   import java.net.MalformedURLException;
6   import java.util.HashMap;
7   import java.util.Map;
8   
9   import javax.xml.parsers.ParserConfigurationException;
10  
11  import org.apache.commons.lang.StringUtils;
12  import org.apache.solr.client.solrj.SolrServer;
13  import org.apache.solr.client.solrj.SolrServerException;
14  import org.apache.solr.client.solrj.embedded.EmbeddedSolrServer;
15  import org.apache.solr.client.solrj.impl.CommonsHttpSolrServer;
16  import org.apache.solr.client.solrj.impl.StreamingUpdateSolrServer;
17  import org.apache.solr.client.solrj.impl.XMLResponseParser;
18  import org.apache.solr.core.CoreContainer;
19  import org.kuali.ole.docstore.discovery.util.PropertyUtil;
20  import org.slf4j.Logger;
21  import org.slf4j.LoggerFactory;
22  import org.xml.sax.SAXException;
23  
24  /**
25   * This class handles the creation of solr server object, used for connecting to solr server (core).
26   * User: tirumalesh.b
27   * Date: 23/12/11 Time: 12:32 PM
28   */
29  public class SolrServerManager {
30  
31      private static final Logger LOG = LoggerFactory.getLogger(SolrServerManager.class);
32  
33      public static final String SOLR_HOME = "solr.solr.home";
34      public static final String SOLR_CORE_MAIN = "bib";
35      public static final String SOLR_SERVER_EMBEDDED = "solr.server.embedded";
36      public static final String SOLR_SERVER_STREAMING = "solr.server.streaming";
37  
38      private static SolrServerManager solrServerMgr = null;
39      private static String docSearchUrl = null;
40      public static Map<String, SolrServer> serverMap = null;
41  
42      public static SolrServerManager getInstance() {
43          if (null == solrServerMgr) {
44              solrServerMgr = new SolrServerManager();
45          }
46          return solrServerMgr;
47      }
48  
49      private SolrServerManager() {
50          init();
51      }
52  
53      protected void init() {
54          LOG.debug("SolrServerManager.init()");
55          docSearchUrl = PropertyUtil.getPropertyUtil().getProperty("docSearchURL");
56          if ((null != docSearchUrl) && !docSearchUrl.endsWith("/")) {
57              docSearchUrl = docSearchUrl + "/";
58          }
59          LOG.info("Actual Document Search URL being used docSearchURL=" + docSearchUrl);
60          serverMap = new HashMap<String, SolrServer>();
61      }
62  
63      /**
64       * Simplest method to get a solr server for the default core.
65       *
66       * @return Returns solr server object.
67       * @throws Exception Throws an exception on error.
68       */
69      public SolrServer getSolrServer() throws SolrServerException {
70          return getSolrServer(SOLR_CORE_MAIN);
71      }
72  
73      /**
74       * Get a solr server for the given core.
75       *
76       * @param solrCore Solr core for which server is required.
77       * @return Returns solr server object.
78       * @throws Exception Throws an exception on error.
79       */
80      public SolrServer getSolrServer(String solrCore) throws SolrServerException {
81          boolean isStreaming = false;
82          boolean isEmbedded = false;
83          String embedPropValue = System.getProperty(SOLR_SERVER_EMBEDDED);
84          if (StringUtils.isNotEmpty(embedPropValue)) {
85              isEmbedded = Boolean.parseBoolean(embedPropValue);
86          }
87          String streamingPropValue = System.getProperty(SOLR_SERVER_STREAMING);
88          if (StringUtils.isNotEmpty(streamingPropValue)) {
89              isStreaming = Boolean.parseBoolean(streamingPropValue);
90          }
91          return getSolrServer(solrCore, isStreaming, isEmbedded);
92      }
93  
94      /**
95       * Get a solr server for the given core, with the specified options for streaming or embedded.
96       * The streaming and embedded options are considered only for the first time, for the sake of simplicity.
97       * The server objects are saved in a map and reused.
98       *
99       * @param solrCore Solr core for which server is required.
100      * @param isStreaming Indicates whether a streaming server is needed.
101      * @param isEmbedded Indicates whether an embedded server is needed.
102      * @return Returns solr server object.
103      * @throws Exception Throws an exception on error.
104      */
105     public SolrServer getSolrServer(String solrCore, boolean isStreaming, boolean isEmbedded)
106             throws SolrServerException {
107         SolrServer solr = null;
108         try {
109             if (null == serverMap) {
110                 serverMap = new HashMap<String, SolrServer>();
111             }
112             solr = serverMap.get(solrCore);
113             if (null != solr) {
114                 return solr;
115             }
116             if (isEmbedded) {
117                 solr = getEmbeddedSolrServer(solrCore);
118             }
119             else if (isStreaming) {
120                 solr = new StreamingUpdateSolrServer(docSearchUrl + solrCore, 100, 5);
121             }
122             else {
123                 solr = getCommonsHttpSolrServer(docSearchUrl + solrCore);
124             }
125             serverMap.put(solrCore, solr);
126         }
127         catch (SolrServerException sse) {
128             throw sse;
129         }
130         catch (Exception e) {
131             throw new SolrServerException(e);
132         }
133         return solr;
134     }
135 
136     public String getSolrCoreURL() {
137         return docSearchUrl + SOLR_CORE_MAIN;
138     }
139 
140     protected static CommonsHttpSolrServer getCommonsHttpSolrServer(String solrUrl) throws MalformedURLException {
141         CommonsHttpSolrServer server = new CommonsHttpSolrServer(solrUrl);
142         server.setConnectionTimeout(100);
143         server.setDefaultMaxConnectionsPerHost(100);
144         server.setMaxTotalConnections(100);
145         server.setFollowRedirects(false);
146         server.setAllowCompression(true);
147         server.setMaxRetries(1);
148         server.setParser(new XMLResponseParser());
149         return server;
150     }
151 
152     protected SolrServer getEmbeddedSolrServer(String solrCore) throws IOException, ParserConfigurationException, SAXException, SolrServerException {
153         String solrHomeDir = System.getProperty(SOLR_HOME);
154         File home = new File(solrHomeDir);
155         File f = new File(home, "solr.xml");
156         CoreContainer container = new CoreContainer();
157         container.load(solrHomeDir, f);
158         return new EmbeddedSolrServer(container, solrCore);
159     }
160 
161 }