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

Java cleanup #4034

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 14 additions & 18 deletions core/src/main/java/hudson/PluginManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -1835,15 +1835,7 @@ public List<Future<UpdateCenter.UpdateCenterJob>> prevalidateConfig(InputStream
LOGGER.log(WARNING, "No such plugin {0} to install", requestedPlugin.getKey());
continue;
}
if (new VersionNumber(toInstall.version).compareTo(requestedPlugin.getValue()) < 0) {
LOGGER.log(WARNING, "{0} can only be satisfied in @{1}", new Object[] {requestedPlugin, toInstall.version});
}
if (toInstall.isForNewerHudson()) {
LOGGER.log(WARNING, "{0}@{1} was built for a newer Jenkins", new Object[] {toInstall.name, toInstall.version});
}
if (toInstall.isForNewerJava()) {
LOGGER.log(WARNING, "{0}@{1} was built for a newer Java", new Object[] {toInstall.name, toInstall.version});
}
logPluginWarnings(requestedPlugin, toInstall);
jobs.add(toInstall.deploy(true));
} else if (pw.isOlderThan(requestedPlugin.getValue())) { // upgrade
UpdateSite.Plugin toInstall = uc.getPlugin(requestedPlugin.getKey(), requestedPlugin.getValue());
Expand All @@ -1855,15 +1847,7 @@ public List<Future<UpdateCenter.UpdateCenterJob>> prevalidateConfig(InputStream
LOGGER.log(WARNING, "{0}@{1} is no newer than what we already have", new Object[] {toInstall.name, toInstall.version});
continue;
}
if (new VersionNumber(toInstall.version).compareTo(requestedPlugin.getValue()) < 0) {
LOGGER.log(WARNING, "{0} can only be satisfied in @{1}", new Object[] {requestedPlugin, toInstall.version});
}
if (toInstall.isForNewerHudson()) {
LOGGER.log(WARNING, "{0}@{1} was built for a newer Jenkins", new Object[] {toInstall.name, toInstall.version});
}
if (toInstall.isForNewerJava()) {
LOGGER.log(WARNING, "{0}@{1} was built for a newer Java", new Object[] {toInstall.name, toInstall.version});
}
logPluginWarnings(requestedPlugin, toInstall);
if (!toInstall.isCompatibleWithInstalledVersion()) {
LOGGER.log(WARNING, "{0}@{1} is incompatible with the installed @{2}", new Object[] {toInstall.name, toInstall.version, pw.getVersion()});
}
Expand All @@ -1873,6 +1857,18 @@ public List<Future<UpdateCenter.UpdateCenterJob>> prevalidateConfig(InputStream
return jobs;
}

private void logPluginWarnings(Map.Entry<String, VersionNumber> requestedPlugin, UpdateSite.Plugin toInstall) {
if (new VersionNumber(toInstall.version).compareTo(requestedPlugin.getValue()) < 0) {
LOGGER.log(WARNING, "{0} can only be satisfied in @{1}", new Object[] {requestedPlugin, toInstall.version});
}
if (toInstall.isForNewerHudson()) {
LOGGER.log(WARNING, "{0}@{1} was built for a newer Jenkins", new Object[] {toInstall.name, toInstall.version});
}
if (toInstall.isForNewerJava()) {
LOGGER.log(WARNING, "{0}@{1} was built for a newer Java", new Object[] {toInstall.name, toInstall.version});
}
}

/**
* Like {@link #doInstallNecessaryPlugins(StaplerRequest)} but only checks if everything is installed
* or if some plugins need updates or installation.
Expand Down
19 changes: 1 addition & 18 deletions core/src/main/java/hudson/cli/ConnectNodeCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,28 +61,11 @@ public String getShortDescription() {
@Override
protected int run() throws Exception {
boolean errorOccurred = false;
final Jenkins jenkins = Jenkins.getActiveInstance();

final HashSet<String> hs = new HashSet<>(nodes);

List<String> names = null;

for (String node_s : hs) {
Computer computer;

try {
computer = jenkins.getComputer(node_s);

if(computer == null) {
if(names == null) {
names = ComputerSet.getComputerNames();
}
String adv = EditDistance.findNearest(node_s, names);
throw new IllegalArgumentException(adv == null ?
hudson.model.Messages.Computer_NoSuchSlaveExistsWithoutAdvice(node_s) :
hudson.model.Messages.Computer_NoSuchSlaveExists(node_s, adv));
}

Computer computer = Computer.resolveForCLI(node_s);
computer.cliConnect(force);
} catch (Exception e) {
if (hs.size() == 1) {
Expand Down
15 changes: 1 addition & 14 deletions core/src/main/java/hudson/cli/OnlineNodeCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,24 +55,11 @@ public String getShortDescription() {
@Override
protected int run() throws Exception {
boolean errorOccurred = false;
final Jenkins jenkins = Jenkins.getActiveInstance();
final HashSet<String> hs = new HashSet<>(nodes);
List<String> names = null;

for (String node_s : hs) {
Computer computer;

try {
computer = jenkins.getComputer(node_s);
if (computer == null) {
if (names == null) {
names = ComputerSet.getComputerNames();
}
String adv = EditDistance.findNearest(node_s, names);
throw new IllegalArgumentException(adv == null ?
hudson.model.Messages.Computer_NoSuchSlaveExistsWithoutAdvice(node_s) :
hudson.model.Messages.Computer_NoSuchSlaveExists(node_s, adv));
}
Computer computer = Computer.resolveForCLI(node_s);
computer.cliOnline();
} catch (Exception e) {
if (hs.size() == 1) {
Expand Down
13 changes: 1 addition & 12 deletions core/src/main/java/hudson/model/Job.java
Original file line number Diff line number Diff line change
Expand Up @@ -997,19 +997,8 @@ public RunT getLastCompletedBuild() {
* if not enough builds satisfying the threshold have been found. Never null.
*/
public List<RunT> getLastBuildsOverThreshold(int numberOfBuilds, Result threshold) {

List<RunT> result = new ArrayList<>(numberOfBuilds);

RunT r = getLastBuild();
while (r != null && result.size() < numberOfBuilds) {
if (!r.isBuilding() &&
(r.getResult() != null && r.getResult().isBetterOrEqualTo(threshold))) {
result.add(r);
}
r = r.getPreviousBuild();
}

return result;
return r.getBuildsOverThreshold(r, numberOfBuilds, threshold);
}

/**
Expand Down
10 changes: 1 addition & 9 deletions core/src/main/java/hudson/model/ListView.java
Original file line number Diff line number Diff line change
Expand Up @@ -245,15 +245,7 @@ private List<TopLevelItem> getItems(boolean recurse) {
@Override
public SearchIndexBuilder makeSearchIndex() {
SearchIndexBuilder sib = new SearchIndexBuilder().addAllAnnotations(this);
sib.add(new CollectionSearchIndex<TopLevelItem>() {// for jobs in the view
protected TopLevelItem get(String key) { return getItem(key); }
protected Collection<TopLevelItem> all() { return getItems(); }
@Override
protected String getName(TopLevelItem o) {
// return the name instead of the display for suggestion searching
return o.getName();
}
});
makeSearchIndex(sib);
// add the display name for each item in the search index
addDisplayNamesToSearchIndex(sib, getItems(true));
return sib;
Expand Down
23 changes: 19 additions & 4 deletions core/src/main/java/hudson/model/Run.java
Original file line number Diff line number Diff line change
Expand Up @@ -948,17 +948,32 @@ protected void dropLinks() {
* @since 1.383
*/
public @Nonnull List<RunT> getPreviousBuildsOverThreshold(int numberOfBuilds, @Nonnull Result threshold) {
List<RunT> builds = new ArrayList<>(numberOfBuilds);

RunT r = getPreviousBuild();
return getBuildsOverThreshold(r, numberOfBuilds, threshold);
}

/**
* Returns the last {@code numberOfBuilds} builds with a build result ≥ {@code threshold}.
*
* @param r the run to search
* @param numberOfBuilds the desired number of builds
* @param threshold the build result threshold
* @return a list with the builds (youngest build first).
* May be smaller than 'numberOfBuilds' or even empty
* if not enough builds satisfying the threshold have been found. Never null.
* @since TODO
*/
protected @Nonnull List<RunT> getBuildsOverThreshold(RunT r, int numberOfBuilds, @Nonnull Result threshold) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That could be even made a public API now

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want it to be, sure. I'm trying not to change public api surfaces in these PRs.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

protected is also a public API, so no big difference 🤷‍♂

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, well, if people are ok w/ switching to public, I don't mind, it seemed like a reasonable improvement, I'm just not terribly familiar w/ how jenkins does api design.

List<RunT> builds = new ArrayList<>(numberOfBuilds);

while (r != null && builds.size() < numberOfBuilds) {
if (!r.isBuilding() &&
if (!r.isBuilding() &&
(r.getResult() != null && r.getResult().isBetterOrEqualTo(threshold))) {
builds.add(r);
}
r = r.getPreviousBuild();
}

return builds;
}

Expand Down
30 changes: 20 additions & 10 deletions core/src/main/java/hudson/model/View.java
Original file line number Diff line number Diff line change
Expand Up @@ -948,19 +948,29 @@ void addDisplayNamesToSearchIndex(SearchIndexBuilder sib, Collection<TopLevelIte
sib.add(item.getSearchUrl(), item.getDisplayName());
}
}


