001: /*
002: * Copyright 2006 Luca Garulli (luca.garulli@assetdata.it)
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.romaframework.aspect.view.echo2;
018:
019: import java.io.IOException;
020:
021: import javax.servlet.ServletConfig;
022: import javax.servlet.ServletException;
023: import javax.servlet.http.HttpServletRequest;
024: import javax.servlet.http.HttpServletResponse;
025:
026: import nextapp.echo2.app.ApplicationInstance;
027: import nextapp.echo2.webcontainer.WebContainerServlet;
028:
029: import org.apache.commons.logging.Log;
030: import org.apache.commons.logging.LogFactory;
031: import org.apache.log4j.NDC;
032: import org.romaframework.aspect.session.SessionAspect;
033: import org.romaframework.aspect.session.SessionInfo;
034: import org.romaframework.core.Utility;
035: import org.romaframework.core.config.RomaApplicationContext;
036: import org.romaframework.core.flow.ObjectContext;
037: import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
038: import org.springframework.context.support.GenericApplicationContext;
039: import org.springframework.core.io.FileSystemResource;
040:
041: /**
042: * Echo2Server implementation.
043: */
044: public class Echo2StartupServlet extends WebContainerServlet {
045:
046: private static final String CONTAINER_CFG_FILE = "/WEB-INF/applicationContext.xml";
047: private static final String GUEST_USER_ID = "guest";
048: protected static Log log = LogFactory
049: .getLog(Echo2StartupServlet.class);
050:
051: @Override
052: public void init(ServletConfig iConfig) throws ServletException {
053: log.info("[Echo2StartupServlet.init] Startup servlet...");
054:
055: // log.info("[Echo2StartupServlet.init] Setting ROMA ClassLoader...");
056: // Thread.currentThread().setContextClassLoader(new RomaClassLoader());
057:
058: synchronized (getClass()) {
059: super .init(iConfig);
060:
061: // SET APPLICATION ABSOLUTE PATH
062: String absolutePath = iConfig.getServletContext()
063: .getRealPath(Utility.PATH_SEPARATOR_STRING);
064: log.info("[Echo2StartupServlet.init] ContextRoot: "
065: + absolutePath);
066:
067: RomaApplicationContext.setApplicationPath(absolutePath);
068:
069: GenericApplicationContext appContext = startContainer();
070:
071: // CONFIGURE ROMA APPLICATION CONTEXT
072: RomaApplicationContext.setup(appContext);
073: }
074:
075: log.info("[Echo2StartupServlet.init] Startup ok.");
076: }
077:
078: private GenericApplicationContext startContainer() {
079: GenericApplicationContext appContext = new GenericApplicationContext();
080: XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(
081: appContext);
082: xmlReader.loadBeanDefinitions(new FileSystemResource(
083: RomaApplicationContext.getApplicationPath()
084: + CONTAINER_CFG_FILE));
085: appContext.refresh();
086: return appContext;
087: }
088:
089: @Override
090: protected void process(HttpServletRequest request,
091: HttpServletResponse response) throws IOException,
092: ServletException {
093: startLogContext(request);
094:
095: super .process(request, response);
096:
097: stopLogContext();
098: }
099:
100: protected void stopLogContext() {
101: NDC.pop();
102: }
103:
104: protected void startLogContext(HttpServletRequest request) {
105: String id = GUEST_USER_ID;
106:
107: if (RomaApplicationContext.getInstance() != null) {
108: SessionInfo sess = ObjectContext.getInstance()
109: .getComponent(SessionAspect.class).getSession(
110: request.getSession());
111: if (sess != null) {
112: Object account = sess.getAccount();
113: if (account != null)
114: id = account.toString();
115: }
116: }
117:
118: NDC.push(id);
119: }
120:
121: @Override
122: public void destroy() {
123: log.info("[Echo2StartupServlet.destroy] Shutdown servlet...");
124: synchronized (getClass()) {
125: ObjectContext.getInstance().shutdown();
126: RomaApplicationContext.getInstance().shutdown();
127: super .destroy();
128: }
129: log.info("[Echo2StartupServlet.destroy] Shutdown ok.");
130:
131: NDC.remove();
132: }
133:
134: /**
135: * Return a new application instance. One per user session.
136: */
137: @Override
138: public ApplicationInstance newApplicationInstance() {
139: Echo2ApplicationContext newCtx = null;
140: synchronized (getClass()) {
141: newCtx = new Echo2ApplicationContext(getServletConfig());
142:
143: // REGISTER APLICATION INSTANCE AS SESSION INFO
144: ObjectContext.getInstance().getComponent(
145: SessionAspect.class).addSession(newCtx);
146: }
147: return newCtx;
148: }
149: }
|