Skip to content

Commit 26eba03

Browse files
committed
DATAES-283 - Get rid of Commons Lang dependency.
Replace all StringUtils and ArrayUtils usages with Springframework's StringUtils and ObjectUtils. Left the commons-lang as test-scope dependency as I believe it brings some values in the tests. Original pull request: #211.
1 parent 55bf1f2 commit 26eba03

12 files changed

+102
-71
lines changed

pom.xml

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
<groupId>commons-lang</groupId>
5757
<artifactId>commons-lang</artifactId>
5858
<version>${commonslang}</version>
59+
<scope>test</scope>
5960
</dependency>
6061

6162
<!-- JODA Time -->

src/main/java/org/springframework/data/elasticsearch/client/NodeClientFactoryBean.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@
1515
*/
1616
package org.springframework.data.elasticsearch.client;
1717

18+
import static java.util.Arrays.*;
19+
1820
import java.io.IOException;
1921
import java.io.InputStream;
2022
import java.util.Collection;
21-
import org.apache.commons.lang.StringUtils;
23+
2224
import org.elasticsearch.client.Client;
2325
import org.elasticsearch.client.node.NodeClient;
2426
import org.elasticsearch.common.settings.Settings;
@@ -31,7 +33,7 @@
3133
import org.springframework.beans.factory.DisposableBean;
3234
import org.springframework.beans.factory.FactoryBean;
3335
import org.springframework.beans.factory.InitializingBean;
34-
import static java.util.Arrays.*;
36+
import org.springframework.util.StringUtils;
3537

3638
/**
3739
* NodeClientFactoryBean
@@ -96,7 +98,7 @@ public void afterPropertiesSet() throws Exception {
9698
}
9799

98100
private Settings loadConfig() throws IOException {
99-
if (StringUtils.isNotBlank(pathConfiguration)) {
101+
if (!StringUtils.isEmpty(pathConfiguration)) {
100102
InputStream stream = getClass().getClassLoader().getResourceAsStream(pathConfiguration);
101103
if (stream != null) {
102104
return Settings.builder().loadFromStream(pathConfiguration, getClass().getClassLoader().getResourceAsStream(pathConfiguration)).build();

src/main/java/org/springframework/data/elasticsearch/client/TransportClientFactoryBean.java

+14-9
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
*/
1616
package org.springframework.data.elasticsearch.client;
1717

18-
import static org.apache.commons.lang.StringUtils.*;
19-
2018
import io.netty.util.ThreadDeathWatcher;
2119
import io.netty.util.concurrent.GlobalEventExecutor;
2220

@@ -48,6 +46,7 @@
4846
import org.springframework.beans.factory.InitializingBean;
4947
import org.springframework.util.Assert;
5048
import org.springframework.util.ClassUtils;
49+
import org.springframework.util.StringUtils;
5150

5251
/**
5352
* TransportClientFactoryBean
@@ -108,13 +107,19 @@ protected void buildClient() throws Exception {
108107

109108
client = new SpringDataTransportClient(settings());
110109
Assert.hasText(clusterNodes, "[Assertion failed] clusterNodes settings missing.");
111-
for (String clusterNode : split(clusterNodes, COMMA)) {
112-
String hostName = substringBeforeLast(clusterNode, COLON);
113-
String port = substringAfterLast(clusterNode, COLON);
114-
Assert.hasText(hostName, "[Assertion failed] missing host name in 'clusterNodes'");
115-
Assert.hasText(port, "[Assertion failed] missing port in 'clusterNodes'");
116-
logger.info("adding transport node : " + clusterNode);
117-
client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(hostName), Integer.valueOf(port)));
110+
String[] clusterNodesArray = StringUtils.split(clusterNodes, COMMA);
111+
if (clusterNodesArray != null) {
112+
for (String clusterNode : clusterNodesArray) {
113+
if (clusterNode != null) {
114+
int colonPosition = clusterName.lastIndexOf(COLON);
115+
String hostName = colonPosition != -1 ? clusterNode.substring(0, colonPosition) : clusterNode;
116+
String port = colonPosition != -1 ? clusterNode.substring(colonPosition, clusterNode.length()) : "";
117+
Assert.hasText(hostName, "[Assertion failed] missing host name in 'clusterNodes'");
118+
Assert.hasText(port, "[Assertion failed] missing port in 'clusterNodes'");
119+
logger.info("adding transport node : " + clusterNode);
120+
client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(hostName), Integer.valueOf(port)));
121+
}
122+
}
118123
}
119124
client.connectedNodes();
120125
}

src/main/java/org/springframework/data/elasticsearch/core/AbstractResultMapper.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,10 @@
1515
*/
1616
package org.springframework.data.elasticsearch.core;
1717

