NIFI-1513: fixed some easy to fix errors

Addressing checkstyle issue.

This closes #221

Signed-off-by: Matt Gilman <matt.c.gilman@gmail.com>
This commit is contained in:
Tony Kurc 2016-02-13 22:37:10 -05:00 committed by Matt Gilman
parent 32f476aaa7
commit c7e24c7569
11 changed files with 82 additions and 72 deletions

View File

@ -44,7 +44,7 @@ public final class Relationship implements Comparable<Relationship> {
protected Relationship(final Builder builder) { protected Relationship(final Builder builder) {
this.name = builder.name == null ? null : builder.name.intern(); this.name = builder.name == null ? null : builder.name.intern();
this.description = builder.description; this.description = builder.description;
this.hashCode = 301 + this.name.hashCode(); // compute only once, since it gets called a bunch and will never change this.hashCode = 301 + ( (name == null) ? 0 :this.name.hashCode() ); // compute only once, since it gets called a bunch and will never change
} }
@Override @Override

View File

@ -99,7 +99,7 @@ public class GetDelimitedFieldEvaluator extends StringEvaluator {
if (escapeString == null || escapeString.isEmpty()) { if (escapeString == null || escapeString.isEmpty()) {
throw new AttributeExpressionLanguageException("Cannot evaluate getDelimitedField function because the escape character " throw new AttributeExpressionLanguageException("Cannot evaluate getDelimitedField function because the escape character "
+ "(which character is used to escape the quote character or delimiter) was not specified"); + "(which character is used to escape the quote character or delimiter) was not specified");
} else if (quoteString.length() > 1) { } else if (escapeString.length() > 1) {
throw new AttributeExpressionLanguageException("Cannot evaluate getDelimitedField function because the escape character " throw new AttributeExpressionLanguageException("Cannot evaluate getDelimitedField function because the escape character "
+ "(which character is used to escape the quote character or delimiter) evaluated to \"" + escapeString + "\", but only a single character is allowed."); + "(which character is used to escape the quote character or delimiter) evaluated to \"" + escapeString + "\", but only a single character is allowed.");
} }

View File

@ -25,6 +25,10 @@ import org.apache.nifi.flowfile.FlowFile;
import org.apache.nifi.processor.ProcessSession; import org.apache.nifi.processor.ProcessSession;
import org.apache.nifi.processor.util.FlowFileSessionWrapper; import org.apache.nifi.processor.util.FlowFileSessionWrapper;
/**
* Note: {@code Bin} objects are NOT thread safe. If multiple threads access a {@code Bin}, the caller must synchronize
* access.
*/
public class Bin { public class Bin {
private final long creationMomentEpochNs; private final long creationMomentEpochNs;
@ -121,8 +125,9 @@ public class Bin {
final String countValue = flowFile.getAttribute(fileCountAttribute); final String countValue = flowFile.getAttribute(fileCountAttribute);
final Integer count = toInteger(countValue); final Integer count = toInteger(countValue);
if (count != null) { if (count != null) {
this.maximumEntries = Math.min(count, this.maximumEntries); int currentMaxEntries = this.maximumEntries;
this.minimumEntries = this.maximumEntries; this.maximumEntries = Math.min(count, currentMaxEntries);
this.minimumEntries = currentMaxEntries;
} }
} }

View File

@ -610,7 +610,7 @@ public interface SiteToSiteClient extends Closeable {
trustManagerFactory = null; trustManagerFactory = null;
} }
if (keyManagerFactory != null || trustManagerFactory != null) { if (keyManagerFactory != null && trustManagerFactory != null) {
try { try {
// initialize the ssl context // initialize the ssl context
final SSLContext sslContext = SSLContext.getInstance("TLS"); final SSLContext sslContext = SSLContext.getInstance("TLS");

View File

@ -3015,8 +3015,9 @@ public class WebClusterManager implements HttpClusterManager, ProtocolHandler, C
final List<FlowFileSummaryDTO> summaryDTOs = new ArrayList<>(flowFileSummaries); final List<FlowFileSummaryDTO> summaryDTOs = new ArrayList<>(flowFileSummaries);
listingRequest.setFlowFileSummaries(summaryDTOs); listingRequest.setFlowFileSummaries(summaryDTOs);
// depends on invariant if numStepsTotal is 0, so is numStepsCompleted, all steps being completed
final int percentCompleted = numStepsCompleted / numStepsTotal; // would be 1
final int percentCompleted = (numStepsTotal == 0) ? 1 : numStepsCompleted / numStepsTotal;
listingRequest.setPercentCompleted(percentCompleted); listingRequest.setPercentCompleted(percentCompleted);
listingRequest.setFinished(finished); listingRequest.setFinished(finished);

View File

@ -133,7 +133,9 @@ public class ExecuteScript extends AbstractScriptProcessor {
try { try {
if (scriptToRun == null && scriptPath != null) { if (scriptToRun == null && scriptPath != null) {
scriptToRun = IOUtils.toString(new FileInputStream(scriptPath)); try (final FileInputStream scriptStream = new FileInputStream(scriptPath)) {
scriptToRun = IOUtils.toString(scriptStream);
}
} }
} catch (IOException ioe) { } catch (IOException ioe) {
throw new ProcessException(ioe); throw new ProcessException(ioe);

View File

@ -398,9 +398,6 @@ public class GetHTTP extends AbstractSessionFactoryProcessor {
clientBuilder.setProxy(new HttpHost(host, port)); clientBuilder.setProxy(new HttpHost(host, port));
} }
// create the http client
final CloseableHttpClient client = clientBuilder.build();
// create request // create request
final HttpGet get = new HttpGet(url); final HttpGet get = new HttpGet(url);
get.setConfig(requestConfigBuilder.build()); get.setConfig(requestConfigBuilder.build());
@ -426,60 +423,65 @@ public class GetHTTP extends AbstractSessionFactoryProcessor {
if (accept != null) { if (accept != null) {
get.addHeader(HEADER_ACCEPT, accept); get.addHeader(HEADER_ACCEPT, accept);
} }
// create the http client
try { try ( final CloseableHttpClient client = clientBuilder.build() ) {
final StopWatch stopWatch = new StopWatch(true); // NOTE: including this inner try in order to swallow exceptions on close
final HttpResponse response = client.execute(get); try {
final int statusCode = response.getStatusLine().getStatusCode(); final StopWatch stopWatch = new StopWatch(true);
if (statusCode == NOT_MODIFIED) { final HttpResponse response = client.execute(get);
logger.info("content not retrieved because server returned HTTP Status Code {}: Not Modified", new Object[]{NOT_MODIFIED}); final int statusCode = response.getStatusLine().getStatusCode();
context.yield(); if (statusCode == NOT_MODIFIED) {
// doing a commit in case there were flow files in the input queue logger.info("content not retrieved because server returned HTTP Status Code {}: Not Modified", new Object[]{NOT_MODIFIED});
session.commit(); context.yield();
return; // doing a commit in case there were flow files in the input queue
} session.commit();
final String statusExplanation = response.getStatusLine().getReasonPhrase(); return;
if (statusCode >= 300) {
logger.error("received status code {}:{} from {}", new Object[]{statusCode, statusExplanation, url});
// doing a commit in case there were flow files in the input queue
session.commit();
return;
}
FlowFile flowFile = session.create();
flowFile = session.putAttribute(flowFile, CoreAttributes.FILENAME.key(), context.getProperty(FILENAME).evaluateAttributeExpressions().getValue());
flowFile = session.putAttribute(flowFile, this.getClass().getSimpleName().toLowerCase() + ".remote.source", source);
flowFile = session.importFrom(response.getEntity().getContent(), flowFile);
final Header contentTypeHeader = response.getFirstHeader("Content-Type");
if (contentTypeHeader != null) {
final String contentType = contentTypeHeader.getValue();
if (!contentType.trim().isEmpty()) {
flowFile = session.putAttribute(flowFile, CoreAttributes.MIME_TYPE.key(), contentType.trim());
} }
final String statusExplanation = response.getStatusLine().getReasonPhrase();
if (statusCode >= 300) {
logger.error("received status code {}:{} from {}", new Object[]{statusCode, statusExplanation, url});
// doing a commit in case there were flow files in the input queue
session.commit();
return;
}
FlowFile flowFile = session.create();
flowFile = session.putAttribute(flowFile, CoreAttributes.FILENAME.key(), context.getProperty(FILENAME).evaluateAttributeExpressions().getValue());
flowFile = session.putAttribute(flowFile, this.getClass().getSimpleName().toLowerCase() + ".remote.source", source);
flowFile = session.importFrom(response.getEntity().getContent(), flowFile);
final Header contentTypeHeader = response.getFirstHeader("Content-Type");
if (contentTypeHeader != null) {
final String contentType = contentTypeHeader.getValue();
if (!contentType.trim().isEmpty()) {
flowFile = session.putAttribute(flowFile, CoreAttributes.MIME_TYPE.key(), contentType.trim());
}
}
final long flowFileSize = flowFile.getSize();
stopWatch.stop();
final String dataRate = stopWatch.calculateDataRate(flowFileSize);
session.getProvenanceReporter().receive(flowFile, url, stopWatch.getDuration(TimeUnit.MILLISECONDS));
session.transfer(flowFile, REL_SUCCESS);
logger.info("Successfully received {} from {} at a rate of {}; transferred to success", new Object[]{flowFile, url, dataRate});
session.commit();
updateStateMap(context,response,beforeStateMap,url);
} catch (final IOException e) {
context.yield();
session.rollback();
logger.error("Failed to retrieve file from {} due to {}; rolling back session", new Object[]{url, e.getMessage()}, e);
throw new ProcessException(e);
} catch (final Throwable t) {
context.yield();
session.rollback();
logger.error("Failed to process due to {}; rolling back session", new Object[]{t.getMessage()}, t);
throw t;
} }
final long flowFileSize = flowFile.getSize();
stopWatch.stop();
final String dataRate = stopWatch.calculateDataRate(flowFileSize);
session.getProvenanceReporter().receive(flowFile, url, stopWatch.getDuration(TimeUnit.MILLISECONDS));
session.transfer(flowFile, REL_SUCCESS);
logger.info("Successfully received {} from {} at a rate of {}; transferred to success", new Object[]{flowFile, url, dataRate});
session.commit();
updateStateMap(context,response,beforeStateMap,url);
} catch (final IOException e) { } catch (final IOException e) {
context.yield(); logger.debug("Error closing client due to {}, continuing.", new Object[]{e.getMessage()});
session.rollback();
logger.error("Failed to retrieve file from {} due to {}; rolling back session", new Object[]{url, e.getMessage()}, e);
throw new ProcessException(e);
} catch (final Throwable t) {
context.yield();
session.rollback();
logger.error("Failed to process due to {}; rolling back session", new Object[]{t.getMessage()}, t);
throw t;
} }
} finally { } finally {
conMan.shutdown(); conMan.shutdown();

View File

@ -25,6 +25,8 @@ import org.apache.nifi.flowfile.FlowFile;
import org.apache.nifi.processor.ProcessSession; import org.apache.nifi.processor.ProcessSession;
/** /**
* Note: {@code Bin} objects are NOT thread safe. If multiple threads access a {@code Bin}, the caller must synchronize
* access.
* @deprecated As of release 0.5.0, replaced by * @deprecated As of release 0.5.0, replaced by
* {@link org.apache.nifi.processor.util.bin.Bin} * {@link org.apache.nifi.processor.util.bin.Bin}
*/ */
@ -125,8 +127,9 @@ public class Bin {
final String countValue = flowFile.getAttribute(fileCountAttribute); final String countValue = flowFile.getAttribute(fileCountAttribute);
final Integer count = toInteger(countValue); final Integer count = toInteger(countValue);
if (count != null) { if (count != null) {
this.maximumEntries = Math.min(count, this.maximumEntries); final int currentMaximumEntries = this.maximumEntries;
this.minimumEntries = this.maximumEntries; this.maximumEntries = Math.min(count, currentMaximumEntries);
this.minimumEntries = currentMaximumEntries;
} }
} }

View File

@ -285,6 +285,8 @@ public class Scrypt {
// Do not enforce this check here. According to the scrypt spec, the salt can be empty. However, in the user-facing ScryptCipherProvider, enforce an arbitrary check to avoid empty salts // Do not enforce this check here. According to the scrypt spec, the salt can be empty. However, in the user-facing ScryptCipherProvider, enforce an arbitrary check to avoid empty salts
logger.warn("An empty salt was used for scrypt key derivation"); logger.warn("An empty salt was used for scrypt key derivation");
// throw new IllegalArgumentException("Salt cannot be empty"); // throw new IllegalArgumentException("Salt cannot be empty");
// as the Exception is not being thrown, prevent NPE if salt is null by setting it to empty array
if( salt == null ) salt = new byte[]{};
} }
if (saltLength < 8 || saltLength > 32) { if (saltLength < 8 || saltLength > 32) {

View File

@ -105,7 +105,6 @@ public class ControllerStatusReportingTask extends AbstractReportingTask {
@Override @Override
public void onTrigger(final ReportingContext context) { public void onTrigger(final ReportingContext context) {
final ProcessGroupStatus controllerStatus = context.getEventAccess().getControllerStatus(); final ProcessGroupStatus controllerStatus = context.getEventAccess().getControllerStatus();
controllerStatus.clone();
final boolean showDeltas = context.getProperty(SHOW_DELTAS).asBoolean(); final boolean showDeltas = context.getProperty(SHOW_DELTAS).asBoolean();

View File

@ -117,8 +117,7 @@ public class StandardGangliaReporter extends AbstractReportingTask {
return 0L; return 0L;
} }
final Long value = status.getBytesReceived(); return status.getBytesReceived();
return (value == null) ? 0L : value;
} }
}); });
@ -130,8 +129,7 @@ public class StandardGangliaReporter extends AbstractReportingTask {
return 0; return 0;
} }
final Integer value = status.getFlowFilesSent(); return status.getFlowFilesSent();
return (value == null) ? 0 : value;
} }
}); });
@ -142,9 +140,7 @@ public class StandardGangliaReporter extends AbstractReportingTask {
if (status == null) { if (status == null) {
return 0L; return 0L;
} }
return status.getBytesSent();
final Long value = status.getBytesSent();
return (value == null) ? 0L : value;
} }
}); });