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

Code cleanup #124

Merged
merged 13 commits into from
Dec 26, 2018
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
import hudson.search.SearchIndexBuilder;
import hudson.search.SearchItem;
import hudson.security.ACL;
import hudson.security.ACLContext;
import hudson.util.AlternativeUiTextProvider;
import hudson.util.CaseInsensitiveComparator;
import hudson.util.CopyOnWriteMap;
Expand All @@ -75,7 +76,6 @@
import hudson.util.FormValidation;
import hudson.util.Function1;
import hudson.util.HttpResponses;
import hudson.util.QuotedStringTokenizer;
import hudson.views.DefaultViewsTabBar;
import hudson.views.ViewsTabBar;
import java.io.File;
Expand Down Expand Up @@ -109,8 +109,6 @@
import jenkins.model.TransientActionFactory;
import net.sf.json.JSONObject;
import org.acegisecurity.AccessDeniedException;
import org.acegisecurity.context.SecurityContext;
import org.acegisecurity.context.SecurityContextHolder;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.kohsuke.accmod.Restricted;
Expand Down Expand Up @@ -164,7 +162,7 @@ public static void loadJobTotal() {
if (!loadJobTotalRan.compareAndSet(false, true)) {
return; // TODO why does Jenkins run the initializer many times?!
}
scan(new File(Jenkins.getActiveInstance().getRootDir(), "jobs"), 0);
scan(new File(Jenkins.get().getRootDir(), "jobs"), 0);
// TODO reset count after reload config from disk (otherwise goes up to 200% etc.)
}
private static void scan(File d, int depth) {
Expand Down Expand Up @@ -600,7 +598,7 @@ private ChildNameGenerator<AbstractFolder<I>,I> childNameGenerator() {
*/
@Override
public AbstractFolderDescriptor getDescriptor() {
return (AbstractFolderDescriptor) Jenkins.getActiveInstance().getDescriptorOrDie(getClass());
return (AbstractFolderDescriptor) Jenkins.get().getDescriptorOrDie(getClass());
}

/**
Expand Down Expand Up @@ -802,7 +800,7 @@ protected SearchIndexBuilder makeSearchIndex() {
*/
@Override
protected SearchItem get(String key) {
return Jenkins.getActiveInstance().getItem(key, grp());
return Jenkins.get().getItem(key, grp());
}

/**
Expand Down Expand Up @@ -889,7 +887,7 @@ public void invalidateBuildHealthReports() {
@Exported(name = "healthReport")
public List<HealthReport> getBuildHealthReports() {
if (healthMetrics == null || healthMetrics.isEmpty()) {
return Collections.<HealthReport>emptyList();
return Collections.emptyList();
}
List<HealthReport> reports = healthReports;
if (reports != null && nextHealthReportsRefreshMillis > System.currentTimeMillis()) {
Expand Down Expand Up @@ -1108,7 +1106,7 @@ public void delete() throws IOException, InterruptedException {
// the 15 second delay for every child item). This happens after queue cancellation, so will be
// a complete set of builds in flight
Map<Executor, Queue.Executable> buildsInProgress = new LinkedHashMap<>();
for (Computer c : Jenkins.getActiveInstance().getComputers()) {
for (Computer c : Jenkins.get().getComputers()) {
for (Executor e : c.getAllExecutors()) {
WorkUnit workUnit = e.getCurrentWorkUnit();
if (workUnit != null) {
Expand Down Expand Up @@ -1172,8 +1170,7 @@ public void delete() throws IOException, InterruptedException {
// (disregard whether they would be deletable in isolation)
// JENKINS-34939: do not hold the monitor on this folder while deleting them
// (thus we cannot do this inside performDelete)
SecurityContext orig = ACL.impersonate(ACL.SYSTEM);
try {
try (ACLContext oldContext = ACL.as(ACL.SYSTEM)) {
for (Item i : new ArrayList<Item>(items.values())) {
try {
i.delete();
Expand All @@ -1184,8 +1181,6 @@ public void delete() throws IOException, InterruptedException {
throw new IOException("Failed to delete " + i.getFullDisplayName(), e);
}
}
} finally {
SecurityContextHolder.setContext(orig);
}
synchronized (this) {
performDelete();
Expand All @@ -1198,7 +1193,7 @@ public void delete() throws IOException, InterruptedException {
}
// END of attempted reuse of JENKINS-35160
getParent().onDeleted(AbstractFolder.this);
Jenkins.getActiveInstance().rebuildDependencyGraphAsync();
Jenkins.get().rebuildDependencyGraphAsync();
}

/**
Expand Down Expand Up @@ -1246,7 +1241,7 @@ public void makeDisabled(boolean disabled) throws IOException {
}
setDisabled(disabled);
if (disabled && this instanceof Queue.Task) {
Jenkins.getActiveInstance().getQueue().cancel((Queue.Task)this);
Jenkins.get().getQueue().cancel((Queue.Task)this);
}
save();
ItemListener.fireOnUpdated(this);
Expand Down Expand Up @@ -1357,7 +1352,7 @@ public void doConfigSubmit(StaplerRequest req, StaplerResponse rsp) throws IOExc
bc.abort();
}

ProjectNamingStrategy namingStrategy = Jenkins.getActiveInstance().getProjectNamingStrategy();
ProjectNamingStrategy namingStrategy = Jenkins.get().getProjectNamingStrategy();
if (namingStrategy.isForceExistingJobs()) {
namingStrategy.checkName(name);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public boolean isIconConfigurable() {
}

public boolean isTabBarConfigurable() {
return Jenkins.getActiveInstance().getDescriptorList(ViewsTabBar.class).size() > 1;
return Jenkins.get().getDescriptorList(ViewsTabBar.class).size() > 1;
}

public boolean isLookAndFeelConfigurable(AbstractFolder<?> folder) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public boolean isApplicable(Class<? extends AbstractFolder> containerType) {
Class<?> applicable = Types.erasure(Types.getTypeArgument(pt, 0));
return applicable.isAssignableFrom(containerType);
} else {
throw new AssertionError(clazz+" doesn't properly parameterize AbstractFolderProperty. The isApplicable() method must be overriden.");
throw new AssertionError(clazz+" doesn't properly parameterize AbstractFolderProperty. The isApplicable() method must be overridden.");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
* {@link #dirNameFromItem(AbstractFolder, TopLevelItem)} regarding the constraints on how to name things</li>
* <li>There are some items which need the {@link Item#getRootDir()} during construction (those are bold evil item types
* that leak side-effects, you should fix them if you find them). While you wait for them to be fixed you will need
* to work-arround the issue by ensuring that you call {@link #beforeCreateItem(AbstractFolder, String, String)}
* to work-around the issue by ensuring that you call {@link #beforeCreateItem(AbstractFolder, String, String)}
* passing the {@link Item#getName()} you want the item to have <strong>and</strong> the ideal unmangled name
* <strong>before</strong> you call {@code new ChildItemType(parent,name)} and then call
* {@link #afterItemCreated(Trace)} when the constructor has returned. Then insure that your
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ public TopLevelItem doCreateItem(StaplerRequest req, StaplerResponse rsp) throws
try {
nue.delete();
} catch (InterruptedException x) {
throw (IOException) new IOException(x.toString()).initCause(x);
throw new IOException(x.toString(), x);
}
throw new IOException("forbidden child type");
}
Expand All @@ -257,15 +257,15 @@ public TopLevelItem createProjectFromXML(String name, InputStream xml) throws IO
try {
nue.delete();
} catch (InterruptedException x) {
throw (IOException) new IOException(x.toString()).initCause(x);
throw new IOException(x.toString(), x);
}
throw new IOException("forbidden child type");
}
return nue;
}

public <T extends TopLevelItem> T createProject(Class<T> type, String name) throws IOException {
return type.cast(createProject((TopLevelItemDescriptor) Jenkins.getActiveInstance().getDescriptor(type), name));
return type.cast(createProject((TopLevelItemDescriptor) Jenkins.get().getDescriptor(type), name));
}

public TopLevelItem createProject(TopLevelItemDescriptor type, String name) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@
Folder d;
if (context instanceof Folder) {
d = ((Folder) context);
} else if (context instanceof View && ((View) context).getOwnerItemGroup() instanceof Folder) {
d = (Folder) ((View) context).getOwnerItemGroup();
} else if (context instanceof View && ((View) context).getOwner().getItemGroup() instanceof Folder) {
d = (Folder) ((View) context).getOwner().getItemGroup();
} else {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,6 @@ protected void setFolder(Folder folder) {}

@Override
public FolderIconDescriptor getDescriptor() {
return (FolderIconDescriptor) Jenkins.getActiveInstance().getDescriptorOrDie(getClass());
return (FolderIconDescriptor) Jenkins.get().getDescriptorOrDie(getClass());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

public abstract class FolderIconDescriptor extends Descriptor<FolderIcon> {
public static DescriptorExtensionList<FolderIcon, FolderIconDescriptor> all() {
return Jenkins.getActiveInstance().<FolderIcon, FolderIconDescriptor>getDescriptorList(FolderIcon.class);
return Jenkins.get().getDescriptorList(FolderIcon.class);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,6 @@ public static List<FolderPropertyDescriptor> getPropertyDescriptors(Class<? exte
@Deprecated
@SuppressWarnings({"unchecked", "rawtypes"})
public static DescriptorExtensionList all() {
return Jenkins.getActiveInstance().getDescriptorList(FolderProperty.class);
return Jenkins.get().getDescriptorList(FolderProperty.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import hudson.model.Item;
import hudson.model.Queue;
import hudson.model.queue.SubTask;
import hudson.model.queue.Tasks;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
Expand Down Expand Up @@ -194,9 +193,9 @@ public boolean shouldSchedule(Queue.Task p, List<Action> actions) {
@CheckForNull
public static hudson.model.Item getItemOf(@Nonnull SubTask t) {
// TODO move to default method on SubTask once code level is Java 8
Queue.Task p = Tasks.getOwnerTaskOf(t);
Queue.Task p = t.getOwnerTask();
while (!(p instanceof hudson.model.Item)) {
Queue.Task o = Tasks.getOwnerTaskOf(p);
Queue.Task o = p.getOwnerTask();
if (o == p) {
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@

package com.cloudbees.hudson.plugins.folder;

import hudson.Extension;
import hudson.ExtensionList;
import hudson.ExtensionPoint;
import hudson.model.Action;
Expand Down Expand Up @@ -65,7 +64,7 @@ public abstract class TransientFolderActionFactory implements ExtensionPoint {
* @return all the registered {@link TransientFolderActionFactory}s.
*/
public static ExtensionList<TransientFolderActionFactory> all() {
return Jenkins.getActiveInstance().getExtensionList(TransientFolderActionFactory.class);
return Jenkins.get().getExtensionList(TransientFolderActionFactory.class);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public abstract class ChildObserver<I extends TopLevelItem> implements AutoClose
}

/**
* Checks whether there is an existing child which should be updated. It is <strong>stronly </strong>recommended to
* Checks whether there is an existing child which should be updated. It is <strong>strongly</strong> recommended to
* call {@link #completed(String)} after completion of processing the proposed {@link Item#getName()} as otherwise
* no other {@link ChildObserver} will be able to proceed with this {@link Item#getName()}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public abstract class ComputedFolder<I extends TopLevelItem> extends AbstractFol
@Nonnull
private transient /* almost final */ ReentrantLock currentObservationsLock;
/**
* Condition to flag whenever the {@link #currentObservationsChanged} has had elements removed.
* Condition to flag whenever the {@link #currentObservations} has had elements removed.
*
* @since 6.0.0
*/
Expand Down Expand Up @@ -314,16 +314,16 @@ private void applyDisabled(I item, boolean disabled) {
} catch (NoSuchMethodException | IllegalAccessException e) {
// it's just not supported, so we cannot expect it to succeed
LOGGER.log(Level.FINE,
"Cannot not " + (disabled ? "disable " : "enanble ") + item.getFullName(), e);
"Cannot not " + (disabled ? "disable " : "enable ") + item.getFullName(), e);
} catch (InvocationTargetException e) {
// it's supported but something went wrong
LOGGER.log(Level.WARNING,
"Could not " + (disabled ? "disable " : "enanble ") + item.getFullName(), e);
"Could not " + (disabled ? "disable " : "enable ") + item.getFullName(), e);
}
}
} catch (IOException e) {
LOGGER.log(Level.WARNING,
"Could not " + (disabled ? "disable " : "enanble ") + item.getFullName(), e);
"Could not " + (disabled ? "disable " : "enable ") + item.getFullName(), e);
}
}