/**
* Add a simple CollectionSearchIndex object to sib
*
* @param sib the SearchIndexBuilder
* @since TODO
*/
protected void makeSearchIndex(SearchIndexBuilder sib) {
sib.add(new CollectionSearchIndex<TopLevelItem>() {// for jobs in the view
protected TopLevelItem get(String key) { return getItem(key); }
protected Collection<TopLevelItem> all() { return getItems(); }
@Override
protected String getName(TopLevelItem o) {
// return the name instead of the display for suggestion searching
return o.getName();
}
});
}

@Override
public SearchIndexBuilder makeSearchIndex() {
SearchIndexBuilder sib = super.makeSearchIndex();
sib.add(new CollectionSearchIndex<TopLevelItem>() {// for jobs in the view
protected TopLevelItem get(String key) { return getItem(key); }
protected Collection<TopLevelItem> all() { return getItems(); }
@Override
protected String getName(TopLevelItem o) {
// return the name instead of the display for suggestion searching
return o.getName();
}
});
makeSearchIndex(sib);

// add the display name for each item in the search index
addDisplayNamesToSearchIndex(sib, getItems());
Expand Down
30 changes: 12 additions & 18 deletions core/src/main/java/hudson/util/ChunkedInputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,18 +111,7 @@ public ChunkedInputStream(
*/
public int read() throws IOException {

if (closed) {
throw new IOException("Attempted read from closed stream.");
}
if (eof) {
return -1;
}
if (pos >= chunkSize) {
nextChunk();
if (eof) {
return -1;
}
}
if (advanceChunk()) return -1;
pos++;
return in.read();
}
Expand All @@ -141,23 +130,28 @@ public int read() throws IOException {
@Override
public int read (byte[] b, int off, int len) throws IOException {

if (advanceChunk()) return -1;
len = Math.min(len, chunkSize - pos);
int count = in.read(b, off, len);
pos += count;
return count;
}

private boolean advanceChunk() throws IOException {
if (closed) {
throw new IOException("Attempted read from closed stream.");
}

if (eof) {
return -1;
return true;
}
if (pos >= chunkSize) {
nextChunk();
if (eof) {
return -1;
return true;
}
}
len = Math.min(len, chunkSize - pos);
int count = in.read(b, off, len);
pos += count;
return count;
return false;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,24 +38,7 @@ public FilePath supplySettings(AbstractBuild<?, ?> build, TaskListener listener)
}

try {
EnvVars env = build.getEnvironment(listener);
String targetPath = Util.replaceMacro(this.path, build.getBuildVariableResolver());
targetPath = env.expand(targetPath);

if (IOUtils.isAbsolute(targetPath)) {
return new FilePath(new File(targetPath));
} else {
FilePath mrSettings = build.getModuleRoot().child(targetPath);
FilePath wsSettings = build.getWorkspace().child(targetPath);
try {
if (!wsSettings.exists() && mrSettings.exists()) {
wsSettings = mrSettings;
}
} catch (Exception e) {
throw new IllegalStateException("failed to find settings.xml at: " + wsSettings.getRemote());
}
return wsSettings;
}
return SettingsPathHelper.getSettings(build, listener, getPath());
} catch (Exception e) {
throw new IllegalStateException("failed to prepare global settings.xml");
}
Expand Down
19 changes: 1 addition & 18 deletions core/src/main/java/jenkins/mvn/FilePathSettingsProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,24 +39,7 @@ public FilePath supplySettings(AbstractBuild<?, ?> build, TaskListener listener)
}

try {
EnvVars env = build.getEnvironment(listener);
String targetPath = Util.replaceMacro(this.path, build.getBuildVariableResolver());
targetPath = env.expand(targetPath);

if (IOUtils.isAbsolute(targetPath)) {
return new FilePath(new File(targetPath));
} else {
FilePath mrSettings = build.getModuleRoot().child(targetPath);
FilePath wsSettings = build.getWorkspace().child(targetPath);
try {
if (!wsSettings.exists() && mrSettings.exists()) {
wsSettings = mrSettings;
}
} catch (Exception e) {
throw new IllegalStateException("failed to find settings.xml at: " + wsSettings.getRemote());
}
return wsSettings;
}
return SettingsPathHelper.getSettings(build, listener, getPath());
} catch (Exception e) {
throw new IllegalStateException("failed to prepare settings.xml");
}
Expand Down
37 changes: 37 additions & 0 deletions core/src/main/java/jenkins/mvn/SettingsPathHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package jenkins.mvn;

import hudson.EnvVars;
import hudson.FilePath;
import hudson.Util;
import hudson.model.AbstractBuild;
import hudson.model.TaskListener;
import hudson.util.IOUtils;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;

import java.io.File;
import java.io.IOException;

@Restricted(NoExternalUse.class)
class SettingsPathHelper {
static FilePath getSettings(AbstractBuild<?, ?> build, TaskListener listener, String path) throws IOException, InterruptedException {
EnvVars env = build.getEnvironment(listener);
String targetPath = Util.replaceMacro(path, build.getBuildVariableResolver());
targetPath = env.expand(targetPath);

if (IOUtils.isAbsolute(targetPath)) {
return new FilePath(new File(targetPath));
} else {
FilePath mrSettings = build.getModuleRoot().child(targetPath);
FilePath wsSettings = build.getWorkspace().child(targetPath);
try {
if (!wsSettings.exists() && mrSettings.exists()) {
wsSettings = mrSettings;
}
} catch (Exception e) {
throw new IllegalStateException("failed to find settings.xml at: " + wsSettings.getRemote());
}
return wsSettings;
}
}
}
Loading