KickJava   Java API By Example, From Geeks To Geeks.

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


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

53 public class ModuleInfo {
54   private static L10N L = new L10N(ModuleInfo.class);
55   private static final Logger JavaDoc log
56     = Logger.getLogger(ModuleInfo.class.getName());
57
58   private ModuleContext _context;
59
60   private String JavaDoc _name;
61   private QuercusModule _module;
62
63   private HashSet JavaDoc<String JavaDoc> _extensionSet
64     = new HashSet JavaDoc<String JavaDoc>();
65
66   private HashMap JavaDoc<String JavaDoc, Value> _constMap
67     = new HashMap JavaDoc<String JavaDoc, Value>();
68
69   private HashMap JavaDoc<String JavaDoc, StaticFunction> _staticFunctions
70     = new HashMap JavaDoc<String JavaDoc, StaticFunction>();
71
72   private HashMap JavaDoc<String JavaDoc, StringValue> _iniMap
73     = new HashMap JavaDoc<String JavaDoc, StringValue>();
74
75   /**
76    * Constructor.
77    */

78   public ModuleInfo(ModuleContext context, String JavaDoc 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 JavaDoc e) {
89       throw new ConfigException(e);
90     }
91   }
92
93   public String JavaDoc getName()
94   {
95     return _name;
96   }
97
98   public QuercusModule getModule()
99   {
100     return _module;
101   }
102   
103   /**
104    * Returns true if an extension is loaded.
105    */

106   public HashSet JavaDoc<String JavaDoc> getLoadedExtensions()
107   {
108     return _extensionSet;
109   }
110
111   public HashMap JavaDoc<String JavaDoc, Value> getConstMap()
112   {
113     return _constMap;
114   }
115
116   /**
117    * Returns a named constant.
118    */

119   public Value getConstant(String JavaDoc name)
120   {
121     return _constMap.get(name);
122   }
123
124   /**
125    * Returns the functions.
126    */

127   public HashMap JavaDoc<String JavaDoc,StaticFunction> getFunctions()
128   {
129     return _staticFunctions;
130   }
131
132   public HashMap JavaDoc<String JavaDoc, StringValue> getDefaultIni()
133   {
134     return _iniMap;
135   }
136
137   /**
138    * Introspects the module class for functions.
139    *
140    * @param cl the class to introspect.
141    */

142   private void introspectPhpModuleClass(Class JavaDoc cl)
143     throws IllegalAccessException JavaDoc, InstantiationException JavaDoc
144   {
145     for (String JavaDoc ext : _module.getLoadedExtensions()) {
146       _extensionSet.add(ext);
147     }
148
149     Map JavaDoc<String JavaDoc, Value> map = _module.getConstMap();
150
151     if (map != null)
152       _constMap.putAll(map);
153
154     for (Field JavaDoc 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 JavaDoc 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 JavaDoc<String JavaDoc, StringValue> iniMap = _module.getDefaultIni();
173
174     if (map != null)
175       _iniMap.putAll(iniMap);
176
177     for (Method JavaDoc method : cl.getMethods()) {
178       if (method.getDeclaringClass().equals(Object JavaDoc.class))
179     continue;
180       
181       if (method.getDeclaringClass().isAssignableFrom(AbstractQuercusModule.class))
182     continue;
183       
184       if (! Modifier.isPublic(method.getModifiers()))
185         continue;
186
187       // XXX: removed for php/0c2o.qa
188
/**
189        Class retType = method.getReturnType();
190
191       if (void.class.isAssignableFrom(retType))
192         continue;
193        */

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 JavaDoc []params = method.getParameterTypes();
203
204       try {
205         StaticFunction function
206       = _context.createStaticFunction(_module, method);
207
208         String JavaDoc methodName = method.getName();
209
210         if (methodName.startsWith("quercus_"))
211           methodName = methodName.substring(8);
212
213         _staticFunctions.put(methodName, function);
214       } catch (Exception JavaDoc e) {
215         log.log(Level.FINE, e.toString(), e);
216       }
217     }
218   }
219
220   private static boolean hasCheckedException(Method JavaDoc method)
221   {
222     for (Class JavaDoc exnCl : method.getExceptionTypes()) {
223       if (! RuntimeException JavaDoc.class.isAssignableFrom(exnCl))
224     return true;
225     }
226
227     return false;
228   }
229
230   public static Value objectToValue(Object JavaDoc obj)
231   {
232     if (obj == null)
233       return NullValue.NULL;
234     else if (Byte JavaDoc.class.equals(obj.getClass()) ||
235              Short JavaDoc.class.equals(obj.getClass()) ||
236              Integer JavaDoc.class.equals(obj.getClass()) ||
237              Long JavaDoc.class.equals(obj.getClass())) {
238       return LongValue.create(((Number JavaDoc) obj).longValue());
239     } else if (Float JavaDoc.class.equals(obj.getClass()) ||
240                Double JavaDoc.class.equals(obj.getClass())) {
241       return DoubleValue.create(((Number JavaDoc) obj).doubleValue());
242     } else if (String JavaDoc.class.equals(obj.getClass())) {
243       return new StringValueImpl((String JavaDoc) obj);
244     } else {
245       // XXX: unknown types, e.g. Character?
246

247       return null;
248     }
249   }
250 }
251
252
Popular Tags