1 29 30 package com.caucho.config; 31 32 import com.caucho.loader.EnvironmentLocal; 33 import com.caucho.util.L10N; 34 import com.caucho.util.Log; 35 import com.caucho.vfs.ReadStream; 36 import com.caucho.vfs.Vfs; 37 38 import java.io.IOException ; 39 import java.io.InputStream ; 40 import java.lang.reflect.Method ; 41 import java.lang.reflect.Modifier ; 42 import java.net.URL ; 43 import java.util.Enumeration ; 44 import java.util.HashMap ; 45 import java.util.logging.Level ; 46 import java.util.logging.Logger ; 47 48 51 public class ConfigLibrary { 52 private static final L10N L = new L10N(ConfigLibrary.class); 53 private static final Logger log = Log.open(ConfigLibrary.class); 54 55 private static EnvironmentLocal<ConfigLibrary> _localLibrary 56 = new EnvironmentLocal<ConfigLibrary>(); 57 58 private HashMap <String ,Method > _methodMap = new HashMap <String ,Method >(); 59 60 private ConfigLibrary() 61 { 62 configureLibrary(); 63 } 64 65 public static ConfigLibrary getLocal() 66 { 67 return getLocal(Thread.currentThread().getContextClassLoader()); 68 } 69 70 public static ConfigLibrary getLocal(ClassLoader loader) 71 { 72 loader = ClassLoader.getSystemClassLoader(); 73 Thread thread = Thread.currentThread(); 74 ClassLoader oldLoader = thread.getContextClassLoader(); 75 76 try { 77 thread.setContextClassLoader(loader); 78 79 ConfigLibrary lib = _localLibrary.getLevel(loader); 80 81 if (lib == null) { 82 lib = new ConfigLibrary(); 83 84 _localLibrary.set(lib, loader); 85 } 86 87 return lib; 88 } finally { 89 thread.setContextClassLoader(oldLoader); 90 } 91 } 92 93 96 public HashMap <String ,Method > getMethodMap() 97 { 98 return _methodMap; 99 } 100 101 104 private void configureLibrary() 105 { 106 Thread thread = Thread.currentThread(); 107 ClassLoader loader = thread.getContextClassLoader(); 108 109 try { 110 String library = "META-INF/services/com.caucho.config.ConfigLibrary"; 111 Enumeration <URL > urls = loader.getResources(library); 112 113 while (urls.hasMoreElements()) { 114 URL url = urls.nextElement(); 115 116 InputStream is = null; 117 ReadStream rs = null; 118 try { 119 is = url.openStream(); 120 rs = Vfs.openRead(is); 121 122 parseServicesModule(rs); 123 } catch (Throwable e) { 124 log.log(Level.WARNING, e.toString(), e); 125 } finally { 126 if (rs != null) 127 rs.close(); 128 if (is != null) 129 is.close(); 130 } 131 } 132 133 } catch (Exception e) { 134 log.log(Level.WARNING, e.toString(), e); 135 } 136 } 137 138 141 private void parseServicesModule(ReadStream in) 142 throws IOException , ClassNotFoundException , 143 IllegalAccessException , InstantiationException 144 { 145 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 146 String line; 147 148 while ((line = in.readLine()) != null) { 149 int p = line.indexOf('#'); 150 151 if (p >= 0) 152 line = line.substring(0, p); 153 154 line = line.trim(); 155 156 if (line.length() > 0) { 157 String className = line; 158 159 Class cl = Class.forName(className, false, loader); 160 161 introspectLibraryClass(cl); 162 } 163 } 164 } 165 166 171 private void introspectLibraryClass(Class cl) 172 throws IllegalAccessException , InstantiationException 173 { 174 log.fine("Config loading library " + cl.getName()); 175 176 for (Method method : cl.getMethods()) { 177 if (! Modifier.isPublic(method.getModifiers())) 178 continue; 179 180 if (! Modifier.isStatic(method.getModifiers())) 181 continue; 182 183 _methodMap.put(method.getName(), method); 184 } 185 } 186 } 187 | Popular Tags |