18-
import static org.apache.commons.lang.StringUtils.*;
19-
2018
import java.io.IOException;
2119

2220
import org.springframework.data.elasticsearch.ElasticsearchException;
21+
import org.springframework.util.StringUtils;
2322

2423
/**
2524
* @author Artur Konczak
@@ -33,7 +32,7 @@ public AbstractResultMapper(EntityMapper entityMapper) {
3332
}
3433

3534
public <T> T mapEntity(String source, Class<T> clazz) {
36-
if (isBlank(source)) {
35+
if (StringUtils.isEmpty(source)) {
3736
return null;
3837
}
3938
try {

src/main/java/org/springframework/data/elasticsearch/core/DefaultResultMapper.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import java.util.LinkedList;
2424
import java.util.List;
2525

26-
import org.apache.commons.lang.StringUtils;
2726
import org.elasticsearch.action.get.GetResponse;
2827
import org.elasticsearch.action.get.MultiGetItemResponse;
2928
import org.elasticsearch.action.get.MultiGetResponse;
@@ -40,6 +39,7 @@
4039
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty;
4140
import org.springframework.data.mapping.context.MappingContext;
4241
import org.springframework.util.Assert;
42+
import org.springframework.util.StringUtils;
4343

4444
import com.fasterxml.jackson.core.JsonEncoding;
4545
import com.fasterxml.jackson.core.JsonFactory;
@@ -84,7 +84,7 @@ public <T> AggregatedPage<T> mapResults(SearchResponse response, Class<T> clazz,
8484
for (SearchHit hit : response.getHits()) {
8585
if (hit != null) {
8686
T result = null;
87-
if (StringUtils.isNotBlank(hit.sourceAsString())) {
87+
if (StringUtils.hasText(hit.sourceAsString())) {
8888
result = mapEntity(hit.sourceAsString(), clazz);
8989
} else {
9090
result = mapEntity(hit.getFields().values(), clazz);

src/main/java/org/springframework/data/elasticsearch/core/ElasticsearchTemplate.java

+39-23
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@
1515
*/
1616
package org.springframework.data.elasticsearch.core;
1717

18+
import static org.elasticsearch.client.Requests.*;
19+
import static org.elasticsearch.index.VersionType.*;
20+
import static org.elasticsearch.index.query.QueryBuilders.*;
21+
import static org.springframework.data.elasticsearch.core.MappingBuilder.*;
22+
import static org.springframework.util.CollectionUtils.isEmpty;
23+
1824
import java.io.BufferedReader;
1925
import java.io.IOException;
2026
import java.io.InputStreamReader;
@@ -26,6 +32,7 @@
2632
import java.util.List;
2733
import java.util.Map;
2834
import java.util.NoSuchElementException;
35+
2936
import org.elasticsearch.action.ListenableActionFuture;
3037
import org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest;
3138
import org.elasticsearch.action.admin.indices.alias.get.GetAliasesRequest;
@@ -86,15 +93,24 @@
8693
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity;
8794
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty;
8895
import org.springframework.data.elasticsearch.core.mapping.SimpleElasticsearchMappingContext;
89-
import org.springframework.data.elasticsearch.core.query.*;
96+
import org.springframework.data.elasticsearch.core.query.AliasQuery;
97+
import org.springframework.data.elasticsearch.core.query.CriteriaQuery;
98+
import org.springframework.data.elasticsearch.core.query.DeleteQuery;
99+
import org.springframework.data.elasticsearch.core.query.FetchSourceFilter;
100+
import org.springframework.data.elasticsearch.core.query.GetQuery;
101+
import org.springframework.data.elasticsearch.core.query.IndexBoost;
102+
import org.springframework.data.elasticsearch.core.query.IndexQuery;
103+
import org.springframework.data.elasticsearch.core.query.MoreLikeThisQuery;
104+
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
105+
import org.springframework.data.elasticsearch.core.query.Query;
106+
import org.springframework.data.elasticsearch.core.query.ScriptField;
107+
import org.springframework.data.elasticsearch.core.query.SearchQuery;
108+
import org.springframework.data.elasticsearch.core.query.SourceFilter;
109+
import org.springframework.data.elasticsearch.core.query.StringQuery;
110+
import org.springframework.data.elasticsearch.core.query.UpdateQuery;
90111
import org.springframework.data.util.CloseableIterator;
91112
import org.springframework.util.Assert;
92-
import static org.apache.commons.lang.StringUtils.*;
93-
import static org.elasticsearch.client.Requests.*;
94-
import static org.elasticsearch.index.VersionType.*;
95-
import static org.elasticsearch.index.query.QueryBuilders.*;
96-
import static org.springframework.data.elasticsearch.core.MappingBuilder.*;
97-
import static org.springframework.util.CollectionUtils.isEmpty;
113+
import org.springframework.util.StringUtils;
98114

