Author: bpoussin Date: 2008-08-21 16:25:08 +0000 (Thu, 21 Aug 2008) New Revision: 1056 Added: trunk/lutinutil/src/main/java/org/codelutin/log/UserLog.java Modified: trunk/lutinutil/src/main/java/org/codelutin/util/ApplicationConfig.java trunk/lutinutil/src/main/java/org/codelutin/util/ListenerSet.java Log: - Ajout d'un nouvelle classe de log UserLog (tout debut) pour permettre des logs graphiques ou non pour l'utilisateur - correction de bug dans ApplicationConfig (si donnee en cache, on retournait null a la la place de la valeur) Added: trunk/lutinutil/src/main/java/org/codelutin/log/UserLog.java =================================================================== --- trunk/lutinutil/src/main/java/org/codelutin/log/UserLog.java (rev 0) +++ trunk/lutinutil/src/main/java/org/codelutin/log/UserLog.java 2008-08-21 16:25:08 UTC (rev 1056) @@ -0,0 +1,153 @@ +/* *##% + * Copyright (C) 2002-2008 Code Lutin, Benjamin Poussin + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, + * USA. + *##%*/ + +package org.codelutin.log; + + +import java.awt.GraphicsEnvironment; +import javax.swing.JOptionPane; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.codelutin.util.ListenerSet; + +/** + * Classe permettant d'afficher facillement des messages a l'utilisateur. + * Si une interface graphique est disponible alors des boites de dialogue + * sont affichees. Il est aussi possible d'indique des composants a utiliser + * pour afficher les messages. N'importe quel composant ayant une methode + * setText convient + * + * @author poussin + * @version $Revision$ + * + * Last update: $Date$ + * by : $Author$ + */ +public class UserLog { + + /** to use log facility, just put in your code: log.info(\"...\"); */ + static private Log log = LogFactory.getLog(UserLog.class); + + static public enum Level { + DEBUG, INFO, WARN, ERROR, FATAL + } + + protected static ListenerSet<Object> listeners = new ListenerSet<Object>(); + protected static boolean graphicUI = true; + + public static void addListeners(ListenerSet<Object> l) { + listeners.add(l); + } + public static void removeListeners(ListenerSet<Object> l) { + listeners.remove(l); + } + + + public static void setGraphicUI(boolean graphicUI) { + UserLog.graphicUI = graphicUI; + } + + public static boolean isGraphicUI() { + return graphicUI && !GraphicsEnvironment.isHeadless(); + } + + /** + * Appel la methode setText de tous les listeners avec le message en + * parametre et si {@link #isGraphicUI} retourne vrai affiche une boite + * de dialogue avec le message + * + * @param level le niveau du message + * @param msg le message + * @param eee l'exception s'il y en a une + */ + protected static void show(Level level, String msg, Throwable eee) { + try { + listeners.fire("setText", msg); + } catch (Exception zzz) { + log.warn("Can't call setText on listener", zzz); + } + + if (isGraphicUI()) { + String title = level.name(); + int type = JOptionPane.PLAIN_MESSAGE; + switch (level) { + case DEBUG: + type = JOptionPane.PLAIN_MESSAGE; + break; + case INFO: + type = JOptionPane.INFORMATION_MESSAGE; + break; + case WARN: + type = JOptionPane.WARNING_MESSAGE; + break; + case ERROR: + case FATAL: + type = JOptionPane.ERROR_MESSAGE; + break; + } + JOptionPane.showMessageDialog(null, msg, title, type); + } + } + + public static void debug(String msg) { + log.debug(msg); + show(Level.DEBUG, msg, null); + } + public static void debug(String msg, Throwable eee) { + log.debug(msg, eee); + show(Level.DEBUG, msg, eee); + } + + public static void info(String msg) { + log.info(msg); + show(Level.INFO, msg, null); + } + public static void info(String msg, Throwable eee) { + log.info(msg, eee); + show(Level.INFO, msg, eee); + } + + public static void warn(String msg) { + log.warn(msg); + show(Level.WARN, msg, null); + } + public static void warn(String msg, Throwable eee) { + log.warn(msg, eee); + show(Level.WARN, msg, eee); + } + + public static void error(String msg) { + log.error(msg); + show(Level.ERROR, msg, null); + } + public static void error(String msg, Throwable eee) { + log.error(msg, eee); + show(Level.ERROR, msg, eee); + } + + public static void fatal(String msg) { + log.fatal(msg); + show(Level.FATAL, msg, null); + } + public static void fatal(String msg, Throwable eee) { + log.fatal(msg, eee); + show(Level.FATAL, msg, eee); + } + +} Modified: trunk/lutinutil/src/main/java/org/codelutin/util/ApplicationConfig.java =================================================================== --- trunk/lutinutil/src/main/java/org/codelutin/util/ApplicationConfig.java 2008-08-20 13:20:48 UTC (rev 1055) +++ trunk/lutinutil/src/main/java/org/codelutin/util/ApplicationConfig.java 2008-08-21 16:25:08 UTC (rev 1056) @@ -272,7 +272,7 @@ */ static final private String CLASS_METHOD_SEPARATOR = "#"; - static final public String CONFIG_FILE_NAME = "configFileName"; + static final public String CONFIG_FILE_NAME = "config.file"; protected boolean useOnlyAliases = false; protected Map<String, List<String>> aliases = new HashMap<String, List<String>>(); @@ -547,6 +547,8 @@ result = (T) ConvertUtils.convert(value, clazz); cacheItem = new CacheItem<T>(result, hash); cacheOption.put(cacheKey, cacheItem); + } else { + result = cacheItem.item; } return result; @@ -884,4 +886,21 @@ throw new ArgumentsParserException("Can't parse argument", eee); } } + + /** + * For debugging + */ + public void printConfig() { + System.out.println("-------------------Value-------------------------"); + System.out.println("defaults " + defaults); + System.out.println("classpath " + classpath); + System.out.println("etcfile " + etcfile); + System.out.println("homefile " + homefile); + System.out.println("curfile " + curfile); + System.out.println("env " + env); + System.out.println("jvm " + jvm); + System.out.println("line " + line); + System.out.println("options " + options); + System.out.println("-------------------------------------------------"); + } } Modified: trunk/lutinutil/src/main/java/org/codelutin/util/ListenerSet.java =================================================================== --- trunk/lutinutil/src/main/java/org/codelutin/util/ListenerSet.java 2008-08-20 13:20:48 UTC (rev 1055) +++ trunk/lutinutil/src/main/java/org/codelutin/util/ListenerSet.java 2008-08-21 16:25:08 UTC (rev 1056) @@ -119,7 +119,7 @@ */ public void addAll(ListenerSet<Listerner> ls) { // if (listenerClass == null || (ls.listenerClass != null && -// listenerClass.isAssignableFrom(ls.listenerClass))) { +// listenerClass.isAssignableFrom(ls.listenDOCUMENTME Description of the ExceptionerClass))) { listeners.addAll(ls.listeners); // } else { // throw new IllegalArgumentException("Listener object (" @@ -133,9 +133,9 @@ * Cette méthode echoue si la methode ou l'objet contenant la methode a * appeler n'est pas public * + * @param methodName le nom de la methode a appeler * @param event l'event a passer en parametre de la methode a appeler - * @param methodName DOCUMENTME Description of the Parameter - * @throws Exception DOCUMENTME Description of the Exception + * @throws Exception si un des listeners leve une exception lors de l'appel */ public void fire(String methodName, Object event) throws Exception { for (Iterator i = iterator(); i.hasNext();) { @@ -150,8 +150,8 @@ * Cette méthode echoue si la methode ou l'objet contenant la methode a * appeler n'est pas public * - * @param methodName DOCUMENTME Description of the Parameter - * @throws Exception DOCUMENTME Description of the Exception + * @param methodName le nom de la methode a appeler + * @throws Exception si un des listeners leve une exception lors de l'appel */ public void fire(String methodName) throws Exception { for (Iterator i = iterator(); i.hasNext();) {