Skip to content
This repository has been archived by the owner on Apr 6, 2022. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
[JENKINS-20874] lazy load FindBugsMessages singleton
- implemented lazy initialization for FindBugsMessages singleton (using
Initialization-on-demand holder idiom)
- I assumed the SaxSetup wrapper in FindBugsPlugin.start() was only
needed for the parsing of message xmls
  • Loading branch information
XN137 committed Apr 30, 2014
1 parent 3fd0246 commit eefcb73
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 52 deletions.
26 changes: 23 additions & 3 deletions plugin/src/main/java/hudson/plugins/findbugs/FindBugsMessages.java
Expand Up @@ -7,7 +7,10 @@
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import hudson.plugins.analysis.util.SaxSetup;
import org.apache.commons.digester3.Digester;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
Expand All @@ -28,15 +31,32 @@ public final class FindBugsMessages {
private final Map<String, String> jaShortMessages = new HashMap<String, String>();
private final Map<String, String> frShortMessages = new HashMap<String, String>();

private static final FindBugsMessages INSTANCE = new FindBugsMessages();
private static class FindBugsMessagesHolder {
private static FindBugsMessages INSTANCE = FindBugsMessages.initializeSingleton();
}

private static FindBugsMessages initializeSingleton() {
FindBugsMessages res = new FindBugsMessages();
SaxSetup sax = new SaxSetup();
try {
res.initialize();
} catch(Exception e) {
Logger.getLogger(FindBugsMessages.class.getName()).log(Level.WARNING, "FindBugsMessages initializeSingleton failed", e);
}
finally {
sax.cleanup();
}
return res;
}

/**
* Returns the singleton instance.
*
* @return the singleton instance
*/
public static FindBugsMessages getInstance() {
return INSTANCE;
// lazily created instance, since inner classes are not loaded until they are referenced
return FindBugsMessagesHolder.INSTANCE;
}

/**
Expand All @@ -48,7 +68,7 @@ public static FindBugsMessages getInstance() {
* if we can't read a file
*/
@edu.umd.cs.findbugs.annotations.SuppressWarnings({"DE", "REC"})
public void initialize() throws IOException, SAXException {
private void initialize() throws IOException, SAXException {
synchronized (messages) {
loadMessages("messages.xml", messages, shortMessages);
loadMessages("fb-contrib-messages.xml", messages, shortMessages);
Expand Down
17 changes: 3 additions & 14 deletions plugin/src/main/java/hudson/plugins/findbugs/FindBugsPlugin.java
@@ -1,11 +1,7 @@
package hudson.plugins.findbugs;

import java.io.IOException;

import hudson.plugins.analysis.util.SaxSetup;
import org.apache.commons.lang.StringUtils;
import org.apache.maven.plugin.MojoExecution;
import org.xml.sax.SAXException;

import hudson.Plugin;

Expand All @@ -19,18 +15,11 @@
*/
public class FindBugsPlugin extends Plugin {
@Override
public void start() throws IOException, SAXException {
SaxSetup sax = new SaxSetup();
try {
initializeMessages();
}
finally {
sax.cleanup();
}
public void start() {
initializeDetails();
}

private void initializeMessages() throws IOException, SAXException {
FindBugsMessages.getInstance().initialize();
private void initializeDetails() {
FindBugsDetailFactory detailBuilder = new FindBugsDetailFactory();
DetailFactory.addDetailBuilder(FindBugsResultAction.class, detailBuilder);
if (PluginDescriptor.isMavenPluginInstalled()) {
Expand Down
Expand Up @@ -101,16 +101,9 @@ public void parseFindbugsSecurityMessages() throws IOException, SAXException {

/**
* Checks that a warning message of each file is correctly parsed.
*
* @throws SAXException
* if we can't read the file
* @throws IOException
* if we can't read the file
*/
@Test
public void parse() throws IOException, SAXException {
FindBugsMessages.getInstance().initialize();

public void parse() {
assertTrue(WRONG_WARNING_MESSAGE, FindBugsMessages.getInstance().getMessage(NP_STORE_INTO_NONNULL_FIELD, Locale.ENGLISH).contains("A value that could be null is stored into a field that has been annotated as NonNull."));
assertTrue(WRONG_WARNING_MESSAGE, FindBugsMessages.getInstance().getMessage(NP_STORE_INTO_NONNULL_FIELD, Locale.GERMAN).contains("A value that could be null is stored into a field that has been annotated as NonNull."));
assertEquals(WRONG_WARNING_MESSAGE, "Store of null value into field annotated NonNull", FindBugsMessages.getInstance().getShortMessage(NP_STORE_INTO_NONNULL_FIELD, Locale.ENGLISH));
Expand All @@ -120,16 +113,9 @@ public void parse() throws IOException, SAXException {

/**
* Checks that localized messages are loaded.
*
* @throws SAXException
* if we can't read the file
* @throws IOException
* if we can't read the file
*/
@Test
public void parseLocalizations() throws IOException, SAXException {
FindBugsMessages.getInstance().initialize();

public void parseLocalizations() {
assertTrue(WRONG_WARNING_MESSAGE, FindBugsMessages.getInstance().getShortMessage(NP_STORE_INTO_NONNULL_FIELD, Locale.FRANCE).contains("Stocke une valeur null dans"));
assertTrue(WRONG_WARNING_MESSAGE, FindBugsMessages.getInstance().getMessage(NP_STORE_INTO_NONNULL_FIELD, Locale.FRANCE).contains("Une valeur qui pourrait"));
}
Expand Down
Expand Up @@ -14,7 +14,6 @@
import hudson.plugins.analysis.test.AbstractEnglishLocaleTest;
import hudson.plugins.analysis.util.SaxSetup;
import hudson.plugins.analysis.util.model.*;
import hudson.plugins.findbugs.FindBugsMessages;
import hudson.plugins.findbugs.Messages;

/**
Expand Down Expand Up @@ -67,14 +66,10 @@ public InputStream getInputStream() throws IOException {
*/
@Test
public void issue7238() throws IOException, SAXException, DocumentException {
FindBugsMessages.getInstance().initialize();

MavenModule module = parseFile("issue7238.xml", false);
assertEquals("Wrong number of warnings", 1820, module.getNumberOfAnnotations());
}



/**
* Parses fb-contrib messages.
*
Expand All @@ -87,8 +82,6 @@ public void issue7238() throws IOException, SAXException, DocumentException {
*/
@Test
public void issue7238withIncludePattern() throws IOException, SAXException, DocumentException {
FindBugsMessages.getInstance().initialize();

MavenModule module = parseFile("issue7238.xml", false, null, "*gti/plc/test*,*gti/plc/server/siemens/libnodave*,*gti/plc/util*");
assertEquals("Wrong number of warnings", 68, module.getNumberOfAnnotations());
}
Expand All @@ -105,8 +98,6 @@ public void issue7238withIncludePattern() throws IOException, SAXException, Docu
*/
@Test
public void issue7238withExcludePattern() throws IOException, SAXException, DocumentException {
FindBugsMessages.getInstance().initialize();

MavenModule module = parseFile("issue7238.xml", false, "*gti/plc/test*,*gti/plc/server/siemens/libnodave*,*gti/plc/util*", null);
assertEquals("Wrong number of warnings", 1752, module.getNumberOfAnnotations());
}
Expand All @@ -123,8 +114,6 @@ public void issue7238withExcludePattern() throws IOException, SAXException, Docu
*/
@Test
public void issue7238withIncludeExcludePattern() throws IOException, SAXException, DocumentException {
FindBugsMessages.getInstance().initialize();

MavenModule module = parseFile("issue7238.xml", false,
"*gti/plc/server/siemens/libnodave*", "*gti/plc/test*,*gti/plc/server/siemens/libnodave*,*gti/plc/util*");
assertEquals("Wrong number of warnings", 57, module.getNumberOfAnnotations());
Expand All @@ -143,8 +132,6 @@ public void issue7238withIncludeExcludePattern() throws IOException, SAXExceptio
*/
@Test
public void issue12314() throws IOException, SAXException, DocumentException {
FindBugsMessages.getInstance().initialize();

MavenModule module = parseFile("issue12314.xml", false);
assertEquals("Wrong number of warnings", 1, module.getNumberOfAnnotations());

Expand All @@ -168,8 +155,6 @@ public void issue12314() throws IOException, SAXException, DocumentException {
*/
@Test
public void issue7312and7932() throws IOException, SAXException, DocumentException {
FindBugsMessages.getInstance().initialize();

String saxParser = this.getClass().getName();
System.setProperty(SaxSetup.SAX_DRIVER_PROPERTY, saxParser);
MavenModule module = parseFile("issue7312.xml", false);
Expand All @@ -190,8 +175,6 @@ public void issue7312and7932() throws IOException, SAXException, DocumentExcepti
*/
@Test
public void issue7932Null() throws IOException, SAXException, DocumentException {
FindBugsMessages.getInstance().initialize();

System.clearProperty(SaxSetup.SAX_DRIVER_PROPERTY);
MavenModule module = parseFile("issue7312.xml", false);
assertEquals("Wrong number of warnings", 0, module.getNumberOfAnnotations());
Expand Down Expand Up @@ -423,8 +406,6 @@ private void scanNativeFile(final String findbugsFile, final String projectName,
final int ranges1, final String fileName2, final String packageName2, final int start2, final int end2, final int ranges2, final boolean isRankActivated)
throws IOException, SAXException, DocumentException {
// CHECKSTYLE:ON
FindBugsMessages.getInstance().initialize();

MavenModule module = parseFile(findbugsFile, isRankActivated);
assertEquals("Wrong project name guessed", projectName, module.getName());

Expand Down

0 comments on commit eefcb73

Please sign in to comment.