Skip to content

Commit

Permalink
Convert to global configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
timja committed May 13, 2019
1 parent cd5531d commit 1b28a47
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 66 deletions.
91 changes: 52 additions & 39 deletions src/main/java/com/sonyericsson/jenkins/plugins/bfa/PluginImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,29 @@
import com.sonyericsson.jenkins.plugins.bfa.model.ScannerJobProperty;
import com.sonyericsson.jenkins.plugins.bfa.sod.ScanOnDemandQueue;
import com.sonyericsson.jenkins.plugins.bfa.sod.ScanOnDemandVariables;
import com.thoughtworks.xstream.XStream;
import hudson.Extension;
import hudson.ExtensionList;
import hudson.Plugin;
import hudson.PluginManager;
import hudson.PluginWrapper;
import hudson.model.Descriptor;
import hudson.XmlFile;
import hudson.init.InitMilestone;
import hudson.init.Initializer;
import hudson.init.Terminator;
import hudson.model.Hudson;
import hudson.model.Job;
import hudson.model.Result;
import hudson.model.Run;
import hudson.security.Permission;
import hudson.security.PermissionGroup;
import hudson.util.CopyOnWriteList;
import hudson.util.XStream2;
import jenkins.model.GlobalConfiguration;
import jenkins.model.Jenkins;
import net.sf.json.JSONObject;
import org.jenkinsci.Symbol;
import org.kohsuke.stapler.StaplerRequest;