Expand Down Expand Up @@ -526,7 +526,7 @@ public List<Action> getActions() {
/**
* Whether it is permissible to recompute this folder at this time.
*
* @return {@code true} if this folder can currenty be recomputed.
* @return {@code true} if this folder can currently be recomputed.
*/
public boolean isBuildable() {
if (isDisabled()) {
Expand Down Expand Up @@ -694,7 +694,7 @@ public Label getAssignedLabel() {
*/
@Override
public Node getLastBuiltOn() {
return Jenkins.getInstance();
return Jenkins.getInstanceOrNull();
}

/**
Expand Down Expand Up @@ -917,7 +917,7 @@ public void created(I child) {
LOGGER.log(Level.WARNING, "Failed to save " + child, x);
}
itemsPut(name, child);
Jenkins j = Jenkins.getInstance();
Jenkins j = Jenkins.getInstanceOrNull();
if (j != null) {
j.rebuildDependencyGraphAsync();
}
Expand Down Expand Up @@ -1038,7 +1038,7 @@ public void created(I child) {
LOGGER.log(Level.WARNING, "Failed to save " + child, x);
}
itemsPut(name, child);
Jenkins j = Jenkins.getInstance();
Jenkins j = Jenkins.getInstanceOrNull();
if (j != null) {
j.rebuildDependencyGraphAsync();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import hudson.XmlFile;
import hudson.console.AnnotatedLargeText;
import hudson.console.PlainTextConsoleOutputStream;
import hudson.model.Action;
import hudson.model.Actionable;
import hudson.model.BallColor;
import hudson.model.Cause;
Expand All @@ -52,7 +51,7 @@
import hudson.util.AlternativeUiTextProvider.Message;
import hudson.util.HttpResponses;
import hudson.util.StreamTaskListener;
import hudson.util.io.ReopenableRotatingFileOutputStream;
import hudson.util.io.RewindableRotatingFileOutputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
Expand Down Expand Up @@ -146,8 +145,8 @@ public void run() {
FileUtils.forceMkdir(logFile.getParentFile());
OutputStream os;
if (BACKUP_LOG_COUNT != null) {
os = new ReopenableRotatingFileOutputStream(logFile, BACKUP_LOG_COUNT);
((ReopenableRotatingFileOutputStream) os).rewind();
os = new RewindableRotatingFileOutputStream(logFile, BACKUP_LOG_COUNT);
((RewindableRotatingFileOutputStream) os).rewind();
} else {
os = new FileOutputStream(logFile);
}
Expand Down Expand Up @@ -241,7 +240,7 @@ public boolean canWriteNow() {
// TODO rework once JENKINS-42248 is solved
GregorianCalendar timestamp = new GregorianCalendar();
timestamp.setTimeInMillis(System.currentTimeMillis() - 10000L);
Queue.Item probe = new Queue.WaitingItem(timestamp, folder, Collections.<Action>emptyList());
Queue.Item probe = new Queue.WaitingItem(timestamp, folder, Collections.emptyList());
for (QueueTaskDispatcher d: QueueTaskDispatcher.all()) {
if (d.canRun(probe) != null) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public void doRun() {
* @param cal the date to check for.
*/
public void checkTriggers(final Calendar cal) {
for (ComputedFolder<?> p : Jenkins.getInstance().allItems(ComputedFolder.class)) {
for (ComputedFolder<?> p : Jenkins.get().allItems(ComputedFolder.class)) {
for (Trigger<?> t : p.getTriggers().values()) {
LOGGER.log(Level.FINE, "cron checking {0}", p.getName());
CronTabList tabs;
Expand Down
Loading