Skip to content

Commit a7615c1

Browse files
author
ehennum
committed
fixup from static analysis #1159
1 parent 0c4f958 commit a7615c1

16 files changed

+78
-74
lines changed

marklogic-client-api/src/main/java/com/marklogic/client/DatabaseClientFactory.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,14 +158,17 @@ public HostnameVerifierAdapter(SSLHostnameVerifier verifier) {
158158
/**
159159
* verify method verifies the incoming hostname and SSLSession.
160160
* @param hostname denotes the hostname.
161-
* @param session represents the SSLSession containing the pper certificates.
161+
* @param session represents the SSLSession containing the peer certificates.
162162
* @return true if the hostname and peer certificates are valid and false if they are invalid.
163163
* */
164164
@Override
165165
public boolean verify(String hostname, SSLSession session) {
166166
try {
167167
Certificate[] certificates = session.getPeerCertificates();
168-
verify(hostname, (X509Certificate) certificates[0]);
168+
verify(
169+
hostname,
170+
(X509Certificate) ((certificates == null || certificates.length == 0) ? null : certificates[0])
171+
);
169172
return true;
170173
} catch(SSLException e) {
171174
return false;
@@ -1496,11 +1499,11 @@ static public class Bean implements Serializable {
14961499
private String password;
14971500
private Authentication authentication;
14981501
private String externalName;
1499-
private SecurityContext securityContext;
15001502
private DatabaseClient.ConnectionType connectionType;
1501-
private HandleFactoryRegistry handleRegistry =
1502-
HandleFactoryRegistryImpl.newDefault();
15031503

1504+
transient private SecurityContext securityContext;
1505+
transient private HandleFactoryRegistry handleRegistry =
1506+
HandleFactoryRegistryImpl.newDefault();
15041507
transient private SSLContext context;
15051508
transient private SSLHostnameVerifier verifier;
15061509

marklogic-client-api/src/main/java/com/marklogic/client/bitemporal/TemporalDocumentManager.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,7 @@
2121
import javax.xml.bind.DatatypeConverter;
2222
import javax.xml.datatype.Duration;
2323

24-
import com.marklogic.client.FailedRequestException;
25-
import com.marklogic.client.ForbiddenUserException;
26-
import com.marklogic.client.ResourceNotFoundException;
27-
import com.marklogic.client.Transaction;
24+
import com.marklogic.client.*;
2825
import com.marklogic.client.bitemporal.TemporalDescriptor;
2926
import com.marklogic.client.document.DocumentDescriptor;
3027
import com.marklogic.client.document.DocumentManager;
@@ -68,8 +65,9 @@ public String toString() {
6865
return "noWipe";
6966
case NOUPDATE:
7067
return "noUpdate";
68+
default:
69+
throw new MarkLogicInternalException("Unknown enumeration");
7170
}
72-
return null;
7371
}
7472
}
7573

marklogic-client-api/src/main/java/com/marklogic/client/datamovement/DeleteListener.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.slf4j.LoggerFactory;
2020

2121
import java.util.ArrayList;
22+
import java.util.Iterator;
2223
import java.util.List;
2324

2425
/**
@@ -97,6 +98,9 @@ public void initializeListener(QueryBatcher queryBatcher) {
9798
@Override
9899
public void processEvent(QueryBatch batch) {
99100
try {
101+
if (batch.getClient() == null) {
102+
throw new IllegalStateException("null DatabaseClient");
103+
}
100104
batch.getClient().newDocumentManager().delete( batch.getItems() );
101105
} catch (Throwable t) {
102106
for ( BatchFailureListener<Batch<String>> listener : failureListeners ) {

marklogic-client-api/src/main/java/com/marklogic/client/datamovement/ExportListener.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,12 @@
2424
import com.marklogic.client.io.Format;
2525
import com.marklogic.client.query.QueryManager;
2626

27+
import com.marklogic.client.query.StructuredQueryDefinition;
2728
import org.slf4j.Logger;
2829
import org.slf4j.LoggerFactory;
2930

30-
import java.util.ArrayList;
31-
import java.util.HashSet;
31+
import java.util.*;
3232
import java.util.function.Consumer;
33-
import java.util.List;
34-
import java.util.Set;
3533

3634
/**
3735
* <p>Reads document contents (and optionally metadata) for each batch, then sends
@@ -87,6 +85,9 @@ public ExportListener() {
8785
}
8886

8987
protected DocumentPage getDocs(QueryBatch batch) {
88+
if (batch.getClient() == null) {
89+
throw new IllegalStateException("null DatabaseClient");
90+
}
9091
GenericDocumentManager docMgr = batch.getClient().newDocumentManager();
9192
if ( view != null ) docMgr.setSearchView(view);
9293
if ( categories != null ) docMgr.setMetadataCategories(categories);

marklogic-client-api/src/main/java/com/marklogic/client/datamovement/ExtractRowsViaTemplateListener.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,9 @@ public ExtractRowsViaTemplateListener onFailure(BatchFailureListener<QueryBatch>
139139

140140
@Override
141141
public void initializeListener(QueryBatcher queryBatcher) {
142+
if (queryBatcher.getPrimaryClient() == null) {
143+
throw new IllegalStateException("null DatabaseClient");
144+
}
142145
pb = queryBatcher.getPrimaryClient().newRowManager().newPlanBuilder();
143146
}
144147

@@ -185,6 +188,9 @@ public void processEvent(QueryBatch batch) {
185188
* create an iterator out of it.
186189
*/
187190
private Iterable<TypedRow> getTypedRows(QueryBatch batch) throws IOException {
191+
if (batch.getClient() == null) {
192+
throw new IllegalStateException("null DatabaseClient");
193+
}
188194
StringHandle uris = new StringHandle(String.join("\n", batch.getItems()))
189195
.withMimetype("text/uri-list");
190196
RESTServices services = ((DatabaseClientImpl) batch.getClient()).getServices();

marklogic-client-api/src/main/java/com/marklogic/client/datamovement/HostAvailabilityListener.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
package com.marklogic.client.datamovement;
1717

1818
import com.marklogic.client.DatabaseClient;
19-
import com.marklogic.client.FailedRetryException;
2019
import org.slf4j.Logger;
2120
import org.slf4j.LoggerFactory;
2221

@@ -26,11 +25,7 @@
2625
import java.net.UnknownHostException;
2726
import javax.net.ssl.SSLException;
2827
import java.time.Duration;
29-
import java.util.ArrayList;
30-
import java.util.Arrays;
31-
import java.util.HashSet;
32-
import java.util.List;
33-
import java.util.Set;
28+
import java.util.*;
3429
import java.util.stream.Collectors;
3530
import java.util.stream.Stream;
3631
import java.util.concurrent.Executors;
@@ -83,6 +78,9 @@ public RetryListener(QueryBatchListener queryBatchListener) {
8378

8479
@Override
8580
public void processFailure(QueryBatch batch, Throwable throwable) {
81+
if (batch.getClient() == null) {
82+
throw new IllegalStateException("null DatabaseClient");
83+
}
8684
boolean isHostUnavailableException = processException(batch.getBatcher(), throwable, batch.getClient().getHost());
8785
if ( isHostUnavailableException == true ) {
8886
try {
@@ -191,6 +189,9 @@ public int getMinHosts() {
191189
* @param throwable the exception
192190
*/
193191
public void processFailure(WriteBatch batch, Throwable throwable) {
192+
if (batch.getClient() == null) {
193+
throw new IllegalStateException("null DatabaseClient");
194+
}
194195
boolean isHostUnavailableException = processException(batch.getBatcher(), throwable, batch.getClient().getHost());
195196
if ( isHostUnavailableException == true ) {
196197
try {
@@ -211,6 +212,9 @@ public void processFailure(WriteBatch batch, Throwable throwable) {
211212
* @param queryBatch the exception with information about the failed query attempt
212213
*/
213214
public void processFailure(QueryBatchException queryBatch) {
215+
if (queryBatch.getClient() == null) {
216+
throw new IllegalStateException("null DatabaseClient");
217+
}
214218
boolean isHostUnavailableException = processException(queryBatch.getBatcher(), queryBatch, queryBatch.getClient().getHost());
215219
if ( isHostUnavailableException == true ) {
216220
try {
@@ -271,7 +275,7 @@ private boolean processForestHostException(Batcher batcher, Throwable throwable,
271275
List<String> availableHosts = Stream.of(preferredHosts)
272276
.filter( (availableHost) -> ! availableHost.equals(host) )
273277
.collect(Collectors.toList());
274-
int randomPos = Math.abs(host.hashCode()) % availableHosts.size();
278+
int randomPos = new Random().nextInt(availableHosts.size());
275279
String randomAvailableHost = availableHosts.get(randomPos);
276280
filteredForestConfig = filteredForestConfig.withRenamedHost(host, randomAvailableHost);
277281
}

marklogic-client-api/src/main/java/com/marklogic/client/datamovement/impl/BatchEventImpl.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ public Calendar getTimestamp() {
4747
}
4848

4949
public BatchEventImpl withTimestamp(Calendar timestamp) {
50+
if (timestamp == null) {
51+
timestamp = Calendar.getInstance();
52+
}
5053
this.timestamp = timestamp;
5154
return this;
5255
}

marklogic-client-api/src/main/java/com/marklogic/client/datamovement/impl/BatcherImpl.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ public abstract class BatcherImpl implements Batcher {
2626
private int batchSize = 100;
2727
private int threadCount = 1;
2828
private ForestConfiguration forestConfig;
29-
private DatabaseClient client;
3029
private DataMovementManagerImpl moveMgr;
3130

3231
protected BatcherImpl(DataMovementManager moveMgr){

marklogic-client-api/src/main/java/com/marklogic/client/datamovement/impl/JobReportImpl.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,16 @@ public class JobReportImpl implements JobReport {
3333
static public JobReportImpl about(JobTicketImpl ticket) {
3434
BatcherImpl batcher = ticket.getBatcher();
3535
JobTicket.JobType jobType = ticket.getJobType();
36+
if (jobType == null) {
37+
throw new InternalError("null job type");
38+
}
3639
switch(jobType) {
3740
case QUERY_BATCHER:
3841
return new JobReportImpl((QueryBatcher) batcher);
3942
case WRITE_BATCHER:
4043
return new JobReportImpl((WriteBatcher) batcher);
4144
default:
42-
throw new InternalError(
43-
(jobType == null) ?
44-
"null job type" :
45-
"unknown job type: "+jobType.name()
46-
);
45+
throw new InternalError("unknown job type: "+jobType.name());
4746
}
4847
}
4948

marklogic-client-api/src/main/java/com/marklogic/client/datamovement/impl/JobTicketImpl.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,15 @@ public JobType getJobType() {
3939

4040
@Override
4141
public BatcherImpl getBatcher() {
42-
switch(this.jobType) {
42+
JobType jobType = getJobType();
43+
if (jobType == null) {
44+
throw new InternalError("null job type");
45+
}
46+
switch(jobType) {
4347
case QUERY_BATCHER: return getQueryBatcher();
4448
case WRITE_BATCHER: return getWriteBatcher();
4549
default:
46-
throw new InternalError(
47-
(jobType == null) ?
48-
"null job type" :
49-
"unknown job type: "+jobType.name()
50-
);
50+
throw new InternalError("unknown job type: "+jobType.name());
5151
}
5252
}
5353

0 commit comments

Comments
 (0)