import javax.annotation.Nonnull;
import java.io.IOException;
import java.io.File;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand All @@ -57,7 +62,9 @@
*
* @author Robert Sandell <robert.sandell@sonyericsson.com>
*/
public class PluginImpl extends Plugin {
@Extension
@Symbol("buildFailureAnalyzer")
public class PluginImpl extends GlobalConfiguration {

private static final Logger logger = Logger.getLogger(PluginImpl.class.getName());

Expand Down Expand Up @@ -144,9 +151,17 @@ public class PluginImpl extends Plugin {
*/
private ScanOnDemandVariables sodVariables;

public PluginImpl() {
load();
}

@Override
public void start() throws Exception {
super.start();
protected XmlFile getConfigFile() {
return new XmlFile(XSTREAM, new File(Jenkins.getInstance().getRootDir(),"build-failure-analyzer.xml")); // for backward compatibility
}

@Initializer(after = InitMilestone.EXTENSIONS_AUGMENTED)
public void start() {
logger.finer("[BFA] Starting...");
load();
if (noCausesMessage == null) {
Expand Down Expand Up @@ -199,9 +214,8 @@ public void start() throws Exception {
}
}

@Override
public void stop() throws Exception {
super.stop();
@Terminator
public void stop() {
ScanOnDemandQueue.shutdown();
knowledgeBase.stop();
}
Expand All @@ -212,22 +226,23 @@ public void stop() throws Exception {
* @return the base URI.
*/
public static String getStaticResourcesBase() {
if (staticResourcesBase == null) {
PluginManager pluginManager = Jenkins.getInstance().getPluginManager();
if (pluginManager != null) {
PluginWrapper wrapper = pluginManager.getPlugin(PluginImpl.class);
if (wrapper != null) {
staticResourcesBase = "/plugin/" + wrapper.getShortName();
}
}
//Did we really find it?
if (staticResourcesBase == null) {
//This is not the preferred way since the module name could change,
//But in some unit test cases we cannot reach the plug-in info.
return "/plugin/build-failure-analyzer";
}
}
return staticResourcesBase;
return "TODO";
// if (staticResourcesBase == null) {
// PluginManager pluginManager = Jenkins.getInstance().getPluginManager();
// if (pluginManager != null) {
//// PluginWrapper wrapper = pluginManager.getPlugin(PluginImpl.class);
// if (wrapper != null) {
// staticResourcesBase = "/plugin/" + wrapper.getShortName();
// }
// }
// //Did we really find it?
// if (staticResourcesBase == null) {
// //This is not the preferred way since the module name could change,
// //But in some unit test cases we cannot reach the plug-in info.
// return "/plugin/build-failure-analyzer";
// }
// }
// return staticResourcesBase;
}

/**
Expand Down Expand Up @@ -303,16 +318,7 @@ public static String getDefaultIcon() {
*/
@Nonnull
public static PluginImpl getInstance() {
Jenkins jenkins = Jenkins.getInstance();
if (jenkins == null) {
throw new AssertionError("Jenkins is not here yet.");
}
PluginImpl plugin = jenkins.getPlugin(PluginImpl.class);
if (plugin == null) {
throw new AssertionError("Not here yet.");
} else {
return plugin;
}
return ExtensionList.lookup(PluginImpl.class).get(0);
}

/**
Expand Down Expand Up @@ -576,7 +582,7 @@ public KnowledgeBase.KnowledgeBaseDescriptor getKnowledgeBaseDescriptor(String d


@Override
public void configure(StaplerRequest req, JSONObject o) throws Descriptor.FormException, IOException {
public boolean configure(StaplerRequest req, JSONObject o) {
noCausesMessage = o.getString("noCausesMessage");
globalEnabled = o.getBoolean("globalEnabled");
doNotAnalyzeAbortedJob = o.optBoolean("doNotAnalyzeAbortedJob", false);
Expand Down Expand Up @@ -640,7 +646,7 @@ public void configure(StaplerRequest req, JSONObject o) throws Descriptor.FormEx
} catch (Exception e) {
logger.log(Level.SEVERE, "Could not start new knowledge base, reverting ", e);
save();
return;
return true;
}
if (o.getBoolean("convertOldKb")) {
try {
Expand All @@ -654,5 +660,12 @@ public void configure(StaplerRequest req, JSONObject o) throws Descriptor.FormEx
}

save();
return true;
}

private static final XStream XSTREAM = new XStream2();

static {
XSTREAM.alias("buildFailureAnalyzer", PluginImpl.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,20 @@
<f:section title="${%Build failure analyzer}">
<f:entry title="${%Enabled}"
description="${%If builds should be scanned or not.}">
<f:checkbox name="globalEnabled" checked="${it.globalEnabled}" default="true"/>
<f:checkbox name="globalEnabled" checked="${descriptor.globalEnabled}" default="true"/>
</f:entry>
<f:entry title="${%Enable graphs}"
description="${%Whether to display graphs or not. NOTE: The selected storage type must support this!}">
<f:checkbox name="graphsEnabled" checked="${it.graphsEnabled}" default="true"/>
<f:checkbox name="graphsEnabled" checked="${descriptor.graphsEnabled}" default="true"/>
</f:entry>
<f:entry title="${%Text when no failure causes are found}"
description="${%If no problems are identified, this text will be shown on the build page}">
<f:textbox name="noCausesMessage" value="${it.noCausesMessage}"/>
<f:textbox name="noCausesMessage" value="${descriptor.noCausesMessage}"/>
</f:entry>
<f:dropdownList name="knowledgeBase" title="${%Storage type}" help="${descriptor.getHelpFile('knowledgeBase')}">
<j:set var="current" value="${it.getKnowledgeBase()}"/>
<j:set var="current" value="${descriptor.getKnowledgeBase()}"/>
<j:set var="defaultDescriptor" value="com.sonyericsson.jenkins.plugins.bfa.db.LocalFileKnowledgeBase.LocalFileKnowledgeBaseDescriptor"/>
<j:forEach var="descriptor" items="${it.getKnowledgeBaseDescriptors()}" varStatus="loop">
<j:forEach var="descriptor" items="${descriptor.getKnowledgeBaseDescriptors()}" varStatus="loop">
<f:dropdownListBlock value="${loop.index}" title="${descriptor.displayName}"
selected="${current.descriptor==descriptor || (current==null and descriptor==defaultDescriptor)}"
staplerClass="${descriptor.clazz.name}"
Expand All @@ -59,21 +59,21 @@
</f:entry>
<f:entry title="${%Send notifications to Gerrit-Trigger-plugin}"
description="${%enableGerritTriggerDescription}">
<f:checkbox name="gerritTriggerEnabled" checked="${it.gerritTriggerEnabled}" default="true"/>
<f:checkbox name="gerritTriggerEnabled" checked="${descriptor.gerritTriggerEnabled}" default="true"/>
</f:entry>
<f:entry title="${%Concurrent scans}"
description="${%nrOfScanThreadsDescription}">
<f:textbox name="nrOfScanThreads" clazz="required positive-number" value="${it.nrOfScanThreads}"/>
<f:textbox name="nrOfScanThreads" clazz="required positive-number" value="${descriptor.nrOfScanThreads}"/>
</f:entry>
<f:block>
<table>
<f:optionalBlock name="testResultParsingEnabled"
title="${%testResultParsingEnabledDescription}"
checked="${it.testResultParsingEnabled}"
checked="${descriptor.testResultParsingEnabled}"
inline="true">
<f:entry title="Categories"
description="${%testResultCategoriesDescription}">
<f:textbox name="testResultCategories" value="${it.testResultCategories}"/>
<f:textbox name="testResultCategories" value="${descriptor.testResultCategories}"/>
</f:entry>
</f:optionalBlock>
</table>
Expand All @@ -83,44 +83,44 @@
<f:section title="${%Scan non-scanned builds}">
<f:entry title="${%Minimum number of Worker Threads}"
description="${%Minimum number of worker threads allotted for failure scan options}">
<f:textbox name="minimumNumberOfWorkerThreads"
value="${it.sodVariables.minimumSodWorkerThreads}"
default="${it.sodVariables.DEFAULT_MINIMUM_SOD_WORKER_THREADS}"/>
<f:number name="minimumNumberOfWorkerThreads"
value="${descriptor.sodVariables.minimumSodWorkerThreads}"
default="${descriptor.sodVariables.DEFAULT_MINIMUM_SOD_WORKER_THREADS}"/>
</f:entry>
<f:entry title="${%Maximum number of Worker Threads}"
description="${%Maximum number of worker threads allotted for failure scan options}">
<f:textbox name="maximumNumberOfWorkerThreads"
value="${it.sodVariables.maximumSodWorkerThreads}"
default="${it.sodVariables.DEFAULT_MAXIMUM_SOD_WORKER_THREADS}"/>
<f:number name="maximumNumberOfWorkerThreads"
value="${descriptor.sodVariables.maximumSodWorkerThreads}"
default="${descriptor.sodVariables.DEFAULT_MAXIMUM_SOD_WORKER_THREADS}"/>
</f:entry>
<f:entry title="${%Thread keep alive time}"
description="${%Thread keep alive time for failure scan options}">
<f:textbox name="threadKeepAliveTime"
value="${it.sodVariables.sodThreadKeepAliveTime}"
default="${it.sodVariables.DEFAULT_SOD_THREADS_KEEP_ALIVE_TIME}"/>
<f:number name="threadKeepAliveTime"
value="${descriptor.sodVariables.sodThreadKeepAliveTime}"
default="${descriptor.sodVariables.DEFAULT_SOD_THREADS_KEEP_ALIVE_TIME}"/>
</f:entry>
<f:entry title="${%Wait for job shutdown timeout}"
description="${%Wait for job shutdown timeout for failure scan options}">
<f:textbox name="waitForJobShutdownTime"
value="${it.sodVariables.sodWaitForJobShutdownTimeout}"
default="${it.sodVariables.DEFAULT_SOD_WAIT_FOR_JOBS_SHUTDOWN_TIMEOUT}"/>
<f:number name="waitForJobShutdownTime"
value="${descriptor.sodVariables.sodWaitForJobShutdownTimeout}"
default="${descriptor.sodVariables.DEFAULT_SOD_WAIT_FOR_JOBS_SHUTDOWN_TIMEOUT}"/>
</f:entry>
<f:entry title="${%Corepool number of Threads}"
description="${%Corepool number of worker threads allotted for failure scan options}">
<f:textbox name="corePoolNumberOfThreads"
value="${it.sodVariables.sodCorePoolNumberOfThreads}"
default="${it.sodVariables.DEFAULT_SOD_COREPOOL_THREADS}"/>
<f:number name="corePoolNumberOfThreads"
value="${descriptor.sodVariables.sodCorePoolNumberOfThreads}"
default="${descriptor.sodVariables.DEFAULT_SOD_COREPOOL_THREADS}"/>
</f:entry>
</f:section>
<f:entry title="${%Do not analyze aborted jobs}"
help="/plugin/build-failure-analyzer/help/help-doNotAnalyzeAbortedJobs.html">
<f:checkbox name="doNotAnalyzeAbortedJob" checked="${it.doNotAnalyzeAbortedJob}" default="false"/>
<f:checkbox name="doNotAnalyzeAbortedJob" checked="${descriptor.doNotAnalyzeAbortedJob}" default="false"/>
</f:entry>
<f:entry title="${%Max size of log file}"
description="${%maxLogSize}">
<f:textbox name="maxLogSize"
value="${it.maxLogSize}"
default="${it.DEFAULT_MAX_LOG_SIZE}"/>
value="${descriptor.maxLogSize}"
default="${descriptor.DEFAULT_MAX_LOG_SIZE}"/>
</f:entry>
</f:advanced>
</j:jelly>

0 comments on commit 1b28a47

Please sign in to comment.