📄 quercus.java
字号:
/* * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved * * This file is part of Resin(R) Open Source * * Each copy or derived work must preserve the copyright notice and this * notice unmodified. * * Resin Open Source is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Resin Open Source is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty * of NON-INFRINGEMENT. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License * along with Resin Open Source; if not, write to the * * Free Software Foundation, Inc. * 59 Temple Place, Suite 330 * Boston, MA 02111-1307 USA * * @author Scott Ferguson */package com.caucho.quercus;import com.caucho.config.ConfigException;import com.caucho.quercus.annotation.ClassImplementation;import com.caucho.quercus.env.*;import com.caucho.quercus.lib.db.JavaSqlDriverWrapper;import com.caucho.quercus.lib.file.FileModule;import com.caucho.quercus.lib.session.QuercusSessionManager;import com.caucho.quercus.module.*;import com.caucho.quercus.page.InterpretedPage;import com.caucho.quercus.page.PageManager;import com.caucho.quercus.page.QuercusPage;import com.caucho.quercus.parser.QuercusParser;import com.caucho.quercus.program.*;import com.caucho.util.*;import com.caucho.vfs.*;import javax.servlet.ServletContext;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.sql.DataSource;import java.io.IOException;import java.io.InputStream;import java.lang.reflect.InvocationTargetException;import java.net.URL;import java.sql.Connection;import java.util.*;import java.util.logging.Level;import java.util.logging.Logger;/** * Facade for the PHP language. */public class Quercus{ private static L10N L = new L10N(Quercus.class); private static final Logger log = Log.open(Quercus.class); private static HashSet<String> _superGlobals = new HashSet<String>(); private static IniDefinitions _ini = new IniDefinitions(); private final PageManager _pageManager; private final QuercusSessionManager _sessionManager; private final ClassLoader _loader; private ModuleContext _moduleContext; private HashMap<String, StringValue> _internMap = new HashMap<String, StringValue>(); private HashMap<String, ModuleInfo> _modules = new HashMap<String, ModuleInfo>(); private HashSet<ModuleStartupListener> _moduleStartupListeners = new HashSet<ModuleStartupListener>(); private HashSet<String> _extensionSet = new HashSet<String>(); private HashMap<String, Value> _constMap = new HashMap<String, Value>(); private HashMap<String, AbstractFunction> _funMap = new HashMap<String, AbstractFunction>(); private HashMap<String, AbstractFunction> _lowerFunMap = new HashMap<String, AbstractFunction>(); /* private ClassDef _stdClassDef; private QuercusClass _stdClass; private HashMap<String, ClassDef> _staticClasses = new HashMap<String, ClassDef>(); private HashMap<String, ClassDef> _lowerStaticClasses = new HashMap<String, ClassDef>(); */ private HashMap<String, JavaClassDef> _javaClassWrappers = new HashMap<String, JavaClassDef>(); private HashMap<String, JavaClassDef> _lowerJavaClassWrappers = new HashMap<String, JavaClassDef>(); private final IniDefinitions _iniDefinitions = new IniDefinitions(); private IdentityHashMap<String, Value> _iniMap; private HashMap<Value, Value> _serverEnvMap = new HashMap<Value, Value>(); private IntMap _classNameMap = new IntMap(8192); private ClassDef []_classMap = new ClassDef[256]; private IntMap _constantNameMap = new IntMap(8192); private IntMap _functionNameMap = new IntMap(8192); private AbstractFunction []_functionMap = new AbstractFunction[256]; private LruCache<String, QuercusProgram> _evalCache = new LruCache<String, QuercusProgram>(4096); private TimedCache<IncludeKey, Path> _includeCache = new TimedCache<IncludeKey, Path>(4096, 10000); //private LruCache<DefinitionKey,SoftReference<DefinitionState>> _defCache // = new LruCache<DefinitionKey,SoftReference<DefinitionState>>(4096); private long _defCacheHitCount; private long _defCacheMissCount; // XXX: needs to be a timed LRU private LruCache<String, SessionArrayValue> _sessionMap = new LruCache<String, SessionArrayValue>(4096); private HashMap<String, Object> _specialMap = new HashMap<String, Object>(); private String _scriptEncoding; private String _phpVersion = "5.2.0"; private String _mySqlVersion; private boolean _isStrict; private boolean _isRequireSource; private DataSource _database; private long _staticId; private Path _pwd; private Path _workDir; private ServletContext _servletContext; /** * Constructor. */ public Quercus() { _loader = Thread.currentThread().getContextClassLoader(); _moduleContext = getLocalContext(); initStaticFunctions(); initStaticClasses(); initStaticClassServices(); _pageManager = createPageManager(); _sessionManager = createSessionManager(); _workDir = getWorkDir(); _iniDefinitions.addAll(_ini); } public Env createEnv(QuercusPage page, WriteStream out, HttpServletRequest request, HttpServletResponse response) { return new Env(this, page, out, request, response); } /** * Returns the working directory. */ public Path getPwd() { if (_pwd == null) _pwd = new FilePath(System.getProperty("user.dir")); return _pwd; } /** * Sets the working directory. */ public void setPwd(Path path) { _pwd = path; } public Path getWorkDir() { if (_workDir == null) _workDir = getPwd().lookup("WEB-INF/work"); return _workDir; } public void setWorkDir(Path workDir) { _workDir = workDir; } public String getCookieName() { return "JSESSIONID"; } public long getDependencyCheckInterval() { return 2000L; } public String getVersion() { return "Open Source 3.2.0"; } public String getVersionDate() { return "20070628T2777"; } protected PageManager createPageManager() { return new PageManager(this); } protected QuercusSessionManager createSessionManager() { return new QuercusSessionManager(); } /** * Returns the context for this class loader. */ public final ModuleContext getLocalContext() { return getLocalContext(_loader); } public ModuleContext getLocalContext(ClassLoader loader) { synchronized (this) { if (_moduleContext == null) _moduleContext = createModuleContext(loader); } return _moduleContext; } protected ModuleContext createModuleContext(ClassLoader loader) { return new ModuleContext(loader); } /** * Returns the module context. */ public ModuleContext getModuleContext() { return _moduleContext; } public QuercusSessionManager getQuercusSessionManager() { return _sessionManager; } /** * true if the pages should be compiled. */ public boolean isCompile() { return _pageManager.isCompile(); } /* * Returns true if this is the Professional version. */ public boolean isPro() { return false; } /* * Returns true if Quercus is running under Resin. */ public boolean isResin() { return false; } /** * Returns true if unicode.semantics is on. */ public boolean isUnicodeSemantics() { boolean value = getIniBoolean("unicode.semantics"); return value; } /* * Returns true if URLs may be arguments of include(). */ public boolean isAllowUrlInclude() { return getIniBoolean("allow_url_include"); } /* * Returns true if URLs may be arguments of fopen(). */ public boolean isAllowUrlFopen() { return getIniBoolean("allow_url_fopen"); } public StringValue createString(String v) { if (isUnicodeSemantics()) return new UnicodeBuilderValue(v); else return new StringBuilderValue(v); } /** * Set true if pages should be compiled. */ public void setCompile(boolean isCompile) { _pageManager.setCompile(isCompile); } /** * Set true if pages should be compiled. */ public void setLazyCompile(boolean isCompile) { _pageManager.setLazyCompile(isCompile); } /* * Returns the expected encoding of php scripts. */ public String getScriptEncoding() { if (_scriptEncoding != null) return _scriptEncoding; else if (isUnicodeSemantics()) return "utf-8"; else return "iso-8859-1"; } /* * Sets the expected encoding of php scripts. */ public void setScriptEncoding(String encoding) { _scriptEncoding = encoding; } /* * Returns the mysql version to report to to PHP applications. * It is user set-able to allow cloaking of the underlying mysql * JDBC driver version for application compatibility. */ public String getMysqlVersion() { return _mySqlVersion; } /* * Sets the mysql version to report to applications. This cloaks * the underlying JDBC driver version, so that when an application * asks for the mysql version, this version string is returned instead. */ public void setMysqlVersion(String version) { _mySqlVersion = version; } public String getPhpVersion() { return _phpVersion; } public void setPhpVersion(String version) { _phpVersion = version; } /* * Sets the ServletContext. */ public void setServletContext(ServletContext servletContext) { _servletContext = servletContext; } /* * Returns the ServletContext. */ public ServletContext getServletContext() { return _servletContext; } /** * Sets the default data source. */ public void setDatabase(DataSource database) { _database = database; } /** * Gets the default data source. */ public DataSource getDatabase() { return _database; } /** * Gets the default data source. */ public DataSource findDatabase(String driver, String url) { if (_database != null) return _database; else { ClassLoader loader = Thread.currentThread().getContextClassLoader(); try { Class cls = loader.loadClass(driver); Object ds = cls.newInstance(); if (ds instanceof DataSource) return (DataSource) ds; else return new JavaSqlDriverWrapper((java.sql.Driver) ds, url); } catch (ClassNotFoundException e) { throw new QuercusModuleException(e); } catch (InstantiationException e) { throw new QuercusModuleException(e); } catch (IllegalAccessException e) { throw new QuercusModuleException(e); } } } /** * Unwrap connection if necessary. */ public Connection getConnection(Connection conn) { return conn; } /* * Marks the connection for removal from the connection pool. */ public void markForPoolRemoval(Connection conn) { return; } /** * Unwrap statement if necessary. */ public java.sql.Statement getStatement(java.sql.Statement stmt) { return stmt; } /** * Sets the strict mode. */ public void setStrict(boolean isStrict) { _isStrict = isStrict; } /** * Gets the strict mode. */ public boolean isStrict() { return _isStrict; } public void setRequireSource(boolean isRequireSource) { _isRequireSource = isRequireSource; } /* * Returns whether the php source is required for compiled files. */ public boolean isRequireSource() { return _isRequireSource; } /** * Adds a module */ public void addModule(QuercusModule module) throws ConfigException { try { introspectPhpModuleClass(module.getClass()); } catch (Exception e) { throw ConfigException.create(e); } } /** * Adds a java class */ public void addJavaClass(String name, Class type) throws ConfigException { try { if (type.isAnnotationPresent(ClassImplementation.class)) introspectJavaImplClass(name, type, null); else introspectJavaClass(name, type, null, null); } catch (Exception e) { throw ConfigException.create(e); } } /** * Adds a java class */ public void addJavaClass(String phpName, String className) { Class type; try { type = Class.forName(className, false, _loader); } catch (ClassNotFoundException e) { throw new QuercusRuntimeException(L.l("`{0}' not valid: {1}", className, e.toString()), e); } addJavaClass(phpName, type); } /** * Adds a impl class */ public void addImplClass(String name, Class type) throws ConfigException { try { introspectJavaImplClass(name, type, null); } catch (Exception e) { throw ConfigException.create(e); } } /** * Adds a java class */ public JavaClassDef getJavaClassDefinition(Class type, String className) { JavaClassDef def; synchronized (_javaClassWrappers) { def = _javaClassWrappers.get(className); if (def == null) { try { def = getModuleContext().getJavaClassDefinition(type, className); _javaClassWrappers.put(className, def); } catch (RuntimeException e) { throw e; } catch (Exception e) { throw new QuercusRuntimeException(e); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -