1 29 30 package com.caucho.quercus.module; 31 32 import com.caucho.config.ConfigException; 33 import com.caucho.quercus.QuercusRuntimeException; 34 import com.caucho.quercus.env.*; 35 import com.caucho.quercus.expr.*; 36 import com.caucho.quercus.function.*; 37 import com.caucho.quercus.program.ClassDef; 38 import com.caucho.quercus.program.JavaClassDef; 39 import com.caucho.quercus.program.JavaImplClassDef; 40 import com.caucho.util.L10N; 41 42 import java.lang.reflect.Constructor ; 43 import java.lang.reflect.InvocationTargetException ; 44 import java.lang.reflect.Method ; 45 import java.util.HashMap ; 46 import java.util.HashSet ; 47 import java.util.logging.*; 48 49 52 public class ModuleContext 53 { 54 private static L10N L = new L10N(ModuleContext.class); 55 private static final Logger log 56 = Logger.getLogger(ModuleContext.class.getName()); 57 58 private ClassLoader _loader; 59 60 private HashMap <String , QuercusModule> _modules 61 = new HashMap <String , QuercusModule>(); 62 63 private HashMap <String , ModuleInfo> _moduleInfoMap 64 = new HashMap <String , ModuleInfo>(); 65 66 private HashSet <String > _extensionSet 67 = new HashSet <String >(); 68 69 private HashMap <String , Value> _constMap 70 = new HashMap <String , Value>(); 71 72 private HashMap <String , StaticFunction> _staticFunctions 73 = new HashMap <String , StaticFunction>(); 74 75 private HashMap <String , ClassDef> _staticClasses 76 = new HashMap <String , ClassDef>(); 77 78 private HashMap <String , ClassDef> _lowerStaticClasses 79 = new HashMap <String , ClassDef>(); 80 81 private HashMap <String , JavaClassDef> _javaClassWrappers 82 = new HashMap <String , JavaClassDef>(); 83 84 private HashMap <String , JavaClassDef> _lowerJavaClassWrappers 85 = new HashMap <String , JavaClassDef>(); 86 87 private HashMap <String , StringValue> _iniMap 88 = new HashMap <String , StringValue>(); 89 90 protected MarshalFactory _marshalFactory; 91 protected ExprFactory _exprFactory; 92 93 96 public ModuleContext() 97 { 98 this(Thread.currentThread().getContextClassLoader()); 99 } 100 101 104 public ModuleContext(ClassLoader loader) 105 { 106 _loader = loader; 107 108 _marshalFactory = new MarshalFactory(this); 109 _exprFactory = new ExprFactory(); 110 } 111 112 public static ModuleContext getLocalContext(ClassLoader loader) 113 { 114 throw new UnsupportedOperationException (); 115 125 } 126 127 130 141 142 145 163 164 167 178 179 182 public ModuleInfo addModule(String name, QuercusModule module) 183 throws ConfigException 184 { 185 synchronized (this) { 186 ModuleInfo info = _moduleInfoMap.get(name); 187 188 if (info == null) { 189 info = new ModuleInfo(this, name, module); 190 _moduleInfoMap.put(name, info); 191 } 192 193 return info; 194 } 195 } 196 197 public JavaClassDef addClass(String name, Class type, 198 String extension, Class javaClassDefClass) 199 throws NoSuchMethodException , 200 InvocationTargetException , 201 IllegalAccessException , 202 InstantiationException  203 { 204 JavaClassDef def = _javaClassWrappers.get(name); 205 206 if (def == null) { 207 if (log.isLoggable(Level.FINEST)) { 208 if (extension == null) 209 log.finest(L.l("PHP loading class {0} with type {1}", name, type.getName())); 210 else 211 log.finest(L.l("PHP loading class {0} with type {1} providing extension {2}", name, type.getName(), extension)); 212 } 213 214 if (javaClassDefClass != null) { 215 Constructor constructor 216 = javaClassDefClass.getConstructor(ModuleContext.class, 217 String .class, 218 Class .class); 219 220 def = (JavaClassDef) constructor.newInstance(this, name, type); 221 } 222 else 223 def = JavaClassDef.create(this, name, type); 224 225 _javaClassWrappers.put(name, def); 226 _lowerJavaClassWrappers.put(name.toLowerCase(), def); 227 228 _staticClasses.put(name, def); 229 _lowerStaticClasses.put(name.toLowerCase(), def); 230 231 233 if (extension != null) 234 _extensionSet.add(extension); 235 } 236 237 return def; 238 } 239 240 public JavaImplClassDef addClassImpl(String name, 241 Class type, 242 String extension) 243 throws IllegalAccessException , InstantiationException  244 { 245 JavaImplClassDef def = (JavaImplClassDef) _staticClasses.get(name); 246 247 if (def == null) 248 def = addJavaImplClass(name, type, extension); 249 250 return def; 251 } 252 253 256 public JavaClassDef getJavaClassDefinition(String className) 257 { 258 JavaClassDef def = _javaClassWrappers.get(className); 259 260 if (def != null) 261 return def; 262 263 try { 264 Class type; 265 266 try { 267 type = Class.forName(className, false, _loader); 268 } 269 catch (ClassNotFoundException e) { 270 throw new ClassNotFoundException (L.l("`{0}' not valid {1}", className, e.toString())); 271 272 } 273 274 def = JavaClassDef.create(this, className, type); 275 276 _javaClassWrappers.put(className, def); 277 278 280 return def; 281 } catch (RuntimeException e) { 282 throw e; 283 } catch (Exception e) { 284 throw new QuercusRuntimeException(e); 285 } 286 } 287 288 291 public ClassDef findJavaClassWrapper(String name) 292 { 293 ClassDef def = _javaClassWrappers.get(name); 294 295 if (def != null) 296 return def; 297 298 return _lowerJavaClassWrappers.get(name.toLowerCase()); 299 } 300 301 public MarshalFactory getMarshalFactory() 302 { 303 return _marshalFactory; 304 } 305 306 public ExprFactory getExprFactory() 307 { 308 return _exprFactory; 309 } 310 311 public Marshal createMarshal(Class type, 312 boolean isNotNull, 313 boolean isNullAsFalse) 314 { 315 return getMarshalFactory().create(type, isNotNull, isNullAsFalse); 316 } 317 318 321 public ArrayValue getDefinedFunctions() 322 { 323 ArrayValue internal = new ArrayValueImpl(); 324 325 for (String name : _staticFunctions.keySet()) { 326 internal.put(name); 327 } 328 329 return internal; 330 } 331 332 335 public ClassDef findClass(String name) 336 { 337 ClassDef def = _staticClasses.get(name); 338 339 if (def == null) 340 def = _lowerStaticClasses.get(name.toLowerCase()); 341 342 return def; 343 } 344 345 348 public HashMap <String , ClassDef> getClassMap() 349 { 350 return _staticClasses; 351 } 352 353 356 public QuercusModule findModule(String name) 357 { 358 return _modules.get(name); 359 } 360 361 364 public boolean isExtensionLoaded(String name) 365 { 366 return _extensionSet.contains(name); 367 } 368 369 372 public HashSet <String > getLoadedExtensions() 373 { 374 return _extensionSet; 375 } 376 377 public HashMap <String , Value> getConstMap() 378 { 379 return _constMap; 380 } 381 382 385 public StaticFunction createStaticFunction(QuercusModule module, 386 Method method) 387 { 388 return new StaticFunction(this, module, method); 389 } 390 391 394 public Value getConstant(String name) 395 { 396 return _constMap.get(name); 397 } 398 399 public static Value objectToValue(Object obj) 400 { 401 if (obj == null) 402 return NullValue.NULL; 403 else if (Byte .class.equals(obj.getClass()) || 404 Short .class.equals(obj.getClass()) || 405 Integer .class.equals(obj.getClass()) || 406 Long .class.equals(obj.getClass())) { 407 return LongValue.create(((Number ) obj).longValue()); 408 } else if (Float .class.equals(obj.getClass()) || 409 Double .class.equals(obj.getClass())) { 410 return DoubleValue.create(((Number ) obj).doubleValue()); 411 } else if (String .class.equals(obj.getClass())) { 412 return new StringValueImpl((String ) obj); 413 } else { 414 416 return null; 417 } 418 } 419 420 427 private JavaImplClassDef addJavaImplClass(String name, 428 Class type, 429 String extension) 430 throws IllegalAccessException , InstantiationException  431 { 432 if (log.isLoggable(Level.FINEST)) { 433 if (extension == null) 434 log.finest(L.l("Quercus loading class {0} with type {1}", name, type.getName())); 435 else 436 log.finest(L.l("Quercus loading class {0} with type {1} providing extension {2}", name, type.getName(), extension)); 437 } 438 439 JavaImplClassDef def = new JavaImplClassDef(this, name, type); 440 441 _staticClasses.put(name, def); 442 _lowerStaticClasses.put(name.toLowerCase(), def); 443 444 def.introspect(this); 445 446 if (extension != null) 447 _extensionSet.add(extension); 448 449 return def; 450 } 451 } 452 453 | Popular Tags |