Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[FIXED JENKINS-14914] migrate to new EP from core and change dependen…
…cy direction between maven-maven and config file provider
  • Loading branch information
imod committed Nov 2, 2012
1 parent 6550d83 commit 65715a7
Show file tree
Hide file tree
Showing 6 changed files with 313 additions and 0 deletions.
@@ -0,0 +1,118 @@
package org.jenkinsci.plugins.configfiles.maven.job;

import hudson.Extension;
import hudson.ExtensionList;
import hudson.FilePath;
import hudson.model.TaskListener;
import hudson.model.AbstractBuild;

import java.io.IOException;
import java.util.Collection;
import java.util.Collections;

import jenkins.model.Jenkins;
import jenkins.mvn.GlobalSettingsProvider;
import jenkins.mvn.GlobalSettingsProviderDescriptor;

import org.apache.commons.lang.StringUtils;
import org.jenkinsci.lib.configprovider.ConfigProvider;
import org.jenkinsci.lib.configprovider.model.Config;
import org.jenkinsci.plugins.configfiles.common.CleanTempFilesAction;
import org.jenkinsci.plugins.configfiles.maven.GlobalMavenSettingsConfig.GlobalMavenSettingsConfigProvider;
import org.kohsuke.stapler.DataBoundConstructor;

/**
* This provider delivers the global settings.xml to the job during job/project execution.
* <br>
* <b>Important: Do not rename this class!!</b>
* For backward compatibility, this class is also created via reflection from the maven-plugin.
*
* @author Dominik Bartholdi (imod)
*/
public class MvnGlobalSettingsProvider extends GlobalSettingsProvider {

private String settingsConfigId;

/**
* Default constructor used to load class via reflection by the maven-plugin for backward compatibility
*/
@Deprecated
public MvnGlobalSettingsProvider() {
}

@DataBoundConstructor
public MvnGlobalSettingsProvider(String settingsConfigId) {
this.settingsConfigId = settingsConfigId;
}

public String getSettingsConfigId() {
return settingsConfigId;
}

public void setSettingsConfigId(String settingsConfigId) {
this.settingsConfigId = settingsConfigId;
}

@Override
public FilePath supplySettings(AbstractBuild<?, ?> build, TaskListener listener) {
if (StringUtils.isNotBlank(settingsConfigId)) {

ConfigProvider provider = getProviderForConfigId(settingsConfigId);
Config config = provider.getConfigById(settingsConfigId);

if (config == null) {
listener.getLogger().println("ERROR: your Apache Maven build is setup to use a config with id " + settingsConfigId + " but can not find the config");
} else {
listener.getLogger().println("using global settings config with name " + config.name);
if (config.content != null) {
try {
final FilePath f = copyConfigContentToFilePath(config.content, build.getWorkspace());
// Temporarily attach info about the files to be deleted to the build - this action gets removed from the build again by
// 'org.jenkinsci.plugins.configfiles.common.CleanTempFilesRunListener'
build.addAction(new CleanTempFilesAction(f.getRemote()));
return f;
} catch (Exception e) {
throw new IllegalStateException("the global settings.xml could not be supplied for the current build: " + e.getMessage());
}
}
}
}

return null;
}

private ConfigProvider getProviderForConfigId(String id) {
if (!StringUtils.isBlank(id)) {
for (ConfigProvider provider : ConfigProvider.all()) {
if (provider.isResponsibleFor(id)) {
return provider;
}
}
}
return null;
}

public static FilePath copyConfigContentToFilePath(String content, FilePath workspace) throws IOException, InterruptedException {
return workspace.createTextTempFile("global-settings", ".xml", content, false);
}

@Extension(ordinal = 10)
public static class DescriptorImpl extends GlobalSettingsProviderDescriptor {

@Override
public String getDisplayName() {
return "provided global settings.xml";
}

public Collection<Config> getAllGlobalMavenSettingsConfigs() {
final ExtensionList<GlobalMavenSettingsConfigProvider> configProviders = Jenkins.getInstance().getExtensionList(GlobalMavenSettingsConfigProvider.class);
if (configProviders != null && configProviders.size() > 0) {
// there is only one implementation...
return configProviders.get(0).getAllConfigs();
}
return Collections.emptyList();
}

}

}
@@ -0,0 +1,117 @@
package org.jenkinsci.plugins.configfiles.maven.job;

import hudson.Extension;
import hudson.ExtensionList;
import hudson.FilePath;
import hudson.model.TaskListener;
import hudson.model.AbstractBuild;

import java.io.IOException;
import java.util.Collection;
import java.util.Collections;

import jenkins.model.Jenkins;
import jenkins.mvn.SettingsProvider;
import jenkins.mvn.SettingsProviderDescriptor;

import org.apache.commons.lang.StringUtils;
import org.jenkinsci.lib.configprovider.ConfigProvider;
import org.jenkinsci.lib.configprovider.model.Config;
import org.jenkinsci.plugins.configfiles.common.CleanTempFilesAction;
import org.jenkinsci.plugins.configfiles.maven.MavenSettingsConfig.MavenSettingsConfigProvider;
import org.kohsuke.stapler.DataBoundConstructor;

/**
* This provider delivers the settings.xml to the job during job/project execution.
* <br>
* <b>Important: Do not rename this class!!</b>
* For backward compatibility, this class is also created via reflection from the maven-plugin.
*
* @author Dominik Bartholdi (imod)
*/
public class MvnSettingsProvider extends SettingsProvider {

private String settingsConfigId;

/**
* Default constructor used to load class via reflection by the maven-plugin for backward compatibility
*/
@Deprecated
public MvnSettingsProvider() {
}

@DataBoundConstructor
public MvnSettingsProvider(String settingsConfigId) {
this.settingsConfigId = settingsConfigId;
}

public String getSettingsConfigId() {
return settingsConfigId;
}

public void setSettingsConfigId(String settingsConfigId) {
this.settingsConfigId = settingsConfigId;
}

@Override
public FilePath supplySettings(AbstractBuild<?, ?> build, TaskListener listener) {
if (StringUtils.isNotBlank(settingsConfigId)) {

ConfigProvider provider = getProviderForConfigId(settingsConfigId);
Config config = provider.getConfigById(settingsConfigId);

if (config == null) {
listener.getLogger().println("ERROR: your Apache Maven build is setup to use a config with id " + settingsConfigId + " but can not find the config");
} else {
listener.getLogger().println("using settings config with name " + config.name);
if (config.content != null) {
try {
final FilePath f = copyConfigContentToFilePath(config.content, build.getWorkspace());
// Temporarily attach info about the files to be deleted to the build - this action gets removed from the build again by
// 'org.jenkinsci.plugins.configfiles.common.CleanTempFilesRunListener'
build.addAction(new CleanTempFilesAction(f.getRemote()));
return f;
} catch (Exception e) {
throw new IllegalStateException("the settings.xml could not be supplied for the current build: " + e.getMessage());
}
}
}
}

return null;
}

private ConfigProvider getProviderForConfigId(String id) {
if (!StringUtils.isBlank(id)) {
for (ConfigProvider provider : ConfigProvider.all()) {
if (provider.isResponsibleFor(id)) {
return provider;
}
}
}
return null;
}

public static FilePath copyConfigContentToFilePath(String content, FilePath workspace) throws IOException, InterruptedException {
return workspace.createTextTempFile("settings", ".xml", content, false);
}

@Extension(ordinal = 10)
public static class DescriptorImpl extends SettingsProviderDescriptor {

@Override
public String getDisplayName() {
return "provided settings.xml";
}

public Collection<Config> getAllMavenSettingsConfigs() {
final ExtensionList<MavenSettingsConfigProvider> configProviders = Jenkins.getInstance().getExtensionList(MavenSettingsConfigProvider.class);
if (configProviders != null && configProviders.size() > 0) {
// there is only one implementation...
return configProviders.get(0).getAllConfigs();
}
return Collections.emptyList();
}
}

}
@@ -0,0 +1,36 @@
<!--
The MIT License
Copyright (c) 2012, Dominik Bartholdi
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-->

<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:f="/lib/form">
<j:set var="mavenGlobalSettingsConfigs" value="${descriptor.allGlobalMavenSettingsConfigs}" />
<f:entry title="${%Provided Global Settings}">
<select class="setting-input" name="settingsConfigId">
<f:option value="">- select -</f:option>
<j:forEach var="config" items="${mavenGlobalSettingsConfigs}">
<f:option selected="${it.globalSettings.settingsConfigId == config.id}" value="${config.id}">${config.name}</f:option>
</j:forEach>
</select>
</f:entry>
</j:jelly>
@@ -0,0 +1,3 @@
<div>
select the provided global settings.xml for this project
</div>
@@ -0,0 +1,36 @@
<!--
The MIT License
Copyright (c) 2012, Dominik Bartholdi
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-->

<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:f="/lib/form">
<j:set var="mavenSettingsConfigs" value="${descriptor.allMavenSettingsConfigs}" />
<f:entry title="${%Provided Settings}">
<select class="setting-input" name="settingsConfigId">
<f:option value="">- select -</f:option>
<j:forEach var="config" items="${mavenSettingsConfigs}">
<f:option selected="${it.settings.settingsConfigId == config.id}" value="${config.id}">${config.name}</f:option>
</j:forEach>
</select>
</f:entry>
</j:jelly>
@@ -0,0 +1,3 @@
<div>
select the provided settings.xml for this project
</div>

0 comments on commit 65715a7

Please sign in to comment.