Skip to content

Commit

Permalink
GH-2419 make executor and httpcontext accessible for subclasses (#2420)
Browse files Browse the repository at this point in the history
  • Loading branch information
abrokenjester authored Aug 6, 2020
1 parent 0883562 commit f94fa33
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,11 @@ public RDF4JProtocolSession(HttpClient client, ScheduledExecutorService executor
super(client, executor);
this.executor = executor;

// we want to preserve bnode ids to allow Sesame API methods to match
// we want to preserve bnode ids to allow RDF4J API methods to match
// blank nodes.
getParserConfig().set(BasicParserSettings.PRESERVE_BNODE_IDS, true);

// Sesame client has preference for binary response formats, as these are
// RDF4J Protocol has a preference for binary response formats, as these are
// most performant
setPreferredTupleQueryResultFormat(TupleQueryResultFormat.BINARY);
setPreferredRDFFormat(RDFFormat.BINARY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;
import org.eclipse.rdf4j.RDF4JConfigException;
import org.eclipse.rdf4j.RDF4JException;
Expand Down Expand Up @@ -127,10 +128,6 @@
*/
public class SPARQLProtocolSession implements HttpClientDependent, AutoCloseable {

/*-----------*
* Constants *
*-----------*/

protected static final Charset UTF8 = StandardCharsets.UTF_8;

/**
Expand Down Expand Up @@ -159,10 +156,6 @@ public class SPARQLProtocolSession implements HttpClientDependent, AutoCloseable

final static Logger logger = LoggerFactory.getLogger(SPARQLProtocolSession.class);

/*-----------*
* Variables *
*-----------*/

private ValueFactory valueFactory;

private String queryURL;
Expand All @@ -187,10 +180,6 @@ public class SPARQLProtocolSession implements HttpClientDependent, AutoCloseable

private Map<String, String> additionalHttpHeaders = Collections.emptyMap();

/*--------------*
* Constructors *
*--------------*/

public SPARQLProtocolSession(HttpClient client, ExecutorService executor) {
this.httpClient = client;
this.httpContext = new HttpClientContext();
Expand All @@ -215,10 +204,6 @@ public SPARQLProtocolSession(HttpClient client, ExecutorService executor) {
this.maximumUrlLength = maximumUrlLength;
}

/*-----------------*
* Get/set methods *
*-----------------*/

@Override
public final HttpClient getHttpClient() {
return httpClient;
Expand Down Expand Up @@ -252,8 +237,7 @@ protected void setUpdateURL(String updateURL) {
}

/**
* Sets the preferred format for encoding tuple query results. The {@link TupleQueryResultFormat#BINARY binary}
* format is preferred by default.
* Sets the preferred format for encoding tuple query results.
*
* @param format The preferred {@link TupleQueryResultFormat}, or <tt>null</tt> to indicate no specific format is
* preferred.
Expand All @@ -263,7 +247,8 @@ public void setPreferredTupleQueryResultFormat(TupleQueryResultFormat format) {
}

/**
* Gets the preferred {@link TupleQueryResultFormat} for encoding tuple query results.
* Gets the preferred {@link TupleQueryResultFormat} for encoding tuple query results. The
* {@link TupleQueryResultFormat#SPARQL SPARQL/XML} format is preferred by default.
*
* @return The preferred format, of <tt>null</tt> if no specific format is preferred.
*/
Expand All @@ -272,8 +257,7 @@ public TupleQueryResultFormat getPreferredTupleQueryResultFormat() {
}

/**
* Sets the preferred format for encoding RDF documents. The {@link RDFFormat#TURTLE Turtle} format is preferred by
* default.
* Sets the preferred format for encoding RDF documents.
*
* @param format The preferred {@link RDFFormat}, or <tt>null</tt> to indicate no specific format is preferred.
*/
Expand All @@ -282,7 +266,8 @@ public void setPreferredRDFFormat(RDFFormat format) {
}

/**
* Gets the preferred {@link RDFFormat} for encoding RDF documents.
* Gets the preferred {@link RDFFormat} for encoding RDF documents. The {@link RDFFormat#TURTLE Turtle} format is
* preferred by default.
*
* @return The preferred format, of <tt>null</tt> if no specific format is preferred.
*/
Expand All @@ -291,8 +276,7 @@ public RDFFormat getPreferredRDFFormat() {
}

/**
* Sets the preferred format for encoding boolean query results. The {@link BooleanQueryResultFormat#TEXT binary}
* format is preferred by default.
* Sets the preferred format for encoding boolean query results.
*
* @param format The preferred {@link BooleanQueryResultFormat}, or <tt>null</tt> to indicate no specific format is
* preferred.
Expand All @@ -302,7 +286,8 @@ public void setPreferredBooleanQueryResultFormat(BooleanQueryResultFormat format
}

/**
* Gets the preferred {@link BooleanQueryResultFormat} for encoding boolean query results.
* Gets the preferred {@link BooleanQueryResultFormat} for encoding boolean query results. The
* {@link BooleanQueryResultFormat#TEXT binary} format is preferred by default.
*
* @return The preferred format, of <tt>null</tt> if no specific format is preferred.
*/
Expand Down Expand Up @@ -1202,4 +1187,13 @@ public void setConnectionTimeout(long timeout) {
}
params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, (int) timeout);
}

/**
* Get the {@link HttpContext} used for sending HTTP requests.
*
* @return the {@link HttpContext} instance used for all protocol session requests.
*/
protected HttpContext getHttpContext() {
return this.httpContext;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public SharedHttpClientSessionManager() {
final int corePoolSize = Integer.getInteger(CORE_POOL_SIZE_PROPERTY, 1);
this.executor = Executors.newScheduledThreadPool(corePoolSize, (Runnable runnable) -> {
Thread thread = backingThreadFactory.newThread(runnable);
thread.setName(String.format("rdf4j-sesameclientimpl-%d", threadCount.getAndIncrement()));
thread.setName(String.format("rdf4j-SharedHttpClientSessionManager-%d", threadCount.getAndIncrement()));
thread.setDaemon(true);
return thread;
});
Expand Down Expand Up @@ -120,18 +120,6 @@ public void setHttpClientBuilder(HttpClientBuilder httpClientBuilder) {
this.httpClientBuilder = httpClientBuilder;
}

private CloseableHttpClient createHttpClient() {
HttpClientBuilder nextHttpClientBuilder = httpClientBuilder;
if (nextHttpClientBuilder != null) {
return nextHttpClientBuilder.build();
}
return HttpClientBuilder.create()
.useSystemProperties()
.disableAutomaticRetries()
.setDefaultRequestConfig(RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD).build())
.build();
}

@Override
public SPARQLProtocolSession createSPARQLProtocolSession(String queryEndpointUrl, String updateEndpointUrl) {
SPARQLProtocolSession session = new SPARQLProtocolSession(getHttpClient(), executor) {
Expand Down Expand Up @@ -169,10 +157,6 @@ public void close() {
return session;
}

/*-----------------*
* Get/set methods *
*-----------------*/

@Override
public void shutDown() {
try {
Expand Down Expand Up @@ -212,4 +196,25 @@ public void shutDown() {
public void initialize() {
}

/**
* Get the {@link ScheduledExecutorService} used by this session manager.
*
* @return a {@link ScheduledExecutorService} used by all {@link SPARQLProtocolSession} and
* {@link RDF4JProtocolSession} instances created by this session manager.
*/
protected final ScheduledExecutorService getExecutorService() {
return this.executor;
}

private CloseableHttpClient createHttpClient() {
HttpClientBuilder nextHttpClientBuilder = httpClientBuilder;
if (nextHttpClientBuilder != null) {
return nextHttpClientBuilder.build();
}
return HttpClientBuilder.create()
.useSystemProperties()
.disableAutomaticRetries()
.setDefaultRequestConfig(RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD).build())
.build();
}
}

0 comments on commit f94fa33

Please sign in to comment.