001    /**
002     * Copyright 2010-2012 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.common.util;
017    
018    import java.util.List;
019    
020    import org.apache.commons.lang3.StringUtils;
021    import org.kuali.common.util.obscure.DefaultObscurer;
022    import org.kuali.common.util.obscure.Obscurer;
023    import org.kuali.common.util.property.Constants;
024    import org.slf4j.Logger;
025    
026    public class LoggerUtils {
027    
028            private static final Obscurer DEFAULT_OBSCURER = new DefaultObscurer();
029    
030            public static int[] getPadding(List<String> columns, List<Object[]> argsList) {
031                    int[] padding = new int[columns.size()];
032                    for (int i = 0; i < padding.length; i++) {
033                            padding[i] = Math.max(padding[i], columns.get(i).length());
034                    }
035                    for (Object[] args : argsList) {
036                            Assert.isTrue(columns.size() == args.length, "Column count must equals args.length");
037                            for (int i = 0; i < args.length; i++) {
038                                    padding[i] = Math.max(padding[i], args[i].toString().length());
039                            }
040                    }
041                    return padding;
042            }
043    
044            public static String getHeader(List<String> columns, int[] padding) {
045                    StringBuilder sb = new StringBuilder();
046                    for (int i = 0; i < columns.size(); i++) {
047                            if (i != 0) {
048                                    sb.append("  ");
049                            }
050                            sb.append(StringUtils.leftPad(columns.get(i), padding[i]));
051                    }
052                    return sb.toString();
053            }
054    
055            public static String getMsg(int count) {
056                    StringBuilder sb = new StringBuilder();
057                    for (int i = 0; i < count; i++) {
058                            if (i != 0) {
059                                    sb.append("  ");
060                            }
061                            sb.append("{}");
062                    }
063                    return sb.toString();
064            }
065    
066            public static void updateArgsList(List<Object[]> argsList, int[] padding) {
067                    for (Object[] args : argsList) {
068                            for (int i = 0; i < args.length; i++) {
069                                    args[i] = StringUtils.leftPad(args[i].toString(), padding[i]);
070                            }
071                    }
072            }
073    
074            public static void logTable(List<String> columns, List<Object[]> argsList, LoggerLevel level, Logger logger) {
075                    int[] padding = getPadding(columns, argsList);
076                    logMsg(getHeader(columns, padding), logger, level);
077                    String msg = getMsg(padding.length);
078                    updateArgsList(argsList, padding);
079                    for (Object[] args : argsList) {
080                            logMsg(msg, args, logger, level);
081                    }
082            }
083    
084            public static void logLines(String s, Logger logger, LoggerLevel level) {
085                    if (s == null) {
086                            return;
087                    }
088                    String[] lines = StringUtils.split(s, "\n");
089                    for (String line : lines) {
090                            LoggerUtils.logMsg(line, logger, level);
091                    }
092            }
093    
094            public static final void logMsg(String msg, Object[] args, Logger logger, LoggerLevel level) {
095                    switch (level) {
096                    case DEBUG:
097                            logger.debug(msg, args);
098                            return;
099                    case TRACE:
100                            logger.trace(msg, args);
101                            return;
102                    case INFO:
103                            logger.info(msg, args);
104                            return;
105                    case WARN:
106                            logger.warn(msg, args);
107                            return;
108                    case ERROR:
109                            logger.error(msg, args);
110                            return;
111                    default:
112                            throw new IllegalArgumentException("Logger level " + level + " is unknown");
113                    }
114            }
115    
116            public static final void logMsg(String msg, Logger logger, LoggerLevel level) {
117                    logMsg(msg, null, logger, level);
118            }
119    
120            public static final String getUsername(String username) {
121                    return getNullAsNone(username);
122            }
123    
124            public static final String getNullAsNone(String string) {
125                    if (string == null) {
126                            return Constants.NONE;
127                    } else {
128                            return string;
129                    }
130            }
131    
132            public static final String getPassword(String username, String password) {
133                    return getPassword(username, password, DEFAULT_OBSCURER);
134            }
135    
136            public static boolean isNullOrNone(String s) {
137                    if (s == null) {
138                            return true;
139                    }
140                    if (StringUtils.equalsIgnoreCase(Constants.NONE, s)) {
141                            return true;
142                    }
143                    return StringUtils.equalsIgnoreCase(Constants.NULL, s);
144            }
145    
146            public static final String getPassword(String username, String password, Obscurer obscurer) {
147                    if (isNullOrNone(password)) {
148                            // There is no password, return NONE
149                            return Constants.NONE;
150                    } else if (StringUtils.equals(username, password)) {
151                            // Not exactly high security, display the clear text value
152                            return password;
153                    } else {
154                            // Otherwise obscure it
155                            return obscurer.obscure(password);
156                    }
157            }
158    
159    }