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.ConcurrentUpdateSolrServer;
16  import org.apache.solr.client.solrj.impl.HttpSolrServer;
17  import org.apache.solr.client.solrj.impl.XMLResponseParser;
18  import org.apache.solr.core.CoreContainer;
19  import org.kuali.rice.core.api.config.property.ConfigContext;
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          if(ConfigContext.getCurrentContextConfig()!=null){
56              docSearchUrl = ConfigContext.getCurrentContextConfig().getProperty("docSearchURL");
57          }
58          if ((null != docSearchUrl) && !docSearchUrl.endsWith("/")) {
59              docSearchUrl = docSearchUrl + "/";
60          }
61          LOG.info("Actual Document Search URL being used docSearchURL=" + docSearchUrl);
62          serverMap = new HashMap<String, SolrServer>();
63      }
64  
65      /**
66       * Simplest method to get a solr server for the default core.
67       *
68       * @return Returns solr server object.
69       * @throws Exception Throws an exception on error.
70       */
71      public SolrServer getSolrServer() throws SolrServerException {
72          return getSolrServer(SOLR_CORE_MAIN);
73      }
74  
75      /**
76       * Get a solr server for the given core.
77       *
78       * @param solrCore Solr core for which server is required.
79       * @return Returns solr server object.
80       * @throws Exception Throws an exception on error.
81       */
82      public SolrServer getSolrServer(String solrCore) throws SolrServerException {
83          boolean isStreaming = false;
84          boolean isEmbedded = false;
85          String embedPropValue = System.getProperty(SOLR_SERVER_EMBEDDED);
86          if (StringUtils.isNotEmpty(embedPropValue)) {
87              isEmbedded = Boolean.parseBoolean(embedPropValue);
88          }
89          String streamingPropValue = System.getProperty(SOLR_SERVER_STREAMING);
90          if (StringUtils.isNotEmpty(streamingPropValue)) {
91              isStreaming = Boolean.parseBoolean(streamingPropValue);
92          }
93          return getSolrServer(solrCore, isStreaming, isEmbedded);
94      }
95  
96      /**
97       * Get a solr server for the given core, with the specified options for streaming or embedded.
98       * The streaming and embedded options are considered only for the first time, for the sake of simplicity.
99       * The server objects are saved in a map and reused.
100      *
101      * @param solrCore    Solr core for which server is required.
102      * @param isStreaming Indicates whether a streaming server is needed.
103      * @param isEmbedded  Indicates whether an embedded server is needed.
104      * @return Returns solr server object.
105      * @throws Exception Throws an exception on error.
106      */
107     public SolrServer getSolrServer(String solrCore, boolean isStreaming, boolean isEmbedded)
108             throws SolrServerException {
109         SolrServer solr = null;
110         try {
111             if (null == serverMap) {
112                 serverMap = new HashMap<String, SolrServer>();
113             }
114             solr = serverMap.get(solrCore);
115             if (null != solr) {
116                 return solr;
117             }
118             if (isEmbedded) {
119                 solr = getEmbeddedSolrServer(solrCore);
120             } else if (isStreaming) {
121                 solr = new ConcurrentUpdateSolrServer(docSearchUrl + solrCore, 100, 5);
122             } else {
123                 solr = getCommonsHttpSolrServer(docSearchUrl + solrCore);
124             }
125             serverMap.put(solrCore, solr);
126         } catch (SolrServerException sse) {
127             throw sse;
128         } catch (Exception e) {
129             throw new SolrServerException(e);
130         }
131         return solr;
132     }
133 
134     public String getSolrCoreURL() {
135         return docSearchUrl + SOLR_CORE_MAIN;
136     }
137 
138     protected static HttpSolrServer getCommonsHttpSolrServer(String solrUrl) throws MalformedURLException {
139         HttpSolrServer server = new HttpSolrServer(solrUrl);
140         server.setConnectionTimeout(100);
141         server.setDefaultMaxConnectionsPerHost(100);
142         server.setMaxTotalConnections(100);
143         server.setFollowRedirects(false);
144         server.setAllowCompression(true);
145         server.setMaxRetries(1);
146         server.setParser(new XMLResponseParser());
147         return server;
148     }
149 
150     protected SolrServer getEmbeddedSolrServer(String solrCore) throws IOException, ParserConfigurationException, SAXException, SolrServerException {
151         String solrHomeDir = System.getProperty(SOLR_HOME);
152         File home = new File(solrHomeDir);
153         File f = new File(home, "solr.xml");
154         CoreContainer container = new CoreContainer();
155         container.load();
156         return new EmbeddedSolrServer(container, solrCore);
157     }
158 
159 }