sbailliez 02/01/26 10:53:26
Modified: src/main/org/apache/tools/ant/listener Log4jListener.java
MailLogger.java
Log:
- Fixed audit violations
- Minor code simplification
- Code layout
Revision Changes Path
1.5 +22 -27
jakarta-ant/src/main/org/apache/tools/ant/listener/Log4jListener.java
Index: Log4jListener.java
===================================================================
RCS file:
/home/cvs/jakarta-ant/src/main/org/apache/tools/ant/listener/Log4jListener.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- Log4jListener.java 22 Nov 2001 08:46:55 -0000 1.4
+++ Log4jListener.java 26 Jan 2002 18:53:26 -0000 1.5
@@ -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 @@
*/
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 @@
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 @@
categoryObject = event.getProject();
}
}
-
+
Category cat =
Category.getInstance(categoryObject.getClass().getName());
switch (event.getPriority()) {
case Project.MSG_ERR:
@@ -173,7 +168,7 @@
case Project.MSG_DEBUG:
cat.debug(event.getMessage());
break;
- default:
+ default:
cat.error(event.getMessage());
break;
}
1.6 +115 -123
jakarta-ant/src/main/org/apache/tools/ant/listener/MailLogger.java
Index: MailLogger.java
===================================================================
RCS file:
/home/cvs/jakarta-ant/src/main/org/apache/tools/ant/listener/MailLogger.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- MailLogger.java 14 Jan 2002 00:59:41 -0000 1.5
+++ MailLogger.java 26 Jan 2002 18:53:26 -0000 1.6
@@ -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.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 @@
*/
public class MailLogger extends DefaultLogger {
- private StringBuffer buffer = new StringBuffer();
+ private StringBuffer buffer = new StringBuffer();
- /**
- * Sends an e-mail with the log results.
- *
- [EMAIL PROTECTED] 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) {
- }
+ /**
+ * 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));
- }
-
- boolean success = (event.getException() == null);
- String prefix = success ? "success" : "failure";
- try {
- boolean notify = Project.toBoolean(getValue(properties,
- prefix + ".notify", "on"));
+ for (Enumeration enum = fileProperties.keys();
enum.hasMoreElements();) {
+ String key = (String) enum.nextElement();
+ properties.put(key, fileProperties.getProperty(key));
+ }
- if (!notify) {
- return;
- }
+ boolean success = (event.getException() == null);
+ String prefix = success ? "success" : "failure";
- String mailhost = getValue(properties, "mailhost", "localhost");
- String from = getValue(properties, "from", null);
+ try {
+ boolean notify = Project.toBoolean(getValue(properties,
+ prefix + ".notify", "on"));
+
+ if (!notify) {
+ return;
+ }
+
+ 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");
+
+ sendMail(mailhost, from, toList, subject, buffer.toString());
+ } catch (Exception e) {
+ System.out.println("MailLogger failed to send e-mail!");
+ e.printStackTrace();
+ }
+ }
- 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).append(StringUtils.LINE_SEP);
}
- }
- /**
- * Receives and buffers log messages.
- *
- [EMAIL PROTECTED] message
- */
- protected void log(String message) {
- buffer.append(message + StringUtils.LINE_SEP);
- }
-
-
- /**
- * Gets the value of a property.
- *
- [EMAIL PROTECTED] properties Properties to obtain value from
- [EMAIL PROTECTED] name suffix of property name. "MailLogger."
will be
- * prepended internally.
- [EMAIL PROTECTED] defaultValue value returned if not present in the
properties. Set
- * to null to make required.
- [EMAIL PROTECTED] The value of the property, or default
value.
- [EMAIL PROTECTED] 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;
+ /**
+ * 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 (object != null) {
- value = (String) object;
- }
+ if (value == null) {
+ value = defaultValue;
+ }
- if (value == null) {
- throw new Exception("Missing required parameter: " + name);
- }
+ if (value == null) {
+ throw new Exception("Missing required parameter: " +
propertyName);
+ }
- return value;
- }
+ return value;
+ }
- /**
- * Send the mail
- *
- [EMAIL PROTECTED] mailhost mail server
- [EMAIL PROTECTED] from from address
- [EMAIL PROTECTED] toList comma-separated recipient list
- [EMAIL PROTECTED] subject mail subject
- [EMAIL PROTECTED] message mail body
- [EMAIL PROTECTED] 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());
- }
+ /**
+ * 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.setSubject(subject);
- PrintStream ps = mailMessage.getPrintStream();
- ps.println(message);
+ PrintStream ps = mailMessage.getPrintStream();
+ ps.println(message);
- mailMessage.sendAndClose();
- }
+ mailMessage.sendAndClose();
+ }
}
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>