1 46 47 48 package org.mr.core.log; 49 50 51 import java.io.File ; 52 import java.util.Collection ; 53 import java.util.HashSet ; 54 55 import javax.xml.parsers.DocumentBuilder ; 56 import javax.xml.parsers.DocumentBuilderFactory ; 57 58 import org.apache.log4j.xml.DOMConfigurator; 59 import org.mr.MantaAgent; 60 import org.w3c.dom.Document ; 61 import org.w3c.dom.Element ; 62 import org.w3c.dom.NodeList ; 63 64 65 70 public class LoggerLoader { 71 72 private static boolean loaded = false; 73 74 private static Collection listeners = new HashSet (1); 75 76 public static void init(String mantaName) { 77 Element configurationElement = MantaAgent.getInstance().getSingletonRepository().getConfigManager().getConfigurationDOMElement(); 81 if (configurationElement != null) 82 parseAndLoadLogger(configurationElement, mantaName); 83 else { 84 String propertyFile = MantaAgent.getInstance().getSingletonRepository().getConfigManager().getConfigFileName(); 85 File f = new File (propertyFile); 86 Element rootElement; 87 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 88 factory.setIgnoringElementContentWhitespace(true); 89 90 try { 91 DocumentBuilder xmlBuilder= factory.newDocumentBuilder(); 92 Document xmlDoc = xmlBuilder.parse(f); 93 94 rootElement = xmlDoc.getDocumentElement(); 95 parseAndLoadLogger(rootElement, mantaName); 96 } 97 catch(Exception e){ 98 StartupLogger.log.error("Problem loading logger", "LoggerLoader"); 99 e.printStackTrace(); 100 } 101 } 102 } 103 104 private static void parseAndLoadLogger(Element configurationElement, String mantaName){ 105 NodeList logConfig = configurationElement.getElementsByTagName("log4j:configuration"); 107 Element logConfigElement = (Element ) logConfig.item(0); 108 109 if (logConfigElement != null){ 110 MantaLogAppender.setMantaName(mantaName); 111 init(logConfigElement); 112 } 113 } 114 115 public static void init(Element config){ 116 DOMConfigurator.configure(config); 118 loaded = true; 119 } 120 121 122 123 124 127 static void ensureConfigFile( String configFile){ 128 129 File logConfigFile = new File (configFile); 130 if( !logConfigFile.exists() ){ 131 StartupLogger.log.error("Did not find configuration file for logger at "+configFile, "LoggerLoader"); 133 } 134 } 135 136 141 static void ensureDirectory(String dirName){ 142 143 File dir = new File (dirName); 144 if(!dir.exists()) { 145 146 boolean createDir = dir.mkdirs(); 147 if( !createDir ){ 148 StartupLogger.log.error(dirName+" directory does not exist and cannot be created !!", "LoggerLoader"); 152 StartupLogger.log.error("Logging Module cannot work properly.", "LoggerLoader"); 153 return; 154 } 155 StartupLogger.log.info(dirName+" directory was created", "LoggerLoader"); 157 } 158 } 159 160 161 164 public static boolean isLoaded() { 165 return loaded; 166 } 167 168 171 public static void setLoaded(boolean loaded) { 172 LoggerLoader.loaded = loaded; 173 } 174 175 176 177 } 178 | Popular Tags |