Skip to content

Instantly share code, notes, and snippets.

@cfalzone
Created December 2, 2013 13:41
Show Gist options
  • Save cfalzone/7749592 to your computer and use it in GitHub Desktop.
Save cfalzone/7749592 to your computer and use it in GitHub Desktop.
Solr Classes in dotCMS OSGI - not working
package com.aquent.solr;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocumentList;
import org.apache.velocity.tools.view.tools.ViewTool;
import com.dotmarketing.util.Logger;
public class SolrTool implements ViewTool {
@Override
public void init(Object arg0) {
}
public SolrDocumentList getResults(String query) {
SolrServer server = SolrUtil.getInstance().getSolrServer();
SolrQuery sq = new SolrQuery().setQuery(query);
QueryResponse qr;
try {
qr = server.query(sq);
} catch (Exception e) {
server = SolrUtil.getInstance().refreshServer();
try {
qr = server.query(sq);
} catch (Exception e1) {
Logger.error(this, "Solr Lookup for query: "+query+" Failed with Exception on 2nd Try", e1);
return null;
}
}
return qr.getResults();
}
}
package com.aquent.solr;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import com.dotmarketing.business.APILocator;
import com.dotmarketing.util.Logger;
public class SolrUtil {
public static final SolrUtil INSTANCE = new SolrUtil();
private SolrServer server = null;
private SolrUtil() { }
public static SolrUtil getInstance() {
return INSTANCE;
}
public SolrServer getSolrServer() {
if(server == null) {
// Get the solrserver from the default host
String solrserver;
try {
solrserver = APILocator.getHostAPI()
.findDefaultHost(APILocator.getUserAPI().getSystemUser(), false)
.getStringProperty("SolrServerUrl");
} catch (Exception e) {
Logger.error(this, "Error getting solr server url from the host", e);
return null;
}
synchronized(SolrUtil.class) {
if(server == null) {
server = new HttpSolrServer(solrserver);
}
}
Logger.info(this, "Initialized solr server");
}
return server;
}
public SolrServer refreshServer() {
String solrserver = null;
try {
solrserver=APILocator.getPluginAPI().loadProperty("com.aquent", "solrserver");
Logger.info(this, "Refreshing solr server with url: "+solrserver);
} catch(Exception e) {
Logger.error(this, "Unable to load plugin properties", e);
}
if(solrserver == null) {
Logger.info(this, "No Solr Server from plugin properties - bailing");
return null;
}
server = new HttpSolrServer(solrserver);
Logger.info(this, "Refreshed solr server");
return server;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment