Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
William Bernardet
committed
May 28, 2013
1 parent
87030ed
commit 97547e3
Showing
4 changed files
with
131 additions
and
0 deletions.
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
src/main/java/hudson/plugins/templateproject/ItemListenerImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package hudson.plugins.templateproject; | ||
|
||
import java.io.IOException; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
import hudson.matrix.MatrixProject; | ||
import hudson.model.Hudson; | ||
import hudson.model.Project; | ||
import hudson.Extension; | ||
import hudson.model.AbstractProject; | ||
import hudson.model.listeners.ItemListener; | ||
import hudson.tasks.Builder; | ||
import java.util.logging.Logger; | ||
|
||
/** | ||
* This ItemListener implementation will force job using | ||
* template for publisher to regenerate the transient actions list. | ||
* | ||
* To do so we use the {@link UpdateTransientProperty} property which does not do | ||
* anything. This is a bit a hack that relies on the behavior, but | ||
* there does not seem to be any better way to force updating projects transients actions. | ||
* | ||
* @author william.bernardet@gmail.com | ||
* | ||
*/ | ||
@Extension | ||
public class ItemListenerImpl extends ItemListener { | ||
private static final Logger LOGGER = Logger.getLogger(ItemListenerImpl.class.getName()); | ||
|
||
/** | ||
* Let's force the projects using either the ProxyPublisher or the ProxyBuilder | ||
* to update their transient actions. | ||
*/ | ||
@Override | ||
public void onLoaded() { | ||
for (AbstractProject<?,?> project : Hudson.getInstance().getAllItems(AbstractProject.class)) { | ||
if (project.getPublishersList().get(ProxyPublisher.class) != null || | ||
hasBuilder(project, ProxyBuilder.class)) { | ||
try { | ||
project.addProperty(new UpdateTransientProperty()); | ||
project.removeProperty(UpdateTransientProperty.class); | ||
} catch (IOException e) { | ||
LOGGER.severe(e.getMessage()); | ||
} | ||
} | ||
} | ||
} | ||
|
||
private List<Builder> getBuilders(AbstractProject<?, ?> project) { | ||
if (project instanceof Project) { | ||
return ((Project)project).getBuilders(); | ||
} else if (project instanceof MatrixProject) { | ||
return ((MatrixProject)project).getBuilders(); | ||
} else { | ||
return Collections.emptyList(); | ||
} | ||
} | ||
|
||
public <T> boolean hasBuilder(AbstractProject<?, ?> project, Class<T> type) { | ||
for (Builder b : getBuilders(project)) { | ||
if (type.isInstance(b)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
src/main/java/hudson/plugins/templateproject/UpdateTransientProperty.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package hudson.plugins.templateproject; | ||
|
||
import org.kohsuke.stapler.DataBoundConstructor; | ||
|
||
import hudson.Extension; | ||
import hudson.model.AbstractProject; | ||
import hudson.model.JobProperty; | ||
import hudson.model.JobPropertyDescriptor; | ||
|
||
/** | ||
* A property that is only used to trigger the transient actions creations | ||
* under a project. | ||
* | ||
* @author william.bernardet@gmail.com | ||
* | ||
*/ | ||
public class UpdateTransientProperty extends JobProperty<AbstractProject<?, ?>> { | ||
|
||
@DataBoundConstructor | ||
public UpdateTransientProperty() { | ||
} | ||
|
||
@Extension | ||
public static class DescriptorImpl extends JobPropertyDescriptor { | ||
|
||
@Override | ||
public String getDisplayName() { | ||
return "Property used to for job to update transient actions."; | ||
} | ||
|
||
} | ||
|
||
} |