/* * The contents of this file are subject to the Mozilla Public License Version 1.1 * (the "License"); you may not use this file except in compliance with the License. * You may obtain a copy of the License at . * * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT * WARRANTY OF ANY KIND, either express or implied. See the License for the specific * language governing rights and limitations under the License. * * The Original Code is the Venice Web Communities System. * * The Initial Developer of the Original Code is Eric J. Bowersox , * for Silverwrist Design Studios. Portions created by Eric J. Bowersox are * Copyright (C) 2001 Eric J. Bowersox/Silverwrist Design Studios. All Rights Reserved. * * Contributor(s): */ package com.silverwrist.venice.servlets; import java.io.*; import java.lang.ref.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; import org.apache.log4j.*; import com.silverwrist.venice.core.*; import com.silverwrist.venice.servlets.format.*; import com.silverwrist.venice.servlets.format.menus.LeftMenu; public class Variables { /*-------------------------------------------------------------------------------- * Static data values *-------------------------------------------------------------------------------- */ // ServletContext ("application") attributes protected static final String ENGINE_ATTRIBUTE = "com.silverwrist.venice.core.Engine"; protected static final String COUNTRYLIST_ATTRIBUTE = "com.silverwrist.venice.db.CountryList"; protected static final String LANGUAGELIST_ATTRIBUTE = "com.silverwrist.venice.db.LanguageList"; protected static final String STYLESHEET_ATTRIBUTE = "com.silverwrist.venice.rendering.StyleSheet"; // HttpSession ("session") attributes protected static final String USERCTXT_ATTRIBUTE = "user.context"; protected static final String MENU_ATTRIBUTE = "current.menu"; // ServletRequest ("request" attributes) protected static final String COOKIEJAR_ATTRIBUTE = "com.silverwrist.venice.servlets.CookieJar"; // Servlet initialization parameters protected static final String ENGINE_INIT_PARAM = "venice.config"; // Cookie name public static final String LOGIN_COOKIE = "VeniceAuth"; private static Category logger = Category.getInstance(Variables.class); private static Integer engine_gate = new Integer(0); /*-------------------------------------------------------------------------------- * External static operations *-------------------------------------------------------------------------------- */ public static VeniceEngine getVeniceEngine(ServletContext ctxt) throws ServletException { synchronized (engine_gate) { // we must synchronize efforts to access the engine Object foo = ctxt.getAttribute(ENGINE_ATTRIBUTE); if (foo!=null) return (VeniceEngine)foo; String root_file_path = ctxt.getRealPath("/"); if (!(root_file_path.endsWith("/"))) root_file_path += "/"; String cfgfile = ctxt.getInitParameter(ENGINE_INIT_PARAM); // get the config file name if (!(cfgfile.startsWith("/"))) cfgfile = root_file_path + cfgfile; logger.info("Initializing Venice engine using config file: " + cfgfile); try { // extract the configuration file name and create the engine VeniceEngine engine = Startup.createEngine(cfgfile); ctxt.setAttribute(ENGINE_ATTRIBUTE,engine); return engine; } // end try catch (ConfigException e) { // configuration failed! post an error message logger.fatal("Engine configuration failed: " + e.getMessage(),e); throw new ServletException("Venice engine configuration failed: " + e.getMessage(),e); } // end catch catch (DataException e2) { // configuration failed! post an error message logger.fatal("Engine data load failed: " + e2.getMessage(),e2); throw new ServletException("Venice engine data load failed: " + e2.getMessage(),e2); } // end catch } // end synchronized block } // end getVeniceEngine public static UserContext getUserContext(ServletContext ctxt, HttpServletRequest request, HttpSession session) throws ServletException { Object uctmp = session.getAttribute(USERCTXT_ATTRIBUTE); if (uctmp!=null) return (UserContext)uctmp; try { // use the Venice engine to create a new user context and save it off VeniceEngine engine = getVeniceEngine(ctxt); UserContext user = engine.createUserContext(request.getRemoteAddr()); // Did the user send a Venice authentication cookie? If so, try to use it. Cookie[] cookies = request.getCookies(); Cookie venice_cookie = null; for (int i=0; (venice_cookie==null) && (i