Browse Source

- Fixed audit violations

- Minor code simplification
- Code layout


git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@270888 13f79535-47bb-0310-9956-ffa450edef68
master
Stephane Bailliez 24 years ago
parent
commit
8c9b85883e
2 changed files with 139 additions and 152 deletions
  1. +22
    -27
      src/main/org/apache/tools/ant/listener/Log4jListener.java
  2. +117
    -125
      src/main/org/apache/tools/ant/listener/MailLogger.java

+ 22
- 27
src/main/org/apache/tools/ant/listener/Log4jListener.java View File

@@ -54,16 +54,15 @@

package org.apache.tools.ant.listener;

import org.apache.tools.ant.BuildListener;
import org.apache.log4j.Category;
import org.apache.log4j.helpers.NullEnumeration;
import org.apache.tools.ant.BuildEvent;
import org.apache.tools.ant.BuildListener;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.Target;
import org.apache.tools.ant.Task;

import org.apache.log4j.Category;
import org.apache.log4j.helpers.NullEnumeration;

/**
* Listener which sends events to Log4j logging system
*
@@ -71,60 +70,57 @@ import org.apache.log4j.helpers.NullEnumeration;
*/
public class Log4jListener implements BuildListener {
final static String LOG4J_CONFIG_PROPERTY = "log4j.configuration";
private boolean initialized = false;
public Log4jListener() {
initialized = false;
Category cat = Category.getInstance("org.apache.tools.ant");
Category rootCat = Category.getRoot();
if (!(rootCat.getAllAppenders() instanceof NullEnumeration)) {
initialized = true;
}
else {
} else {
cat.error("No log4j.properties in build area");
}
}
public void buildStarted(BuildEvent event) {
if (initialized) {
Category cat = Category.getInstance(Project.class.getName());
cat.info("Build started.");
}
}
public void buildFinished(BuildEvent event) {
if (initialized) {
Category cat = Category.getInstance(Project.class.getName());
if (event.getException() == null) {
cat.info("Build finished.");
}
else {
} else {
cat.error("Build finished with error.", event.getException());
}
}
}
}
public void targetStarted(BuildEvent event) {
if (initialized) {
Category cat = Category.getInstance(Target.class.getName());
cat.info("Target \"" + event.getTarget().getName() + "\" started.");
}
}
public void targetFinished(BuildEvent event) {
if (initialized) {
String targetName = event.getTarget().getName();
Category cat = Category.getInstance(Target.class.getName());
if (event.getException() == null) {
cat.info("Target \"" + event.getTarget().getName() + "\" finished.");
}
else {
cat.error("Target \"" + event.getTarget().getName() + "\" finished with error.", event.getException());
cat.info("Target \"" + targetName + "\" finished.");
} else {
cat.error("Target \"" + targetName + "\" finished with error.", event.getException());
}
}
}
}
public void taskStarted(BuildEvent event) {
if (initialized) {
Task task = event.getTask();
@@ -132,20 +128,19 @@ public class Log4jListener implements BuildListener {
cat.info("Task \"" + task.getTaskName() + "\" started.");
}
}
public void taskFinished(BuildEvent event) {
if (initialized) {
Task task = event.getTask();
Category cat = Category.getInstance(task.getClass().getName());
if (event.getException() == null) {
cat.info("Task \"" + task.getTaskName() + "\" finished.");
}
else {
} else {
cat.error("Task \"" + task.getTaskName() + "\" finished with error.", event.getException());
}
}
}
public void messageLogged(BuildEvent event) {
if (initialized) {
Object categoryObject = event.getTask();
@@ -155,7 +150,7 @@ public class Log4jListener implements BuildListener {
categoryObject = event.getProject();
}
}
Category cat = Category.getInstance(categoryObject.getClass().getName());
switch (event.getPriority()) {
case Project.MSG_ERR:
@@ -173,7 +168,7 @@ public class Log4jListener implements BuildListener {
case Project.MSG_DEBUG:
cat.debug(event.getMessage());
break;
default:
default:
cat.error(event.getMessage());
break;
}


+ 117
- 125
src/main/org/apache/tools/ant/listener/MailLogger.java View File

@@ -1,7 +1,7 @@
/*
* The Apache Software License, Version 1.1
*
* Copyright (c) 2001 The Apache Software Foundation. All rights
* Copyright (c) 2001-2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -57,7 +57,6 @@ import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;

import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Properties;
@@ -100,145 +99,138 @@ import org.apache.tools.mail.MailMessage;
*/
public class MailLogger extends DefaultLogger {

private StringBuffer buffer = new StringBuffer();

/**
* Sends an e-mail with the log results.
*
*@param event
*/
public void buildFinished(BuildEvent event) {
super.buildFinished(event);

Project project = event.getProject();
Hashtable properties = project.getProperties();

// overlay specified properties file (if any), which overrides project
// settings
Properties fileProperties = new Properties();
String filename = (String) properties.get("MailLogger.properties.file");
if (filename != null) {
InputStream is = null;
try {
is = new FileInputStream(filename);
if (is != null) {
fileProperties.load(is);
}
}
catch (IOException ioe) {
// ignore because properties file is not required
}
finally {
if (is != null) {
try {
is.close();
}
catch (IOException e) {
}
private StringBuffer buffer = new StringBuffer();

/**
* Sends an e-mail with the log results.
*
* @param event
*/
public void buildFinished(BuildEvent event) {
super.buildFinished(event);

Project project = event.getProject();
Hashtable properties = project.getProperties();

// overlay specified properties file (if any), which overrides project
// settings
Properties fileProperties = new Properties();
String filename = (String) properties.get("MailLogger.properties.file");
if (filename != null) {
InputStream is = null;
try {
is = new FileInputStream(filename);
fileProperties.load(is);
} catch (IOException ioe) {
// ignore because properties file is not required
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
}
}
}
}
}
}

for (Enumeration enum = fileProperties.keys(); enum.hasMoreElements(); ) {
String key = (String) enum.nextElement();
properties.put(key, fileProperties.getProperty(key));
}
for (Enumeration enum = fileProperties.keys(); enum.hasMoreElements();) {
String key = (String) enum.nextElement();
properties.put(key, fileProperties.getProperty(key));
}

boolean success = (event.getException() == null);
String prefix = success ? "success" : "failure";
boolean success = (event.getException() == null);
String prefix = success ? "success" : "failure";

try {
boolean notify = Project.toBoolean(getValue(properties,
prefix + ".notify", "on"));
try {
boolean notify = Project.toBoolean(getValue(properties,
prefix + ".notify", "on"));

if (!notify) {
return;
}
if (!notify) {
return;
}

String mailhost = getValue(properties, "mailhost", "localhost");
String from = getValue(properties, "from", null);
String mailhost = getValue(properties, "mailhost", "localhost");
String from = getValue(properties, "from", null);

String toList = getValue(properties, prefix + ".to", null);
String subject = getValue(properties, prefix + ".subject",
(success) ? "Build Success" : "Build Failure");
String toList = getValue(properties, prefix + ".to", null);
String subject = getValue(properties, prefix + ".subject",
(success) ? "Build Success" : "Build Failure");

sendMail(mailhost, from, toList, subject, buffer.toString());
}
catch (Exception e) {
System.out.println("MailLogger failed to send e-mail!");
e.printStackTrace();
}
}


/**
* Receives and buffers log messages.
*
*@param message
*/
protected void log(String message) {
buffer.append(message + StringUtils.LINE_SEP);
}


/**
* Gets the value of a property.
*
*@param properties Properties to obtain value from
*@param name suffix of property name. "MailLogger." will be
* prepended internally.
*@param defaultValue value returned if not present in the properties. Set
* to null to make required.
*@return The value of the property, or default value.
*@exception Exception thrown if no default value is specified and the
* property is not present in properties.
*/
private String getValue(Hashtable properties, String name, String defaultValue)
throws Exception {
name = "MailLogger." + name;
Object object = properties.get(name);
String value = defaultValue;

if (object != null) {
value = (String) object;
sendMail(mailhost, from, toList, subject, buffer.toString());
} catch (Exception e) {
System.out.println("MailLogger failed to send e-mail!");
e.printStackTrace();
}
}

if (value == null) {
throw new Exception("Missing required parameter: " + name);

/**
* Receives and buffers log messages.
*
* @param message
*/
protected void log(String message) {
buffer.append(message).append(StringUtils.LINE_SEP);
}

return value;
}


/**
* Send the mail
*
*@param mailhost mail server
*@param from from address
*@param toList comma-separated recipient list
*@param subject mail subject
*@param message mail body
*@exception IOException thrown if sending message fails
*/
private void sendMail(String mailhost, String from, String toList,
String subject, String message) throws IOException {
MailMessage mailMessage = new MailMessage(mailhost);

mailMessage.from(from);

StringTokenizer t = new StringTokenizer(toList, ", ", false);
while (t.hasMoreTokens()) {
mailMessage.to(t.nextToken());

/**
* Gets the value of a property.
*
* @param properties Properties to obtain value from
* @param name suffix of property name. "MailLogger." will be
* prepended internally.
* @param defaultValue value returned if not present in the properties. Set
* to null to make required.
* @return The value of the property, or default value.
* @exception Exception thrown if no default value is specified and the
* property is not present in properties.
*/
private String getValue(Hashtable properties, String name, String defaultValue)
throws Exception {
String propertyName = "MailLogger." + name;
String value = (String) properties.get(propertyName);

if (value == null) {
value = defaultValue;
}

if (value == null) {
throw new Exception("Missing required parameter: " + propertyName);
}

return value;
}

mailMessage.setSubject(subject);

PrintStream ps = mailMessage.getPrintStream();
ps.println(message);
/**
* Send the mail
*
* @param mailhost mail server
* @param from from address
* @param toList comma-separated recipient list
* @param subject mail subject
* @param message mail body
* @exception IOException thrown if sending message fails
*/
private void sendMail(String mailhost, String from, String toList,
String subject, String message) throws IOException {
MailMessage mailMessage = new MailMessage(mailhost);

mailMessage.from(from);

StringTokenizer t = new StringTokenizer(toList, ", ", false);
while (t.hasMoreTokens()) {
mailMessage.to(t.nextToken());
}

mailMessage.setSubject(subject);

mailMessage.sendAndClose();
}
PrintStream ps = mailMessage.getPrintStream();
ps.println(message);

mailMessage.sendAndClose();
}
}



Loading…
Cancel
Save