Global Configuration
Until now, we have con-
centrated on how to con-
figure a plug-in at the job
level. However, some of the
configuration of an exten-
sion could be global. For
example, if you use Git for
source configuration man-
agement (SCM) in a project, you might
want to configure two different jobs to
force Git to check out from a different
repository. So in both the projects, the
Git SCM plug-in must be configured to
use two different repository URLs.
However, if the Git SCM extension
needs to know about the Git native
binaries, placing the Git configuration
UI that configures the Git binary location at the job level makes little sense,
because it doesn’t vary from project to
project. It makes sense to put such a
configuration in Hudson’s global configuration page.
For Hudson to include the configuration of a plug-in in its global configuration page, the configuration must be
declared in a file called global.jelly. The
namespace convention for this file is
similar to the convention for local configuration. For the Hello WorldBuilder
global config file, it is org/sample/
hudson/HelloWorldBuilder/ global.jelly.
In Part 1 of this series, we saw that
the Hello WorldBuilder.perform(..) method
includes the code shown in Listing 6.
Hello WorldBuilder can be configured
to say hello in either French or English.
The configuration of which language
to use is set globally. Once set, all
GET /job/TestProject/descriptorByName/ org.sample.hudson.Hello WorldBuilder/
checkName?value=xy HTTP/1.1
Host: localhost:8080
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10. 6; rv: 6.0.1) Gecko/20100101
Firefox/6.0.1
Accept: text/javascript, text/html, application/xml, text/xml, */*
Figure 6
Hudson evaluates this request,
finds the extension Hello WorldBuilder,
executes the method checkName(), and
returns the result. Again, Hudson uses
the convention doCheck followed by
{nameOf TheField} as part of the Ajax URL.
Note: By default, for every f:textbox tag in
the Jelly config file, Hudson will render
the Ajax check. However, if your extension class does not include the corresponding method (in this case,
doCheckName()), Hudson will silently
ignore the check request.
The doCheckName() method is straightforward. The Ajax request specifies the
checkName method with the parameter
value as {..}/checkName?value="xy". Hence,
the parameter value of doCheckName
must be annotated with the annotation
@QueryParameter. Also, the method must
return a FormValidation object, which
determines the outcome of the check.
See Listing 5.
The method doCheckName returns
FormValidation.error(..) when an error
occurs. The HTML sent back to the client
displays the text in red along with an error
icon (see Figure 3). If FormValidation
.warning() is returned, the HTML sent
back displays the message in yellow along
with a warning icon (see Figure 4).
JAVA IN ACTION
See all listings as text
the builds of various jobs that use
Hello WorldBuilder would say hello in
either French or English.
The global configuration of the
Hello WorldBuilder plug-in is done in the
Hudson configuration page in the Hello
World Builder section (see Figure 6).
Here, the user sets the global configuration to control whether French is
used as the language. In global.jelly, the
checkbox is defined as shown in Listing 7.
When the user edits the Hudson
configuration page, the decision about
whether this checkbox should be
selected comes from the extension
itself. The JEXL expression ${descriptor
.useFrench()} would resolve to
HelloBuilder.DescriptorImpl.useFrench(),
which is defined as follows:
the checkbox. Once the global configura-
tion is submitted, by convention,
Hudson calls the method HelloBuilder
. DescriptorImpl.configure() and passes a
JSON object corresponding to the page
submission. It is up to the extension to
find its submitted value and then apply
it. Hello WorldBuilder defines this method
as shown in Listing 8.
ABOUT US
public boolean useFrench() {
return useFrench;
}
blog
useFrench is a field in Hello WorldBuilder.
This field is set to true if the user selects
Conclusion
The Hudson plug-in development environment provides a rich set of extension
points with which plug-in developers
can develop custom plug-ins. In this
article, we explored ways to configure
the extensions in a plug-in using the
Jelly UI framework . </article>