1 29 30 package com.caucho.quercus.module; 31 32 import com.caucho.config.ConfigException; 33 import com.caucho.quercus.env.DoubleValue; 34 import com.caucho.quercus.env.LongValue; 35 import com.caucho.quercus.env.NullValue; 36 import com.caucho.quercus.env.StringValue; 37 import com.caucho.quercus.env.StringValueImpl; 38 import com.caucho.quercus.env.Value; 39 import com.caucho.util.L10N; 40 41 import java.lang.reflect.Field ; 42 import java.lang.reflect.Method ; 43 import java.lang.reflect.Modifier ; 44 import java.util.HashMap ; 45 import java.util.HashSet ; 46 import java.util.Map ; 47 import java.util.logging.Level ; 48 import java.util.logging.Logger ; 49 50 53 public class ModuleInfo { 54 private static L10N L = new L10N(ModuleInfo.class); 55 private static final Logger log 56 = Logger.getLogger(ModuleInfo.class.getName()); 57 58 private ModuleContext _context; 59 60 private String _name; 61 private QuercusModule _module; 62 63 private HashSet <String > _extensionSet 64 = new HashSet <String >(); 65 66 private HashMap <String , Value> _constMap 67 = new HashMap <String , Value>(); 68 69 private HashMap <String , StaticFunction> _staticFunctions 70 = new HashMap <String , StaticFunction>(); 71 72 private HashMap <String , StringValue> _iniMap 73 = new HashMap <String , StringValue>(); 74 75 78 public ModuleInfo(ModuleContext context, String name, QuercusModule module) 79 throws ConfigException 80 { 81 _context = context; 82 83 _name = name; 84 _module = module; 85 86 try { 87 introspectPhpModuleClass(module.getClass()); 88 } catch (Exception e) { 89 throw new ConfigException(e); 90 } 91 } 92 93 public String getName() 94 { 95 return _name; 96 } 97 98 public QuercusModule getModule() 99 { 100 return _module; 101 } 102 103 106 public HashSet <String > getLoadedExtensions() 107 { 108 return _extensionSet; 109 } 110 111 public HashMap <String , Value> getConstMap() 112 { 113 return _constMap; 114 } 115 116 119 public Value getConstant(String name) 120 { 121 return _constMap.get(name); 122 } 123 124 127 public HashMap <String ,StaticFunction> getFunctions() 128 { 129 return _staticFunctions; 130 } 131 132 public HashMap <String , StringValue> getDefaultIni() 133 { 134 return _iniMap; 135 } 136 137 142 private void introspectPhpModuleClass(Class cl) 143 throws IllegalAccessException , InstantiationException 144 { 145 for (String ext : _module.getLoadedExtensions()) { 146 _extensionSet.add(ext); 147 } 148 149 Map <String , Value> map = _module.getConstMap(); 150 151 if (map != null) 152 _constMap.putAll(map); 153 154 for (Field field : cl.getFields()) { 155 if (! Modifier.isPublic(field.getModifiers())) 156 continue; 157 158 if (! Modifier.isStatic(field.getModifiers())) 159 continue; 160 161 if (! Modifier.isFinal(field.getModifiers())) 162 continue; 163 164 Object obj = field.get(null); 165 166 Value value = objectToValue(obj); 167 168 if (value != null) 169 _constMap.put(field.getName(), value); 170 } 171 172 Map <String , StringValue> iniMap = _module.getDefaultIni(); 173 174 if (map != null) 175 _iniMap.putAll(iniMap); 176 177 for (Method method : cl.getMethods()) { 178 if (method.getDeclaringClass().equals(Object .class)) 179 continue; 180 181 if (method.getDeclaringClass().isAssignableFrom(AbstractQuercusModule.class)) 182 continue; 183 184 if (! Modifier.isPublic(method.getModifiers())) 185 continue; 186 187 194 195 if (hasCheckedException(method)) { 196 log.warning(L.l("Module method '{0}.{1}' may not throw checked exceptions", 197 method.getDeclaringClass().getName(), 198 method.getName())); 199 continue; 200 } 201 202 Class []params = method.getParameterTypes(); 203 204 try { 205 StaticFunction function 206 = _context.createStaticFunction(_module, method); 207 208 String methodName = method.getName(); 209 210 if (methodName.startsWith("quercus_")) 211 methodName = methodName.substring(8); 212 213 _staticFunctions.put(methodName, function); 214 } catch (Exception e) { 215 log.log(Level.FINE, e.toString(), e); 216 } 217 } 218 } 219 220 private static boolean hasCheckedException(Method method) 221 { 222 for (Class exnCl : method.getExceptionTypes()) { 223 if (! RuntimeException .class.isAssignableFrom(exnCl)) 224 return true; 225 } 226 227 return false; 228 } 229 230 public static Value objectToValue(Object obj) 231 { 232 if (obj == null) 233 return NullValue.NULL; 234 else if (Byte .class.equals(obj.getClass()) || 235 Short .class.equals(obj.getClass()) || 236 Integer .class.equals(obj.getClass()) || 237 Long .class.equals(obj.getClass())) { 238 return LongValue.create(((Number ) obj).longValue()); 239 } else if (Float .class.equals(obj.getClass()) || 240 Double .class.equals(obj.getClass())) { 241 return DoubleValue.create(((Number ) obj).doubleValue()); 242 } else if (String .class.equals(obj.getClass())) { 243 return new StringValueImpl((String ) obj); 244 } else { 245 247 return null; 248 } 249 } 250 } 251 252 | Popular Tags |