Skip to content

Commit

Permalink
Merge pull request #12 from jonesbusy/feature/JENKINS-52205-jcasc-sup…
Browse files Browse the repository at this point in the history
…port

[JENKINS-52205] Add support for JCasC
  • Loading branch information
jonesbusy authored Feb 28, 2022
2 parents 7e4c9fa + d7eae4b commit ef00c6c
Show file tree
Hide file tree
Showing 9 changed files with 110 additions and 29 deletions.
7 changes: 5 additions & 2 deletions Jenkinsfile
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
// Build the plugin using https://github.com/jenkins-infra/pipeline-library
buildPlugin(jenkinsVersions: [null, '2.107.2'])
/*
See the documentation for more options:
https://github.com/jenkins-infra/pipeline-library/
*/
buildPlugin(useContainerAgent: true)
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ you're using have been translated into the specified language).
To additionally force this language on all users, overriding their browser language,
you can check the "Ignore browser preference and force this language to all users" option.

Since version 1.5 JCasC is supported

```
unclassified:
locale:
systemLocale: en
ignoreAcceptLanguage: true
```

### Changelog

* See [GitHub releases](https://github.com/jenkinsci/locale-plugin/releases) for new releases
Expand Down
38 changes: 33 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>3.15</version>
<version>4.34</version>
<relativePath />
</parent>

<properties>
<jenkins.version>1.625.3</jenkins.version>
<java.level>7</java.level>
<jenkins.version>2.289.3</jenkins.version>
<java.level>8</java.level>
<configuration-as-code.version>1346.ve8cfa_3473c94</configuration-as-code.version>
</properties>

<groupId>io.jenkins.plugins</groupId>
Expand Down Expand Up @@ -58,7 +60,33 @@
<url>https://github.com/jenkinsci/locale-plugin</url>
<tag>HEAD</tag>
</scm>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.jenkins.tools.bom</groupId>
<artifactId>bom-2.289.x</artifactId>
<version>1148.v7261f385f859</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>io.jenkins</groupId>
<artifactId>configuration-as-code</artifactId>
<version>${configuration-as-code.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.jenkins.configuration-as-code</groupId>
<artifactId>test-harness</artifactId>
<version>${configuration-as-code.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

</project>



2 changes: 1 addition & 1 deletion src/main/java/hudson/plugins/locale/LocaleFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public void init(FilterConfig filterConfig)
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
if (request instanceof HttpServletRequest) {
PluginImpl plugin = (PluginImpl) Jenkins.getActiveInstance().getPlugin("locale");
PluginImpl plugin = PluginImpl.get();
if (plugin != null && plugin.isIgnoreAcceptLanguage()) {
request = new HttpServletRequestWrapper((HttpServletRequest) request) {
@Override
Expand Down
49 changes: 32 additions & 17 deletions src/main/java/hudson/plugins/locale/PluginImpl.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package hudson.plugins.locale;

import com.thoughtworks.xstream.XStream;
import hudson.Plugin;
import hudson.Extension;
import hudson.Util;
import hudson.XmlFile;
import hudson.model.Descriptor.FormException;
import hudson.init.InitMilestone;
import hudson.init.Initializer;
import hudson.util.PluginServletFilter;
import hudson.util.XStream2;
import jenkins.model.GlobalConfiguration;
import jenkins.model.Jenkins;
import net.sf.json.JSONObject;
import org.jenkinsci.Symbol;
import org.jvnet.localizer.LocaleProvider;
import org.kohsuke.stapler.StaplerRequest;

Expand All @@ -20,7 +24,9 @@
/**
* @author Kohsuke Kawaguchi
*/
public class PluginImpl extends Plugin {
@Extension
@Symbol("locale")
public class PluginImpl extends GlobalConfiguration {

private String systemLocale;
private boolean ignoreAcceptLanguage;
Expand All @@ -30,9 +36,25 @@ public class PluginImpl extends Plugin {
*/
private transient final Locale originalLocale = Locale.getDefault();

public static PluginImpl get() {
return Jenkins.get().getExtensionList(PluginImpl.class).get(0);
}

public PluginImpl() {
load();
}

@Override
public void start()
throws Exception {
protected XmlFile getConfigFile() {
return new XmlFile(XSTREAM, new File(Jenkins.get().getRootDir(), "locale.xml")); // for backward compatibility
}

@Initializer(after = InitMilestone.EXTENSIONS_AUGMENTED)
public static void init() throws Exception {
PluginImpl.get().start();
}

private void start() throws ServletException {
load();
LocaleProvider.setProvider(new LocaleProvider() {
LocaleProvider original = LocaleProvider.getProvider();
Expand All @@ -50,23 +72,17 @@ public Locale get() {
}

@Override
protected void load()
throws IOException {
public void load() {
super.load();
setSystemLocale(systemLocale); // make the loaded value take effect
}

@Override
protected XmlFile getConfigXml() {
return new XmlFile(XSTREAM, new File(Jenkins.getActiveInstance().getRootDir(), "locale.xml"));
}

@Override
public void configure(StaplerRequest req, JSONObject jsonObject)
throws IOException, ServletException, FormException {
setSystemLocale(jsonObject.getString("systemLocale"));
ignoreAcceptLanguage = jsonObject.getBoolean("ignoreAcceptLanguage");
public boolean configure(StaplerRequest req, JSONObject jsonObject) throws FormException {
req.bindJSON(this, jsonObject);
save();
return false;
}

public boolean isIgnoreAcceptLanguage() {
Expand All @@ -77,8 +93,7 @@ public String getSystemLocale() {
return systemLocale;
}

public void setSystemLocale(String systemLocale)
throws IOException {
public void setSystemLocale(String systemLocale) {
systemLocale = Util.fixEmptyAndTrim(systemLocale);
Locale.setDefault(systemLocale == null ? originalLocale : parse(systemLocale));
this.systemLocale = systemLocale;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form">
<f:section title="${%Locale}">
<f:entry title="${%Default Language}" help="/plugin/locale/help/default-language.html">
<f:textbox name="systemLocale" value="${it.systemLocale}" />
<f:entry title="${%Default Language}" field="systemLocale">
<f:textbox />
</f:entry>
<f:entry>
<f:checkbox name="ignoreAcceptLanguage" checked="${it.ignoreAcceptLanguage}" title="${%description}" />
<f:checkbox field="ignoreAcceptLanguage" />
</f:entry>
</f:section>
</j:jelly>
22 changes: 22 additions & 0 deletions src/test/java/hudson/plugins/locale/ConfigurationAsCodeTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package hudson.plugins.locale;

import jenkins.model.Jenkins;
import io.jenkins.plugins.casc.misc.ConfiguredWithCode;
import io.jenkins.plugins.casc.misc.JenkinsConfiguredWithCodeRule;
import org.junit.Rule;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class ConfigurationAsCodeTest {

@Rule public JenkinsConfiguredWithCodeRule r = new JenkinsConfiguredWithCodeRule();

@Test
@ConfiguredWithCode("configuration-as-code.yml")
public void should_support_configuration_as_code() throws Exception {
PluginImpl plugin = Jenkins.get().getExtensionList(PluginImpl.class).get(0);
assertEquals("fr", plugin.getSystemLocale());
assertEquals(true, plugin.isIgnoreAcceptLanguage());
}
}
2 changes: 1 addition & 1 deletion src/test/java/hudson/plugins/locale/MigrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class MigrationTest {
@LocalData
@Test
public void dataMigration_13() {
PluginImpl plugin = (PluginImpl) Jenkins.getActiveInstance().getPlugin("locale");
PluginImpl plugin = Jenkins.get().getExtensionList(PluginImpl.class).get(0);
assertEquals("en-US", plugin.getSystemLocale());
assertEquals(true, plugin.isIgnoreAcceptLanguage());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
unclassified:
locale:
systemLocale: fr
ignoreAcceptLanguage: true

0 comments on commit ef00c6c

Please sign in to comment.