Skip to content

Commit

Permalink
fix errors to make maven-findbugs happy
Browse files Browse the repository at this point in the history
  • Loading branch information
arroyc authored and clguiman committed Feb 16, 2017
1 parent c540f1c commit a32453f
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 30 deletions.
12 changes: 12 additions & 0 deletions exclude-findbugs.xml
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Jenkins.getInstance() should never be null, but findbug is showing warning/error on that so filetring it.
-->
<FindBugsFilter>
<Match>
<!-- The issue was fixed, but the error is still there -->
<Class name="com.microsoftopentechnologies.windowsazurestorage.helper.Utils" />
<Method name="getJenkinsInstance"/>
<Bug pattern="NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE" />
</Match>
</FindBugsFilter>
3 changes: 2 additions & 1 deletion pom.xml
Expand Up @@ -19,7 +19,8 @@
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<jenkins.version>1.645</jenkins.version>
<java.level>7</java.level>
<findbugs.failOnError>false</findbugs.failOnError>
<findbugs.failOnError>true</findbugs.failOnError>
<findbugs.excludeFilterFile>exclude-findbugs.xml</findbugs.excludeFilterFile>
<maven.javadoc.skip>true</maven.javadoc.skip>
</properties>

Expand Down
Expand Up @@ -2,6 +2,7 @@

import com.microsoftopentechnologies.windowsazurestorage.WAStoragePublisher.WAStorageDescriptor;
import com.microsoftopentechnologies.windowsazurestorage.beans.StorageAccountInfo;
import com.microsoftopentechnologies.windowsazurestorage.helper.Utils;
import hudson.model.Api;
import hudson.model.Run;
import hudson.model.RunAction;
Expand Down Expand Up @@ -90,7 +91,7 @@ public boolean getAllowAnonymousAccess() {
}

private WAStoragePublisher.WAStorageDescriptor getWAStorageDescriptor() {
WAStoragePublisher.WAStorageDescriptor desc = Jenkins.getInstance().getDescriptorByType(WAStoragePublisher.WAStorageDescriptor.class);
WAStoragePublisher.WAStorageDescriptor desc = Utils.getJenkinsInstance().getDescriptorByType(WAStoragePublisher.WAStorageDescriptor.class);
return desc;
}

