Skip to content

Commit

Permalink
Cleanup and fixes based on FindBugs analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
csadilek committed Jul 5, 2016
1 parent 515cd32 commit 76d075d
Show file tree
Hide file tree
Showing 36 changed files with 478 additions and 695 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ private Hash() {
hashCounter = random.nextInt();
random.nextBytes(seed = new byte[512]);
}
catch (NoSuchAlgorithmException e) {
catch (final NoSuchAlgorithmException e) {
throw new RuntimeException("runtime does not support secure random algorithm: " + secureRandomAlgorithm);
}
}

public static byte[] getRandomBytes(final byte[] bytes) {
if (hashCounter < 0) {
hashCounter -= hashCounter;
hashCounter = 0;
}
for (int i = 0; i < bytes.length; i++) {
hashCounter++;
Expand Down Expand Up @@ -82,7 +82,7 @@ private static String nextSecureHash(final String algorithm, final byte[] additi
md.update(additionalSeed);
}

byte[] randBytes = new byte[32];
final byte[] randBytes = new byte[32];
getRandomBytes(randBytes);

// 1,000 rounds.
Expand All @@ -92,14 +92,14 @@ private static String nextSecureHash(final String algorithm, final byte[] additi

return hashToHexString(md.digest());
}
catch (Exception e) {
catch (final Exception e) {
throw new RuntimeException("failed to generate session id hash", e);
}
}

public static String hashToHexString(byte[] hash) {
final StringBuilder hexString = new StringBuilder(hash.length);
for (byte mdbyte : hash) {
for (final byte mdbyte : hash) {
hexString.append(Integer.toHexString(0xFF & mdbyte));
}
return hexString.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,7 @@ public boolean error(Object message, Throwable throwable) {

@Override
public void setErrorCallback(ErrorCallback errorCallback) {
if (errorCallback == null) {
errorCallback = defaultErrorCallback;
}
else {
if (errorCallback != null) {
this.errorCallback = errorCallback;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,10 @@ protected AbstractRPCMethodCallback(final ServiceInstanceProvider genericSvc,
}

public Object invokeMethodFromMessage(Message message) {
@SuppressWarnings("unchecked")
final List<Object> parms = message.get(List.class, "MethodParms");

if ((parms == null && targetTypes.length != 0) || (parms.size() != targetTypes.length)) {
if ((parms == null && targetTypes.length != 0) || (parms != null && parms.size() != targetTypes.length)) {
throw new MessageDeliveryFailure(
"wrong number of arguments sent to endpoint. (received: "
+ (parms == null ? 0 : parms.size())
Expand All @@ -61,19 +62,19 @@ public Object invokeMethodFromMessage(Message message) {

try {
RpcContext.set(message);
return method.invoke(serviceProvider.get(message), parms.toArray(new Object[parms.size()]));
return method.invoke(serviceProvider.get(message), (parms != null) ? parms.toArray(new Object[parms.size()]) : new Object[0]);
}
catch (QueueUnavailableException e) {
catch (final QueueUnavailableException e) {
throw e;
}
catch (MessageDeliveryFailure e) {
catch (final MessageDeliveryFailure e) {
throw e;
}
catch (InvocationTargetException e) {
catch (final InvocationTargetException e) {
log.debug("RPC endpoint threw exception:", e.getCause());
throw new MessageDeliveryFailure("error invoking RPC endpoint " + method, e.getCause(), true);
}
catch (Exception e) {
catch (final Exception e) {
throw new MessageDeliveryFailure("error invoking endpoint", e);
}
finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@

package org.jboss.errai.bus.server.io;

import java.lang.reflect.Method;
import java.util.List;

import org.jboss.errai.bus.client.api.base.MessageDeliveryFailure;
import org.jboss.errai.bus.client.api.messaging.Message;
import org.jboss.errai.bus.client.api.messaging.MessageCallback;
import org.jboss.errai.bus.client.api.base.MessageDeliveryFailure;
import org.mvel2.DataConversion;

import java.lang.reflect.Method;
import java.util.List;

/**
* <tt>EndpointCallback</tt> is a callback function for a message being sent. It invokes the endpoint function
* specified
Expand Down Expand Up @@ -52,11 +52,12 @@ public EndpointCallback(final Object genericSvc, final Method method) {
* @param message
* - the message
*/
@Override
@SuppressWarnings("unchecked")
public void callback(final Message message) {
final List<Object> parms = message.get(List.class, "MethodParms");

if ((parms == null && targetTypes.length != 0) || (parms.size() != targetTypes.length)) {
if ((parms == null && targetTypes.length != 0) || (parms != null && parms.size() != targetTypes.length)) {
throw new MessageDeliveryFailure("wrong number of arguments sent to endpoint. (received: "
+ (parms == null ? 0 : parms.size()) + "; required: " + targetTypes.length + ")");
}
Expand All @@ -78,7 +79,7 @@ else if (parms != null) {
try {
method.invoke(genericSvc, parms);
}
catch (Exception e) {
catch (final Exception e) {
throw new MessageDeliveryFailure("error invoking endpoint", e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

package org.jboss.errai.bus.server.io.buffers;

import org.jboss.errai.bus.server.io.ByteWriteAdapter;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
Expand All @@ -28,6 +26,8 @@
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.locks.ReentrantLock;

import org.jboss.errai.bus.server.io.ByteWriteAdapter;

/**
* A ring-based buffer implementation that provides contention-free writing of <i>1..n</i> colors. In this case,
* colors refer to the unique attribute that separates one topic of data from another. Global data, which is visible
Expand Down Expand Up @@ -244,8 +244,12 @@ public void write(final int writeSize,
headSequence = writeHead + allocSize;
}
finally {
bufferColor.wake();
lock.unlock();
try {
bufferColor.wake();
}
finally {
lock.unlock();
}
}
}

Expand All @@ -263,31 +267,36 @@ public void write(final int writeSize,
*/
@Override
public boolean read(final ByteWriteAdapter outputStream, final BufferColor bufferColor) throws IOException {
// obtain this color's read lock
bufferColor.lock.lock();
long lastSeq = -1l;

try {
// obtain this color's read lock
bufferColor.lock.lock();

// get the current head position.
final long writeHead = headSequence;
// get the current head position.
final long writeHead = headSequence;

// get the tail position for the color.
long read = bufferColor.sequence.get();
long lastSeq = read;
// get the tail position for the color.
long read = bufferColor.sequence.get();
lastSeq = read;

// checkOverflow(read);
// checkOverflow(read);

try {
while ((read = readNextChunk(writeHead, read, bufferColor, outputStream, null)) != -1)
lastSeq = read;

return lastSeq != read;
}
finally {
try {
// move the tail sequence for this color up.
if (lastSeq != -1)
bufferColor.sequence.set(lastSeq);

// release the read lock on this color/
bufferColor.lock.unlock();
}
finally {
// release the read lock on this color/
bufferColor.lock.unlock();
}
}
}

Expand Down Expand Up @@ -413,7 +422,7 @@ public boolean readWait(final ByteWriteAdapter outputStream,
// lets wait for some data to come available
bufferColor.dataWaiting.await();
}
catch (InterruptedException e) {
catch (final InterruptedException e) {
// if there's another reader contending, they should probably know about this.
bufferColor.dataWaiting.signal();
throw e;
Expand Down Expand Up @@ -479,7 +488,7 @@ public boolean readWait(final TimeUnit unit,
// wait around for some data.
nanos = bufferColor.dataWaiting.awaitNanos(nanos);
}
catch (InterruptedException e) {
catch (final InterruptedException e) {
bufferColor.dataWaiting.signal();
throw e;
}
Expand Down Expand Up @@ -566,7 +575,7 @@ public boolean readWait(final TimeUnit unit,
try {
nanos = bufferColor.dataWaiting.awaitNanos(nanos);
}
catch (InterruptedException e) {
catch (final InterruptedException e) {
bufferColor.dataWaiting.signal();
throw e;
}
Expand Down Expand Up @@ -712,10 +721,10 @@ private long readNextChunk(final long head,
* @return the size in bytes.
*/
private int readChunkSize(final int position) {
return ((((int) _buffer.get(position + 3)) & 0xFF)) +
((((int) _buffer.get(position + 2)) & 0xFF) << 8) +
((((int) _buffer.get(position + 1)) & 0xFF) << 16) +
((((int) _buffer.get(position)) & 0xFF) << 24);
return (((_buffer.get(position + 3)) & 0xFF)) +
(((_buffer.get(position + 2)) & 0xFF) << 8) +
(((_buffer.get(position + 1)) & 0xFF) << 16) +
(((_buffer.get(position)) & 0xFF) << 24);
}

private void writeChunkSize(final int position, final int size) {
Expand All @@ -741,7 +750,7 @@ public void dumpSegments(final PrintWriter writer) {
int pos = i * segmentSize;
int length = readChunkSize(pos);
build.append("Segment ").append(i).append(" <color:")
.append((int) segmentMap[i]).append(";length:").append(length)
.append(segmentMap[i]).append(";length:").append(length)
.append(";location:").append(pos).append(">");

pos += SEGMENT_HEADER_SIZE;
Expand All @@ -765,7 +774,6 @@ public void dumpSegments(final PrintWriter writer) {
}
}

@SuppressWarnings("UnusedDeclaration")
public List<String> dumpSegmentsAsList() {
final List<String> list = new ArrayList<String>();

Expand Down
Loading

0 comments on commit 76d075d

Please sign in to comment.