package org.saf.settings;
import java.io.File;
import java.util.Properties;
import org.apache.log4j.PropertyConfigurator;
import org.apache.log4j.xml.DOMConfigurator;
import org.ini4j.Ini;
import org.ini4j.Ini.Section;
import com.logica.smpp.SmppObject;
import com.logica.smpp.debug.Debug;
import com.logica.smpp.debug.FileDebug;
public class LogSettings extends AbstractSettings {
static String SECTION = "log";
private final static String KEY_LOG_LEVEL = "log_level";
private final static String KEY_LOG_FILE_NAME = "log_file_name";
private final static String KEY_LOG4J_XML = "log4j_xml";
private final static String KEY_CONSOLE_DEBUG = "console_debug";
private final static String KEY_LOG_SMPP_ENABLED = "enable_log_smpp";
private final static String DEFAULT_LOG_LEVEL = "DEBUG";
private final static String DEFAULT_LOG_FILE_NAME =
"log" + File.separator + "jsmppgateway.log";
private final static String DEFAULT_LOG_SMPP_DIR =
File.separator + "log" + File.separator + "smpp";
private final static String DEFAULT_LOG_SMPP_FILE =
File.separator + "smpp.log";
private final static String DEFAULT_LOG_SMPP_ENABLED = "N";
String logLevel = null;
String logFileName = null;
String log4jXml = null;
boolean consoleDebug = false;
boolean logSMPPEnabled = false;
private boolean configuredSMPPlog = false;
public LogSettings() {
super();
init();
}
private void init() {
this.logLevel = DEFAULT_LOG_LEVEL;
this.logFileName = DEFAULT_LOG_FILE_NAME;
this.log4jXml = "";
this.consoleDebug = false;
this.logSMPPEnabled = false;
}
@Override
void createDefaults(Ini ini) {
configureDefaultLog4J();
Section logSection = ini.add(SECTION);
logSection.put(KEY_LOG_LEVEL, DEFAULT_LOG_LEVEL);
logSection.put(KEY_LOG_FILE_NAME, DEFAULT_LOG_FILE_NAME);
logSection.put(KEY_LOG_SMPP_ENABLED, DEFAULT_LOG_SMPP_ENABLED);
}
@Override
void load(Section logSection) {
load(logSection, true);
}
@Override
void refresh(Section logSection) {
load(logSection, false);
}
private void load(Section logSection, boolean firtTime) {
boolean changed = false;
boolean smppchanged = false;
if(logSection != null) {
String tmp;
tmp = logSection.get(KEY_LOG_LEVEL);
if(tmp != null) {
if(tmp.compareTo( this.logLevel) != 0) {
changed = true;
}
this.logLevel = tmp;
}
tmp = logSection.get(KEY_LOG_FILE_NAME);
if(tmp != null) {
if(tmp.compareTo( this.logFileName) != 0) {
changed = true;
}
this.logFileName = tmp;
}
tmp = logSection.get(KEY_CONSOLE_DEBUG);
if(tmp != null) {
boolean tmpConsoleDebug = false;
if(tmp.compareTo("Y") == 0) {
tmpConsoleDebug = true;
}
if(tmpConsoleDebug != this.consoleDebug) {
changed = true;
}
this.consoleDebug = tmpConsoleDebug;
}
tmp = logSection.get(KEY_LOG4J_XML);
if(tmp != null) {
if(tmp.compareTo( this.log4jXml) != 0){
changed = true;
}
this.log4jXml = tmp;
}
//SMMP log parameters.
tmp = logSection.get(KEY_LOG_SMPP_ENABLED);
if(tmp != null) {
boolean tmpSMPPEnabled = false;
if(tmp.compareTo("Y") == 0) {
tmpSMPPEnabled = true;
}
if(tmpSMPPEnabled != this.logSMPPEnabled) {
smppchanged = true;
}
this.logSMPPEnabled = tmpSMPPEnabled;
}
tmp = null;
}
if(firtTime == true) {
configureLog4J(this.logLevel, this.logFileName,
this.log4jXml, this.consoleDebug);
if(this.logSMPPEnabled == true) {
activateLogSMPP(this.logSMPPEnabled);
}
} else {
if(changed == true) {
configureLog4J(this.logLevel, this.logFileName,
this.log4jXml, this.consoleDebug);
}
if(smppchanged == true) {
activateLogSMPP(this.logSMPPEnabled);
}
}
}
private void configureDefaultLog4J() {
Properties p = new Properties();
p.setProperty("log4j.rootLogger", "INFO, A2");
p.setProperty("log4j.appender.A2",
"org.apache.log4j.ConsoleAppender");
p.setProperty("log4j.appender.A2.layout",
"org.apache.log4j.PatternLayout");
p.setProperty("log4j.appender.A2.layout.ConversionPattern",
"%d{ISO8601} %t %-5p [%c{1}] %m%n");
PropertyConfigurator.configure(p);
}
private void configureLog4J(String logLevel, String logFile,
String log4jXml, boolean consoleDebug){
if(log4jXml != null && log4jXml.length() > 0) {
DOMConfigurator.configureAndWatch(log4jXml);
} else {
Properties p = new Properties();
p.setProperty("log4j.appender.A1",
"org.apache.log4j.DailyRollingFileAppender");
p.setProperty("log4j.appender.A1.File", logFile);
p.setProperty("log4j.appender.A1.DatePattern", "'.'yyyy-MM-dd");
p.setProperty("log4j.appender.A1.Append", "true");
p.setProperty("log4j.appender.A1.ImmediateFlush", "true");
p.setProperty("log4j.appender.A1.layout",
"org.apache.log4j.PatternLayout");
p.setProperty("log4j.appender.A1.layout.ConversionPattern",
"%d{ISO8601} %t %-5p [%c{1}] %m%n");
if(consoleDebug == false) {
p.setProperty("log4j.rootLogger", logLevel + ", A1");
} else {
p.setProperty("log4j.rootLogger", logLevel + ", A1, A2");
p.setProperty("log4j.appender.A2",
"org.apache.log4j.ConsoleAppender");
p.setProperty("log4j.appender.A2.layout",
"org.apache.log4j.PatternLayout");
p.setProperty("log4j.appender.A2.layout.ConversionPattern",
"%d{ISO8601} %t %-5p [%c{1}] %m%n");
}
PropertyConfigurator.configure(p);
}
}
private void configureLogSMPP() {
if (configuredSMPPlog == false) {
String dir = System.getProperty("user.dir") + DEFAULT_LOG_SMPP_DIR;
String file = DEFAULT_LOG_SMPP_FILE;
File f = new File(dir);
if(f.exists() == false) {
f.mkdirs();
}
FileDebug debug = new FileDebug(dir, file);
SmppObject.setDebug(debug);
configuredSMPPlog = true;
}
}
private void activateLogSMPP(boolean active) {
configureLogSMPP();
Debug debug = SmppObject.getDebug();
if(debug != null) {
if(active == true) {
debug.activate();
} else {
debug.deactivate();
}
}
}
}