001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.log;
016    
017    import java.util.Hashtable;
018    import java.util.Iterator;
019    import java.util.List;
020    import java.util.Map;
021    import java.util.Vector;
022    
023    /**
024     * @author Brian Wing Shun Chan
025     */
026    public class LogFactoryWrapper implements LogFactory {
027    
028            public LogFactoryWrapper(LogFactory logFactory) {
029                    _logFactory = logFactory;
030            }
031    
032            public void setLogFactory(LogFactory logFactory) {
033                    _logFactory = logFactory;
034    
035                    Iterator<Map.Entry<String, List<Log>>> itr1 =
036                            _logs.entrySet().iterator();
037    
038                    while (itr1.hasNext()) {
039                            Map.Entry<String, List<Log>> entry = itr1.next();
040    
041                            String name = entry.getKey();
042                            List<Log> list = entry.getValue();
043    
044                            Iterator<Log> itr2 = list.iterator();
045    
046                            while (itr2.hasNext()) {
047                                    LogWrapper logWrapper = (LogWrapper)itr2.next();
048    
049                                    logWrapper.setLog(_logFactory.getLog(name));
050                            }
051                    }
052            }
053    
054            public Log getLog(Class<?> c) {
055                    return getLog(c.getName());
056            }
057    
058            public Log getLog(String name) {
059                    List<Log> list = _logs.get(name);
060    
061                    if (list == null) {
062                            list = new Vector<Log>();
063    
064                            _logs.put(name, list);
065                    }
066    
067                    Log log = new LogWrapper(_logFactory.getLog(name));
068    
069                    list.add(log);
070    
071                    return log;
072            }
073    
074            private LogFactory _logFactory;
075            private Map<String, List<Log>> _logs = new Hashtable<String, List<Log>>();
076    
077    }