1 17 package org.apache.ws.jaxme.logging; 18 19 import java.io.BufferedReader ; 20 import java.io.InputStream ; 21 import java.io.InputStreamReader ; 22 import java.net.URL ; 23 24 25 32 public class LoggerAccess { 33 private static LoggerFactory theFactory; 34 35 37 public static synchronized void setLoggerFactory(LoggerFactory pFactory) { 38 theFactory = pFactory; 39 } 40 41 43 private static LoggerFactory newLoggerFactory(String pName) { 44 Class c = null; 45 Throwable t = null; 46 try { 47 c = Class.forName(pName); 48 } catch (Throwable th) { 49 t = th; 50 } 51 if (c == null) { 52 try { 53 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 54 if (cl == null) { 55 cl = LoggerAccess.class.getClassLoader(); 56 if (cl == null) { 57 cl = ClassLoader.getSystemClassLoader(); 58 } 59 } 60 c = cl.loadClass(pName); 61 } catch (Throwable th) { 62 if (t == null) { 63 t = th; 64 } 65 } 66 } 67 if (c == null) { 68 if (t == null) { 69 t = new ClassNotFoundException (pName); 70 } 71 t.printStackTrace(); 72 return null; 73 } 74 75 try { 76 return (LoggerFactory) c.newInstance(); 77 } catch (Throwable th) { 78 t.printStackTrace(); 79 return null; 80 } 81 } 82 83 95 public static LoggerFactory newLoggerFactory() { 96 String p = LoggerFactory.class.getName(); 97 String v = System.getProperty(p); 98 LoggerFactory result; 99 if (v != null) { 100 result = newLoggerFactory(v); 101 if (result != null) { 102 return result; 103 } 104 } 105 106 String res = "META-INF/services/" + p; 107 ClassLoader cl = LoggerAccess.class.getClassLoader(); 108 if (cl == null) { 109 cl = ClassLoader.getSystemClassLoader(); 110 } 111 URL url = cl.getResource(res); 112 if (url == null) { 113 cl = Thread.currentThread().getContextClassLoader(); 114 if (cl != null) { 115 url = cl.getResource(res); 116 } 117 } 118 if (url != null) { 119 InputStream istream = null; 120 try { 121 istream = url.openStream(); 122 BufferedReader reader = new BufferedReader (new InputStreamReader (istream)); 123 v = reader.readLine(); 124 if (v != null) { 125 result = newLoggerFactory(v); 126 } 127 istream.close(); 128 istream = null; 129 } catch (Throwable t) { 130 t.printStackTrace(); 131 } finally { 132 if (istream != null) { try { istream.close(); } catch (Throwable ignore) {} } 133 } 134 } 135 136 return new LoggerFactoryImpl() { 137 public Logger newLogger(String pName) { 138 return new LoggerImpl(pName); 139 } 140 }; 141 } 142 143 148 public static synchronized LoggerFactory getLoggerFactory() { 149 if (theFactory == null) { 150 setLoggerFactory(newLoggerFactory()); 151 } 152 return theFactory; 153 } 154 155 157 public static synchronized Logger getLogger(String pName) { 158 LoggerFactory factory = getLoggerFactory(); 159 return factory.getLogger(pName); 160 } 161 162 164 public static synchronized Logger getLogger(Class pClass) { 165 return getLogger(pClass.getName()); 166 } 167 } 168 | Popular Tags |