99115
/**
100116
* ElasticsearchTemplate
@@ -178,9 +194,9 @@ public boolean createIndex(String indexName) {
178194
public <T> boolean putMapping(Class<T> clazz) {
179195
if (clazz.isAnnotationPresent(Mapping.class)) {
180196
String mappingPath = clazz.getAnnotation(Mapping.class).mappingPath();
181-
if (isNotBlank(mappingPath)) {
197+
if (StringUtils.hasText(mappingPath)) {
182198
String mappings = readFileFromClasspath(mappingPath);
183-
if (isNotBlank(mappings)) {
199+
if (StringUtils.hasText(mappings)) {
184200
return putMapping(clazz, mappings);
185201
}
186202
} else {
@@ -564,9 +580,9 @@ public UpdateResponse update(UpdateQuery query) {
564580
}
565581

566582
private UpdateRequestBuilder prepareUpdate(UpdateQuery query) {
567-
String indexName = isNotBlank(query.getIndexName()) ? query.getIndexName()
583+
String indexName = StringUtils.hasText(query.getIndexName()) ? query.getIndexName()
568584
: getPersistentEntityFor(query.getClazz()).getIndexName();
569-
String type = isNotBlank(query.getType()) ? query.getType()
585+
String type = StringUtils.hasText(query.getType()) ? query.getType()
570586
: getPersistentEntityFor(query.getClazz()).getIndexType();
571587
Assert.notNull(indexName, "No index defined for Query");
572588
Assert.notNull(type, "No type define for Query");
@@ -666,9 +682,9 @@ public <T> String delete(Class<T> clazz, String id) {
666682
@Override
667683
public <T> void delete(DeleteQuery deleteQuery, Class<T> clazz) {
668684

669-
String indexName = isNotBlank(deleteQuery.getIndex()) ? deleteQuery.getIndex()
685+
String indexName = StringUtils.hasText(deleteQuery.getIndex()) ? deleteQuery.getIndex()
670686
: getPersistentEntityFor(clazz).getIndexName();
671-
String typeName = isNotBlank(deleteQuery.getType()) ? deleteQuery.getType()
687+
String typeName = StringUtils.hasText(deleteQuery.getType()) ? deleteQuery.getType()
672688
: getPersistentEntityFor(clazz).getIndexType();
673689
Integer pageSize = deleteQuery.getPageSize() != null ? deleteQuery.getPageSize() : 1000;
674690
Long scrollTimeInMillis = deleteQuery.getScrollTimeInMillis() != null ? deleteQuery.getScrollTimeInMillis()
@@ -825,8 +841,8 @@ public void clearScroll(String scrollId) {
825841
public <T> Page<T> moreLikeThis(MoreLikeThisQuery query, Class<T> clazz) {
826842

827843
ElasticsearchPersistentEntity persistentEntity = getPersistentEntityFor(clazz);
828-
String indexName = isNotBlank(query.getIndexName()) ? query.getIndexName() : persistentEntity.getIndexName();
829-
String type = isNotBlank(query.getType()) ? query.getType() : persistentEntity.getIndexType();
844+
String indexName = StringUtils.hasText(query.getIndexName()) ? query.getIndexName() : persistentEntity.getIndexName();
845+
String type = StringUtils.hasText(query.getType()) ? query.getType() : persistentEntity.getIndexType();
830846

831847
Assert.notNull(indexName, "No 'indexName' defined for MoreLikeThisQuery");
832848
Assert.notNull(type, "No 'type' defined for MoreLikeThisQuery");
@@ -921,9 +937,9 @@ private <T> boolean createIndexIfNotCreated(Class<T> clazz) {
921937
private <T> boolean createIndexWithSettings(Class<T> clazz) {
922938
if (clazz.isAnnotationPresent(Setting.class)) {
923939
String settingPath = clazz.getAnnotation(Setting.class).settingPath();
924-
if (isNotBlank(settingPath)) {
940+
if (StringUtils.hasText(settingPath)) {
925941
String settings = readFileFromClasspath(settingPath);
926-
if (isNotBlank(settings)) {
942+
if (StringUtils.hasText(settings)) {
927943
return createIndex(getPersistentEntityFor(clazz).getIndexName(), settings);
928944
}
929945
} else {
@@ -1026,15 +1042,15 @@ else if (order.getNullHandling() == Sort.NullHandling.NULLS_LAST) {
10261042

10271043
private IndexRequestBuilder prepareIndex(IndexQuery query) {
10281044
try {
1029-
String indexName = isBlank(query.getIndexName())
1045+
String indexName = StringUtils.isEmpty(query.getIndexName())
10301046
? retrieveIndexNameFromPersistentEntity(query.getObject().getClass())[0] : query.getIndexName();
1031-
String type = isBlank(query.getType()) ? retrieveTypeFromPersistentEntity(query.getObject().getClass())[0]
1047+
String type = StringUtils.isEmpty(query.getType()) ? retrieveTypeFromPersistentEntity(query.getObject().getClass())[0]
10321048
: query.getType();
10331049

10341050
IndexRequestBuilder indexRequestBuilder = null;
10351051

10361052
if (query.getObject() != null) {
1037-
String id = isBlank(query.getId()) ? getPersistentEntityId(query.getObject()) : query.getId();
1053+
String id = StringUtils.isEmpty(query.getId()) ? getPersistentEntityId(query.getObject()) : query.getId();
10381054
// If we have a query id and a document id, do not ask ES to generate one.
10391055
if (id != null) {
10401056
indexRequestBuilder = client.prepareIndex(indexName, type, id);
@@ -1084,11 +1100,11 @@ public Boolean addAlias(AliasQuery query) {
10841100
aliasAction.filter(query.getFilterBuilder());
10851101
} else if (query.getFilter() != null) {
10861102
aliasAction.filter(query.getFilter());
1087-
} else if (isNotBlank(query.getRouting())) {
1103+
} else if (StringUtils.hasText(query.getRouting())) {
10881104
aliasAction.routing(query.getRouting());
1089-
} else if (isNotBlank(query.getSearchRouting())) {
1105+
} else if (StringUtils.hasText(query.getSearchRouting())) {
10901106
aliasAction.searchRouting(query.getSearchRouting());
1091-
} else if (isNotBlank(query.getIndexRouting())) {
1107+
} else if (StringUtils.hasText(query.getIndexRouting())) {
10921108
aliasAction.indexRouting(query.getIndexRouting());
10931109
}
10941110
return client.admin().indices().prepareAliases().addAliasAction(aliasAction).execute().actionGet().isAcknowledged();

src/main/java/org/springframework/data/elasticsearch/core/MappingBuilder.java

+12-10
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,15 @@
1515
*/
1616
package org.springframework.data.elasticsearch.core;
1717

