001: //========================================================================
002: //$Id: $
003: //Copyright 2006 Mort Bay Consulting Pty. Ltd.
004: //------------------------------------------------------------------------
005: //Licensed under the Apache License, Version 2.0 (the "License");
006: //you may not use this file except in compliance with the License.
007: //You may obtain a copy of the License at
008: //http://www.apache.org/licenses/LICENSE-2.0
009: //Unless required by applicable law or agreed to in writing, software
010: //distributed under the License is distributed on an "AS IS" BASIS,
011: //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: //See the License for the specific language governing permissions and
013: //limitations under the License.
014: //========================================================================
015: package org.jboss.jetty;
016:
017: import org.jboss.jetty.security.JBossUserRealm;
018: import org.jboss.logging.Logger;
019: import org.jboss.web.WebApplication;
020: import org.jboss.web.AbstractWebContainer.WebDescriptorParser;
021: import org.mortbay.j2ee.J2EEWebAppContext;
022: import org.mortbay.j2ee.session.AbstractReplicatedStore;
023: import org.mortbay.j2ee.session.Manager;
024: import org.mortbay.j2ee.session.Store;
025: import org.mortbay.jetty.servlet.SessionHandler;
026: import org.mortbay.jetty.servlet.jsr77.Jsr77ServletHandler;
027: import org.mortbay.jetty.webapp.WebAppClassLoader;
028:
029: /**
030: * JBossWebApplicationContext
031: *
032: * Customize the jetty WebAppContext to jboss environment.
033: *
034: */
035: public class JBossWebAppContext extends J2EEWebAppContext {
036: protected static Logger __log = Logger
037: .getLogger(JBossWebAppContext.class);
038:
039: protected WebDescriptorParser _descriptorParser;
040: protected WebApplication _webApp;
041: private String _subjAttrName = "j_subject";//TODO what was this doing here?
042: private JBossUserRealm _realm = null;
043: // this is a hack - but we need the session timeout - in case we are
044: // going to use a distributable session manager....
045: protected boolean _timeOutPresent = false;
046: protected int _timeOutMinutes = 0;
047:
048: /**
049: * Constructor
050: * @param descriptorParser
051: * @param webApp
052: * @param warUrl
053: * @throws Exception
054: */
055: public JBossWebAppContext(WebDescriptorParser descriptorParser,
056: WebApplication webApp, String warUrl) throws Exception {
057: super (null, new SessionHandler(), new Jsr77ServletHandler(),
058: null);
059: setWar(warUrl);
060: ((Jsr77ServletHandler) getServletHandler())
061: .setWebAppContext(this );
062: _descriptorParser = descriptorParser;
063: _webApp = webApp;
064: //very important - establish the classloader now, as it is the one
065: //that is being used for the performDeploy step
066: ClassLoader loader = Thread.currentThread()
067: .getContextClassLoader();
068: if (getDistributable()
069: && getDistributableSessionManager() != null)
070: setUpDistributableSessionManager(loader);
071:
072: setClassLoader(new WebAppClassLoader(loader, this ));
073: }
074:
075: /* ------------------------------------------------------------ */
076: public void doStop() throws Exception {
077: super .doStop();
078: _descriptorParser = null;
079: _webApp = null;
080: _subjAttrName = null;
081: _realm = null;
082: }
083:
084: public void setRealm(JBossUserRealm realm) {
085: _realm = realm;
086: }
087:
088: public JBossUserRealm getRealm() {
089: return _realm;
090: }
091:
092: public void setSubjectAttribute(String subjAttr) {
093: _subjAttrName = subjAttr;
094: }
095:
096: public String getSubjectAttribute() {
097: return _subjAttrName;
098: }
099:
100: public String getUniqueName() {
101: return _descriptorParser.getDeploymentInfo().getCanonicalName();
102: }
103:
104: protected void startContext() throws Exception {
105: //set up the java:comp/env namespace so that it can be refered to
106: //in other parts of the startup
107: setUpENC(getClassLoader());
108: super .startContext();
109: if (_realm != null) {
110: //start the realm from within the webapp's classloader as it wants
111: //to do JNDI lookups
112: ClassLoader currentLoader = Thread.currentThread()
113: .getContextClassLoader();
114: Thread.currentThread().setContextClassLoader(
115: getClassLoader());
116: try {
117: _realm.init();
118: } finally {
119: Thread.currentThread().setContextClassLoader(
120: currentLoader);
121: }
122: }
123: }
124:
125: protected void setUpDistributableSessionManager(ClassLoader loader) {
126: try {
127: Manager sm = (Manager) getDistributableSessionManager();
128: Store store = sm.getStore();
129: if (store instanceof AbstractReplicatedStore)
130: ((AbstractReplicatedStore) store).setLoader(loader);
131: if (_timeOutPresent)
132: sm.setMaxInactiveInterval(_timeOutMinutes * 60);
133: getSessionHandler().setSessionManager(sm);
134: } catch (Exception e) {
135: __log
136: .error(
137: "could not set up Distributable HttpSession Manager - using local one",
138: e);
139: }
140: }
141:
142: protected void setUpENC(ClassLoader loader) throws Exception {
143: _webApp.setClassLoader(loader);
144: _webApp.setName(getDisplayName());
145: _webApp.setAppData(this );
146: __log.debug("setting up ENC...");
147: _descriptorParser.parseWebAppDescriptors(loader, _webApp
148: .getMetaData());
149: __log.debug("setting up ENC succeeded");
150: }
151: }
|