1 2 18 19 package org.apache.avalon.logging.log4j; 20 21 import java.io.File ; 22 23 import java.net.URL ; 24 25 import java.util.Map ; 26 27 import org.apache.avalon.logging.impl.DefaultLoggingCriteria; 28 import org.apache.avalon.logging.provider.LoggingCriteria; 29 import org.apache.avalon.logging.provider.LoggingFactory; 30 import org.apache.avalon.logging.provider.LoggingManager; 31 import org.apache.avalon.logging.provider.LoggingException; 32 33 import org.apache.avalon.repository.provider.InitialContext; 34 35 import org.apache.avalon.util.i18n.ResourceManager; 36 import org.apache.avalon.util.i18n.Resources; 37 38 import org.apache.log4j.PropertyConfigurator; 39 import org.apache.log4j.xml.DOMConfigurator; 40 41 47 public class Log4JLoggingFactory 48 implements LoggingFactory 49 { 50 private static final Resources REZ = 51 ResourceManager.getPackageResources( Log4JLoggingFactory.class ); 52 53 private final ClassLoader m_Classloader; 54 private final InitialContext m_Context; 55 private File m_BaseDirectory; 56 57 62 public Log4JLoggingFactory( InitialContext context, ClassLoader classloader ) 63 { 64 m_Context = context; 65 m_Classloader = classloader; 66 } 67 68 72 77 public LoggingCriteria createDefaultLoggingCriteria() 78 { 79 return new DefaultLoggingCriteria( m_Context ); 80 } 81 82 88 public LoggingManager createLoggingManager( LoggingCriteria criteria ) 89 throws LoggingException 90 { 91 try 92 { 93 return (LoggingManager) create( criteria ); 94 } 95 catch( Throwable e ) 96 { 97 final String error = 98 "Cannot build logging manager."; 99 throw new LoggingException( error, e ); 100 } 101 } 102 103 107 111 public Map createDefaultCriteria() 112 { 113 return createDefaultLoggingCriteria(); 114 } 115 116 120 public Object create() throws Exception 121 { 122 return new LoggingManagerImpl(); 123 } 124 125 130 public Object create( Map criteriaMap ) 131 throws Exception 132 { 133 if( null == criteriaMap ) 134 { 135 throw new NullPointerException ( "criteriaMap" ); 136 } 137 LoggingCriteria criteria = getLoggingCriteria( criteriaMap ); 138 m_BaseDirectory = criteria.getBaseDirectory(); 139 140 String cwd = System.getProperty( "user.dir" ); 141 try 142 { 143 System.setProperty( "user.dir", m_BaseDirectory.getAbsolutePath() ); 144 URL conf = criteria.getLoggingConfiguration(); 145 long updateInterval = criteria.getUpdateInterval(); 146 configure( conf, updateInterval ); 147 return new LoggingManagerImpl(); 148 } finally 149 { 150 System.setProperty( "user.dir", cwd ); 151 } 152 } 153 154 private void configure( URL url, long interval ) 155 { 156 String src = url.toExternalForm(); 157 if( src.startsWith( "file:" ) ) 158 { 159 src = src.substring( 5 ); 160 while( src.startsWith( "/" ) ) 161 src = src.substring( 1 ); 162 configureFile( src, interval ); 163 } 164 configureURL( url ); 165 } 166 167 private void configureFile( String src, long interval ) 168 { 169 if( interval > 0 ) 170 { 171 if( src.endsWith( ".xml" ) ) 172 { 173 DOMConfigurator.configureAndWatch( src, interval ); 174 } 175 else 176 { 177 PropertyConfigurator.configureAndWatch( src, interval ); 178 } 179 } 180 else 181 { 182 if( src.endsWith( ".xml" ) ) 183 { 184 DOMConfigurator.configureAndWatch( src ); 185 } 186 else 187 { 188 PropertyConfigurator.configureAndWatch( src ); 189 } 190 } 191 } 192 193 private void configureURL( URL url ) 194 { 195 String src = url.toExternalForm(); 196 197 if( src.endsWith( ".xml" ) ) 198 { 199 DOMConfigurator.configure( url ); 200 } 201 else 202 { 203 PropertyConfigurator.configure( url ); 204 } 205 } 206 207 private LoggingCriteria getLoggingCriteria( Map criteriaMap ) 208 { 209 if( criteriaMap instanceof LoggingCriteria ) 210 { 211 return (LoggingCriteria) criteriaMap; 212 } 213 else 214 { 215 final String error = 216 REZ.getString( 217 "factory.bad-criteria", 218 criteriaMap.getClass().getName() ); 219 throw new IllegalArgumentException ( error ); 220 } 221 } 222 } 223 | Popular Tags |