rwaldhoff 01/08/02 09:27:06
Modified: httpclient/src/conf .cvsignore log4j.properties.sample
httpclient/src/java/org/apache/commons/httpclient
Base64.java HeaderElement.java HttpClient.java
Added: httpclient/src/conf simplelog.properties.sample
httpclient/src/java/org/apache/commons/httpclient/log
Log.java Log4JCategoryLog.java LogSource.java
NoOpLog.java SimpleLog.java
httpclient/src/test/org/apache/commons/httpclient
TestBase64.java
Log:
refactored logging so that log4j is no longer a runtime depedency
updates to documentation to come
Revision Changes Path
1.2 +2 -1 jakarta-commons/httpclient/src/conf/.cvsignore
Index: .cvsignore
===================================================================
RCS file: /home/cvs/jakarta-commons/httpclient/src/conf/.cvsignore,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- .cvsignore 2001/07/27 22:53:52 1.1
+++ .cvsignore 2001/08/02 16:27:06 1.2
@@ -1 +1,2 @@
-log4j.properties
\ No newline at end of file
+log4j.properties
+simplelog.properties
\ No newline at end of file
1.2 +3 -3 jakarta-commons/httpclient/src/conf/log4j.properties.sample
Index: log4j.properties.sample
===================================================================
RCS file: /home/cvs/jakarta-commons/httpclient/src/conf/log4j.properties.sample,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- log4j.properties.sample 2001/07/27 22:53:52 1.1
+++ log4j.properties.sample 2001/08/02 16:27:06 1.2
@@ -1,6 +1,6 @@
-######################################
-# Configure the log4j logging system
-######################################
+###################################################
+# Sample configuraion for the log4j logging system
+###################################################
# Set the root output level and appenders
log4j.rootCategory=INFO, consoleAppender
1.1 jakarta-commons/httpclient/src/conf/simplelog.properties.sample
Index: simplelog.properties.sample
===================================================================
####################################################
# Sample configuraion for the simple logging system
####################################################
# httpclient.simplelog.defaultlog
# - Default log level for all SimpleLogs.
httpclient.simplelog.defaultlog=FATAL
# httpclient.simplelog.showlogname
# - When true, show the name of the log in log output.
httpclient.simplelog.showlogname=false
# httpclient.simplelog.showdate
# - When true, show a timestamp the log in log output.
httpclient.simplelog.showdate=true
# httpclient.simplelog.dateformat
# - A SimpleDateFormat format string for timestamps in the log output
httpclient.simplelog.dateformat=yyyy/MM/dd HH:mm:ss:SSS zzz
# httpclient.simplelog.log.*
# - Sets the logging level for specific logs.
# Note that if that if you set the log level
# for a log named "a.b", it will apply to all
# logs with names of the form "a.b.c[.d]*"
# unless overriden by a more specific log setting.
#httpclient.simplelog.log.org.apache.commons.httpclient=DEBUG
#httpclient.simplelog.log.org.apache.commons.httpclient.HttpClient=DEBUG
#httpclient.simplelog.log.org.apache.commons.httpclient.HeaderElement=DEBUG
#httpclient.simplelog.log.org.apache.commons.httpclient.Base64=DEBUG
1.3 +6 -6
jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/Base64.java
Index: Base64.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/Base64.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- Base64.java 2001/07/27 22:53:53 1.2
+++ Base64.java 2001/08/02 16:27:06 1.3
@@ -1,7 +1,7 @@
/*
- * $Header:
/home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/Base64.java,v
1.2 2001/07/27 22:53:53 rwaldhoff Exp $
- * $Revision: 1.2 $
- * $Date: 2001/07/27 22:53:53 $
+ * $Header:
/home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/Base64.java,v
1.3 2001/08/02 16:27:06 rwaldhoff Exp $
+ * $Revision: 1.3 $
+ * $Date: 2001/08/02 16:27:06 $
*
* ====================================================================
*
@@ -64,7 +64,7 @@
package org.apache.commons.httpclient;
-import org.apache.log4j.Category;
+import org.apache.commons.httpclient.log.*;
/**
* This class provides encode/decode for RFC 2045 Base64 as
@@ -75,7 +75,7 @@
* This class is used by XML Schema binary format validation
*
* @author Jeffrey Rodriguez
- * @version $Id: Base64.java,v 1.2 2001/07/27 22:53:53 rwaldhoff Exp $
+ * @version $Id: Base64.java,v 1.3 2001/08/02 16:27:06 rwaldhoff Exp $
*/
public final class Base64 {
@@ -90,7 +90,7 @@
static private final byte PAD = ( byte ) '=';
static private byte [] base64Alphabet = new byte[BASELENGTH];
static private byte [] lookUpBase64Alphabet = new byte[LOOKUPLENGTH];
- static private final Category log =
Category.getInstance("org.apache.commons.httpclient.Base64");
+ static private final Log log =
LogSource.getInstance("org.apache.commons.httpclient.Base64");
static {
1.3 +5 -5
jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HeaderElement.java
Index: HeaderElement.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HeaderElement.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- HeaderElement.java 2001/07/27 22:53:53 1.2
+++ HeaderElement.java 2001/08/02 16:27:06 1.3
@@ -1,7 +1,7 @@
/*
- * $Header:
/home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HeaderElement.java,v
1.2 2001/07/27 22:53:53 rwaldhoff Exp $
- * $Revision: 1.2 $
- * $Date: 2001/07/27 22:53:53 $
+ * $Header:
/home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HeaderElement.java,v
1.3 2001/08/02 16:27:06 rwaldhoff Exp $
+ * $Revision: 1.3 $
+ * $Date: 2001/08/02 16:27:06 $
*
* ====================================================================
*
@@ -67,7 +67,7 @@
import java.util.NoSuchElementException;
import java.util.StringTokenizer;
import java.util.Vector;
-import org.apache.log4j.Category;
+import org.apache.commons.httpclient.log.*;
/**
* Some HTTP headers (such as the set-cookie header) have values that
@@ -131,7 +131,7 @@
}
// -------------------------------------------------------- Class Variables
- static private final Category log =
Category.getInstance("org.apache.commons.httpclient.HeaderElement");
+ static private final Log log =
LogSource.getInstance("org.apache.commons.httpclient.HeaderElement");
private static final BitSet SEPARATORS = new BitSet(128);
private static final BitSet TOKEN_CHAR = new BitSet(128);
1.22 +5 -5
jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpClient.java
Index: HttpClient.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpClient.java,v
retrieving revision 1.21
retrieving revision 1.22
diff -u -r1.21 -r1.22
--- HttpClient.java 2001/07/27 22:53:53 1.21
+++ HttpClient.java 2001/08/02 16:27:06 1.22
@@ -1,7 +1,7 @@
/*
- * $Header:
/home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpClient.java,v
1.21 2001/07/27 22:53:53 rwaldhoff Exp $
- * $Revision: 1.21 $
- * $Date: 2001/07/27 22:53:53 $
+ * $Header:
/home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpClient.java,v
1.22 2001/08/02 16:27:06 rwaldhoff Exp $
+ * $Revision: 1.22 $
+ * $Date: 2001/08/02 16:27:06 $
*
* ====================================================================
*
@@ -76,7 +76,7 @@
import java.lang.reflect.Method;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
-import org.apache.log4j.Category;
+import org.apache.commons.httpclient.log.*;
/**
* HTTP client main class.
@@ -104,7 +104,7 @@
// -------------------------------------------------------------- Constants
- static private final Category log =
Category.getInstance("org.apache.commons.httpclient.HttpClient");
+ static private final Log log =
LogSource.getInstance("org.apache.commons.httpclient.HttpClient");
/**
* HTTP Date format pattern (RFC 2068, 822, 1123).
1.1
jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/log/Log.java
Index: Log.java
===================================================================
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE file.
*/
package org.apache.commons.httpclient.log;
/**
* A simple logging interface abstracting log4j.
* @author Rod Waldhoff
* @version $Id: Log.java,v 1.1 2001/08/02 16:27:06 rwaldhoff Exp $
*/
public interface Log {
public void assert(boolean assertion, String msg);
public void debug(Object message);
public void debug(Object message, Throwable t);
public void info(Object message);
public void info(Object message, Throwable t);
public void warn(Object message);
public void warn(Object message, Throwable t);
public void error(Object message);
public void error(Object message, Throwable t);
public void fatal(Object message);
public void fatal(Object message, Throwable t);
}
1.1
jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/log/Log4JCategoryLog.java
Index: Log4JCategoryLog.java
===================================================================
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE file.
*/
package org.apache.commons.httpclient.log;
import org.apache.log4j.Category;
/**
* @author Rod Waldhoff
* @version $Id: Log4JCategoryLog.java,v 1.1 2001/08/02 16:27:06 rwaldhoff Exp $
*/
public class Log4JCategoryLog implements Log {
Category _category = null;
public Log4JCategoryLog(String name) {
_category = Category.getInstance(name);
}
public final void assert(boolean assertion, String msg) {
_category.assert(assertion,msg);
}
public final void debug(Object message) {
_category.debug(message);
}
public final void debug(Object message, Throwable t) {
_category.debug(message,t);
}
public final void info(Object message) {
_category.info(message);
}
public final void info(Object message, Throwable t) {
_category.info(message,t);
}
public final void warn(Object message) {
_category.warn(message);
}
public final void warn(Object message, Throwable t) {
_category.warn(message,t);
}
public final void error(Object message) {
_category.error(message);
}
public final void error(Object message, Throwable t) {
_category.error(message,t);
}
public final void fatal(Object message) {
_category.fatal(message);
}
public final void fatal(Object message, Throwable t) {
_category.fatal(message,t);
}
}
1.1
jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/log/LogSource.java
Index: LogSource.java
===================================================================
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE file.
*/
package org.apache.commons.httpclient.log;
import java.util.HashMap;
import java.lang.reflect.Constructor;
/**
* @author Rod Waldhoff
* @version $Id: LogSource.java,v 1.1 2001/08/02 16:27:06 rwaldhoff Exp $
*/
public class LogSource {
static protected HashMap _logs = new HashMap();
static protected boolean _log4jIsAvailable = false;
static {
try {
if(null != Class.forName("org.apache.log4j.Category")) {
_log4jIsAvailable = true;
} else {
_log4jIsAvailable = false;
}
} catch(ClassNotFoundException e) {
_log4jIsAvailable = false;
}
}
private LogSource() {
}
static public Log getInstance(String name) {
Log log = (Log)(_logs.get(name));
if(null == log) {
log = makeNewLogInstance(name);
_logs.put(name,log);
}
return log;
}
static public Log getInstance(Class clazz) {
return getInstance(clazz.getName());
}
/**
* Create a new {@link Log} implementation, based
* on the given <i>name</i>
* <p>
* The specific {@link Log} implementation returned
* is determined by the value of the
* <tt>httpclient.log</tt> property.
* The value of <tt>httpclient.log</tt> may be set to
* the fully specified name of a class that implements
* the {@link Log} interface. This class must also
* have a public constructor that takes a single
* {@link String} argument (containing the <i>name</i>
* of the {@link Log} to be constructed.
* <p>
* When <tt>httpclient.log</tt> is not set,
* or when no corresponding class can be found,
* this method will return a {@link Log4JCategoryLog}
* if the log4j {@link org.apache.log4j.Category} class is
* available in the {@link LogSource}'s classpath, or
* a {@link NoOpLog} if it is not.
*
* @param name the log name (or category)
*/
static public Log makeNewLogInstance(String name) {
Log log = null;
String logclassname =
System.getProperty("httpclient.log","org.apache.commons.httpclient.log.NoOpLog");
try {
Class logclass = Class.forName(logclassname);
Class[] argtypes = new Class[1];
argtypes[0] = "".getClass();
Constructor ctor = logclass.getConstructor(argtypes);
Object[] args = new Object[1];
args[0] = name;
log = (Log)(ctor.newInstance(args));
} catch(Exception e) {
log = null;
}
if(null == log) {
if(_log4jIsAvailable) {
return new Log4JCategoryLog(name);
} else {
log = new NoOpLog(name);
}
}
return log;
}
}
1.1
jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/log/NoOpLog.java
Index: NoOpLog.java
===================================================================
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE file.
*/
package org.apache.commons.httpclient.log;
/**
* @author Rod Waldhoff
* @version $Id: NoOpLog.java,v 1.1 2001/08/02 16:27:06 rwaldhoff Exp $
*/
public final class NoOpLog implements Log {
public NoOpLog() { }
public NoOpLog(String name) { }
public void assert(boolean assertion, String msg) { }
public void debug(Object message) { }
public void debug(Object message, Throwable t) { }
public void info(Object message) { }
public void info(Object message, Throwable t) { }
public void warn(Object message) { }
public void warn(Object message, Throwable t) { }
public void error(Object message) { }
public void error(Object message, Throwable t) { }
public void fatal(Object message) { }
public void fatal(Object message, Throwable t) { }
}
1.1
jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/log/SimpleLog.java
Index: SimpleLog.java
===================================================================
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE file.
*/
package org.apache.commons.httpclient.log;
import java.util.Properties;
import java.util.Enumeration;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.text.DateFormat;
import java.util.Date;
/**
* @author Rod Waldhoff
* @version $Id: SimpleLog.java,v 1.1 2001/08/02 16:27:06 rwaldhoff Exp $
*/
public class SimpleLog implements Log {
static protected final Properties _simplelogProps = new Properties();
static protected boolean _showlogname = false;
static protected boolean _showtime = false;
static protected DateFormat _df = null;
static {
// add all system props that start with "httpclient."
Enumeration enum = System.getProperties().propertyNames();
while(enum.hasMoreElements()) {
String name = (String)(enum.nextElement());
if(null != name && name.startsWith("httpclient.")) {
_simplelogProps.setProperty(name,System.getProperty(name));
}
}
// add props from the resource simplelog.properties
InputStream in =
ClassLoader.getSystemResourceAsStream("simplelog.properties");
if(null != in) {
try {
_simplelogProps.load(in);
in.close();
} catch(java.io.IOException e) {
// ignored
}
}
try {
} catch(Throwable t) {
// ignored
}
_showlogname =
"true".equalsIgnoreCase(_simplelogProps.getProperty("httpclient.simplelog.showlogname","true"));
_showtime =
"true".equalsIgnoreCase(_simplelogProps.getProperty("httpclient.simplelog.showdate","true"));
if(_showtime) {
_df = new
SimpleDateFormat(_simplelogProps.getProperty("httpclient.simplelog.dateformat","yyyy/MM/dd
HH:mm:ss:SSS zzz"));
}
}
protected static final int DEBUG = 5;
protected static final int INFO = 4;
protected static final int WARN = 3;
protected static final int ERROR = 2;
protected static final int FATAL = 1;
protected int _logLevel = 2;
protected String _name = null;
public SimpleLog(String name) {
_name = name;
String lvl = _simplelogProps.getProperty("httpclient.simplelog.log." +
_name);
int i = String.valueOf(name).lastIndexOf(".");
while(null == lvl && i > -1) {
name = name.substring(0,i);
lvl = _simplelogProps.getProperty("httpclient.simplelog.log." + name);
i = String.valueOf(name).lastIndexOf(".");
}
if(null == lvl) {
lvl = _simplelogProps.getProperty("httpclient.simplelog.defaultlog");
}
if("debug".equalsIgnoreCase(lvl)) {
_logLevel = DEBUG;
} else if("info".equalsIgnoreCase(lvl)) {
_logLevel = INFO;
} else if("warn".equalsIgnoreCase(lvl)) {
_logLevel = WARN;
} else if("error".equalsIgnoreCase(lvl)) {
_logLevel = ERROR;
} else if("fatal".equalsIgnoreCase(lvl)) {
_logLevel = FATAL;
}
}
protected void log(int type, Object message, Throwable t) {
if(_logLevel >= type) {
StringBuffer buf = new StringBuffer();
if(_showtime) {
buf.append(_df.format(new Date()));
buf.append(" ");
}
switch(type) {
case DEBUG: buf.append("[DEBUG] "); break;
case INFO: buf.append("[INFO] "); break;
case WARN: buf.append("[WARN] "); break;
case ERROR: buf.append("[ERROR] "); break;
case FATAL: buf.append("[FATAL] "); break;
}
if(_showlogname) {
buf.append(String.valueOf(_name)).append(" - ");
}
buf.append(String.valueOf(message));
if(t != null) {
buf.append(" <");
buf.append(t.toString());
buf.append(">");
t.printStackTrace();
}
System.out.println(buf.toString());
}
}
public final void assert(boolean assertion, String msg) {
if(!assertion) { error(msg); }
}
public final void debug(Object message) {
log(DEBUG,message,null);
}
public final void debug(Object message, Throwable t) {
log(DEBUG,message,t);
}
public final void info(Object message) {
log(INFO,message,null);
}
public final void info(Object message, Throwable t) {
log(INFO,message,t);
}
public final void warn(Object message) {
log(WARN,message,null);
}
public final void warn(Object message, Throwable t) {
log(WARN,message,t);
}
public final void error(Object message) {
log(ERROR,message,null);
}
public final void error(Object message, Throwable t) {
log(ERROR,message,t);
}
public final void fatal(Object message) {
log(FATAL,message,null);
}
public final void fatal(Object message, Throwable t) {
log(FATAL,message,t);
}
}
1.1
jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestBase64.java
Index: TestBase64.java
===================================================================
/*
* $Header:
/home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestBase64.java,v
1.1 2001/08/02 16:27:06 rwaldhoff Exp $
* $Revision: 1.1 $
* $Date: 2001/08/02 16:27:06 $
* ====================================================================
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE file.
*/
package org.apache.commons.httpclient;
import junit.framework.*;
import java.util.Random;
/**
* Simple tests of Base64.
*
* @author Rodney Waldhoff
* @version $Id: TestBase64.java,v 1.1 2001/08/02 16:27:06 rwaldhoff Exp $
*/
public class TestBase64 extends TestCase {
// ------------------------------------------------------------ Constructor
public TestBase64(String testName) {
super(testName);
}
// ------------------------------------------------------------------- Main
public static void main(String args[]) {
String[] testCaseName = { TestBase64.class.getName() };
junit.textui.TestRunner.main(testCaseName);
}
// ------------------------------------------------------- TestCase Methods
public static Test suite() {
return new TestSuite(TestBase64.class);
}
public void setUp() throws Exception {
}
private Random _rand = new Random();
// encode/decode random arrays from size 1 to size 11
public void testEncodeDecodeSmall() {
for(int i=1;i<12;i++) {
byte[] data = new byte[i];
_rand.nextBytes(data);
byte[] enc = Base64.encode(data);
assert(Base64.isBase64(new String(enc)));
byte[] data2 = Base64.decode(enc);
assert(toString(data) + ";" + toString(data2),isEqual(data,data2));
}
}
// encode/decode a large random array
public void testEncodeDecodeRandom() {
for(int i=1;i<5;i++) {
byte[] data = new byte[_rand.nextInt(10000)+1];
_rand.nextBytes(data);
byte[] enc = Base64.encode(data);
assert(Base64.isBase64(new String(enc)));
byte[] data2 = Base64.decode(enc);
assert(isEqual(data,data2));
}
}
public void testBrownFox() {
String str = "The quick brown fox jumped over the lazy dogs.";
byte[] strbytes = str.getBytes();
String enc =
"VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wZWQgb3ZlciB0aGUgbGF6eSBkb2dzLg==";
byte[] encbytes = enc.getBytes();
assert(!Base64.isBase64(str));
assert(Base64.isBase64(enc));
byte[] encstrbytes = Base64.encode(strbytes);
assertEquals(enc,new String(encstrbytes));
assert(isEqual(strbytes,Base64.decode(encstrbytes)));
assertEquals(enc,new String(encstrbytes));
}
// --------------------------------------------------------- Privae Methods
private boolean isEqual(byte[] a, byte[] b) {
if(a.length < 1) { return false; }
if(a.length != b.length) { return false; }
for(int i=0;i<a.length;i++) {
if(a[i] != b[i]) { return false; }
}
return true;
}
private String toString(byte[] data) {
StringBuffer buf = new StringBuffer();
for(int i=0;i<data.length;i++) {
buf.append(data[i]);
if(i != data.length-1) {
buf.append(",");
}
}
return buf.toString();
}
}