提交 a8f5cd5e 编写于 作者: K Kohsuke Kawaguchi

splitting out a new tag to configure a TCP/IP port for a daemon

上级 8b92f50f
package jenkins.util;
import net.sf.json.JSONObject;
import org.kohsuke.stapler.DataBoundConstructor;
/**
* Used in conjunction with /lib/form/serverTcpPort tag to parse the submitted JSON back into a port number.
*
* @author Kohsuke Kawaguchi
* @since 1.445
*/
public class ServerTcpPort {
private int value;
private String type;
@DataBoundConstructor
public ServerTcpPort(int value, String type) {
this.value = value;
this.type = type;
}
public ServerTcpPort(JSONObject o) {
type = o.getString("type");
value = o.optInt("value");
}
/**
* Parses the value back into the port number
*/
public int getPort() {
if (type.equals("fixed")) return value;
if (type.equals("random")) return 0;
return -1;
}
}
......@@ -27,6 +27,7 @@ import hudson.Extension;
import hudson.markup.MarkupFormatter;
import jenkins.model.GlobalConfiguration;
import jenkins.model.Jenkins;
import jenkins.util.ServerTcpPort;
import net.sf.json.JSONObject;
import org.kohsuke.stapler.StaplerRequest;
......@@ -43,34 +44,26 @@ public class GlobalSecurityConfiguration extends GlobalConfiguration {
return Jenkins.getInstance().getMarkupFormatter();
}
public int getSlaveAgentPort() {
return Jenkins.getInstance().getSlaveAgentPort();
}
@Override
public boolean configure(StaplerRequest req, JSONObject json) throws FormException {
// for compatibility reasons, the actual value is stored in Jenkins
Jenkins j = Jenkins.getInstance();
try {
String v = req.getParameter("slaveAgentPortType");
if(!j.isUseSecurity() || v==null || v.equals("random"))
j.setSlaveAgentPort(0);
else
if(v.equals("disable"))
j.setSlaveAgentPort(-1);
else {
try {
j.setSlaveAgentPort(Integer.parseInt(req.getParameter("slaveAgentPort")));
} catch (NumberFormatException e) {
throw new FormException(jenkins.model.Messages.Hudson_BadPortNumber(req.getParameter("slaveAgentPort")),"slaveAgentPort");
}
}
} catch (IOException e) {
throw new FormException(e,"slaveAgentPortType");
}
if (json.has("useSecurity")) {
JSONObject security = json.getJSONObject("useSecurity");
j.setSecurityRealm(SecurityRealm.all().newInstanceFromRadioList(security, "realm"));
j.setAuthorizationStrategy(AuthorizationStrategy.all().newInstanceFromRadioList(security, "authorization"));
try {
j.setSlaveAgentPort(new ServerTcpPort(security.getJSONObject("slaveAgentPort")).getPort());
} catch (IOException e) {
throw new FormException(e,"slaveAgentPortType");
}
if (security.has("markupFormatter")) {
j.setMarkupFormatter(req.bindJSON(MarkupFormatter.class, security.getJSONObject("markupFormatter")));
} else {
......
......@@ -7,28 +7,7 @@ def f=namespace(lib.FormTagLib)
f.optionalBlock( field:"useSecurity", title:_("Enable security"), checked:app.useSecurity) {
f.entry (title:_("TCP port for JNLP slave agents"), field:"slaveAgentPort") {
int port = app.slaveAgentPort
f.radio(name:"slaveAgentPortType", value:"fixed", id:"sat.fixed",
checked:port>0, onclick:"\$('sat.port').disabled=false")
label("for":"sat.fixed", _("Fixed"))
text(" : ")
input(type:"number", "class":"number", name:"slaveAgentPort", id:"sat.port",
value: port>0 ? port : null, disabled: port>0 ? null : "true",
min:0, max:65535, step:1)
raw(" ") ////////////////////////////
f.radio(name:"slaveAgentPortType", value:"random", id:"sat.random",
checked:port==0, onclick:"\$('sat.port').disabled=true")
label("for":"sat.random", _("Random"))
raw(" ") ////////////////////////////
f.radio(name:"slaveAgentPortType", value:"disable", id:"sat.disabled",
checked:port==-1, onclick:"\$('sat.port').disabled=true")
label("for":"sat.disabled", _("Disable"))
f.serverTcpPort()
}
f.dropdownDescriptorSelector(title:_("Markup Formatter"),field:"markupFormatter")
......
package lib.form
/**
* Generates a UI for selecting server TCP/IP port (for some kind of daemon, typically)
*
* The user can specify a fixed port (1-65535), or let Jenkins allocate random port (0), or disable it (-1).
*
* On the databinding side, use {@link jenkins.util.ServerTcpPort} to handle this structure back into a single
* port number. The getter method should just expose the port number integer.
*/
int port = instance?instance[field]:0;
def f=namespace(lib.FormTagLib)
def type = "${field}.type"
def id = "${field}Id" // TODO: get rid of this
div(name:field) {
label {
f.radio(name: type, value:"fixed",
checked:port>0, onclick:"\$('${id}').disabled=false")
text(_("Fixed"))
text(" : ")
}
input(type:"number", "class":"number", name:"value", id:id,
value: port>0 ? port : null, disabled: port>0 ? null : "true",
min:0, max:65535, step:1)
raw(" ") ////////////////////////////
label {
f.radio(name:type, value:"random",
checked:port==0, onclick:"\$('{id}').disabled=true")
text(_("Random"))
}
raw(" ") ////////////////////////////
label {
f.radio(name:type, value:"disable",
checked:port==-1, onclick:"\$('{id}').disabled=true")
text(_("Disable"))
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册