diff --git a/src/main/java/hudson/plugins/violations/render/FileModelProxy.java b/src/main/java/hudson/plugins/violations/render/FileModelProxy.java index ffe7222..560200c 100644 --- a/src/main/java/hudson/plugins/violations/render/FileModelProxy.java +++ b/src/main/java/hudson/plugins/violations/render/FileModelProxy.java @@ -1,6 +1,7 @@ package hudson.plugins.violations.render; import static com.google.common.collect.Lists.newArrayList; +import static java.util.logging.Level.WARNING; import hudson.Functions; import hudson.model.AbstractBuild; import hudson.plugins.violations.generate.XMLUtil; @@ -16,9 +17,10 @@ import java.util.Map.Entry; import java.util.Set; import java.util.TreeSet; -import java.util.logging.Level; import java.util.logging.Logger; +import com.google.common.base.Supplier; + /** * A proxy class for FileModel used to allow lazy loading of FileModel. This * class is also used to render the FileModel. @@ -27,7 +29,24 @@ public class FileModelProxy { private static final Logger LOG = Logger.getLogger(FileModelProxy.class.getName()); private final File xmlFile; - private FileModel fileModel; + private final Supplier fileModel = new Supplier() { + @Override + public FileModel get() { + if (!xmlFile.exists()) { + LOG.log(WARNING, "The file " + xmlFile + " does not exist"); + return null; + } + try { + FileModel t = new FileModel(); + ParseXML.parse(xmlFile, new FileModelParser().fileModel(t)); + return t; + } catch (Exception ex) { + LOG.log(WARNING, "Unable to parse " + xmlFile, ex); + return null; + } + + } + }; private String contextPath; private AbstractBuild build; @@ -83,7 +102,7 @@ public FileModelProxy contextPath(String contextPath) { * number of suppressed violations. */ public String typeLine(String type) { - FileModel.LimitType l = fileModel.getLimitTypeMap().get(type); + FileModel.LimitType l = fileModel.get().getLimitTypeMap().get(type); StringBuilder b = new StringBuilder(); if (l == null) { return type + " ?number?"; @@ -137,7 +156,7 @@ public List getFileContent() { int startLine = 0; int currentLine = -1; - for (Map.Entry e : fileModel.getLines().entrySet()) { + for (Map.Entry e : fileModel.get().getLines().entrySet()) { currentLine = e.getKey(); String line = e.getValue(); // Check if at start of block @@ -145,7 +164,7 @@ public List getFileContent() { // Start of block // Check if need to write previous block if (b.length() > 0) { - blockDataList.add(new BlockData(startLine, previousLine, b.toString(), new File(fileModel + blockDataList.add(new BlockData(startLine, previousLine, b.toString(), new File(fileModel.get() .getDisplayName()).getName())); } b = new StringBuilder(); @@ -153,7 +172,7 @@ public List getFileContent() { } previousLine = currentLine; - Set v = fileModel.getLineViolationMap().get(currentLine); + Set v = fileModel.get().getLineViolationMap().get(currentLine); // TR b.append(""); @@ -195,15 +214,15 @@ public List getFileContent() { b.append("\n"); } if (b.length() > 0) { - blockDataList.add(new BlockData(startLine, previousLine, b.toString(), new File(fileModel.getDisplayName()) - .getName())); + blockDataList.add(new BlockData(startLine, previousLine, b.toString(), new File(fileModel.get() + .getDisplayName()).getName())); } return blockDataList; } public String getVisualStudioLink(Violation v) { StringBuilder ret = new StringBuilder(); - String uriBase = "devenv:?file=" + this.fileModel.getDisplayName(); + String uriBase = "devenv:?file=" + this.fileModel.get().getDisplayName(); ret.append(""); ret.append(""); ret.append(""); @@ -270,22 +289,7 @@ public String getViolationsSummary() { * @return the file model or null if unable to parse. */ public FileModel getFileModel() { - if (fileModel != null) { - return fileModel; - } - if (!xmlFile.exists()) { - LOG.log(Level.WARNING, "The file " + xmlFile + " does not exist"); - return null; - } - try { - FileModel t = new FileModel(); - ParseXML.parse(xmlFile, new FileModelParser().fileModel(t)); - fileModel = t; - return fileModel; - } catch (Exception ex) { - LOG.log(Level.WARNING, "Unable to parse " + xmlFile, ex); - return null; - } + return this.fileModel.get(); } private String getSeverityIcon(int level) { @@ -410,7 +414,7 @@ private void showDiv(StringBuilder b, Set violations) { } public String getFileNameAlt() { - return new File(fileModel.getDisplayName()).getName(); + return new File(fileModel.get().getDisplayName()).getName(); } public String getSummaryTable() { @@ -427,7 +431,7 @@ public String getSummaryTable() { gst.append(" Description\n"); gst.append(" \n"); - Set violations = fileModel.getLineViolationMap().get(0); + Set violations = fileModel.get().getLineViolationMap().get(0); for (Violation v : violations) { ++count;