18+
import static org.elasticsearch.common.xcontent.XContentFactory.*;
19+
import static org.springframework.util.StringUtils.*;
20+
1821
import java.io.IOException;
1922
import java.util.ArrayList;
2023
import java.util.Arrays;
2124
import java.util.List;
2225
import java.util.Map;
26+
2327
import org.elasticsearch.common.xcontent.XContentBuilder;
2428
import org.springframework.core.ResolvableType;
2529
import org.springframework.core.io.ClassPathResource;
@@ -37,9 +41,7 @@
3741
import org.springframework.data.mapping.model.SimpleTypeHolder;
3842
import org.springframework.data.util.ClassTypeInformation;
3943
import org.springframework.data.util.TypeInformation;
40-
import static org.apache.commons.lang.StringUtils.*;
41-
import static org.elasticsearch.common.xcontent.XContentFactory.*;
42-
import static org.springframework.util.StringUtils.*;
44+
import org.springframework.util.StringUtils;
4345

4446
/**
4547
* @author Rizwan Idrees
@@ -88,7 +90,7 @@ static XContentBuilder buildMapping(Class clazz, String indexType, String idFiel
8890
// Properties
8991
XContentBuilder xContentBuilder = mapping.startObject(FIELD_PROPERTIES);
9092

91-
mapEntity(xContentBuilder, clazz, true, idFieldName, EMPTY, false, FieldType.Auto, null);
93+
mapEntity(xContentBuilder, clazz, true, idFieldName, "", false, FieldType.Auto, null);
9294

9395
return xContentBuilder.endObject().endObject().endObject();
9496
}
@@ -119,7 +121,7 @@ private static void mapEntity(XContentBuilder xContentBuilder, Class clazz, bool
119121

120122
if (field.isAnnotationPresent(Mapping.class)) {
121123
String mappingPath = field.getAnnotation(Mapping.class).mappingPath();
122-
if (isNotBlank(mappingPath)) {
124+
if (StringUtils.hasText(mappingPath)) {
123125
ClassPathResource mappings = new ClassPathResource(mappingPath);
124126
if (mappings.exists()) {
125127
xContentBuilder.rawField(field.getName(), mappings.getInputStream());
@@ -137,7 +139,7 @@ private static void mapEntity(XContentBuilder xContentBuilder, Class clazz, bool
137139
continue;
138140
}
139141
boolean nestedOrObject = isNestedOrObjectField(field);
140-
mapEntity(xContentBuilder, getFieldType(field), false, EMPTY, field.getName(), nestedOrObject, singleField.type(), field.getAnnotation(Field.class));
142+
mapEntity(xContentBuilder, getFieldType(field), false, "", field.getName(), nestedOrObject, singleField.type(), field.getAnnotation(Field.class));
141143
if (nestedOrObject) {
142144
continue;
143145
}
@@ -203,10 +205,10 @@ private static void applyCompletionFieldMapping(XContentBuilder xContentBuilder,
203205
xContentBuilder.field(COMPLETION_MAX_INPUT_LENGTH, annotation.maxInputLength());
204206
xContentBuilder.field(COMPLETION_PRESERVE_POSITION_INCREMENTS, annotation.preservePositionIncrements());
205207
xContentBuilder.field(COMPLETION_PRESERVE_SEPARATORS, annotation.preserveSeparators());
206-
if (isNotBlank(annotation.searchAnalyzer())) {
208+
if (StringUtils.hasText(annotation.searchAnalyzer())) {
207209
xContentBuilder.field(FIELD_SEARCH_ANALYZER, annotation.searchAnalyzer());
208210
}
209-
if (isNotBlank(annotation.analyzer())) {
211+
if (StringUtils.hasText(annotation.analyzer())) {
210212
xContentBuilder.field(FIELD_INDEX_ANALYZER, annotation.analyzer());
211213
}
212214
}
@@ -311,10 +313,10 @@ private static void addFieldMappingParameters(XContentBuilder builder, Object an
311313
if (!index) {
312314
builder.field(FIELD_INDEX, index);
313315
}
314-
if (isNotBlank(analyzer)) {
316+
if (StringUtils.hasText(analyzer)) {
315317
builder.field(FIELD_INDEX_ANALYZER, analyzer);
316318
}
317-
if (isNotBlank(searchAnalyzer)) {
319+
if (StringUtils.hasText(searchAnalyzer)) {
318320
builder.field(FIELD_SEARCH_ANALYZER, searchAnalyzer);
319321
}
320322
}

src/main/java/org/springframework/data/elasticsearch/core/facet/request/HistogramFacetRequest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@
1616

1717
package org.springframework.data.elasticsearch.core.facet.request;
1818

19-
import org.apache.commons.lang.StringUtils;
2019
import org.elasticsearch.search.aggregations.AbstractAggregationBuilder;
2120
import org.elasticsearch.search.aggregations.AggregationBuilders;
2221
import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramAggregationBuilder;
2322
import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramInterval;
2423
import org.springframework.data.elasticsearch.core.facet.AbstractFacetRequest;
2524
import org.springframework.util.Assert;
25+
import org.springframework.util.StringUtils;
2626

2727

2828
/**
@@ -54,7 +54,7 @@ public void setTimeUnit(DateHistogramInterval timeUnit) {
5454

5555
public AbstractAggregationBuilder getFacet() {
5656
Assert.notNull(getName(), "Facet name can't be a null !!!");
57-
Assert.isTrue(StringUtils.isNotBlank(field), "Please select field on which to build the facet !!!");
57+
Assert.isTrue(StringUtils.hasText(field), "Please select field on which to build the facet !!!");
5858
Assert.isTrue(interval > 0, "Please provide interval as positive value greater them zero !!!");
5959

6060
DateHistogramAggregationBuilder dateHistogramBuilder = AggregationBuilders.dateHistogram(getName());

0 commit comments

Comments
 (0)