Skip to content

Commit

Permalink
FindBugs: Use Jenkins#getActiveInstance() instead of the old Hudson c…
Browse files Browse the repository at this point in the history
…lass
  • Loading branch information
oleg-nenashev committed Jun 12, 2017
1 parent a5ebdbb commit cccc9e2
Show file tree
Hide file tree
Showing 14 changed files with 39 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@
import com.google.common.base.Function;
import com.google.common.base.Objects;
import com.google.common.io.Files;
import hudson.model.Hudson;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Date;
import jenkins.model.Jenkins;

public class BackupObject implements Comparable {

Expand Down Expand Up @@ -75,7 +75,7 @@ public static Function<File, BackupObject> getFromFile() {
public BackupObject apply(File file) {
if(file != null) {
try {
return (BackupObject) Hudson.XSTREAM.fromXML(Files.toString(file, Charset.defaultCharset()));
return (BackupObject) Jenkins.XSTREAM.fromXML(Files.toString(file, Charset.defaultCharset()));
} catch (IOException e) {
return null;
}
Expand All @@ -94,7 +94,7 @@ public static Function<String, BackupObject> getFromString() {
return new Function<String, BackupObject>() {
public BackupObject apply(String content) {
if (content != null) {
return (BackupObject) Hudson.XSTREAM.fromXML(content);
return (BackupObject) Jenkins.XSTREAM.fromXML(content);
} else {
return null;
}
Expand All @@ -113,7 +113,7 @@ public String getDisplayName() {
}

public String getAsString() {
return Hudson.XSTREAM.toXML(this);
return Jenkins.XSTREAM.toXML(this);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import java.util.Arrays;
import java.util.List;
import java.util.logging.Logger;
import jenkins.model.Jenkins;

/**
*
Expand All @@ -59,7 +60,7 @@ public String getDisplayName() {

@Override
public Iterable<File> getFilesToBackup() throws PeriodicBackupException {
File rootDir = Hudson.getInstance().getRootDir();
File rootDir = Jenkins.getActiveInstance().getRootDir();
List<File> filesToBackup = Lists.newArrayList();
addRootFiles(rootDir, filesToBackup);
addJobFiles(rootDir, filesToBackup);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@
import hudson.DescriptorExtensionList;
import hudson.model.AbstractModelObject;
import hudson.model.Describable;
import hudson.model.Hudson;

import java.io.File;
import java.io.IOException;
import jenkins.model.Jenkins;

/**
*
Expand Down Expand Up @@ -68,13 +68,14 @@ public void restoreFiles(File finalResultDir) throws IOException, PeriodicBackup
* @return Collection of FileManager Descriptors
*/
public static DescriptorExtensionList<FileManager, FileManagerDescriptor> all() {
return Hudson.getInstance().getDescriptorList(FileManager.class);
return Jenkins.getActiveInstance().getDescriptorList(FileManager.class);
}

public FileManagerDescriptor getDescriptor() {
return (FileManagerDescriptor) Hudson.getInstance().getDescriptor(getClass());
return (FileManagerDescriptor) Jenkins.getActiveInstance().getDescriptor(getClass());
}

@Override
public String getSearchUrl() {
return "FileManager";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import hudson.Extension;
import hudson.model.Hudson;
import org.apache.tools.ant.DirectoryScanner;
import org.kohsuke.stapler.DataBoundConstructor;

import java.io.File;
import java.util.Collections;
import java.util.List;
import jenkins.model.Jenkins;


/**
Expand All @@ -51,7 +51,7 @@ public class FullBackup extends FileManager {

@DataBoundConstructor
public FullBackup(String excludesString) {
this(excludesString, Hudson.getInstance().getRootDir());
this(excludesString, Jenkins.getActiveInstance().getRootDir());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@
import hudson.DescriptorExtensionList;
import hudson.model.AbstractModelObject;
import hudson.model.Describable;
import hudson.model.Hudson;

import java.io.File;
import java.io.IOException;
import jenkins.model.Jenkins;

/**
*
Expand Down Expand Up @@ -87,13 +87,15 @@ public Location(boolean enabled) {
* @return Collection of FileManager Descriptors
*/
public static DescriptorExtensionList<Location, LocationDescriptor> all() {
return Hudson.getInstance().getDescriptorList(Location.class);
return Jenkins.getActiveInstance().getDescriptorList(Location.class);
}

@Override
public LocationDescriptor getDescriptor() {
return (LocationDescriptor) Hudson.getInstance().getDescriptor(getClass());
return (LocationDescriptor) Jenkins.getActiveInstance().getDescriptor(getClass());
}

@Override
public String getSearchUrl() {
return "Location";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@

import com.google.common.collect.Lists;
import hudson.Extension;
import hudson.model.Hudson;
import org.apache.commons.io.FileUtils;
import org.kohsuke.stapler.DataBoundConstructor;

import java.io.File;
import java.io.IOException;
import java.util.logging.Logger;
import jenkins.model.Jenkins;

public class NullStorage extends Storage {

Expand Down Expand Up @@ -63,7 +63,7 @@ public void backupStart(String tempDirectoryPath, String archiveFilenameBase) th
public void backupAddFile(File fileToStore) throws PeriodicBackupException {
try {
FileUtils.copyFile(fileToStore, new File(destinationDirectory,
Util.getRelativePath(fileToStore, Hudson.getInstance().getRootDir())));
Util.getRelativePath(fileToStore, Jenkins.getActiveInstance().getRootDir())));
} catch (IOException e) {
LOGGER.warning("Could not copy " + fileToStore.getAbsolutePath() + " to " + destinationDirectory);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@

package org.jenkinsci.plugins.periodicbackup;

import hudson.model.Hudson;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.util.logging.Logger;
import jenkins.model.Jenkins;

/**
*
Expand All @@ -41,7 +41,7 @@ public class OverwriteRestorePolicy implements RestorePolicy {
private static final Logger LOGGER = Logger.getLogger(OverwriteRestorePolicy.class.getName());

public void restore(File tempDir) throws IOException {
File hudsonRoot = Hudson.getInstance().getRootDir();
File hudsonRoot = Jenkins.getActiveInstance().getRootDir();

FileUtils.copyDirectory(tempDir, hudsonRoot);
LOGGER.info("Restoring of files finished");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import java.io.IOException;
import java.util.Collection;
import java.util.Map;
import jenkins.model.Jenkins;

/**
*
Expand Down Expand Up @@ -192,12 +193,12 @@ public void save() throws IOException {

protected XmlFile getConfigXml() {
return new XmlFile(Hudson.XSTREAM,
new File(Hudson.getInstance().getRootDir(), "periodicBackup.xml"));
new File(Jenkins.getActiveInstance().getRootDir(), "periodicBackup.xml"));
}

@SuppressWarnings("unused")
public String getRootDirectory() {
return Hudson.getInstance().getRootDir().getAbsolutePath();
return Jenkins.getActiveInstance().getRootDir().getAbsolutePath();
}

@SuppressWarnings("unused")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
import hudson.Plugin;
import hudson.logging.LogRecorder;
import hudson.logging.LogRecorder.Target;
import hudson.model.Hudson;
import hudson.security.ACL;

import java.io.IOException;
import java.util.logging.Level;
import jenkins.model.Jenkins;

import org.acegisecurity.Authentication;
import org.acegisecurity.context.SecurityContextHolder;
Expand Down Expand Up @@ -53,11 +53,11 @@ private void updateDebugLogger(final Level level, final String... debugLoggerNam

try {
for (String current : debugLoggerNames) {
LogRecorder recorder = Hudson.getInstance().getLog().getLogRecorder(current);
LogRecorder recorder = Jenkins.getActiveInstance().getLog().getLogRecorder(current);

if (recorder == null) {
Hudson.getInstance().getLog().doNewLogRecorder(current);
recorder = Hudson.getInstance().getLog().getLogRecorder(current);
Jenkins.getActiveInstance().getLog().doNewLogRecorder(current);
recorder = Jenkins.getActiveInstance().getLog().getLogRecorder(current);
recorder.targets.add(new Target(current, level));

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@
package org.jenkinsci.plugins.periodicbackup;

import com.google.common.collect.Lists;
import hudson.model.Hudson;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.logging.Logger;
import jenkins.model.Jenkins;

/**
*
Expand All @@ -46,7 +46,7 @@ public class ReplaceRestorePolicy implements RestorePolicy {
private transient int filesDeleted, filesReplaced, filesKept;

public void restore(File tempDir) throws IOException, PeriodicBackupException {
hudsonRoot = Hudson.getInstance().getRootDir();
hudsonRoot = Jenkins.getActiveInstance().getRootDir();
if(hudsonRoot == null) {
throw new PeriodicBackupException("HOME directory is unidentified.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@

package org.jenkinsci.plugins.periodicbackup;

import hudson.model.Hudson;
import hudson.security.ACL;
import org.acegisecurity.Authentication;
import org.acegisecurity.context.SecurityContextHolder;
Expand All @@ -34,6 +33,7 @@
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import jenkins.model.Jenkins;

public class RestoreExecutor implements Runnable {

Expand Down Expand Up @@ -107,7 +107,7 @@ public void run() {
Authentication origAuth = SecurityContextHolder.getContext().getAuthentication();
SecurityContextHolder.getContext().setAuthentication(ACL.SYSTEM);
try {
Hudson.getInstance().doReload();
Jenkins.getActiveInstance().doReload();
} catch (IOException e) {
LOGGER.log(Level.WARNING, "Error reloading config files from disk: {0}", e.getMessage());
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
import hudson.DescriptorExtensionList;
import hudson.model.AbstractModelObject;
import hudson.model.Describable;
import hudson.model.Hudson;

import java.io.File;
import jenkins.model.Jenkins;

/**
*
Expand Down Expand Up @@ -76,7 +76,7 @@ public abstract class Storage extends AbstractModelObject implements Describable
public abstract void unarchiveFiles(Iterable<File> archives, File finalResultDir);

public StorageDescriptor getDescriptor() {
return (StorageDescriptor) Hudson.getInstance().getDescriptor(getClass());
return (StorageDescriptor) Jenkins.getActiveInstance().getDescriptor(getClass());
}

public String getSearchUrl() {
Expand All @@ -90,7 +90,7 @@ public String getSearchUrl() {
* @return Collection of FileManager Descriptors
*/
public static DescriptorExtensionList<Storage, StorageDescriptor> all() {
return Hudson.getInstance().getDescriptorList(Storage.class);
return Jenkins.getActiveInstance().getDescriptorList(Storage.class);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@

import com.google.common.collect.Lists;
import hudson.Extension;
import hudson.model.Hudson;
import org.codehaus.plexus.archiver.ArchiverException;
import org.codehaus.plexus.archiver.tar.TarArchiver;
import org.codehaus.plexus.archiver.tar.TarGZipUnArchiver;
Expand All @@ -37,6 +36,7 @@
import java.io.File;
import java.io.IOException;
import java.util.logging.Logger;
import jenkins.model.Jenkins;

public class TarGzStorage extends Storage {

Expand Down Expand Up @@ -76,7 +76,7 @@ public void backupStart(String tempDirectoryPath, String archiveFilenameBase) th
@Override
public void backupAddFile(File fileToStore) throws PeriodicBackupException {
try {
archiver.addFile(fileToStore, Util.getRelativePath(fileToStore, Hudson.getInstance().getRootDir()));
archiver.addFile(fileToStore, Util.getRelativePath(fileToStore, Jenkins.getActiveInstance().getRootDir()));
} catch (ArchiverException e) {
LOGGER.warning("Could not add file to the archive. " + e.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@

import com.google.common.collect.Sets;
import hudson.Extension;
import hudson.model.Hudson;
import net.sf.json.JSONObject;
import org.codehaus.plexus.archiver.ArchiverException;
import org.codehaus.plexus.archiver.zip.ZipArchiver;
Expand All @@ -39,6 +38,7 @@
import java.io.IOException;
import java.util.Set;
import java.util.logging.Logger;
import jenkins.model.Jenkins;

public class ZipStorage extends Storage {

Expand Down Expand Up @@ -183,7 +183,7 @@ private void createNewArchive() {
*/
private void addFile(File fileToStore) {
try {
archiver.addFile(fileToStore, Util.getRelativePath(fileToStore, Hudson.getInstance().getRootDir()));
archiver.addFile(fileToStore, Util.getRelativePath(fileToStore, Jenkins.getActiveInstance().getRootDir()));
currentArchiveFilesCount++;
currentArchiveTotalFilesSize += fileToStore.length();
} catch (ArchiverException e) {
Expand Down

0 comments on commit cccc9e2

Please sign in to comment.