001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.quercus.module;
031:
032: import com.caucho.config.ConfigException;
033: import com.caucho.quercus.env.*;
034: import com.caucho.quercus.program.*;
035: import com.caucho.util.L10N;
036:
037: import java.lang.reflect.Field;
038: import java.lang.reflect.Method;
039: import java.lang.reflect.Modifier;
040: import java.util.HashMap;
041: import java.util.HashSet;
042: import java.util.Map;
043: import java.util.logging.Level;
044: import java.util.logging.Logger;
045:
046: /**
047: * Class-loader specific context for loaded PHP.
048: */
049: public class ModuleInfo {
050: private static L10N L = new L10N(ModuleInfo.class);
051: private static final Logger log = Logger.getLogger(ModuleInfo.class
052: .getName());
053:
054: private ModuleContext _context;
055:
056: private String _name;
057: private QuercusModule _module;
058:
059: private HashSet<String> _extensionSet = new HashSet<String>();
060:
061: private HashMap<String, Value> _constMap = new HashMap<String, Value>();
062:
063: private HashMap<String, AbstractFunction> _staticFunctions = new HashMap<String, AbstractFunction>();
064:
065: private IniDefinitions _iniDefinitions = new IniDefinitions();
066:
067: private HashSet<String> _extensionClassMap = new HashSet<String>();
068:
069: public void addExtensionClass(String name) {
070: _extensionClassMap.add(name);
071: }
072:
073: /**
074: * Constructor.
075: */
076: public ModuleInfo(ModuleContext context, String name,
077: QuercusModule module) throws ConfigException {
078: _context = context;
079:
080: _name = name;
081: _module = module;
082:
083: try {
084: introspectPhpModuleClass(module.getClass());
085: } catch (Exception e) {
086: throw ConfigException.create(e);
087: }
088: }
089:
090: public String getName() {
091: return _name;
092: }
093:
094: public QuercusModule getModule() {
095: return _module;
096: }
097:
098: /**
099: * Returns true if an extension is loaded.
100: */
101: public HashSet<String> getLoadedExtensions() {
102: return _extensionSet;
103: }
104:
105: public HashMap<String, Value> getConstMap() {
106: return _constMap;
107: }
108:
109: /**
110: * Returns a named constant.
111: */
112: public Value getConstant(String name) {
113: return _constMap.get(name);
114: }
115:
116: /**
117: * Returns the functions.
118: */
119: public HashMap<String, AbstractFunction> getFunctions() {
120: return _staticFunctions;
121: }
122:
123: public IniDefinitions getIniDefinitions() {
124: return _iniDefinitions;
125: }
126:
127: /**
128: * Introspects the module class for functions.
129: *
130: * @param cl the class to introspect.
131: */
132: private void introspectPhpModuleClass(Class cl)
133: throws IllegalAccessException, InstantiationException {
134: for (String ext : _module.getLoadedExtensions()) {
135: _extensionSet.add(ext);
136: }
137:
138: Map<String, Value> map = _module.getConstMap();
139:
140: if (map != null)
141: _constMap.putAll(map);
142:
143: for (Field field : cl.getFields()) {
144: if (!Modifier.isPublic(field.getModifiers()))
145: continue;
146:
147: if (!Modifier.isStatic(field.getModifiers()))
148: continue;
149:
150: if (!Modifier.isFinal(field.getModifiers()))
151: continue;
152:
153: Object obj = field.get(null);
154:
155: Value value = objectToValue(obj);
156:
157: if (value != null)
158: _constMap.put(field.getName(), value);
159: }
160:
161: IniDefinitions iniDefinitions = _module.getIniDefinitions();
162:
163: if (map != null)
164: _iniDefinitions.addAll(iniDefinitions);
165:
166: for (Method method : cl.getMethods()) {
167: if (method.getDeclaringClass().equals(Object.class))
168: continue;
169:
170: if (method.getDeclaringClass().isAssignableFrom(
171: AbstractQuercusModule.class))
172: continue;
173:
174: if (!Modifier.isPublic(method.getModifiers()))
175: continue;
176:
177: // XXX: removed for php/0c2o.qa
178: /**
179: Class retType = method.getReturnType();
180:
181: if (void.class.isAssignableFrom(retType))
182: continue;
183: */
184:
185: Class[] params = method.getParameterTypes();
186:
187: // php/1a10
188: if ("getLoadedExtensions".equals(method.getName()))
189: continue;
190:
191: if (hasCheckedException(method)) {
192: log
193: .warning(L
194: .l(
195: "Module method '{0}.{1}' may not throw checked exceptions",
196: method.getDeclaringClass()
197: .getName(), method
198: .getName()));
199: continue;
200: }
201:
202: try {
203: if (method.getName().startsWith("quercus_"))
204: throw new UnsupportedOperationException(L.l(
205: "{0}: use @Name instead", method));
206:
207: StaticFunction function = _context
208: .createStaticFunction(_module, method);
209:
210: String functionName = function.getName();
211:
212: AbstractJavaMethod oldFunction = (AbstractJavaMethod) _staticFunctions
213: .get(functionName);
214:
215: if (oldFunction != null)
216: _staticFunctions.put(functionName, oldFunction
217: .overload(function));
218: else
219: _staticFunctions.put(functionName, function);
220: } catch (Exception e) {
221: log.log(Level.FINE, e.toString(), e);
222: }
223: }
224: }
225:
226: private static boolean hasCheckedException(Method method) {
227: for (Class exnCl : method.getExceptionTypes()) {
228: if (!RuntimeException.class.isAssignableFrom(exnCl))
229: return true;
230: }
231:
232: return false;
233: }
234:
235: public static Value objectToValue(Object obj) {
236: if (obj == null)
237: return NullValue.NULL;
238: else if (Byte.class.equals(obj.getClass())
239: || Short.class.equals(obj.getClass())
240: || Integer.class.equals(obj.getClass())
241: || Long.class.equals(obj.getClass())) {
242: return LongValue.create(((Number) obj).longValue());
243: } else if (Float.class.equals(obj.getClass())
244: || Double.class.equals(obj.getClass())) {
245: return DoubleValue.create(((Number) obj).doubleValue());
246: } else if (String.class.equals(obj.getClass())) {
247: // XXX: need unicode semantics check
248: return new StringBuilderValue((String) obj);
249: } else {
250: // XXX: unknown types, e.g. Character?
251:
252: return null;
253: }
254: }
255: }
|