Skip to content

Commit 2789a51

Browse files
general code cleanup to remove most IntelliJ warnings (#16)
1 parent ed02e86 commit 2789a51

22 files changed

+101
-124
lines changed

src/main/java/com/emc/rest/smart/Host.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,13 @@
4242
* </ul>
4343
*/
4444
public class Host implements HostStats {
45-
4645
private static final Logger log = LoggerFactory.getLogger(Host.class);
4746

4847
public static final int DEFAULT_ERROR_WAIT_MS = 1500;
4948
public static final int LOG_DELAY = 60000; // 1 minute
5049
public static final int MAX_COOL_DOWN_EXP = 4;
5150

52-
private String name;
51+
private final String name;
5352
private boolean healthy = true;
5453
protected int errorWaitTime = DEFAULT_ERROR_WAIT_MS;
5554

@@ -81,7 +80,7 @@ public synchronized void connectionClosed() {
8180
if (openConnections < 0) {
8281
long currentTime = System.currentTimeMillis();
8382
if (currentTime - lastLogTime > LOG_DELAY) {
84-
log.warn("openConnections for host {} is {} !", this.toString(), Integer.toString(openConnections));
83+
log.warn("openConnections for host {} is {} !", this, openConnections);
8584
lastLogTime = currentTime;
8685
}
8786
}
@@ -174,7 +173,7 @@ public int hashCode() {
174173
@Override
175174
public String toString() {
176175
return String.format("%s{totalConnections=%d, totalErrors=%d, openConnections=%d, lastConnectionTime=%s}",
177-
name, totalConnections, totalErrors, openConnections, new Date(lastConnectionTime).toString());
176+
name, totalConnections, totalErrors, openConnections, new Date(lastConnectionTime));
178177
}
179178

180179
public int getErrorWaitTime() {

src/main/java/com/emc/rest/smart/HostStats.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,11 @@
2929
import java.util.Date;
3030

3131
public interface HostStats {
32-
3332
long getTotalConnections();
3433

35-
3634
long getTotalErrors();
3735

38-
3936
int getOpenConnections();
4037

41-
4238
Date getLastConnectionTime();
43-
4439
}

src/main/java/com/emc/rest/smart/LoadBalancer.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
import java.util.*;
3030

3131
public class LoadBalancer {
32-
private final Deque<Host> hosts = new ArrayDeque<Host>();
32+
private final Deque<Host> hosts = new ArrayDeque<>();
3333
private List<HostVetoRule> vetoRules;
3434

3535
public LoadBalancer(List<Host> initialHosts) {
@@ -92,14 +92,14 @@ protected boolean shouldVeto(Host host, Map<String, Object> requestProperties) {
9292
* Returns a list of all known hosts. This list is a clone; modification will not affect the load balancer
9393
*/
9494
public synchronized List<Host> getAllHosts() {
95-
return new ArrayList<Host>(hosts);
95+
return new ArrayList<>(hosts);
9696
}
9797

9898
/**
9999
* Returns stats for all active hosts in this load balancer
100100
*/
101101
public synchronized HostStats[] getHostStats() {
102-
return hosts.toArray(new HostStats[hosts.size()]);
102+
return hosts.toArray(new HostStats[0]);
103103
}
104104

105105
/**
@@ -138,9 +138,9 @@ public long getOpenConnections() {
138138
/**
139139
* Ensure this method is called sparingly as it will block getTopHost() calls, pausing all new connections!
140140
*/
141-
protected void updateHosts(List<Host> updatedHosts) throws Exception {
141+
protected void updateHosts(List<Host> updatedHosts) {
142142
// don't modify the parameter
143-
List<Host> hostList = new ArrayList<Host>(updatedHosts);
143+
List<Host> hostList = new ArrayList<>(updatedHosts);
144144

145145
// remove hosts from stored list that are not present in updated list
146146
// remove hosts in updated list that are already present in stored list
@@ -166,9 +166,7 @@ protected void updateHosts(List<Host> updatedHosts) throws Exception {
166166
}
167167

168168
// what's left in the updated list are new hosts, so add them
169-
for (Host newHost : hostList) {
170-
hosts.add(newHost);
171-
}
169+
hosts.addAll(hostList);
172170
}
173171
}
174172

src/main/java/com/emc/rest/smart/OctetStreamXmlProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
@Produces("application/octet-stream")
4949
@Consumes("application/octet-stream")
5050
public class OctetStreamXmlProvider implements MessageBodyReader<Object> {
51-
private MessageBodyReader<Object> delegate;
51+
private final MessageBodyReader<Object> delegate;
5252

5353
public OctetStreamXmlProvider(@Context Injectable<SAXParserFactory> spf, @Context Providers ps) {
5454
this.delegate = new XMLRootElementProvider.General(spf, ps);

src/main/java/com/emc/rest/smart/PollingDaemon.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public class PollingDaemon extends Thread {
3737

3838
private static final Logger log = LoggerFactory.getLogger(PollingDaemon.class);
3939

40-
private SmartConfig smartConfig;
40+
private final SmartConfig smartConfig;
4141
private boolean running = true;
4242

4343
public PollingDaemon(SmartConfig smartConfig) {
@@ -85,9 +85,9 @@ public void run() {
8585

8686
long callTime = System.currentTimeMillis() - start;
8787
try {
88-
long sleepTime = smartConfig.getPollInterval() * 1000 - callTime;
88+
long sleepTime = smartConfig.getPollInterval() * 1000L - callTime;
8989
if (sleepTime < 0) sleepTime = 0;
90-
log.debug("polling daemon finished; will poll again in {}ms..", Long.toString(sleepTime));
90+
log.debug("polling daemon finished; will poll again in {}ms..", sleepTime);
9191
if (sleepTime > 0) Thread.sleep(sleepTime);
9292
} catch (InterruptedException e) {
9393
log.warn("interrupted while sleeping", e);

src/main/java/com/emc/rest/smart/SizeOverrideWriter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
import java.lang.reflect.Type;
4242

4343
public class SizeOverrideWriter<T> implements MessageBodyWriter<T> {
44-
private static final ThreadLocal<Long> entitySize = new ThreadLocal<Long>();
44+
private static final ThreadLocal<Long> entitySize = new ThreadLocal<>();
4545

4646
public static Long getEntitySize() {
4747
return entitySize.get();
@@ -51,7 +51,7 @@ public static void setEntitySize(Long size) {
5151
entitySize.set(size);
5252
}
5353

54-
private MessageBodyWriter<T> delegate;
54+
private final MessageBodyWriter<T> delegate;
5555

5656
public SizeOverrideWriter(MessageBodyWriter<T> delegate) {
5757
this.delegate = delegate;

src/main/java/com/emc/rest/smart/SmartClientFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ static ApacheHttpClient4Handler createApacheClientHandler(SmartConfig smartConfi
147147
ClientConfig clientConfig = new DefaultClientConfig();
148148

149149
// set up multi-threaded connection pool
150-
org.apache.http.impl.conn.PoolingClientConnectionManager connectionManager = new org.apache.http.impl.conn.PoolingClientConnectionManager();
150+
org.apache.http.impl.conn.PoolingHttpClientConnectionManager connectionManager = new org.apache.http.impl.conn.PoolingHttpClientConnectionManager();
151151
// 999 maximum active connections (max allowed)
152152
connectionManager.setDefaultMaxPerRoute(999);
153153
connectionManager.setMaxTotal(999);

src/main/java/com/emc/rest/smart/SmartConfig.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,19 @@ public class SmartConfig {
4242
private String proxyUser;
4343
private String proxyPass;
4444

45-
private LoadBalancer loadBalancer;
45+
private final LoadBalancer loadBalancer;
4646
private HostListProvider hostListProvider;
4747
private int pollInterval = DEFAULT_POLL_INTERVAL;
4848
private boolean hostUpdateEnabled = true;
4949
private boolean healthCheckEnabled = true;
5050

51-
private Map<String, Object> properties = new HashMap<String, Object>();
51+
private final Map<String, Object> properties = new HashMap<>();
5252

5353
/**
5454
* @see #SmartConfig(LoadBalancer)
5555
*/
5656
public SmartConfig(String... initialHostNames) {
57-
List<Host> hostList = new ArrayList<Host>();
57+
List<Host> hostList = new ArrayList<>();
5858
for (String hostName : initialHostNames) {
5959
hostList.add(new Host(hostName));
6060
}

src/main/java/com/emc/rest/smart/SmartFilter.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
public class SmartFilter extends ClientFilter {
4141
public static final String BYPASS_LOAD_BALANCER = "com.emc.rest.smart.bypassLoadBalancer";
4242

43-
private SmartConfig smartConfig;
43+
private final SmartConfig smartConfig;
4444

4545
public SmartFilter(SmartConfig smartConfig) {
4646
this.smartConfig = smartConfig;
@@ -75,12 +75,8 @@ public ClientResponse handle(ClientRequest request) throws ClientHandlerExceptio
7575
ClientResponse response = getNext().handle(request);
7676

7777
// capture request stats
78-
if (response.getStatus() >= 500 && response.getStatus() != 501) {
79-
// except for 501 (not implemented), all 50x responses are considered server errors
80-
host.callComplete(true);
81-
} else {
82-
host.callComplete(false);
83-
}
78+
// except for 501 (not implemented), all 50x responses are considered server errors
79+
host.callComplete(response.getStatus() >= 500 && response.getStatus() != 501);
8480

8581
// wrap the input stream so we can capture the actual connection close
8682
response.setEntityInputStream(new WrappedInputStream(response.getEntityInputStream(), host));
@@ -99,8 +95,8 @@ public ClientResponse handle(ClientRequest request) throws ClientHandlerExceptio
9995
/**
10096
* captures closure in host statistics
10197
*/
102-
protected class WrappedInputStream extends FilterInputStream {
103-
private Host host;
98+
protected static class WrappedInputStream extends FilterInputStream {
99+
private final Host host;
104100
private boolean closed = false;
105101

106102
public WrappedInputStream(InputStream in, Host host) {

src/main/java/com/emc/rest/smart/ecs/EcsHostListProvider.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import javax.crypto.spec.SecretKeySpec;
4040
import java.net.URI;
4141
import java.net.URISyntaxException;
42+
import java.nio.charset.StandardCharsets;
4243
import java.text.SimpleDateFormat;
4344
import java.util.*;
4445

@@ -50,10 +51,10 @@ public class EcsHostListProvider implements HostListProvider {
5051
public static final int DEFAULT_PORT = 9021;
5152

5253
protected final SimpleDateFormat rfc822DateFormat;
53-
private Client client;
54-
private LoadBalancer loadBalancer;
55-
private String user;
56-
private String secret;
54+
private final Client client;
55+
private final LoadBalancer loadBalancer;
56+
private final String user;
57+
private final String secret;
5758
private String protocol = DEFAULT_PROTOCOL;
5859
private int port = DEFAULT_PORT;
5960
private List<Vdc> vdcs;
@@ -70,7 +71,7 @@ public EcsHostListProvider(Client client, LoadBalancer loadBalancer, String user
7071
public List<Host> getHostList() {
7172
if (vdcs == null || vdcs.isEmpty()) return getDataNodes(loadBalancer.getTopHost(null));
7273

73-
List<Host> hostList = new ArrayList<Host>();
74+
List<Host> hostList = new ArrayList<>();
7475

7576
for (Vdc vdc : vdcs) {
7677
if (vdc.getHosts().isEmpty()) log.warn("VDC " + vdc.getName() + " has no hosts!");
@@ -111,8 +112,7 @@ public void runHealthCheck(Host host) {
111112
PingItem pingItem = response.getPingItemMap().get(PingItem.MAINTENANCE_MODE);
112113
if (pingItem != null) status = pingItem.getStatus();
113114
}
114-
if (status == PingItem.Status.ON) ((VdcHost) host).setMaintenanceMode(true);
115-
else ((VdcHost) host).setMaintenanceMode(false);
115+
((VdcHost) host).setMaintenanceMode(status == PingItem.Status.ON);
116116
}
117117
}
118118

@@ -153,7 +153,7 @@ protected List<Host> getDataNodes(Host host) {
153153
log.debug("retrieving VDC node list from {}", host.getName());
154154
List<String> dataNodes = request.get(ListDataNode.class).getDataNodes();
155155

156-
List<Host> hosts = new ArrayList<Host>();
156+
List<Host> hosts = new ArrayList<>();
157157
for (String node : dataNodes) {
158158
hosts.add(new Host(node));
159159
}
@@ -171,8 +171,8 @@ protected URI getRequestUri(Host host, String path) {
171171

172172
protected String getSignature(String canonicalString, String secret) throws Exception {
173173
Mac mac = Mac.getInstance("HmacSHA1");
174-
mac.init(new SecretKeySpec(secret.getBytes("UTF-8"), "HmacSHA1"));
175-
String signature = new String(Base64.encodeBase64(mac.doFinal(canonicalString.getBytes("UTF-8"))));
174+
mac.init(new SecretKeySpec(secret.getBytes(StandardCharsets.UTF_8), "HmacSHA1"));
175+
String signature = new String(Base64.encodeBase64(mac.doFinal(canonicalString.getBytes(StandardCharsets.UTF_8))));
176176
log.debug("canonicalString:\n" + canonicalString);
177177
log.debug("signature:\n" + signature);
178178
return signature;
@@ -182,7 +182,7 @@ protected void updateVdcNodes(Vdc vdc, List<Host> nodeList) {
182182
if (nodeList == null || nodeList.isEmpty()) throw new RuntimeException("node list is empty");
183183

184184
// make sure the hosts are associated with the VDC first
185-
List<VdcHost> vdcNodeList = new ArrayList<VdcHost>();
185+
List<VdcHost> vdcNodeList = new ArrayList<>();
186186
for (Host host : nodeList) {
187187
vdcNodeList.add(new VdcHost(vdc, host.getName()));
188188
}

0 commit comments

Comments
 (0)