KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > quercus > module > ModuleContext


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

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 JavaDoc;
43 import java.lang.reflect.InvocationTargetException JavaDoc;
44 import java.lang.reflect.Method JavaDoc;
45 import java.util.HashMap JavaDoc;
46 import java.util.HashSet JavaDoc;
47 import java.util.logging.*;
48
49 /**
50  * Class-loader specific context for loaded PHP.
51  */

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 JavaDoc _loader;
59
60   private HashMap JavaDoc<String JavaDoc, QuercusModule> _modules
61     = new HashMap JavaDoc<String JavaDoc, QuercusModule>();
62
63   private HashMap JavaDoc<String JavaDoc, ModuleInfo> _moduleInfoMap
64     = new HashMap JavaDoc<String JavaDoc, ModuleInfo>();
65
66   private HashSet JavaDoc<String JavaDoc> _extensionSet
67     = new HashSet JavaDoc<String JavaDoc>();
68
69   private HashMap JavaDoc<String JavaDoc, Value> _constMap
70     = new HashMap JavaDoc<String JavaDoc, Value>();
71
72   private HashMap JavaDoc<String JavaDoc, StaticFunction> _staticFunctions
73     = new HashMap JavaDoc<String JavaDoc, StaticFunction>();
74
75   private HashMap JavaDoc<String JavaDoc, ClassDef> _staticClasses
76     = new HashMap JavaDoc<String JavaDoc, ClassDef>();
77
78   private HashMap JavaDoc<String JavaDoc, ClassDef> _lowerStaticClasses
79     = new HashMap JavaDoc<String JavaDoc, ClassDef>();
80
81   private HashMap JavaDoc<String JavaDoc, JavaClassDef> _javaClassWrappers
82     = new HashMap JavaDoc<String JavaDoc, JavaClassDef>();
83
84   private HashMap JavaDoc<String JavaDoc, JavaClassDef> _lowerJavaClassWrappers
85     = new HashMap JavaDoc<String JavaDoc, JavaClassDef>();
86
87   private HashMap JavaDoc<String JavaDoc, StringValue> _iniMap
88     = new HashMap JavaDoc<String JavaDoc, StringValue>();
89
90   protected MarshalFactory _marshalFactory;
91   protected ExprFactory _exprFactory;
92
93   /**
94    * Constructor.
95    */

96   public ModuleContext()
97   {
98     this(Thread.currentThread().getContextClassLoader());
99   }
100
101   /**
102    * Constructor.
103    */

104   public ModuleContext(ClassLoader JavaDoc loader)
105   {
106     _loader = loader;
107     
108     _marshalFactory = new MarshalFactory(this);
109     _exprFactory = new ExprFactory();
110   }
111
112   public static ModuleContext getLocalContext(ClassLoader JavaDoc loader)
113   {
114     throw new UnsupportedOperationException JavaDoc();
115     /*
116     ModuleContext context = _localModuleContext.getLevel(loader);
117
118     if (context == null) {
119       context = new ModuleContext(loader);
120       _localModuleContext.set(context, loader);
121     }
122
123     return context;
124     */

125   }
126
127   /**
128    * Adds a module
129    */

130   /*
131   public static ModuleInfo addModule(String name, QuercusModule module)
132     throws ConfigException
133   {
134     ClassLoader loader = module.getClass().getClassLoader();
135
136     ModuleContext context = getLocalContext(loader);
137
138     return context.addModuleInfo(name, module);
139   }
140   */

141
142   /**
143    * Adds a class
144    */

145   /*
146   public static JavaClassDef addClass(String name,
147                                       Class type,
148                                       String ext,
149                                       Class javaClassDefClass)
150     throws ConfigException,
151            IllegalAccessException,
152            InstantiationException,
153            NoSuchMethodException,
154            InvocationTargetException
155   {
156     ClassLoader loader = type.getClassLoader();
157
158     ModuleContext context = getLocalContext(loader);
159
160     return context.addClassInfo(name, type, ext, javaClassDefClass);
161   }
162   */

163
164   /**
165    * Adds a class
166    */

167   /*
168   public static JavaImplClassDef addClassImpl(String name, Class type, String ext)
169     throws ConfigException, IllegalAccessException, InstantiationException
170   {
171     ClassLoader loader = type.getClassLoader();
172
173     ModuleContext context = getLocalContext(loader);
174
175     return context.addClassImplInfo(name, type, ext);
176   }
177   */

178
179   /**
180    * Adds module info.
181    */

