Browse Source

Bug-56850: null safe property access

master
Jan Matèrne 11 years ago
parent
commit
d92b5275c6
2 changed files with 54 additions and 15 deletions
  1. +17
    -15
      src/main/org/apache/tools/ant/XmlLogger.java
  2. +37
    -0
      src/tests/junit/org/apache/tools/ant/XmlLoggerTest.java

+ 17
- 15
src/main/org/apache/tools/ant/XmlLogger.java View File

@@ -177,14 +177,8 @@ public class XmlLogger implements BuildLogger {
stacktrace.appendChild(errText);
synchronizedAppend(buildElement.element, stacktrace);
}
String outFilename = event.getProject().getProperty("XmlLogger.file");
if (outFilename == null) {
outFilename = "log.xml";
}
String xslUri = event.getProject().getProperty("ant.XmlLogger.stylesheet.uri");
if (xslUri == null) {
xslUri = "log.xsl";
}
String outFilename = getProperty(event, "XmlLogger.file", "log.xml");
String xslUri = getProperty(event, "ant.XmlLogger.stylesheet.uri", "log.xsl");
Writer out = null;
try {
// specify output in UTF8 otherwise accented characters will blow
@@ -208,6 +202,14 @@ public class XmlLogger implements BuildLogger {
buildElement = null;
}

private String getProperty(BuildEvent event, String propertyName, String defaultValue) {
String rv = defaultValue;
if (event != null && event.getProject() != null && event.getProject().getProperty(propertyName) != null) {
rv = event.getProject().getProperty(propertyName);
}
return rv;
}

/**
* Returns the stack of timed elements for the current thread.
* @return the stack of timed elements for the current thread
@@ -252,7 +254,7 @@ public class XmlLogger implements BuildLogger {
*/
public void targetFinished(BuildEvent event) {
Target target = event.getTarget();
TimedElement targetElement = (TimedElement) targets.get(target);
TimedElement targetElement = targets.get(target);
if (targetElement != null) {
long totalTime = System.currentTimeMillis() - targetElement.startTime;
targetElement.element.setAttribute(TIME_ATTR, DefaultLogger.formatTime(totalTime));
@@ -266,7 +268,7 @@ public class XmlLogger implements BuildLogger {
+ " finished target element = " + targetElement);
}
if (!threadStack.empty()) {
parentElement = (TimedElement) threadStack.peek();
parentElement = threadStack.peek();
}
}
if (parentElement == null) {
@@ -312,7 +314,7 @@ public class XmlLogger implements BuildLogger {
*/
public void taskFinished(BuildEvent event) {
Task task = event.getTask();
TimedElement taskElement = (TimedElement) tasks.get(task);
TimedElement taskElement = tasks.get(task);
if (taskElement == null) {
throw new RuntimeException("Unknown task " + task + " not in " + tasks);
}
@@ -321,7 +323,7 @@ public class XmlLogger implements BuildLogger {
Target target = task.getOwningTarget();
TimedElement targetElement = null;
if (target != null) {
targetElement = (TimedElement) targets.get(target);
targetElement = targets.get(target);
}
if (targetElement == null) {
synchronizedAppend(buildElement.element, taskElement.element);
@@ -346,7 +348,7 @@ public class XmlLogger implements BuildLogger {
* may be hiding the real task
*/
private TimedElement getTaskElement(Task task) {
TimedElement element = (TimedElement) tasks.get(task);
TimedElement element = tasks.get(task);
if (element != null) {
return element;
}
@@ -354,7 +356,7 @@ public class XmlLogger implements BuildLogger {
Task key = e.nextElement();
if (key instanceof UnknownElement) {
if (((UnknownElement) key).getTask() == task) {
return (TimedElement) tasks.get(key);
return tasks.get(key);
}
}
}
@@ -412,7 +414,7 @@ public class XmlLogger implements BuildLogger {
parentElement = getTaskElement(task);
}
if (parentElement == null && target != null) {
parentElement = (TimedElement) targets.get(target);
parentElement = targets.get(target);
}
if (parentElement != null) {
synchronizedAppend(parentElement.element, messageElement);


+ 37
- 0
src/tests/junit/org/apache/tools/ant/XmlLoggerTest.java View File

@@ -0,0 +1,37 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package org.apache.tools.ant;

import org.apache.tools.ant.taskdefs.Cvs;
import org.junit.Test;

public class XmlLoggerTest {

@Test
// see https://issues.apache.org/bugzilla/show_bug.cgi?id=56850
// "NPE in XmlLogger.buildFinished"
public void test() throws Throwable {
final XmlLogger logger = new XmlLogger();
final Cvs task = new Cvs();
final BuildEvent event = new BuildEvent(task);
logger.buildStarted(event);
logger.buildFinished(event);
}

}

Loading…
Cancel
Save