From 5d58ce9f2f16c5849401d7fc761c2035655f9701 Mon Sep 17 00:00:00 2001 From: Daniel Beck Date: Tue, 27 Sep 2016 18:20:20 +0200 Subject: [PATCH 01/17] Merge pull request #2545 from Vlatombe/JENKINS-38187 [FIX JENKINS-38187] Fix setting JNLP port through system property (cherry picked from commit 3f5fe7f6b9b8dab37e82b360574102c6388729e8) --- .../security/GlobalSecurityConfiguration.java | 12 ++++ .../java/jenkins/install/SetupWizard.java | 7 ++- core/src/main/java/jenkins/model/Jenkins.java | 62 ++++++++++++++++++- .../GlobalSecurityConfiguration/index.groovy | 14 ++++- .../index.properties | 3 + .../message.jelly | 34 ++++++++++ .../message.properties | 2 + .../jenkins/model/Messages.properties | 2 + 8 files changed, 130 insertions(+), 6 deletions(-) create mode 100644 core/src/main/resources/hudson/security/GlobalSecurityConfiguration/index.properties create mode 100644 core/src/main/resources/jenkins/model/Jenkins/EnforceSlaveAgentPortAdministrativeMonitor/message.jelly create mode 100644 core/src/main/resources/jenkins/model/Jenkins/EnforceSlaveAgentPortAdministrativeMonitor/message.properties diff --git a/core/src/main/java/hudson/security/GlobalSecurityConfiguration.java b/core/src/main/java/hudson/security/GlobalSecurityConfiguration.java index 5fa446fef7..a27e575ee5 100644 --- a/core/src/main/java/hudson/security/GlobalSecurityConfiguration.java +++ b/core/src/main/java/hudson/security/GlobalSecurityConfiguration.java @@ -49,6 +49,9 @@ import net.sf.json.JSONArray; import net.sf.json.JSONObject; import org.jenkinsci.Symbol; +import org.kohsuke.accmod.Restricted; +import org.kohsuke.accmod.restrictions.DoNotUse; +import org.kohsuke.accmod.restrictions.NoExternalUse; import org.kohsuke.stapler.StaplerRequest; import org.kohsuke.stapler.StaplerResponse; @@ -72,6 +75,15 @@ public class GlobalSecurityConfiguration extends ManagementLink implements Descr return Jenkins.getInstance().getSlaveAgentPort(); } + /** + * @since TODO + * @return true if the slave agent port is enforced on this instance. + */ + @Restricted(DoNotUse.class) // only for index.groovy + public boolean isSlaveAgentPortEnforced() { + return Jenkins.getInstance().isSlaveAgentPortEnforced(); + } + public Set getAgentProtocols() { return Jenkins.getInstance().getAgentProtocols(); } diff --git a/core/src/main/java/jenkins/install/SetupWizard.java b/core/src/main/java/jenkins/install/SetupWizard.java index 4196a061a6..6368fb4b8f 100644 --- a/core/src/main/java/jenkins/install/SetupWizard.java +++ b/core/src/main/java/jenkins/install/SetupWizard.java @@ -20,6 +20,7 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequestWrapper; import javax.servlet.http.HttpServletResponse; +import jenkins.util.SystemProperties; import org.acegisecurity.Authentication; import org.acegisecurity.context.SecurityContextHolder; import org.acegisecurity.providers.UsernamePasswordAuthenticationToken; @@ -117,9 +118,9 @@ public class SetupWizard extends PageDecorator { authStrategy.setAllowAnonymousRead(false); jenkins.setAuthorizationStrategy(authStrategy); - // Shut down all the ports we can by default: - jenkins.setSlaveAgentPort(-1); // -1 to disable - + // Disable jnlp by default, but honor system properties + jenkins.setSlaveAgentPort(SystemProperties.getInteger(Jenkins.class.getName()+".slaveAgentPort",-1)); + // require a crumb issuer jenkins.setCrumbIssuer(new DefaultCrumbIssuer(false)); diff --git a/core/src/main/java/jenkins/model/Jenkins.java b/core/src/main/java/jenkins/model/Jenkins.java index 66090849f9..b25fbe2cc0 100644 --- a/core/src/main/java/jenkins/model/Jenkins.java +++ b/core/src/main/java/jenkins/model/Jenkins.java @@ -30,6 +30,7 @@ import antlr.ANTLRException; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Lists; +import com.google.inject.Inject; import com.google.inject.Injector; import com.thoughtworks.xstream.XStream; import hudson.BulkChange; @@ -596,7 +597,16 @@ public class Jenkins extends AbstractCIBase implements DirectlyModifiableTopLeve * TCP agent port. * 0 for random, -1 to disable. */ - private int slaveAgentPort = SystemProperties.getInteger(Jenkins.class.getName()+".slaveAgentPort",0); + private int slaveAgentPort = getSlaveAgentPortInitialValue(0); + + private static int getSlaveAgentPortInitialValue(int def) { + return SystemProperties.getInteger(Jenkins.class.getName()+".slaveAgentPort", def); + } + + /** + * If -Djenkins.model.Jenkins.slaveAgentPort is defined, enforce it on every start instead of only the first one. + */ + private static final boolean SLAVE_AGENT_PORT_ENFORCE = SystemProperties.getBoolean(Jenkins.class.getName()+".slaveAgentPortEnforce", false); /** * The TCP agent protocols that are explicitly disabled (we store the disabled ones so that newer protocols @@ -982,6 +992,9 @@ public class Jenkins extends AbstractCIBase implements DirectlyModifiableTopLeve if (jdks == null) { jdks = new ArrayList<>(); } + if (SLAVE_AGENT_PORT_ENFORCE) { + slaveAgentPort = getSlaveAgentPortInitialValue(slaveAgentPort); + } return this; } @@ -1091,11 +1104,26 @@ public class Jenkins extends AbstractCIBase implements DirectlyModifiableTopLeve return slaveAgentPort; } + /** + * @since TODO + */ + public boolean isSlaveAgentPortEnforced() { + return Jenkins.SLAVE_AGENT_PORT_ENFORCE; + } + /** * @param port * 0 to indicate random available TCP port. -1 to disable this service. */ public void setSlaveAgentPort(int port) throws IOException { + if (SLAVE_AGENT_PORT_ENFORCE) { + LOGGER.log(Level.WARNING, "setSlaveAgentPort({0}) call ignored because system property {1} is true", new String[] { Integer.toString(port), Jenkins.class.getName()+".slaveAgentPortEnforce" }); + } else { + forceSetSlaveAgentPort(port); + } + } + + private void forceSetSlaveAgentPort(int port) throws IOException { this.slaveAgentPort = port; launchTcpSlaveAgentListener(); } @@ -1206,6 +1234,38 @@ public class Jenkins extends AbstractCIBase implements DirectlyModifiableTopLeve } } + @Extension + @Restricted(NoExternalUse.class) + public static class EnforceSlaveAgentPortAdministrativeMonitor extends AdministrativeMonitor { + @Inject + Jenkins j; + + @Override + public String getDisplayName() { + return jenkins.model.Messages.EnforceSlaveAgentPortAdministrativeMonitor_displayName(); + } + + public String getSystemPropertyName() { + return Jenkins.class.getName() + ".slaveAgentPort"; + } + + public int getExpectedPort() { + int slaveAgentPort = j.slaveAgentPort; + return Jenkins.getSlaveAgentPortInitialValue(slaveAgentPort); + } + + public void doAct(StaplerRequest req, StaplerResponse rsp) throws IOException { + j.forceSetSlaveAgentPort(getExpectedPort()); + rsp.sendRedirect2(req.getContextPath() + "/manage"); + } + + @Override + public boolean isActivated() { + int slaveAgentPort = Jenkins.getInstance().slaveAgentPort; + return SLAVE_AGENT_PORT_ENFORCE && slaveAgentPort != Jenkins.getSlaveAgentPortInitialValue(slaveAgentPort); + } + } + public void setNodeName(String name) { throw new UnsupportedOperationException(); // not allowed } diff --git a/core/src/main/resources/hudson/security/GlobalSecurityConfiguration/index.groovy b/core/src/main/resources/hudson/security/GlobalSecurityConfiguration/index.groovy index e56f86720c..f082a3fce5 100644 --- a/core/src/main/resources/hudson/security/GlobalSecurityConfiguration/index.groovy +++ b/core/src/main/resources/hudson/security/GlobalSecurityConfiguration/index.groovy @@ -26,8 +26,18 @@ l.layout(norefresh:true, permission:app.ADMINISTER, title:my.displayName, csscla set("descriptor", my.descriptor); f.optionalBlock( field:"useSecurity", title:_("Enable security"), checked:app.useSecurity) { - f.entry (title:_("TCP port for JNLP agents"), field:"slaveAgentPort") { - f.serverTcpPort() + f.entry(title: _("TCP port for JNLP agents"), field: "slaveAgentPort") { + if (my.slaveAgentPortEnforced) { + if (my.slaveAgentPort == -1) { + text(_("slaveAgentPortEnforcedDisabled")) + } else if (my.slaveAgentPort == 0) { + text(_("slaveAgentPortEnforcedRandom")) + } else { + text(_("slaveAgentPortEnforced", my.slaveAgentPort)) + } + } else { + f.serverTcpPort() + } } f.advanced(title: _("Agent protocols"), align:"left") { f.entry(title: _("Agent protocols")) { diff --git a/core/src/main/resources/hudson/security/GlobalSecurityConfiguration/index.properties b/core/src/main/resources/hudson/security/GlobalSecurityConfiguration/index.properties new file mode 100644 index 0000000000..4c959311a8 --- /dev/null +++ b/core/src/main/resources/hudson/security/GlobalSecurityConfiguration/index.properties @@ -0,0 +1,3 @@ +slaveAgentPortEnforced=enforced to {0,number,#} on startup through system property. +slaveAgentPortEnforcedRandom=enforced to random port on startup through system property. +slaveAgentPortEnforcedDisabled=disabled on startup through system property. diff --git a/core/src/main/resources/jenkins/model/Jenkins/EnforceSlaveAgentPortAdministrativeMonitor/message.jelly b/core/src/main/resources/jenkins/model/Jenkins/EnforceSlaveAgentPortAdministrativeMonitor/message.jelly new file mode 100644 index 0000000000..bc2149f752 --- /dev/null +++ b/core/src/main/resources/jenkins/model/Jenkins/EnforceSlaveAgentPortAdministrativeMonitor/message.jelly @@ -0,0 +1,34 @@ + + + +
+ ${%description(it.systemPropertyName, it.expectedPort)} +
+
+ +
+
+
+
diff --git a/core/src/main/resources/jenkins/model/Jenkins/EnforceSlaveAgentPortAdministrativeMonitor/message.properties b/core/src/main/resources/jenkins/model/Jenkins/EnforceSlaveAgentPortAdministrativeMonitor/message.properties new file mode 100644 index 0000000000..e4f6f0fd8c --- /dev/null +++ b/core/src/main/resources/jenkins/model/Jenkins/EnforceSlaveAgentPortAdministrativeMonitor/message.properties @@ -0,0 +1,2 @@ +description=JNLP Agent Port has been changed but was specified through system property {0} on startup. Its value will be reset to {1,number,#} on restart. +reset=Reset to {0,number,#} diff --git a/core/src/main/resources/jenkins/model/Messages.properties b/core/src/main/resources/jenkins/model/Messages.properties index a1bb72ffe1..99fc5c3960 100644 --- a/core/src/main/resources/jenkins/model/Messages.properties +++ b/core/src/main/resources/jenkins/model/Messages.properties @@ -68,3 +68,5 @@ ParameterizedJobMixIn.build_now=Build Now BlockedBecauseOfBuildInProgress.shortDescription=Build #{0} is already in progress{1} BlockedBecauseOfBuildInProgress.ETA=\ (ETA:{0}) BuildDiscarderProperty.displayName=Discard old builds + +EnforceSlaveAgentPortAdministrativeMonitor.displayName=Enforce JNLP Slave Agent Port From 4421d1b94d143956475f20a03c63fb1a367321f2 Mon Sep 17 00:00:00 2001 From: Akbashev Alexander Date: Sat, 15 Oct 2016 17:35:09 +0200 Subject: [PATCH 02/17] [FIX JENKINS-23244] Slave build history page has no data and spawns a ton of very long-lived blocking threads on the master (#2584) Mainly commit are doing two things: 1) Show only selected (visible) builds 2) Query build one-by-one - not it parallel (cherry picked from commit 2a0ac4f0989407a20e277444a7737e9c5f7ea78a) --- .../model/BuildTimelineWidget/control.jelly | 49 +++++++++++-------- 1 file changed, 28 insertions(+), 21 deletions(-) diff --git a/core/src/main/resources/hudson/model/BuildTimelineWidget/control.jelly b/core/src/main/resources/hudson/model/BuildTimelineWidget/control.jelly index f695275f06..a9efb2525b 100644 --- a/core/src/main/resources/hudson/model/BuildTimelineWidget/control.jelly +++ b/core/src/main/resources/hudson/model/BuildTimelineWidget/control.jelly @@ -41,35 +41,42 @@ THE SOFTWARE. var tz = ${(tz.rawOffset + tz.DSTSavings) / 3600000}; var tl = null; + var interval = 24*60*60*1000; Date: Sat, 28 Nov 2015 16:23:59 +0800 Subject: [PATCH 03/17] fix JENKINS-31768 dead lock while removing computer (cherry picked from commit c7fd90a8796fe0ad80bfe37a9837af4515fbf5cb) --- core/src/main/java/hudson/model/Computer.java | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/hudson/model/Computer.java b/core/src/main/java/hudson/model/Computer.java index 00f52e197c..9085656728 100644 --- a/core/src/main/java/hudson/model/Computer.java +++ b/core/src/main/java/hudson/model/Computer.java @@ -862,7 +862,19 @@ public /*transient*/ abstract class Computer extends Actionable implements Acces protected void onRemoved(){ } - private synchronized void setNumExecutors(int n) { + /** + * Calling path, *means protected by Queue.withLock + * + * Computer.doConfigSubmit -> Computer.replaceBy ->Jenkins.setNodes* ->Computer.setNode + * AbstractCIBase.updateComputerList->Computer.inflictMortalWound* + * AbstractCIBase.updateComputerList->AbstractCIBase.updateComputer* ->Computer.setNode + * AbstractCIBase.updateComputerList->AbstractCIBase.killComputer->Computer.kill + * Computer.constructor->Computer.setNode + * Computer.kill is called after numExecutors set to zero(Computer.inflictMortalWound) so not need the Queue.lock + * + * @param number of executors + */ + private void setNumExecutors(int n) { this.numExecutors = n; final int diff = executors.size()-n; From 2f0632ae189a11b439e10a59918434f3248a5787 Mon Sep 17 00:00:00 2001 From: lpancescu Date: Sun, 18 Sep 2016 06:15:23 +0200 Subject: [PATCH 04/17] [JENKINS-10912] Use setInterval in refreshPart (#2539) * [JENKINS-10912] Use setInterval in refreshPart The current implementation of refreshPart creates a new closure every 5 seconds, which, in combination with XMLHttpRequest objects, results in a significant memory leak in all major browsers. By using window.setInterval to schedule periodic refreshes, only one closure per id is created. Please see issue #10912 in the Jenkins tracker for further details. * Stop periodical calls if we can't find the div * Don't check if isRunAsTest changed after page load (cherry picked from commit 096614a6d72c19633909248cb5b06a179b8b4040) --- war/src/main/webapp/scripts/hudson-behavior.js | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/war/src/main/webapp/scripts/hudson-behavior.js b/war/src/main/webapp/scripts/hudson-behavior.js index 1b89841a45..2966527713 100644 --- a/war/src/main/webapp/scripts/hudson-behavior.js +++ b/war/src/main/webapp/scripts/hudson-behavior.js @@ -1486,6 +1486,7 @@ function expandTextArea(button,id) { // refresh a part of the HTML specified by the given ID, // by using the contents fetched from the given URL. function refreshPart(id,url) { + var intervalID = null; var f = function() { if(isPageVisible()) { new Ajax.Request(url, { @@ -1493,6 +1494,8 @@ function refreshPart(id,url) { var hist = $(id); if (hist == null) { console.log("There's no element that has ID of " + id); + if (intervalID !== null) + window.clearInterval(intervalID); return; } var p = hist.up(); @@ -1505,22 +1508,14 @@ function refreshPart(id,url) { Behaviour.applySubtree(node); layoutUpdateCallback.call(); - - if(isRunAsTest) return; - refreshPart(id,url); } - }); - } else { - // Reschedule - if(isRunAsTest) return; - refreshPart(id,url); + }); } - }; // if run as test, just do it once and do it now to make sure it's working, // but don't repeat. if(isRunAsTest) f(); - else window.setTimeout(f, 5000); + else intervalID = window.setInterval(f, 5000); } From 6edd35d11b384a935aadcdab20baca1a6df8b3d6 Mon Sep 17 00:00:00 2001 From: Andres Rodriguez Date: Wed, 12 Oct 2016 17:45:38 +0200 Subject: [PATCH 05/17] [JENKINS-38814] Use GREEDY `RemoteInputStream`s (#2583) (cherry picked from commit b62ad15fef4790444af746ac4ae9149c37e89e07) --- core/src/main/java/hudson/FilePath.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/hudson/FilePath.java b/core/src/main/java/hudson/FilePath.java index 6b2529eabe..67499703e9 100644 --- a/core/src/main/java/hudson/FilePath.java +++ b/core/src/main/java/hudson/FilePath.java @@ -555,7 +555,7 @@ public final class FilePath implements Serializable { * @see #unzip(FilePath) */ public void unzipFrom(InputStream _in) throws IOException, InterruptedException { - final InputStream in = new RemoteInputStream(_in); + final InputStream in = new RemoteInputStream(_in, Flag.GREEDY); act(new SecureFileCallable() { public Void invoke(File dir, VirtualChannel channel) throws IOException { unzip(dir, in); @@ -724,7 +724,7 @@ public final class FilePath implements Serializable { */ public void untarFrom(InputStream _in, final TarCompression compression) throws IOException, InterruptedException { try { - final InputStream in = new RemoteInputStream(_in); + final InputStream in = new RemoteInputStream(_in, Flag.GREEDY); act(new SecureFileCallable() { public Void invoke(File dir, VirtualChannel channel) throws IOException { readFromTar("input stream",dir, compression.extract(in)); From 62a5dddb3c2b00c4602a504ffefd65320cf81915 Mon Sep 17 00:00:00 2001 From: Oleg Nenashev Date: Sun, 6 Nov 2016 23:09:15 +0100 Subject: [PATCH 06/17] Update remoting to 2.62.2 This PR picks the latest available version of remoting stable-2.x. All the fixes have been integrated into remoting-3.0 and soaked enough. * [JENKINS-38539](https://issues.jenkins-ci.org/browse/JENKINS-38539) - Stability: Turn on SO_KEEPALIVE by default and provide CLI option to turn it off again. (https://github.com/jenkinsci/remoting/pull/110) * [JENKINS-37539](https://issues.jenkins-ci.org/browse/JENKINS-37539) - Prevent NullPointerException in Engine#connect() when host or port parameters are null or empty. (https://github.com/jenkinsci/remoting/pull/101) * [CID-152201] - Fix resource leak in remoting.jnlp.Main. (https://github.com/jenkinsci/remoting/pull/102) * [CID-152200,CID-152202] - Resource leak in Encryption Cipher I/O streams on exceptional paths. (https://github.com/jenkinsci/remoting/pull/104) --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index b3a1b40b91..7c42bbddb3 100644 --- a/pom.xml +++ b/pom.xml @@ -180,7 +180,7 @@ THE SOFTWARE. org.jenkins-ci.main remoting - 2.62 + 2.62.2 From e85e3e2b907f894b536332d80b997711f73c4174 Mon Sep 17 00:00:00 2001 From: Daniel Beck Date: Tue, 25 Oct 2016 11:13:55 +0200 Subject: [PATCH 07/17] [JENKINS-26940] Print message when installer isn't applicable (#2598) * [JENKINS-26940] Print message when installer isn't applicable * [JENKINS-26940] Only print when none found, add test (cherry picked from commit ae29e6b2463754778c3988e62292a07a846ffe57) --- .../hudson/tools/InstallerTranslator.java | 12 ++++++ .../hudson/tools/Messages.properties | 1 + .../hudson/tools/InstallerTranslatorTest.java | 39 +++++++++++++++++++ 3 files changed, 52 insertions(+) diff --git a/core/src/main/java/hudson/tools/InstallerTranslator.java b/core/src/main/java/hudson/tools/InstallerTranslator.java index 87d1dcd1e3..fd45212bd7 100644 --- a/core/src/main/java/hudson/tools/InstallerTranslator.java +++ b/core/src/main/java/hudson/tools/InstallerTranslator.java @@ -28,6 +28,7 @@ import hudson.Extension; import hudson.model.Node; import hudson.model.TaskListener; import java.io.IOException; +import java.util.ArrayList; import java.util.Map; import java.util.WeakHashMap; import java.util.concurrent.Semaphore; @@ -50,6 +51,9 @@ public class InstallerTranslator extends ToolLocationTranslator { if (isp == null) { return null; } + + ArrayList inapplicableInstallersMessages = new ArrayList(); + for (ToolInstaller installer : isp.installers) { if (installer.appliesTo(node)) { Semaphore semaphore; @@ -69,8 +73,16 @@ public class InstallerTranslator extends ToolLocationTranslator { } finally { semaphore.release(); } + } else { + inapplicableInstallersMessages.add(Messages.CannotBeInstalled( + installer.getDescriptor().getDisplayName(), + tool.getName(), + node.getDisplayName())); } } + for (String message : inapplicableInstallersMessages) { + log.getLogger().println(message); + } return null; } diff --git a/core/src/main/resources/hudson/tools/Messages.properties b/core/src/main/resources/hudson/tools/Messages.properties index dabfa4c840..a59e064de7 100644 --- a/core/src/main/resources/hudson/tools/Messages.properties +++ b/core/src/main/resources/hudson/tools/Messages.properties @@ -37,3 +37,4 @@ JDKInstaller.DescriptorImpl.displayName=Install from java.sun.com JDKInstaller.DescriptorImpl.doCheckId=Define JDK ID JDKInstaller.DescriptorImpl.doCheckAcceptLicense=You must agree to the license to download the JDK. ToolDescriptor.NotADirectory={0} is not a directory on the Jenkins master (but perhaps it exists on some agents) +CannotBeInstalled=Installer "{0}" cannot be used to install "{1}" on the node "{2}" diff --git a/test/src/test/java/hudson/tools/InstallerTranslatorTest.java b/test/src/test/java/hudson/tools/InstallerTranslatorTest.java index 3d9e2a58fe..d38f780972 100644 --- a/test/src/test/java/hudson/tools/InstallerTranslatorTest.java +++ b/test/src/test/java/hudson/tools/InstallerTranslatorTest.java @@ -35,6 +35,8 @@ import hudson.slaves.RetentionStrategy; import hudson.tasks.Shell; import hudson.util.StreamTaskListener; import java.nio.charset.Charset; +import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import org.junit.Test; import static org.junit.Assert.*; @@ -98,4 +100,41 @@ public class InstallerTranslatorTest { r.assertLogContains("/opt/jdk1", b6); } + @Issue("JENKINS-26940") + @Test + public void testMessageLoggedWhenNoInstallerFound() throws Exception { + final CommandInstaller ci = new CommandInstaller("wrong1", "echo hello", "/opt/jdk"); + final BatchCommandInstaller bci = new BatchCommandInstaller("wrong2", "echo hello", "/opt/jdk2"); + InstallSourceProperty isp = new InstallSourceProperty(Arrays.asList(ci, bci)); + + JDK jdk = new JDK("jdk", null, Collections.singletonList(isp)); + r.jenkins.getJDKs().add(jdk); + + + FreeStyleProject p = r.createFreeStyleProject(); + p.setJDK(jdk); + p.getBuildersList().add(new Shell("echo $JAVA_HOME")); + FreeStyleBuild b1 = r.buildAndAssertSuccess(p); + r.assertLogContains(hudson.tools.Messages.CannotBeInstalled(ci.getDescriptor().getDisplayName(), jdk.getName(), r.jenkins.getDisplayName()), b1); + r.assertLogContains(hudson.tools.Messages.CannotBeInstalled(bci.getDescriptor().getDisplayName(), jdk.getName(), r.jenkins.getDisplayName()), b1); + } + + @Issue("JENKINS-26940") + @Test + public void testNoMessageLoggedWhenAnyInstallerFound() throws Exception { + final CommandInstaller ci = new CommandInstaller("wrong1", "echo hello", "/opt/jdk"); + final CommandInstaller ci2 = new CommandInstaller("master", "echo hello", "/opt/jdk2"); + InstallSourceProperty isp = new InstallSourceProperty(Arrays.asList(ci, ci2)); + + JDK jdk = new JDK("jdk", null, Collections.singletonList(isp)); + r.jenkins.getJDKs().add(jdk); + + + FreeStyleProject p = r.createFreeStyleProject(); + p.setJDK(jdk); + p.getBuildersList().add(new Shell("echo $JAVA_HOME")); + FreeStyleBuild b1 = r.buildAndAssertSuccess(p); + r.assertLogNotContains(ci.getDescriptor().getDisplayName(), b1); + } + } From 7e8ea4a967057c3fc9ae4dc72028f780d9fa39e1 Mon Sep 17 00:00:00 2001 From: Jonathan Fuentes Date: Sat, 17 Sep 2016 21:41:14 -0700 Subject: [PATCH 08/17] [FIXED JENKINS-36539] (#2495) (cherry picked from commit fbb4dc05b5122d9ba09419c60a2a6092e675e7a8) --- war/src/main/js/add-item.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/war/src/main/js/add-item.js b/war/src/main/js/add-item.js index 22338ece65..7156144471 100644 --- a/war/src/main/js/add-item.js +++ b/war/src/main/js/add-item.js @@ -107,7 +107,6 @@ $.when(getItems()).done(function(data) { btn.removeClass('disabled'); btn.prop('disabled', false); } - btn.focus(); } else { if (!btn.hasClass('disabled')) { btn.addClass('disabled'); @@ -233,7 +232,7 @@ $.when(getItems()).done(function(data) { $("#add-item-panel").find("#name").focus(); // Init NameField - $('input[name="name"]', '#createItem').blur(function() { + $('input[name="name"]', '#createItem').on("blur input", function() { if (!isItemNameEmpty()) { var itemName = $('input[name="name"]', '#createItem').val(); $.get("checkJobName", { value: itemName }).done(function(data) { From b10440702f74c70786820bdf38d417569d86644d Mon Sep 17 00:00:00 2001 From: Oleg Nenashev Date: Sat, 5 Nov 2016 21:42:20 +0400 Subject: [PATCH 09/17] [FIXED JENKINS-38487] - Jenkins startup must not fail in the case of ComputerListener failure (#2610) Without this code Jenkinbs startup fails if EnvInject fails to find global property file on startup. Javadoc says "Exceptions will be recorded to the listener. Note that throwing an exception doesn't put the computer offline." regarding the listener method exception, hence we should not block Jenkins startup (cherry picked from commit 58e1228c99fd3d5063b0967d7780dc536bc2c463) --- .../java/hudson/slaves/ComputerListener.java | 2 +- core/src/main/java/jenkins/model/Jenkins.java | 24 +++++++++++++++---- .../test/java/jenkins/model/JenkinsTest.java | 19 +++++++++++++++ 3 files changed, 39 insertions(+), 6 deletions(-) diff --git a/core/src/main/java/hudson/slaves/ComputerListener.java b/core/src/main/java/hudson/slaves/ComputerListener.java index c2e4dc1f70..b166a6c40d 100644 --- a/core/src/main/java/hudson/slaves/ComputerListener.java +++ b/core/src/main/java/hudson/slaves/ComputerListener.java @@ -143,7 +143,7 @@ public abstract class ComputerListener implements ExtensionPoint { * Starting Hudson 1.312, this method is also invoked for the master, not just for agents. * * @param listener - * This is connected to the launch log of the computer. + * This is connected to the launch log of the computer or Jenkins master. * Since this method is called synchronously from the thread * that launches a computer, if this method performs a time-consuming * operation, this listener should be notified of the progress. diff --git a/core/src/main/java/jenkins/model/Jenkins.java b/core/src/main/java/jenkins/model/Jenkins.java index b25fbe2cc0..3a3a89c45a 100644 --- a/core/src/main/java/jenkins/model/Jenkins.java +++ b/core/src/main/java/jenkins/model/Jenkins.java @@ -957,11 +957,25 @@ public class Jenkins extends AbstractCIBase implements DirectlyModifiableTopLeve updateComputerList(); - {// master is online now - Computer c = toComputer(); - if(c!=null) - for (ComputerListener cl : ComputerListener.all()) - cl.onOnline(c, new LogTaskListener(LOGGER, INFO)); + {// master is online now, it's instance must always exist + final Computer c = toComputer(); + if(c != null) { + for (ComputerListener cl : ComputerListener.all()) { + try { + cl.onOnline(c, new LogTaskListener(LOGGER, INFO)); + } catch (Throwable t) { + if (t instanceof Error) { + // We propagate Runtime errors, because they are fatal. + throw t; + } + + // Other exceptions should be logged instead of failing the Jenkins startup (See listener's Javadoc) + // We also throw it for InterruptedException since it's what is expected according to the javadoc + LOGGER.log(SEVERE, String.format("Invocation of the computer listener %s failed for the Jenkins master node", + cl.getClass()), t); + } + } + } } for (ItemListener l : ItemListener.all()) { diff --git a/test/src/test/java/jenkins/model/JenkinsTest.java b/test/src/test/java/jenkins/model/JenkinsTest.java index 936e1ae977..27528d2e04 100644 --- a/test/src/test/java/jenkins/model/JenkinsTest.java +++ b/test/src/test/java/jenkins/model/JenkinsTest.java @@ -42,6 +42,7 @@ import com.gargoylesoftware.htmlunit.html.HtmlPage; import hudson.maven.MavenModuleSet; import hudson.maven.MavenModuleSetBuild; +import hudson.model.Computer; import hudson.model.Failure; import hudson.model.RestartListener; import hudson.model.RootAction; @@ -51,6 +52,7 @@ import hudson.security.FullControlOnceLoggedInAuthorizationStrategy; import hudson.security.HudsonPrivateSecurityRealm; import hudson.util.HttpResponses; import hudson.model.FreeStyleProject; +import hudson.model.TaskListener; import hudson.security.GlobalMatrixAuthorizationStrategy; import hudson.security.LegacySecurityRealm; import hudson.security.Permission; @@ -70,6 +72,7 @@ import org.kohsuke.stapler.HttpResponse; import org.mockito.ArgumentCaptor; import org.mockito.Mockito; +import java.io.IOException; import java.net.HttpURLConnection; import java.net.URL; @@ -452,4 +455,20 @@ public class JenkinsTest { assertThat(rsp.getContentAsString(), containsString("Node is offline")); assertThat(rsp.getStatusCode(), equalTo(404)); } + + @Test + @Issue("JENKINS-38487") + public void startupShouldNotFailOnFailingOnlineListener() { + // We do nothing, FailingOnOnlineListener & JenkinsRule should cause the + // boot failure if the issue is not fixed. + } + + @TestExtension(value = "startupShouldNotFailOnFailingOnlineListener") + public static final class FailingOnOnlineListener extends ComputerListener { + + @Override + public void onOnline(Computer c, TaskListener listener) throws IOException, InterruptedException { + throw new IOException("Something happened (the listener always throws this exception)"); + } + } } From f80c59ab3daae29d8a0b9e92af2d6ebc5cca1497 Mon Sep 17 00:00:00 2001 From: Vincent Latombe Date: Sun, 2 Oct 2016 09:15:56 -0700 Subject: [PATCH 10/17] [JENKINS-38651] Display actions created through TransientActionFactory in label view (cherry picked from commit 8d55b1f555194be546152a972294f0a64a052da9) --- core/src/main/resources/hudson/model/Label/index.jelly | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/main/resources/hudson/model/Label/index.jelly b/core/src/main/resources/hudson/model/Label/index.jelly index 931d7932df..10b239fd63 100644 --- a/core/src/main/resources/hudson/model/Label/index.jelly +++ b/core/src/main/resources/hudson/model/Label/index.jelly @@ -36,7 +36,7 @@ THE SOFTWARE. - + From 936091b42c425abc2a0afbcb5b7e49497eff3e95 Mon Sep 17 00:00:00 2001 From: gusreiber Date: Sun, 9 Oct 2016 23:19:37 -0700 Subject: [PATCH 11/17] [JENKINS-35263] displaying codeMirror as table to fix page sizing (#2575) (cherry picked from commit 67df10db2d4e0620ba4e7fde399c4340d39604b6) --- core/src/main/resources/lib/form/textarea/textarea.css | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 core/src/main/resources/lib/form/textarea/textarea.css diff --git a/core/src/main/resources/lib/form/textarea/textarea.css b/core/src/main/resources/lib/form/textarea/textarea.css new file mode 100644 index 0000000000..71300ae0e3 --- /dev/null +++ b/core/src/main/resources/lib/form/textarea/textarea.css @@ -0,0 +1,5 @@ +td.setting-main .CodeMirror { + display: table; + table-layout: fixed; + width: 100%; +} \ No newline at end of file From d2514bbe0816a6bb2e00588d5fb4d577054319bb Mon Sep 17 00:00:00 2001 From: Felix Belzunce Arcos Date: Sun, 16 Oct 2016 01:59:06 +0200 Subject: [PATCH 12/17] [FIXED JENKINS-31871] Properly handle single quotes in item names (#1943) (cherry picked from commit bee66ede31d45b9d0814e4a45cbf2223585817e2) --- core/src/main/java/hudson/util/FormValidation.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/main/java/hudson/util/FormValidation.java b/core/src/main/java/hudson/util/FormValidation.java index 4c52f19b6d..c6dddca183 100644 --- a/core/src/main/java/hudson/util/FormValidation.java +++ b/core/src/main/java/hudson/util/FormValidation.java @@ -638,7 +638,7 @@ public abstract class FormValidation extends IOException implements HttpResponse */ public String toStemUrl() { if (names==null) return null; - return jsStringEscape(Descriptor.getCurrentDescriptorByNameUrl()) + '/' + relativePath(); + return Descriptor.getCurrentDescriptorByNameUrl() + '/' + relativePath(); } public String getDependsOn() { From 47a1682d8e0feb4eda4f399348fa02b33aaf1316 Mon Sep 17 00:00:00 2001 From: Oleg Nenashev Date: Tue, 8 Nov 2016 00:07:40 +0100 Subject: [PATCH 13/17] [JENKINS-39465] - Fix the AgentProtocol settings persistency handling (#2621) * [JENKINS-39465] - Tweak processing of enabled and disabled protocols in Jenkins instance Due to whatever reason, without a definition of an array recipient field the data goes to the disk in the following way: ``` JNLP3-connect JNLP4-connect ``` It is supposed to processed by Implicit array correctly, but it does not actually happen. With a fix the data is being stored in another format: ``` JNLP3-connect JNLP4-connect ``` This data now works correctly and gets deserialized correctly. readResolve() just adds a fallback for the case when Implicit array handling starts behaving correctly (?). * [JENKINS-39465] - Add configuration roundtrip tests * [JENKINS-39465] - Jenkins#agentProtocols cache must be invalidated when we reload the configuration * [JENKINS-39465] - Remove obsolete comment from Tests (cherry picked from commit 3e2e01717976a1f1221874bfd576429c5c48b8a6) --- core/src/main/java/jenkins/model/Jenkins.java | 24 +++- .../test/java/jenkins/model/JenkinsTest.java | 121 ++++++++++++++++++ 2 files changed, 143 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/jenkins/model/Jenkins.java b/core/src/main/java/jenkins/model/Jenkins.java index 3a3a89c45a..5e034b3ffb 100644 --- a/core/src/main/java/jenkins/model/Jenkins.java +++ b/core/src/main/java/jenkins/model/Jenkins.java @@ -616,6 +616,11 @@ public class Jenkins extends AbstractCIBase implements DirectlyModifiableTopLeve */ @CheckForNull private List disabledAgentProtocols; + /** + * @deprecated Just a temporary buffer for XSTream migration code from JENKINS-39465, do not use + */ + @Deprecated + private transient String[] _disabledAgentProtocols; /** * The TCP agent protocols that are {@link AgentProtocol#isOptIn()} and explicitly enabled. @@ -625,6 +630,11 @@ public class Jenkins extends AbstractCIBase implements DirectlyModifiableTopLeve */ @CheckForNull private List enabledAgentProtocols; + /** + * @deprecated Just a temporary buffer for XSTream migration code from JENKINS-39465, do not use + */ + @Deprecated + private transient String[] _enabledAgentProtocols; /** * The TCP agent protocols that are enabled. Built from {@link #disabledAgentProtocols} and @@ -1009,6 +1019,16 @@ public class Jenkins extends AbstractCIBase implements DirectlyModifiableTopLeve if (SLAVE_AGENT_PORT_ENFORCE) { slaveAgentPort = getSlaveAgentPortInitialValue(slaveAgentPort); } + if (disabledAgentProtocols == null && _disabledAgentProtocols != null) { + disabledAgentProtocols = Arrays.asList(_disabledAgentProtocols); + _disabledAgentProtocols = null; + } + if (enabledAgentProtocols == null && _enabledAgentProtocols != null) { + enabledAgentProtocols = Arrays.asList(_enabledAgentProtocols); + _enabledAgentProtocols = null; + } + // Invalidate the protocols cache after the reload + agentProtocols = null; return this; } @@ -5023,8 +5043,8 @@ public class Jenkins extends AbstractCIBase implements DirectlyModifiableTopLeve // for backward compatibility with <1.75, recognize the tag name "view" as well. XSTREAM.alias("view", ListView.class); XSTREAM.alias("listView", ListView.class); - XSTREAM.addImplicitCollection(Jenkins.class, "disabledAgentProtocols", "disabledAgentProtocol", String.class); - XSTREAM.addImplicitCollection(Jenkins.class, "enabledAgentProtocols", "enabledAgentProtocol", String.class); + XSTREAM.addImplicitArray(Jenkins.class, "_disabledAgentProtocols", "disabledAgentProtocol"); + XSTREAM.addImplicitArray(Jenkins.class, "_enabledAgentProtocols", "enabledAgentProtocol"); XSTREAM2.addCriticalField(Jenkins.class, "securityRealm"); XSTREAM2.addCriticalField(Jenkins.class, "authorizationStrategy"); // this seems to be necessary to force registration of converter early enough diff --git a/test/src/test/java/jenkins/model/JenkinsTest.java b/test/src/test/java/jenkins/model/JenkinsTest.java index 27528d2e04..8e88f7f0e3 100644 --- a/test/src/test/java/jenkins/model/JenkinsTest.java +++ b/test/src/test/java/jenkins/model/JenkinsTest.java @@ -25,7 +25,10 @@ package jenkins.model; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.greaterThan; import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.hasItem; +import static org.hamcrest.Matchers.not; import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertEquals; @@ -75,6 +78,12 @@ import org.mockito.Mockito; import java.io.IOException; import java.net.HttpURLConnection; import java.net.URL; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Set; +import org.junit.Assume; /** * @author kingfai @@ -471,4 +480,116 @@ public class JenkinsTest { throw new IOException("Something happened (the listener always throws this exception)"); } } + + @Test + @Issue("JENKINS-39465") + public void agentProtocols_singleEnable_roundtrip() throws Exception { + final Set defaultProtocols = Collections.unmodifiableSet(j.jenkins.getAgentProtocols()); + Assume.assumeThat("We assume that JNLP3-connect is disabled", + defaultProtocols, not(hasItem("JNLP3-connect"))); + + final Set newProtocols = new HashSet<>(defaultProtocols); + newProtocols.add("JNLP3-connect"); + j.jenkins.setAgentProtocols(newProtocols); + j.jenkins.save(); + final Set agentProtocolsBeforeReload = j.jenkins.getAgentProtocols(); + assertThat("JNLP3-connect must be enabled before the roundtrip", + j.jenkins.getAgentProtocols(), hasItem("JNLP3-connect")); + + j.jenkins.reload(); + + final Set reloadedProtocols = j.jenkins.getAgentProtocols(); + assertFalse("The protocol list must have been really reloaded", agentProtocolsBeforeReload == reloadedProtocols); + assertThat("We should have additional enabled protocol", + reloadedProtocols.size(), equalTo(defaultProtocols.size() + 1)); + assertThat("JNLP3-connect must be enabled after the roundtrip", + reloadedProtocols, hasItem("JNLP3-connect")); + } + + @Test + @Issue("JENKINS-39465") + public void agentProtocols_multipleDisable_roundtrip() throws Exception { + final Set defaultProtocols = Collections.unmodifiableSet(j.jenkins.getAgentProtocols()); + Assume.assumeThat("At least one protocol is enabled", defaultProtocols.size(), greaterThan(0)); + + final String protocolToDisable = defaultProtocols.iterator().next(); + + final Set newProtocols = new HashSet<>(defaultProtocols); + newProtocols.remove(protocolToDisable); + j.jenkins.setAgentProtocols(newProtocols); + j.jenkins.save(); + assertThat(protocolToDisable + " must be disabled before the roundtrip", + j.jenkins.getAgentProtocols(), not(hasItem(protocolToDisable))); + final Set agentProtocolsBeforeReload = j.jenkins.getAgentProtocols(); + j.jenkins.reload(); + + assertFalse("The protocol list must have been really refreshed", agentProtocolsBeforeReload == j.jenkins.getAgentProtocols()); + assertThat("We should have disabled one protocol", + j.jenkins.getAgentProtocols().size(), equalTo(defaultProtocols.size() - 1)); + assertThat(protocolToDisable + " must be disabled after the roundtrip", + j.jenkins.getAgentProtocols(), not(hasItem(protocolToDisable))); + } + + @Test + @Issue("JENKINS-39465") + public void agentProtocols_multipleEnable_roundtrip() throws Exception { + final Set defaultProtocols = Collections.unmodifiableSet(j.jenkins.getAgentProtocols()); + Assume.assumeThat("We assume that JNLP3-connect is disabled", + defaultProtocols, not(hasItem("JNLP3-connect"))); + Assume.assumeThat("We assume that JNLP4-connect is disabled", + defaultProtocols, not(hasItem("JNLP4-connect"))); + + final Set newProtocols = new HashSet<>(defaultProtocols); + newProtocols.add("JNLP3-connect"); + newProtocols.add("JNLP4-connect"); + j.jenkins.setAgentProtocols(newProtocols); + j.jenkins.save(); + final Set agentProtocolsBeforeReload = j.jenkins.getAgentProtocols(); + assertThat("JNLP3-connect must be enabled before the roundtrip", + j.jenkins.getAgentProtocols(), hasItem("JNLP3-connect")); + assertThat("JNLP4-connect must be enabled before the roundtrip", + j.jenkins.getAgentProtocols(), hasItem("JNLP4-connect")); + + j.jenkins.reload(); + + final Set reloadedProtocols = j.jenkins.getAgentProtocols(); + assertFalse("The protocol list must have been really reloaded", agentProtocolsBeforeReload == reloadedProtocols); + assertThat("We should have two additional enabled protocols", + reloadedProtocols.size(), equalTo(defaultProtocols.size() + 2)); + assertThat("JNLP3-connect must be enabled after the roundtrip", + reloadedProtocols, hasItem("JNLP3-connect")); + assertThat("JNLP3-connect must be enabled after the roundtrip", + reloadedProtocols, hasItem("JNLP4-connect")); + } + + @Test + @Issue("JENKINS-39465") + public void agentProtocols_singleDisable_roundtrip() throws Exception { + final Set defaultProtocols = Collections.unmodifiableSet(j.jenkins.getAgentProtocols()); + Assume.assumeThat("At least two protocol should be enabled", defaultProtocols.size(), greaterThan(1)); + + Iterator iterator = defaultProtocols.iterator(); + final String protocolToDisable1 = iterator.next(); + final String protocolToDisable2 = iterator.next(); + + final Set newProtocols = new HashSet<>(defaultProtocols); + newProtocols.remove(protocolToDisable1); + newProtocols.remove(protocolToDisable2); + j.jenkins.setAgentProtocols(newProtocols); + j.jenkins.save(); + assertThat(protocolToDisable1 + " must be disabled before the roundtrip", + j.jenkins.getAgentProtocols(), not(hasItem(protocolToDisable1))); + assertThat(protocolToDisable2 + " must be disabled before the roundtrip", + j.jenkins.getAgentProtocols(), not(hasItem(protocolToDisable2))); + final Set agentProtocolsBeforeReload = j.jenkins.getAgentProtocols(); + j.jenkins.reload(); + + assertFalse("The protocol list must have been really reloaded", agentProtocolsBeforeReload == j.jenkins.getAgentProtocols()); + assertThat("We should have disabled two protocols", + j.jenkins.getAgentProtocols().size(), equalTo(defaultProtocols.size() - 2)); + assertThat(protocolToDisable1 + " must be disaabled after the roundtrip", + j.jenkins.getAgentProtocols(), not(hasItem(protocolToDisable1))); + assertThat(protocolToDisable2 + " must be disaabled after the roundtrip", + j.jenkins.getAgentProtocols(), not(hasItem(protocolToDisable2))); + } } From 2b39a3839b9eb0c9ce18c9579cfc5f32979a2a88 Mon Sep 17 00:00:00 2001 From: Daniel Beck Date: Tue, 4 Oct 2016 12:40:25 +0200 Subject: [PATCH 14/17] [FIX JENKINS-38615] Add user to restart log message (cherry picked from commit 4462eda829944a39f2ddfdab607d7105be8aface) --- core/src/main/java/hudson/model/UpdateCenter.java | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/hudson/model/UpdateCenter.java b/core/src/main/java/hudson/model/UpdateCenter.java index ed1915e53d..16100a7727 100644 --- a/core/src/main/java/hudson/model/UpdateCenter.java +++ b/core/src/main/java/hudson/model/UpdateCenter.java @@ -30,6 +30,7 @@ import hudson.Functions; import hudson.PluginManager; import hudson.PluginWrapper; import hudson.ProxyConfiguration; +import hudson.security.ACLContext; import jenkins.util.SystemProperties; import hudson.Util; import hudson.XmlFile; @@ -1365,6 +1366,11 @@ public class UpdateCenter extends AbstractModelObject implements Saveable, OnMas @Exported(inline=true) public volatile RestartJenkinsJobStatus status = new Pending(); + /** + * The name of the user that started this job + */ + private String authentication; + /** * Cancel job */ @@ -1378,6 +1384,7 @@ public class UpdateCenter extends AbstractModelObject implements Saveable, OnMas public RestartJenkinsJob(UpdateSite site) { super(site); + this.authentication = Jenkins.getAuthentication().getName(); } public synchronized void run() { @@ -1386,7 +1393,10 @@ public class UpdateCenter extends AbstractModelObject implements Saveable, OnMas } status = new Running(); try { - Jenkins.getInstance().safeRestart(); + // safeRestart records the current authentication for the log, so set it to the managing user + try (ACLContext _ = ACL.as(User.get(authentication, false, Collections.emptyMap()))) { + Jenkins.getInstance().safeRestart(); + } } catch (RestartNotSupportedException exception) { // ignore if restart is not allowed status = new Failure(); From 288a66dfabac0a31ce799cc3eaf285dc801cd668 Mon Sep 17 00:00:00 2001 From: Thorsten Scherler Date: Sat, 22 Oct 2016 12:21:56 +0200 Subject: [PATCH 15/17] [FIX JENKINS-35845] Internationalisation for Blue Ocean and JDL (#2586) * Load i18n resource bundles from plugins if not found in jenkins core Signed-off-by: Thorsten Scherler * Issue 404 response for missing i18n resource bundles Currently issues a 200 with an "error" response payload. This change still issues the error response payload, but also sets the HTTP response. Signed-off-by: Thorsten Scherler * [JENKINS-35845] Fix test since we return now a 404 * [JENKINS-35845] add test for getting locale from plugin. fix comments from oleg. * [JENKINS-35845] Fix description * [JENKINS-35845] Update PR with comments from Oleg * [JENKINS-35845] Add feedback from tom * eslint - formating changes and fix offences * eslint - formating changes and fix offences * [JENKINS-35845] remove code concerning 404 response. Fix resourceBundle test by prevent NPE to happen * [JENKINS-35845] Link to issue on which we introduced the test (cherry picked from commit ab16e52519260c0e9398f15256ed8061a4c00bf0) --- .../main/java/hudson/util/HttpResponses.java | 1 + .../java/jenkins/util/ResourceBundleUtil.java | 47 ++++++++++++++++++- test/src/test/java/jenkins/I18nTest.java | 21 +++++++-- 3 files changed, 65 insertions(+), 4 deletions(-) diff --git a/core/src/main/java/hudson/util/HttpResponses.java b/core/src/main/java/hudson/util/HttpResponses.java index 511a3f20e0..ccef89059e 100644 --- a/core/src/main/java/hudson/util/HttpResponses.java +++ b/core/src/main/java/hudson/util/HttpResponses.java @@ -29,6 +29,7 @@ import org.kohsuke.stapler.HttpResponse; import org.kohsuke.stapler.StaplerRequest; import org.kohsuke.stapler.StaplerResponse; +import javax.annotation.CheckForNull; import javax.annotation.Nonnull; import javax.servlet.ServletException; import java.io.File; diff --git a/core/src/main/java/jenkins/util/ResourceBundleUtil.java b/core/src/main/java/jenkins/util/ResourceBundleUtil.java index a62c3fa772..edf8de5fab 100644 --- a/core/src/main/java/jenkins/util/ResourceBundleUtil.java +++ b/core/src/main/java/jenkins/util/ResourceBundleUtil.java @@ -27,12 +27,16 @@ import net.sf.json.JSONObject; import org.kohsuke.accmod.Restricted; import org.kohsuke.accmod.restrictions.NoExternalUse; +import javax.annotation.CheckForNull; import javax.annotation.Nonnull; +import hudson.PluginWrapper; +import java.util.logging.Logger; import java.util.Locale; import java.util.Map; import java.util.MissingResourceException; import java.util.ResourceBundle; import java.util.concurrent.ConcurrentHashMap; +import jenkins.model.Jenkins; /** * Simple {@link java.util.ResourceBundle} utility class. @@ -42,6 +46,7 @@ import java.util.concurrent.ConcurrentHashMap; @Restricted(NoExternalUse.class) public class ResourceBundleUtil { + private static final Logger logger = Logger.getLogger("jenkins.util.ResourceBundle"); private static final Map bundles = new ConcurrentHashMap<>(); private ResourceBundleUtil() { @@ -72,7 +77,23 @@ public class ResourceBundleUtil { return bundleJSON; } - ResourceBundle bundle = ResourceBundle.getBundle(baseName, locale); + ResourceBundle bundle = getBundle(baseName, locale, Jenkins.class.getClassLoader()); + if (bundle == null) { + // Not in Jenkins core. Check the plugins. + Jenkins jenkins = Jenkins.getInstance(); // will never return null + if (jenkins != null) { + for (PluginWrapper plugin : jenkins.getPluginManager().getPlugins()) { + bundle = getBundle(baseName, locale, plugin.classLoader); + if (bundle != null) { + break; + } + } + } + } + if (bundle == null) { + throw new MissingResourceException("Can't find bundle for base name " + + baseName + ", locale " + locale, baseName + "_" + locale, ""); + } bundleJSON = toJSONObject(bundle); bundles.put(bundleKey, bundleJSON); @@ -80,6 +101,30 @@ public class ResourceBundleUtil { return bundleJSON; } + /** + * Get a plugin bundle using the supplied Locale and classLoader + * + * @param baseName The bundle base name. + * @param locale The Locale. + * @param classLoader The classLoader + * @return The bundle JSON. + */ + private static @CheckForNull ResourceBundle getBundle(@Nonnull String baseName, @Nonnull Locale locale, @Nonnull ClassLoader classLoader) { + try { + return ResourceBundle.getBundle(baseName, locale, classLoader); + } catch (MissingResourceException e) { + // fall through and return null. + logger.warning(e.getMessage()); + } + return null; + } + + /** + * Create a JSON representation of a resource bundle + * + * @param bundle The resource bundle. + * @return The bundle JSON. + */ private static JSONObject toJSONObject(@Nonnull ResourceBundle bundle) { JSONObject json = new JSONObject(); for (String key : bundle.keySet()) { diff --git a/test/src/test/java/jenkins/I18nTest.java b/test/src/test/java/jenkins/I18nTest.java index a934422532..5f2da8e793 100644 --- a/test/src/test/java/jenkins/I18nTest.java +++ b/test/src/test/java/jenkins/I18nTest.java @@ -23,6 +23,7 @@ */ package jenkins; +import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException; import net.sf.json.JSONObject; import org.junit.Assert; import org.junit.Rule; @@ -31,6 +32,7 @@ import org.jvnet.hudson.test.JenkinsRule; import org.xml.sax.SAXException; import java.io.IOException; +import org.jvnet.hudson.test.Issue; /** * @author tom.fennelly@gmail.com @@ -49,9 +51,22 @@ public class I18nTest { @Test public void test_baseName_unknown() throws IOException, SAXException { - JSONObject response = jenkinsRule.getJSON("i18n/resourceBundle?baseName=com.acme.XyzWhatever").getJSONObject(); - Assert.assertEquals("error", response.getString("status")); - Assert.assertTrue(response.getString("message").contains("com.acme.XyzWhatever")); + try { + JSONObject response = jenkinsRule.getJSON("i18n/resourceBundle?baseName=com.acme.XyzWhatever").getJSONObject(); + } catch (FailingHttpStatusCodeException e) { + Assert.assertNotNull(e); + Assert.assertTrue(e.getMessage().contains("com.acme.XyzWhatever")); + } + } + + @Issue("JENKINS-35270") + @Test + public void test_baseName_plugin() throws IOException, SAXException { + // ssh-slaves plugin is installed by default + JSONObject response = jenkinsRule.getJSON("i18n/resourceBundle?baseName=hudson.plugins.sshslaves.Messages").getJSONObject(); + Assert.assertEquals("ok", response.getString("status")); + JSONObject data = response.getJSONObject("data"); + Assert.assertEquals("The launch timeout must be a number.", data.getString("SSHConnector.LaunchTimeoutMustBeANumber")); } @Test From e0bfe6ee79b516f6c496a035d2ed6e644ee92fa9 Mon Sep 17 00:00:00 2001 From: Oleg Nenashev Date: Wed, 9 Nov 2016 00:25:50 +0100 Subject: [PATCH 16/17] [FIXED JENKINS-39604] - ResourceBundleUtil#getBundle() should report resource misses on the low level I propose the FINER level. (cherry picked from commit 500cefddbca6135cbeab922b9998070f2db5daa5) --- core/src/main/java/jenkins/util/ResourceBundleUtil.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/main/java/jenkins/util/ResourceBundleUtil.java b/core/src/main/java/jenkins/util/ResourceBundleUtil.java index edf8de5fab..b2eee336ab 100644 --- a/core/src/main/java/jenkins/util/ResourceBundleUtil.java +++ b/core/src/main/java/jenkins/util/ResourceBundleUtil.java @@ -114,7 +114,7 @@ public class ResourceBundleUtil { return ResourceBundle.getBundle(baseName, locale, classLoader); } catch (MissingResourceException e) { // fall through and return null. - logger.warning(e.getMessage()); + logger.finer(e.getMessage()); } return null; } From 8533fb628c2bfcda14b0ec7466ada87c95e08c45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20Gond=C5=BEa?= Date: Thu, 10 Nov 2016 11:55:17 +0100 Subject: [PATCH 17/17] Skip irrelevant test --- test/src/test/java/jenkins/model/JenkinsTest.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/src/test/java/jenkins/model/JenkinsTest.java b/test/src/test/java/jenkins/model/JenkinsTest.java index 8e88f7f0e3..e20d7f9a48 100644 --- a/test/src/test/java/jenkins/model/JenkinsTest.java +++ b/test/src/test/java/jenkins/model/JenkinsTest.java @@ -64,6 +64,7 @@ import hudson.slaves.DumbSlave; import hudson.slaves.OfflineCause; import hudson.util.FormValidation; +import org.junit.Ignore; import org.junit.Rule; import org.junit.Test; import org.jvnet.hudson.test.Issue; @@ -532,6 +533,7 @@ public class JenkinsTest { @Test @Issue("JENKINS-39465") + @Ignore("No JNLP4 for 2.19.3") public void agentProtocols_multipleEnable_roundtrip() throws Exception { final Set defaultProtocols = Collections.unmodifiableSet(j.jenkins.getAgentProtocols()); Assume.assumeThat("We assume that JNLP3-connect is disabled",