Package org.olat.core.servlets

Source Code of org.olat.core.servlets.OLATServlet

/**
* OLAT - Online Learning and Training<br>
* http://www.olat.org
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); <br>
* you may not use this file except in compliance with the License.<br>
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing,<br>
* software distributed under the License is distributed on an "AS IS" BASIS, <br>
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. <br>
* See the License for the specific language governing permissions and <br>
* limitations under the License.
* <p>
* Copyright (c) 1999-2006 at Multimedia- & E-Learning Services (MELS),<br>
* University of Zurich, Switzerland.
* <p>
*/

package org.olat.core.servlets;

import java.io.IOException;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.LogManager;
import org.apache.log4j.PropertyConfigurator;
import org.olat.core.CoreSpringFactory;
import org.olat.core.configuration.ModuleConfig;
import org.olat.core.configuration.ModuleLifeCycle;
import org.olat.core.dispatcher.Dispatcher;
import org.olat.core.logging.OLog;
import org.olat.core.logging.Tracing;
import org.olat.core.logging.activity.ThreadLocalUserActivityLoggerInstaller;
import org.olat.core.service.ServiceFactory;
import org.olat.core.util.i18n.I18nManager;
import org.olat.core.util.threadlog.RequestBasedLogLevelManager;
import org.olat.core.util.threadlog.ThreadLocalLogLevelManager;

/**
* Initial Date:  Apr 28, 2004
  *
* @author Mike Stock
*
* Comment: 
*
*/
public class OLATServlet extends HttpServlet {
  static {
    try{
      // OLAT-5698: work in progress to allow debugging slow requests better
      //TODO: Move this to CoreSpringFactory in 7.0 since in 7.0 CoreSpringFactory is loaded first, while in 6.3.x OlatServlet is loaded first
      //      (and triggers the CoreSpringFactory to load via the log.info call)
      ThreadLocalLogLevelManager.install();
      // OLAT-5698: end of work in progress
    } catch(Exception e) {
      e.printStackTrace();
    }
  }
 
  private static OLog log = Tracing.createLoggerFor(OLATServlet.class);
  private Dispatcher dispatcher;
 
  /**
   * @see javax.servlet.Servlet#init(javax.servlet.ServletConfig)
   */
  public void init(ServletConfig servletConfig) throws ServletException {
    super.init(servletConfig);
    ServletContext servletContext = getServletContext();
   
    // initialize log system. do not make any calls to Logger "log" before log system has been initialized.
    String prefix =  servletContext.getRealPath("/");
    String file = "WEB-INF/log4j.properties";
    PropertyConfigurator.configure(prefix+file);

    // ----- startup OLAT -----
    String moduleID = null;
    try {
      log.info("*** Initializing OLAT servlet");
      ThreadLocalUserActivityLoggerInstaller.initEmptyUserActivityLogger();

      log.info("*** Brasato framework: Preparing Spring and startup");
      // ----- startup brasato -----
      ServiceFactory.init(servletContext);

      log.info("*** OLAT framework: starting all modules");
      // this is the first call to get the Spring beans, all beans are initialized after this point
      ModuleConfig mc = (ModuleConfig)CoreSpringFactory.getBean("org.olat.core.configuration.ModuleConfig");
      Map modules = mc.getModules();
      // init all registered OLATModules - olat::: we assume silently that the order is not important
      for (Iterator iter = modules.keySet().iterator(); iter.hasNext();) {
        moduleID = (String) iter.next();
        ModuleLifeCycle mlc = (ModuleLifeCycle)modules.get(moduleID);
        mlc.init(servletContext);
      }
      // now activate the dispatcher     
      dispatcher = (Dispatcher) ServiceFactory.getService(org.olat.core.dispatcher.Dispatcher.class);
    } catch (Error er) {
      log.error("Module initialization error in module::" + moduleID, er);
      throw er;
    } catch (Exception e) {
      log.error("Module initialization error in module::" + moduleID, e);
      throw new RuntimeException(e);
    } finally {
      ThreadLocalUserActivityLoggerInstaller.resetUserActivityLogger();
    }
  }
 
  /**
   * @see javax.servlet.Servlet#destroy()
   */
  public void destroy() {
    log.info("*** Destroying OLAT servlet.");
   
    //send all modules the destroy signal
    ModuleConfig mc = (ModuleConfig)CoreSpringFactory.getBean("org.olat.core.configuration.ModuleConfig");
    Map modules = mc.getModules();
    for (Iterator iter = modules.keySet().iterator(); iter.hasNext();) {
      String moduleID = (String) iter.next();
      try {
        ModuleLifeCycle mlc = (ModuleLifeCycle)modules.get(moduleID);
        mlc.destroy();
      } catch (Exception e) {
        // log error and continue with with next module
        log.error("Problem while destroying module::" + moduleID, e);
      }
    }
    log.info("*** Destroy CoreSpringFactory");
    //shutting down spring context, these triggers all the beans destroy methods which you can add as bean attribute "destroy-method"
    CoreSpringFactory.destroy();
    log.info("*** Shutting down the logging system - do not use logger after this point!");
    LogManager.shutdown();
  }

  /**
   * Called when the HTTP request method is GET. This method just calls the
   * doPost() method.
   *
   * @param request
   *          The HTTP request
   * @param response
   *          The HTTP response
   * @throws ServletException
   * @throws IOException
   */
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    doPost(request, response);
  }

  /**
   * Called when the HTTP request method is POST. This method provides the main
   * control logic.
   *
   * @param request
   *          The HTTP request
   * @param response
   *          The HTTP response
   * @throws ServletException
   * @throws IOException
   */
  public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // initalize tracing with request, this allows debugging information as IP, User-Agent.
    Tracing.setUreq(request);
    I18nManager.attachI18nInfoToThread(request);
    ThreadLocalUserActivityLoggerInstaller.initUserActivityLogger(request);

    try{
      RequestBasedLogLevelManager.activateRequestBasedLogLevel(request);
   
      dispatcher.execute(request, response, null);
     
    } finally {
      RequestBasedLogLevelManager.deactivateRequestBasedLogLevel();
      ThreadLocalUserActivityLoggerInstaller.resetUserActivityLogger();
      I18nManager.remove18nInfoFromThread();
      Tracing.setUreq(null);
    }
  }

}
TOP

Related Classes of org.olat.core.servlets.OLATServlet

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.