1 package com.protomatter.syslog.commons; 2 3 52 53 import java.io.File ; 54 import java.util.Map ; 55 import java.util.HashMap ; 56 import java.text.MessageFormat ; 57 import org.apache.commons.logging.*; 58 import com.protomatter.syslog.*; 59 import com.protomatter.syslog.xml.*; 60 import com.protomatter.util.Debug; 61 62 65 public class SyslogChannelLogFactory 66 extends LogFactory 67 { 68 private static Object lock = new Object (); 69 private static boolean configured = false; 70 71 private Map attributes = new HashMap (); 72 73 private static String CONFIG_FILE_ATTRIBUTE = "Syslog.config.xml"; 74 75 private static String CLASSLOADER_WARNING_PROPERTY = "Syslog.classloader.warning"; 79 private static String WARNING_OFF = "off"; 80 81 private static boolean WARNING_DEFAULT = true; 83 84 91 public Log getInstance(Class clazz) 92 throws LogConfigurationException 93 { 94 String name = clazz.getName(); 95 int index = name.lastIndexOf("."); 96 if (index > 0) 97 return getInstance(name.substring(0, index)); 98 return getInstance(name); 99 } 100 101 106 public Log getInstance(String name) 107 throws LogConfigurationException 108 { 109 if (!configured) 110 { 111 synchronized (lock) 112 { 113 if (!configured) 114 { 115 try 116 { 117 String configFileName = (String )attributes.get(CONFIG_FILE_ATTRIBUTE); 118 String xmlParserClass = (String )attributes.get(SyslogXML.XML_PARSER_PROPERTY); 119 boolean showClassloaderWarning = WARNING_DEFAULT; 120 if (attributes.get(CLASSLOADER_WARNING_PROPERTY) != null) 121 { 122 showClassloaderWarning = !(WARNING_OFF.equalsIgnoreCase((String )attributes.get(CLASSLOADER_WARNING_PROPERTY))); 123 } 124 125 if (configFileName == null) 126 { 127 throw new LogConfigurationException( 128 MessageFormat.format(Syslog.getResourceString(MessageConstants.MUST_SPECIFY_INIT_PARAM_MESSAGE), 129 new Object [] { CONFIG_FILE_ATTRIBUTE } )); 130 } 131 132 SyslogXML.configure(new File (configFileName), xmlParserClass, showClassloaderWarning); 133 } 134 catch (SyslogInitException x) 135 { 136 throw new LogConfigurationException(Syslog.getResourceString(MessageConstants.CANNOT_CONFIGURE_MESSAGE), x); 137 } 138 } 139 } 140 } 141 142 return new SyslogChannelLog(name); 143 } 144 145 public void release() 146 { 147 } 148 149 public Object getAttribute(String name) 150 { 151 return attributes.get(name); 152 } 153 154 public String [] getAttributeNames() 155 { 156 return (String [])attributes.keySet().toArray(new String [0]); 157 } 158 159 public void removeAttribute(String name) 160 { 161 attributes.remove(name); 162 } 163 164 public void setAttribute(String name, Object value) 165 { 166 if (value == null) 167 removeAttribute(name); 168 else 169 attributes.put(name, value); 170 } 171 } 172 | Popular Tags |