1 package org.grlea.logBridge; 2 3 6 18 import org.grlea.logBridge.impl.NullLogBridgeFactory; 19 20 import java.io.IOException ; 21 import java.io.InputStream ; 22 import java.net.URL ; 23 import java.util.Properties ; 24 25 32 public class 33 LogBridgeManager 34 { 35 40 public static final String FACTORY_KEY = LogBridgeFactory.class.getName(); 41 42 47 public static final String PROPERTIES_FILE = "logBridge.properties"; 48 49 52 private static LogBridgeFactory globalFactory = null; 53 54 57 private static final Object factoryLock = new Object (); 58 59 62 private 63 LogBridgeManager() 64 {} 65 66 72 public static LogBridgeFactory 73 getFactory() 74 { 75 synchronized (factoryLock) 76 { 77 if (globalFactory == null) 78 createFactory(); 79 } 80 81 return globalFactory; 82 } 83 84 98 private static void 99 createFactory() 100 { 101 { 103 String className = System.getProperty(FACTORY_KEY); 104 if (className != null) 105 { 106 LogBridgeFactory factory = createFactory(className); 107 if (factory != null) 108 { 109 globalFactory = factory; 110 return; 111 } 112 } 113 } 114 115 URL propertiesFile = LogBridgeManager.class.getClassLoader().getResource(PROPERTIES_FILE); 117 if (propertiesFile != null) 118 { 119 try 120 { 121 InputStream in = propertiesFile.openStream(); 122 Properties properties = new Properties (); 123 properties.load(in); 124 String className = properties.getProperty(FACTORY_KEY); 125 if (className != null) 126 { 127 LogBridgeFactory factory = createFactory(className); 128 if (factory != null) 129 { 130 globalFactory = factory; 131 return; 132 } 133 } 134 } 135 catch (IOException e) 136 { 137 System.err.println("ERROR (LogBridgeManager): Error reading properties file '" + 138 propertiesFile + "': " + e); 139 } 140 } 141 142 globalFactory = new NullLogBridgeFactory(); 144 } 145 146 155 private static LogBridgeFactory 156 createFactory(String className) 157 { 158 try 159 { 160 ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); 161 if (classLoader == null) 162 classLoader = LogBridgeManager.class.getClassLoader(); 163 164 Class factoryClass = classLoader.loadClass(className); 165 Object newObject = factoryClass.newInstance(); 166 if (!(newObject instanceof LogBridgeFactory)) 167 { 168 System.err.println("ERROR (LogBridgeManager): Class '" + className + 169 "' is not an implementation of " + LogBridgeFactory.class.getName()); 170 return null; 171 } 172 return (LogBridgeFactory) newObject; 173 } 174 catch (ClassNotFoundException e) 175 { 176 System.err.println( 177 "ERROR (LogBridgeManager): Class '" + className + "' not found: " + e); 178 } 179 catch (InstantiationException e) 180 { 181 System.err.println( 182 "ERROR (LogBridgeManager): Error creating instance of " + className + ": " + e); 183 } 184 catch (IllegalAccessException e) 185 { 186 System.err.println( 187 "ERROR (LogBridgeManager): Error creating instance of " + className + ": " + e); 188 } 189 return null; 190 } 191 192 199 public static void 200 setFactory(LogBridgeFactory newFactory) 201 { 202 if (newFactory == null) 203 throw new IllegalArgumentException ("factory cannot be set to null."); 204 globalFactory = newFactory; 205 } 206 207 215 public static LogBridge 216 createLogBridge(Class sourceClass) 217 { 218 return getFactory().getLogBridge(sourceClass); 219 } 220 } 221 | Popular Tags |