182   public ModuleInfo addModule(String JavaDoc 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 JavaDoc name, Class JavaDoc type,
198                    String JavaDoc extension, Class JavaDoc javaClassDefClass)
199     throws NoSuchMethodException JavaDoc,
200        InvocationTargetException JavaDoc,
201        IllegalAccessException JavaDoc,
202        InstantiationException JavaDoc
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 JavaDoc constructor
216           = javaClassDefClass.getConstructor(ModuleContext.class,
217                                               String JavaDoc.class,
218                                               Class JavaDoc.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       // def.introspect();
232

233       if (extension != null)
234         _extensionSet.add(extension);
235     }
236
237     return def;
238   }
239
240   public JavaImplClassDef addClassImpl(String JavaDoc name,
241                        Class JavaDoc type,
242                        String JavaDoc extension)
243     throws IllegalAccessException JavaDoc, InstantiationException JavaDoc
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   /**
254    * Adds a java class
255    */

256   public JavaClassDef getJavaClassDefinition(String JavaDoc className)
257   {
258     JavaClassDef def = _javaClassWrappers.get(className);
259
260     if (def != null)
261       return def;
262
263     try {
264       Class JavaDoc type;
265
266       try {
267         type = Class.forName(className, false, _loader);
268       }
269       catch (ClassNotFoundException JavaDoc e) {
270         throw new ClassNotFoundException JavaDoc(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       // def.introspect();
279

280       return def;
281     } catch (RuntimeException JavaDoc e) {
282       throw e;
283     } catch (Exception JavaDoc e) {
284       throw new QuercusRuntimeException(e);
285     }
286   }
287
288   /**
289    * Finds the java class wrapper.
290    */

291   public ClassDef findJavaClassWrapper(String JavaDoc 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 JavaDoc type,
312                    boolean isNotNull,
313                    boolean isNullAsFalse)
314   {
315     return getMarshalFactory().create(type, isNotNull, isNullAsFalse);
316   }
317
318   /**
319    * Returns an array of the defined functions.
320    */

321   public ArrayValue getDefinedFunctions()
322   {
323     ArrayValue internal = new ArrayValueImpl();
324
325     for (String JavaDoc name : _staticFunctions.keySet()) {
326       internal.put(name);
327     }
328
329     return internal;
330   }
331
332   /**
333    * Returns the class with the given name.
334    */

335   public ClassDef findClass(String JavaDoc 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   /**
346    * Returns the class maps.
347    */

348   public HashMap JavaDoc<String JavaDoc, ClassDef> getClassMap()
349   {
350     return _staticClasses;
351   }
352
353   /**
354    * Returns the module with the given name.
355    */

356   public QuercusModule findModule(String JavaDoc name)
357   {
358     return _modules.get(name);
359   }
360
361   /**
362    * Returns true if an extension is loaded.
363    */

364   public boolean isExtensionLoaded(String JavaDoc name)
365   {
366     return _extensionSet.contains(name);
367   }
368
369   /**
370    * Returns true if an extension is loaded.
371    */

372   public HashSet JavaDoc<String JavaDoc> getLoadedExtensions()
373   {
374     return _extensionSet;
375   }
376
377   public HashMap JavaDoc<String JavaDoc, Value> getConstMap()
378   {
379     return _constMap;
380   }
381
382   /**
383    * Creates a static function.
384    */

385   public StaticFunction createStaticFunction(QuercusModule module,
386                          Method JavaDoc method)
387   {
388     return new StaticFunction(this, module, method);
389   }
390
391   /**
392    * Returns a named constant.
393    */

394   public Value getConstant(String JavaDoc name)
395   {
396     return _constMap.get(name);
397   }
398
399   public static Value objectToValue(Object JavaDoc obj)
400   {
401     if (obj == null)
402       return NullValue.NULL;
403     else if (Byte JavaDoc.class.equals(obj.getClass()) ||
404              Short JavaDoc.class.equals(obj.getClass()) ||
405              Integer JavaDoc.class.equals(obj.getClass()) ||
406              Long JavaDoc.class.equals(obj.getClass())) {
407       return LongValue.create(((Number JavaDoc) obj).longValue());
408     } else if (Float JavaDoc.class.equals(obj.getClass()) ||
409                Double JavaDoc.class.equals(obj.getClass())) {
410       return DoubleValue.create(((Number JavaDoc) obj).doubleValue());
411     } else if (String JavaDoc.class.equals(obj.getClass())) {
412       return new StringValueImpl((String JavaDoc) obj);
413     } else {
414       // XXX: unknown types, e.g. Character?
415

416       return null;
417     }
418   }
419
420   /**
421    * Introspects the module class for functions.
422    *
423    * @param name the php class name
424    * @param type the class to introspect.
425    * @param extension the extension provided by the class, or null
426    */

427   private JavaImplClassDef addJavaImplClass(String JavaDoc name,
428                                             Class JavaDoc type,
429                                             String JavaDoc extension)
430     throws IllegalAccessException JavaDoc, InstantiationException JavaDoc
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