Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
package com.rbmhtechnology.vind.api;
import com.rbmhtechnology.vind.SearchServerException;
import com.rbmhtechnology.vind.SearchServerInstantiateException;
import com.rbmhtechnology.vind.SearchServerProviderLoaderException;
import com.rbmhtechnology.vind.annotations.AnnotationUtil;
import com.rbmhtechnology.vind.api.query.FulltextSearch;
import com.rbmhtechnology.vind.api.query.delete.Delete;
import com.rbmhtechnology.vind.api.query.get.RealTimeGet;
import com.rbmhtechnology.vind.api.query.inverseSearch.InverseSearch;
import com.rbmhtechnology.vind.api.query.suggestion.ExecutableSuggestionSearch;
import com.rbmhtechnology.vind.api.query.update.Update;
import com.rbmhtechnology.vind.api.result.BeanGetResult;
import com.rbmhtechnology.vind.api.result.BeanSearchResult;
import com.rbmhtechnology.vind.api.result.DeleteResult;
import com.rbmhtechnology.vind.api.result.GetResult;
import com.rbmhtechnology.vind.api.result.IndexResult;
import com.rbmhtechnology.vind.api.result.InverseSearchResult;
import com.rbmhtechnology.vind.api.result.SearchResult;
import com.rbmhtechnology.vind.api.result.StatusResult;
import com.rbmhtechnology.vind.api.result.SuggestionResult;
import com.rbmhtechnology.vind.configure.SearchConfiguration;
import com.rbmhtechnology.vind.model.DocumentFactory;
import com.rbmhtechnology.vind.model.InverseSearchQuery;
import org.apache.commons.lang3.NotImplementedException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.Closeable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ServiceConfigurationError;
import java.util.ServiceLoader;
/**
* Abstract class which offers a common set of methods to be implemented by the specific server implementations
* (Solr, ElasticSearch,..).
*
* @author Thomas Kurz ([email protected])
* @author Jakob Frank ([email protected])
* @since 15.06.16.
*/
public abstract class SearchServer implements Closeable {
protected static Logger log = LoggerFactory.getLogger(SearchServer.class);
/**
* Gets a {@link SearchServer} implementation object defined in the classpath from the ServiceLoader.
* @return {@link SearchServer} specific implementation.
*/
public static SearchServer getInstance() {
String providerClassName = SearchConfiguration.get(SearchConfiguration.SERVER_PROVIDER, null);
//Backwards compatibility needed
final String solrProviderClassName = SearchConfiguration.get(SearchConfiguration.SERVER_SOLR_PROVIDER, null);
if (providerClassName == null && solrProviderClassName != null) {
providerClassName = solrProviderClassName;
}
final List errorMessages = new ArrayList<>();
final ServiceLoader loader = ServiceLoader.load(SearchServer.class);
final Iterator it = loader.iterator();
SearchServer server = null;
if (!it.hasNext()) {
log.error("No SearchServer in classpath");
throw new RuntimeException("No SearchServer in classpath");
} else {
//if there is no service provider specified the first one found will work
if (providerClassName == null) {
server = it.next();
} else {
try {
final Class> providerClass = Class.forName(providerClassName);
while (it.hasNext() && server == null) {
try {
server = it.next();
if (server == null
|| server.getServiceProviderClass() == null
|| !server.getServiceProviderClass().getCanonicalName().equals(providerClassName)) {
server = null;
}
} catch (ServiceConfigurationError e) {
final Throwable cause = e.getCause();
log.debug(cause.getMessage(), cause);
if(SearchServerProviderLoaderException.class.isAssignableFrom(cause.getClass())){
final SearchServerProviderLoaderException loaderException = (SearchServerProviderLoaderException) cause;
if (loaderException.getServerClass().isAssignableFrom(providerClass)) {
errorMessages.add(loaderException);
}
} else if (SearchServerInstantiateException.class.isAssignableFrom(cause.getClass())){
final SearchServerInstantiateException instanceException = (SearchServerInstantiateException) cause;
errorMessages.add(instanceException);
}
} catch ( Exception e) {
log.debug("Cannot instantiate search server: {}", e.getMessage(), e);
}
}
} catch (ClassNotFoundException e) {
log.warn("Specified Vind Provider class {} is not in classpath",providerClassName, e);
throw new SearchServerProviderLoaderException(
String.format("Specified class %s is not in classpath", providerClassName),
ServiceProvider.class
);
}
}
}
if (server == null) {
log.error("Unable to found/instantiate SearchServer of class [{}] in classpath", providerClassName);
final SearchServerException searchServerException = new SearchServerException(
"Unable to found/instantiate SearchServer of class [" + providerClassName + "] in classpath");
errorMessages.forEach(searchServerException::addSuppressed);
throw searchServerException;
}
return server;
}
/**
* Gets a {@link SearchServer} implementation object defined in the classpath from the ServiceLoader.
* @param collection Name of the core/collection to be used by the instance.
* @return {@link SearchServer} specific implementation.
*/
public static SearchServer getInstance(String collection) {
SearchConfiguration.set(SearchConfiguration.SERVER_COLLECTION, collection);
return getInstance();
}
/**
* Gets a {@link SearchServer} implementation object defined in the classpath from the ServiceLoader.
* @param host server which the instance should connect to.
* @param collection Name of the core/collection to be used by the instance.
* @return {@link SearchServer} specific implementation.
*/
public static SearchServer getInstance(String host, String collection) {
SearchConfiguration.set(SearchConfiguration.SERVER_HOST, host);
SearchConfiguration.set(SearchConfiguration.SERVER_COLLECTION, collection);
return getInstance();
}
/**
* Gets a {@link SearchServer} implementation object defined in the classpath from the ServiceLoader.
* @param providerClassName class name of the server provider
* @param host server which the instance should connect to.
* @param collection Name of the core/collection to be used by the instance.
* @return {@link SearchServer} specific implementation.
*/
public static SearchServer getInstance(String providerClassName,String host, String collection) {
SearchConfiguration.set(SearchConfiguration.SERVER_PROVIDER, providerClassName);
SearchConfiguration.set(SearchConfiguration.SERVER_HOST, host);
SearchConfiguration.set(SearchConfiguration.SERVER_COLLECTION, collection);
return getInstance();
}
public void indexBean(Object ... t) {
List beanDocuments = new ArrayList<>();
for (Object bean : t){
beanDocuments.add(AnnotationUtil.createDocument(bean));
}
index(beanDocuments);
}
public void indexBean(List