Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bugs from static analysis #1177

Merged
merged 11 commits into from
Dec 13, 2019
Original file line number Diff line number Diff line change
Expand Up @@ -239,20 +239,22 @@ private void addMethods(ClassInstrumentationData classData, Element classNode) {

private Element getTopTag(File configurationFile) throws ParserConfigurationException, IOException, SAXException {
DocumentBuilder builder = createDocumentBuilder();
Document doc = builder.parse(new FileInputStream(configurationFile));
doc.getDocumentElement().normalize();
try (final FileInputStream fis = new FileInputStream(configurationFile)) {
Document doc = builder.parse(fis);
doc.getDocumentElement().normalize();

NodeList topTags = doc.getElementsByTagName(MAIN_TAG);
if (topTags == null || topTags.getLength() == 0) {
return null;
}
NodeList topTags = doc.getElementsByTagName(MAIN_TAG);
if (topTags == null || topTags.getLength() == 0) {
return null;
}

Node topNodeTag = topTags.item(0);
if (topNodeTag.getNodeType() != Node.ELEMENT_NODE) {
return null;
}
Node topNodeTag = topTags.item(0);
if (topNodeTag.getNodeType() != Node.ELEMENT_NODE) {
return null;
}

return (Element) topNodeTag;
return (Element) topNodeTag;
}
}

private DocumentBuilder createDocumentBuilder() throws ParserConfigurationException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,9 @@ public Boolean call() {
}
}
catch (Exception e) {
InternalLogger.INSTANCE.warn("Failed to obtain heartbeat property, stack trace"
+ "is: %s", ExceptionUtils.getStackTrace(e));
if (InternalLogger.INSTANCE.isWarnEnabled()) {
InternalLogger.INSTANCE.warn("Failed to obtain heartbeat property: %s", ExceptionUtils.getStackTrace(e));
}
}
}
return hasSetValues;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -520,16 +520,19 @@ private TelemetryContext createInitializedContext() {
ctx.getCloud().setRole(roleName);
}
for (ContextInitializer init : configuration.getContextInitializers()) {
if (init == null) { // since collection reference is exposed, we need a null check here
InternalLogger.INSTANCE.warn("Found null ContextInitializer in configuration. Skipping...");
continue;
}

try {
init.initialize(ctx);
} catch (ThreadDeath td) {
throw td;
} catch (Throwable t) {
try {
if (InternalLogger.INSTANCE.isErrorEnabled()) {
InternalLogger.INSTANCE.error("Exception in context initializer%s: %s",
init == null ? " (context initializer is null)" : ", "+init.getClass().getSimpleName(),
ExceptionUtils.getStackTrace(t));
InternalLogger.INSTANCE.error("Exception in context initializer, %s: %s", init.getClass().getSimpleName(), ExceptionUtils.getStackTrace(t));
}
} catch (ThreadDeath td) {
throw td;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ final class SenderThreadLocalBackOffData {
* @param addMilliseconds The amount of seconds that will be added to the 'large' intervals to distinct between sender threads.
*/
public SenderThreadLocalBackOffData(long[] backOffTimeoutsInMillis, long addMilliseconds) {
Preconditions.checkNotNull(backOffTimeoutsInMillis, "backOffTimeoutsInSeconds must be not null");
Preconditions.checkArgument(backOffTimeoutsInMillis.length > 0, "backOffTimeoutsInSeconds must not be empty");
Preconditions.checkArgument(addMilliseconds >= 0, "addSeconds must not be >= 0");
Preconditions.checkNotNull(backOffTimeoutsInMillis, "backOffTimeoutsInMillis must be not null");
Preconditions.checkArgument(backOffTimeoutsInMillis.length > 0, "backOffTimeoutsInMillis must not be empty");
Preconditions.checkArgument(addMilliseconds >= 0, "addMilliseconds must not be >= 0");

currentBackOffIndex = -1;
instanceIsActive = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,7 @@ public synchronized void stopAllSendersBackOffActivities() {

@Override
protected SenderThreadLocalBackOffData initialValue() {
int addSeconds = threadsSecondsDifference.incrementAndGet();
senderThreadLocalData = new SenderThreadLocalBackOffData(backOffTimeoutsInMilliseconds, addSeconds * 1000);
senderThreadLocalData = new SenderThreadLocalBackOffData(backOffTimeoutsInMilliseconds, threadsSecondsDifference.incrementAndGet() * 1000L);
registerSenderData(senderThreadLocalData);
return senderThreadLocalData;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public TransmissionFileSystemOutput(String folderPath, String maxTransmissionSto
DEFAULT_CAPACITY_MEGABYTES,
MAX_TRANSMISSION_STORAGE_CAPACITY_NAME,
maxTransmissionStorageCapacity);
capacityInBytes = capacityEnforcer.getCurrentValue() * 1024 * 1024;
capacityInBytes = capacityEnforcer.getCurrentValue() * 1024L * 1024L;

folder = new File(folderPath);

Expand Down Expand Up @@ -212,7 +212,7 @@ public Transmission fetchOldestFile() {
}

public void setCapacity(int suggestedCapacity) {
this.capacityInBytes = capacityEnforcer.normalizeValue(suggestedCapacity) * 1024 * 1024;
this.capacityInBytes = capacityEnforcer.normalizeValue(suggestedCapacity) * 1024L * 1024L;
}

private List<File> sortOldestLastAndTrim(Collection<File> transmissions, int limit) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public void setSamplingPercentage(Double samplingPercentage) {

@Override
public boolean isSampledIn(Telemetry telemetry) {
Double currentSamplingPercentage = samplingPercentage.get();
double currentSamplingPercentage = samplingPercentage.get();

if (currentSamplingPercentage < 100.0 - 1.0E-12) {
if (telemetry instanceof SupportSampling) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,9 @@ public Boolean call() {
}
}
catch (Exception e) {
InternalLogger.INSTANCE.warn("Failed to obtain heartbeat property, stack trace"
+ "is: %s", ExceptionUtils.getStackTrace(e));
if (InternalLogger.INSTANCE.isWarnEnabled()) {
InternalLogger.INSTANCE.warn("Failed to obtain heartbeat property: %s", ExceptionUtils.getStackTrace(e));
}
}
}
return hasSetValues;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,9 @@ public Boolean call() {
}
}
catch (Exception e) {
InternalLogger.INSTANCE.warn("Failed to obtain heartbeat property, stack trace"
+ "is: %s", ExceptionUtils.getStackTrace(e));
if (InternalLogger.INSTANCE.isWarnEnabled()) {
InternalLogger.INSTANCE.warn("Failed to obtain heartbeat property: %s", ExceptionUtils.getStackTrace(e));
}
}
}
return hasSetValues;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,12 +209,10 @@ private static void extractToLocalFolder(File dllOnDisk, String libraryToLoad) t

InternalLogger.INSTANCE.trace("Successfully extracted '%s' to local folder", libraryToLoad);
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
InternalLogger.INSTANCE.error("Failed to close input stream for dll extraction: %s", e.toString());
}
try {
in.close();
} catch (IOException e) {
InternalLogger.INSTANCE.error("Failed to close input stream for dll extraction: %s", e.toString());
}
if (out != null) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import java.io.Closeable;
import java.util.List;
import java.util.ArrayList;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;

Expand Down Expand Up @@ -214,15 +213,17 @@ public void stopAll() {

private SDKShutdownAction getShutdownAction() {
if (shutdownAction == null) {
synchronized (this) {
synchronized (INSTANCE) {
if (shutdownAction == null) {
try {
shutdownAction = new SDKShutdownAction();
Thread t = new Thread(shutdownAction, SDKShutdownActivity.class.getSimpleName()+"-ShutdownHook");
SDKShutdownAction action = new SDKShutdownAction();
Thread t = new Thread(action, SDKShutdownActivity.class.getSimpleName()+"-ShutdownHook");
Runtime.getRuntime().addShutdownHook(t);
shutdownAction = action;
} catch (Exception e) {
InternalLogger.INSTANCE.error("Error while adding shutdown hook in getShutDownThread call");
InternalLogger.INSTANCE.trace("Stack trace generated is %s", ExceptionUtils.getStackTrace(e));
if (InternalLogger.INSTANCE.isErrorEnabled()) {
InternalLogger.INSTANCE.error("Error while adding shutdown hook in getShutDownThread call: %s", ExceptionUtils.getStackTrace(e));
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,9 @@ public enum SystemInformation {

private final static String DEFAULT_PROCESS_NAME = "Java_Process";

private String processId;
private final String processId = initializeProcessId();

public String getProcessId() {
setProcessId();
return processId;
}

Expand All @@ -51,27 +50,22 @@ public boolean isUnix() {
return SystemUtils.IS_OS_UNIX;
}

private synchronized void setProcessId() {
if (!Strings.isNullOrEmpty(processId)) {
return;
}

private String initializeProcessId() {
String rawName = ManagementFactory.getRuntimeMXBean().getName();
if (!Strings.isNullOrEmpty(rawName)) {
int i = rawName.indexOf("@");
if (i != -1) {
String processIdAsString = rawName.substring(0, i);
try {
Integer.parseInt(processIdAsString);
processId = processIdAsString;
return;
return processIdAsString;
} catch (Exception e) {
InternalLogger.INSTANCE.error("Failed to fetch process id: '%s'", e.toString());
}
}
}

// Default
processId = DEFAULT_PROCESS_NAME;
return DEFAULT_PROCESS_NAME;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,10 @@ public int getMilliseconds() {
* @return The total milliseconds of the duration.
*/
public long getTotalMilliseconds() {
return (days * SECONDS_IN_ONE_DAY * 1000) +
(hours * SECONDS_IN_ONE_HOUR * 1000) +
(minutes * SECONDS_IN_ONE_MINUTE * 1000) +
(seconds * 1000) + milliseconds;
return (days * SECONDS_IN_ONE_DAY * 1000L) +
(hours * SECONDS_IN_ONE_HOUR * 1000L) +
(minutes * SECONDS_IN_ONE_MINUTE * 1000L) +
(seconds * 1000L) + milliseconds;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ protected void onInitializeTelemetry(Telemetry telemetry) {
// region Private

private void updateRequestNameIfRequestTelemetry(Telemetry telemetry, String operationName) {
if (!(telemetry instanceof RequestTelemetry)) {
if (!(telemetry instanceof RequestTelemetry)) { // null instanceof Object == false
return;
}

RequestTelemetry requestTelemetry = (RequestTelemetry)telemetry;

// We only update the request telemetry name if not already provided by the user.
if (requestTelemetry != null && CommonUtils.isNullOrEmpty(requestTelemetry.getName())) {
if (CommonUtils.isNullOrEmpty(requestTelemetry.getName())) {
requestTelemetry.setName(operationName);
}
}
Expand Down