/* $This file is distributed under the terms of the license in /doc/license.txt$ */

package edu.cornell.mannlib.vitro.webapp.utils.log;

import java.lang.reflect.TypeVariable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;

/**
 * Some static methods that might help with logging for debug purposes.
 */
public class LogUtils {
	public static String deepFormatForLog(Log log, String level, Object o) {
		if (!isLevelEnabled(log, level)) {
			return "";
		}
		return new LogUtils(log).deepFormat(o);
	}

	private static boolean isLevelEnabled(Log log, String level) {
		if ("fatal".equalsIgnoreCase(level)) {
			return log.isFatalEnabled();
		} else if ("error".equalsIgnoreCase(level)) {
			return log.isErrorEnabled();
		} else if ("warn".equalsIgnoreCase(level)) {
			return log.isWarnEnabled();
		} else if ("info".equalsIgnoreCase(level)) {
			return log.isInfoEnabled();
		} else if ("debug".equalsIgnoreCase(level)) {
			return log.isDebugEnabled();
		} else {
			return log.isTraceEnabled();
		}
	}

	private final Log log;
	private final List<Object> dontFormatAgain = new ArrayList<Object>();

	private LogUtils(Log log) {
		this.log = log;
	}

	public String deepFormat(Object o) {
		if (o == null) {
			return "null";
		}
		if (o instanceof String) {
			return "\"" + o + "\"";
		}
		if (dontFormatAgain.contains(o)) {
			return "...";
		}

		dontFormatAgain.add(o);

		if (o instanceof Collection<?>) {
			return formatCollection((Collection<?>) o);
		}
		if (o instanceof Map<?, ?>) {
			return formatMap((Map<?, ?>) (o));
		}
		if (o.getClass().isArray()
				&& (!o.getClass().getComponentType().isPrimitive())) {
			return formatClass(o) + ": " + Arrays.deepToString((Object[]) o);
		}
		return formatClass(o) + ": " + String.valueOf(o);
	}

	private String formatClass(Object o) {
		if (o == null) {
			return "";
		}

		Class<?> clazz = o.getClass();
		TypeVariable<?>[] generics = clazz.getTypeParameters();

		if (generics.length == 0) {
			return clazz.getName();
		} else {
			return clazz.getName() + '<' + StringUtils.join(generics, ", ")
					+ '>';
		}
	}

	private String formatCollection(Collection<?> collection) {
		StringBuilder result = new StringBuilder(formatClass(collection));
		result.append(": ");

		result.append('{');
		for (Iterator<?> it = collection.iterator(); it.hasNext();) {
			result.append(deepFormat(it.next()));
			if (it.hasNext()) {
				result.append(", ");
			}
		}
		result.append('}');

		return result.toString();
	}

	private String formatMap(Map<?, ?> map) {
		StringBuilder result = new StringBuilder(formatClass(map));
		result.append(": ");

		result.append('{');
		for (Iterator<?> it = map.keySet().iterator(); it.hasNext();) {
			Object key = it.next();
			result.append(deepFormat(key));
			result.append('=');
			result.append(deepFormat(map.get(key)));
			if (it.hasNext()) {
				result.append(", ");
			}
		}
		result.append('}');

		return result.toString();
	}

}

@@ -323,16 +323,89 @@
 til.Map;
+%0Aimport java.util.TreeSet;%0A%0Aimport javax.servlet.http.HttpServletRequest;
 %0A%0Aimport
@@ -591,175 +591,1088 @@
  %7B%0A%09
-public static String deepFormatForLog(Log log, String level, Object o) %7B%0A%09%09if (!isLevelEnabled(log, level)) %7B%0A%09%09%09return %22%22;%0A%09%09%7D%0A%09%09return new LogUtils(log).deepFormat(o
+// ----------------------------------------------------------------------%0A%09// Public Static methods%0A%09// ----------------------------------------------------------------------%0A%0A%09public static String deepFormatForLog(Log log, String level, Object o) %7B%0A%09%09if (!isLevelEnabled(log, level)) %7B%0A%09%09%09return %22%22;%0A%09%09%7D%0A%09%09return new LogUtils(log).deepFormat(o);%0A%09%7D%0A%0A%09public static String formatRequestProperties(Log log, String level,%0A%09%09%09HttpServletRequest req) %7B%0A%09%09if (!isLevelEnabled(log, level)) %7B%0A%09%09%09return %22%22;%0A%09%09%7D%0A%09%09return new LogUtils(log).requestProperties(req);%0A%09%7D%0A%0A%09// ----------------------------------------------------------------------%0A%09// The instance%0A%09// ----------------------------------------------------------------------%0A%0A%09private String requestProperties(HttpServletRequest req) %7B%0A%09%09@SuppressWarnings(%22unchecked%22)%0A%09%09Map%3CString, String%5B%5D%3E map = req.getParameterMap();%0A%0A%09%09String s = req.getRequestURL().append('%5Cn').toString();%0A%09%09for (String name : new TreeSet%3CString%3E(map.keySet())) %7B%0A%09%09%09s += %22   %22 + name + %22 = %22 + Arrays.toString(map.get(name)) + '%5Cn';%0A%09%09%7D%0A%09%09return s.trim(
 );%0A%09

Add another LogUtil method