Expand Down
Expand Up @@ -19,6 +19,7 @@
import com.cloudbees.plugins.credentials.common.StandardListBoxModel;
import com.cloudbees.plugins.credentials.domains.DomainRequirement;
import com.microsoftopentechnologies.windowsazurestorage.beans.StorageAccountInfo;
import com.microsoftopentechnologies.windowsazurestorage.exceptions.WAStorageException;
import com.microsoftopentechnologies.windowsazurestorage.helper.AzureCredentials;
import com.microsoftopentechnologies.windowsazurestorage.helper.Utils;
import hudson.DescriptorExtensionList;
Expand All @@ -44,12 +45,14 @@
import hudson.tasks.BuildStepMonitor;
import hudson.tasks.Builder;
import hudson.util.ListBoxModel;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import jenkins.model.Jenkins;
import jenkins.tasks.SimpleBuildStep;
import net.sf.json.JSONObject;
import org.acegisecurity.AccessDeniedException;
import org.kohsuke.stapler.AncestorInPath;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.QueryParameter;
Expand Down Expand Up @@ -173,17 +176,13 @@ public String getStorageCredentialId() {
*/
@Override
public synchronized void perform(Run<?, ?> run, FilePath workspace, Launcher launcher, TaskListener listener) {
StorageAccountInfo strAcc = null;
try {
// Get storage account
StorageAccountInfo strAcc = AzureCredentials.convertToStorageAccountInfo(AzureCredentials.getStorageCreds(this.storageCredentialId, this.storageAccName));
try{
final EnvVars envVars = run.getEnvironment(listener);

// Get storage account
if (this.storageCreds == null) {
this.storageCreds = AzureCredentials.getStorageCreds(storageCredentialId, storageAccName);
this.storageCredentialId = this.storageCreds.getId();
}
strAcc = AzureCredentials.convertToStorageAccountInfo(this.storageCreds);

if (envVars == null) {
throw new IllegalStateException("Failed to capture information about running environment.");
}
// Resolve include patterns
String expIncludePattern = Util.replaceMacro(includeFilesPattern, envVars);

Expand Down Expand Up @@ -218,7 +217,7 @@ public synchronized void perform(Run<?, ?> run, FilePath workspace, Launcher lau
strAcc, expIncludePattern, expExcludePattern, Util.replaceMacro(downloadDirLoc, envVars),
flattenDirectories, workspace, expContainerName);
} else {
Job<?, ?> job = Jenkins.getInstance().getItemByFullName(expProjectName, Job.class);
Job<?, ?> job = Utils.getJenkinsInstance().getItemByFullName(expProjectName, Job.class);
if (job != null) {
// Resolve download location
BuildFilter filter = new BuildFilter();
Expand All @@ -245,7 +244,7 @@ public synchronized void perform(Run<?, ?> run, FilePath workspace, Launcher lau
listener.getLogger().println(Messages.AzureStorageBuilder_files_downloaded_count(filesDownloaded));
}

} catch (Exception e) {
} catch (WAStorageException | IOException | InterruptedException | AccessDeniedException e) {
e.printStackTrace(listener.error(Messages.AzureStorageBuilder_download_err(strAcc.getStorageAccName())));
run.setResult(Result.UNSTABLE);
}
Expand All @@ -267,7 +266,7 @@ private int downloadArtifacts(Run<?, ?> source, Run<?, ?> run, Launcher launcher
strAcc, blob, includeFilter, excludeFilter,
downloadDir, flattenDirectories, workspace);

} catch (Exception e) {
} catch (WAStorageException | IOException | InterruptedException e) {
run.setResult(Result.UNSTABLE);
}

Expand Down Expand Up @@ -340,7 +339,7 @@ public ListBoxModel doFillStorageCredentialIdItems(@AncestorInPath Item owner) {

public AutoCompletionCandidates doAutoCompleteProjectName(@QueryParameter String value) {
AutoCompletionCandidates projectList = new AutoCompletionCandidates();
for (AbstractProject<?, ?> project : Jenkins.getInstance().getItems(AbstractProject.class)) {
for (AbstractProject<?, ?> project : Utils.getJenkinsInstance().getItems(AbstractProject.class)) {
if (project.getName().toLowerCase().startsWith(value.toLowerCase())) {
projectList.add(project.getName());
}
Expand All @@ -359,8 +358,7 @@ public String getDisplayName() {
}

public StorageAccountInfo[] getStorageAccounts() {
WAStoragePublisher.WAStorageDescriptor publisherDescriptor = Jenkins
.getInstance().getDescriptorByType(
WAStoragePublisher.WAStorageDescriptor publisherDescriptor = Utils.getJenkinsInstance().getDescriptorByType(
WAStoragePublisher.WAStorageDescriptor.class);

StorageAccountInfo[] sa = publisherDescriptor.getStorageAccounts();
Expand Down
Expand Up @@ -379,7 +379,7 @@ public static int upload(Run<?, ?> run, Launcher launcher, TaskListener listener
tempPath.deleteRecursive();
}

} catch (Exception e) {
} catch (StorageException | IOException | InterruptedException | URISyntaxException e) {
throw new WAStorageException(e.getMessage(), e.getCause());
}
return filesUploaded;
Expand Down
Expand Up @@ -288,11 +288,7 @@ public synchronized void perform(@Nonnull Run<?, ?> run, @Nonnull FilePath ws, @
final EnvVars envVars = run.getEnvironment(listener);

// Get storage account and set formatted blob endpoint url.
if(this.storageCreds == null) {
this.storageCreds = AzureCredentials.getStorageCreds(storageCredentialId, storageAccName);
this.storageCredentialId = this.storageCreds.getId();
}
StorageAccountInfo strAcc = AzureCredentials.convertToStorageAccountInfo(this.storageCreds);
StorageAccountInfo strAcc = AzureCredentials.convertToStorageAccountInfo(AzureCredentials.getStorageCreds(this.storageCredentialId, this.storageAccName));
// Resolve container name
String expContainerName = replaceMacro(containerName, envVars, Locale.ENGLISH);

Expand Down
Expand Up @@ -17,9 +17,12 @@

import com.microsoft.azure.storage.OperationContext;
import com.microsoft.azure.storage.core.BaseRequest;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Locale;
import javax.annotation.Nonnull;
import jenkins.model.Jenkins;

public class Utils {
Expand Down Expand Up @@ -126,29 +129,41 @@ public static String getBlobEP(String blobURL) {
public static String getDefaultBlobURL() {
return Constants.DEF_BLOB_URL;
}

@Nonnull
public static Jenkins getJenkinsInstance() {
return Jenkins.getInstance();
}

/**
* Returns md5 hash in string format for a given string
*
* @param plainText
* @return
*/
public static String getMD5(String plainText) {
try {
java.security.MessageDigest md = java.security.MessageDigest.getInstance(Constants.HASH_TYPE);
byte[] array = md.digest(plainText.getBytes());
StringBuffer sb = new StringBuffer();
byte[] array = md.digest(plainText.getBytes(Charset.forName("UTF-8")));
StringBuilder sb = new StringBuilder();
for (int i = 0; i < array.length; ++i) {
sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100).substring(1, 3));
}
return sb.toString();
} catch (java.security.NoSuchAlgorithmException e) {
e.printStackTrace();
}
return null;
}

public static String getWorkDirectory() {
return Jenkins.getInstance().root.getAbsolutePath();
File jenkinsRoot;
jenkinsRoot = Utils.getJenkinsInstance().root;
if (jenkinsRoot == null) {
throw new IllegalStateException("Root isn't configured. Couldn't find Jenkins work directory.");
}

return jenkinsRoot.getAbsolutePath();

}

public static OperationContext updateUserAgent() throws IOException {
Expand All @@ -159,7 +174,12 @@ public static OperationContext updateUserAgent() throws IOException {

try {
version = Utils.class.getPackage().getImplementationVersion();
instanceId = Jenkins.getInstance().getLegacyInstanceId();
if(Utils.getJenkinsInstance().getLegacyInstanceId() == null) {
instanceId = "local-instance";
}
else {
instanceId = Utils.getJenkinsInstance().getLegacyInstanceId();
}
} catch (Exception e) {
}

Expand All @@ -183,6 +203,6 @@ public static OperationContext updateUserAgent() throws IOException {

opContext.setUserHeaders(temp);
return opContext;
}
}

}

0 comments on commit a32453f

Please sign in to comment.