001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019package lucee.commons.io.log;
020
021import java.util.Iterator;
022import java.util.Map;
023import java.util.Map.Entry;
024
025import lucee.commons.digest.HashUtil;
026import lucee.commons.io.log.log4j.Log4jUtil;
027import lucee.commons.io.log.log4j.LogAdapter;
028import lucee.runtime.config.Config;
029
030import org.apache.log4j.Appender;
031import org.apache.log4j.Layout;
032import org.apache.log4j.Level;
033import org.apache.log4j.Logger;
034
035/**
036 * 
037 */
038public final class LoggerAndSourceData {
039    
040    private LogAdapter _log;
041    private final String strAppender;
042    private Appender _appender;
043        private final Map<String, String> appenderArgs;
044        private final String strLayout;
045        private Layout layout;
046        private final Map<String, String> layoutArgs;
047        private final Level level;
048        private final String name;
049        private final Config config;
050        private final boolean readOnly;
051        private final String id;
052
053 
054    public LoggerAndSourceData(Config config,String id,String name,String appender, Map<String, String> appenderArgs, String layout, Map<String, String> layoutArgs, Level level, boolean readOnly) {
055        //this.log=new LogAdapter(logger);
056        this.config=config;
057        this.id=id;
058        this.name=name;
059        this.strAppender=appender;
060        this.appenderArgs=appenderArgs;
061        this.strLayout=layout;
062        this.layoutArgs=layoutArgs;
063        this.level=level;
064        this.readOnly=readOnly;
065    }
066
067        public String id() {
068                return id;
069        }
070        public String getName() {
071                return name;
072        }
073        
074        public String getAppenderName() {
075                return strAppender;
076        }
077        
078        public Appender getAppender() {
079                getLog();// initilaize if necessary
080                return _appender;
081        }
082        
083        public void close() {
084                if(_log!=null) {
085                        Appender a = _appender;
086                _log=null;
087                        layout = null;
088                if(a!=null)a.close();
089                _appender=null;
090        }
091        }
092
093
094        public Map<String, String> getAppenderArgs() {
095                getLog();// initilaize if necessary
096                return appenderArgs;
097        }
098
099        public Layout getLayout() {
100                getLog();// initilaize if necessary
101                return layout;
102        }
103        public String getLayoutName() {
104                return strLayout;
105        }
106
107        public Map<String, String> getLayoutArgs() {
108                getLog();// initilaize if necessary
109                return layoutArgs;
110        }
111
112        public Level getLevel() {
113                return level;
114        }
115
116        public boolean getReadOnly() {
117                return readOnly;
118        }
119
120    public Log getLog() {
121        if(_log==null) {
122                layout = Log4jUtil.getLayout(strLayout, layoutArgs);
123                _appender = Log4jUtil.getAppender(config, layout,name, strAppender, appenderArgs);
124                _log=new LogAdapter(Log4jUtil.getLogger(config, _appender, name, level));
125        }
126        return _log;
127    }
128    
129    public Logger getLogger() {
130        getLog();// make sure it exists
131        return _log.getLogger();
132    }
133
134        public static String id(String name
135                        ,String strAppender, Map<String, String> appenderArgs
136                        ,String strLayout, Map<String, String> layoutArgs
137                        ,Level level,
138                        boolean readOnly) {
139                StringBuilder sb = new StringBuilder(name)
140                .append(';')
141                .append(strAppender)
142                .append(';');
143                toString(sb,appenderArgs);
144                sb.append(';')
145                .append(strLayout)
146                .append(';');
147                toString(sb,layoutArgs);
148                sb.append(';')
149                .append(level.toInt())
150                .append(';')
151                .append(readOnly);
152                
153                return HashUtil.create64BitHashAsString( sb.toString(),Character.MAX_RADIX);
154        }
155
156        private static void toString(StringBuilder sb, Map<String, String> map) {
157                if(map==null) return;
158                Iterator<Entry<String, String>> it = map.entrySet().iterator();
159                Entry<String, String> e;
160                while(it.hasNext()){
161                        e = it.next();
162                        sb.append(e.getKey()).append(':').append(e.getValue()).append('|');
163                }
164        }
165
166    
167}