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;
031:
032: import com.caucho.loader.*;
033: import com.caucho.quercus.module.ModuleContext;
034: import com.caucho.quercus.module.ResinModuleContext;
035: import com.caucho.server.webapp.*;
036: import com.caucho.server.session.*;
037: import com.caucho.sql.*;
038: import com.caucho.util.L10N;
039: import com.caucho.util.Log;
040: import com.caucho.vfs.*;
041: import com.caucho.java.*;
042:
043: import javax.sql.DataSource;
044: import java.sql.*;
045: import java.util.logging.Logger;
046:
047: /**
048: * Facade for the PHP language.
049: */
050: public class ResinQuercus extends Quercus {
051: private static L10N L = new L10N(ResinQuercus.class);
052: private static final Logger log = Log.open(ResinQuercus.class);
053:
054: private static EnvironmentLocal<ModuleContext> _localModuleContext = new EnvironmentLocal<ModuleContext>();
055:
056: private WebApp _webApp;
057:
058: /**
059: * Constructor.
060: */
061: public ResinQuercus() {
062: super ();
063:
064: setPwd(Vfs.lookup());
065: setWorkDir(WorkDir.getLocalWorkDir());
066: }
067:
068: public void setWebApp(WebApp webApp) {
069: _webApp = webApp;
070: }
071:
072: public WebApp getWebApp() {
073: return _webApp;
074: }
075:
076: @Override
077: public ModuleContext getLocalContext(ClassLoader loader) {
078: synchronized (_localModuleContext) {
079: ModuleContext context = _localModuleContext
080: .getLevel(loader);
081:
082: if (context == null) {
083: context = createModuleContext(loader);
084: _localModuleContext.set(context, loader);
085: }
086:
087: return context;
088: }
089: }
090:
091: @Override
092: protected ModuleContext createModuleContext(ClassLoader loader) {
093: return new ResinModuleContext(loader);
094: }
095:
096: public String getCookieName() {
097: SessionManager sm = getSessionManager();
098:
099: if (sm != null)
100: return sm.getCookieName();
101: else
102: return "JSESSIONID";
103: }
104:
105: public SessionManager getSessionManager() {
106: if (_webApp != null)
107: return _webApp.getSessionManager();
108: else
109: return null;
110: }
111:
112: @Override
113: public String getVersion() {
114: return com.caucho.Version.VERSION;
115: }
116:
117: @Override
118: public String getVersionDate() {
119: return com.caucho.Version.VERSION_DATE;
120: }
121:
122: @Override
123: public DataSource findDatabase(String driver, String url) {
124: try {
125: if (getDatabase() != null)
126: return getDatabase();
127: else
128: return DatabaseManager.findDatabase(url, driver);
129: } catch (Exception e) {
130: throw new QuercusModuleException(e);
131: }
132: }
133:
134: /**
135: * Unwrap connection if necessary.
136: */
137: @Override
138: public Connection getConnection(Connection conn) {
139: try {
140: return ((UserConnection) conn).getConnection();
141: } catch (Exception e) {
142: throw new QuercusModuleException(e);
143: }
144: }
145:
146: /*
147: * Marks the connection for removal from the connection pool.
148: */
149: @Override
150: public void markForPoolRemoval(Connection conn) {
151: ManagedConnectionImpl mConn = ((UserConnection) conn)
152: .getMConn();
153:
154: String url = mConn.getURL();
155: String driver = mConn.getDriverClass().getCanonicalName();
156:
157: DataSource ds = findDatabase(driver, url);
158:
159: ((DBPool) ds).markForPoolRemoval(mConn);
160: }
161:
162: /**
163: * Unwrap statement if necessary.
164: */
165: public Statement getStatement(Statement stmt) {
166: return ((UserStatement) stmt).getStatement();
167: }
168:
169: /*
170: * Returns true if Quercus is running under Resin.
171: */
172: @Override
173: public boolean isResin() {
174: return true;
175: }
176: }
|