Package de.odysseus.calyxo.base.misc

Source Code of de.odysseus.calyxo.base.misc.CalyxoVariableResolver

/*
* Copyright 2004, 2005, 2006 Odysseus Software GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.odysseus.calyxo.base.misc;

import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.el.ELException;
import javax.servlet.jsp.el.VariableResolver;

import de.odysseus.calyxo.base.ModuleContext;
import de.odysseus.calyxo.base.ModuleSupport;
import de.odysseus.calyxo.base.util.MapFacade;

/**
* This variable resolver supports the following implicite objects:
* <ul>
* <li>requestScope</li>
* <li>sessionScope</li>
* <li>moduleScope</li>
* <li>applicationScope</li>
* <li>param</li>
* <li>moduleContext</li>
* </ul>
* This variable resolver searches scopes request, session,
* module and application (in that order).
*
* @author Christoph Beck
*/
public class CalyxoVariableResolver implements VariableResolver {
  protected final HttpServletRequest request;
  protected final ModuleContext context;

  private Map requestScope, sessionScope, moduleScope, applicationScope;
  private Map param;
 

  /**
   * Constructor.
   */
  public CalyxoVariableResolver(HttpServletRequest request) {
    this(request, ModuleSupport.getInstance(request).getModuleContext(request));
  }

  /**
   * Constructor.
   */
  public CalyxoVariableResolver(HttpServletRequest request, ModuleContext context) {
    super();

    this.request = request;
    this.context = context;
  }

  /**
   * Search request, session, module, application scope for specified variable.
   */
  protected Object findVariable(String variable) throws ELException {
    Object result = request.getAttribute(variable);
    if (result == null) {
      HttpSession session = request.getSession(false);
      if (session != null) {
        result = session.getAttribute(variable);
      }
      if (result == null) {
        result = context.getAttribute(variable);
        if (result == null) {
          result = context.getServletContext().getAttribute(variable);
        }
      }
    }
    return result;
  }

  /* (non-Javadoc)
   * @see javax.servlet.jsp.el.VariableResolver#resolveVariable(java.lang.String)
   */
  public Object resolveVariable(String variable) throws ELException {
    if ("requestScope".equals(variable)) {
      return getRequestScope();
    } else if ("sessionScope".equals(variable)) {
      return getSessionScope();
    } else if ("moduleScope".equals(variable)) {
      return getModuleScope();
    } else if ("applicationScope".equals(variable)) {
      return getApplicationScope();
    } else if ("param".equals(variable)) {
      return getParam();
    } else if ("moduleScope".equals(variable)) {
      return context;
    }
    return findVariable(variable);
  }

  private Map getApplicationScope() {
    if (applicationScope == null) {
      applicationScope = new MapFacade() {
        ServletContext servletContext = context.getServletContext();
        public Object get(Object key) {
          if (key == null) {
            return null;
          }
          return servletContext.getAttribute(key.toString());
        }
      };
    }
    return applicationScope;
  }

  private Map getModuleScope() {
    if (moduleScope == null) {
      moduleScope = new MapFacade() {
        public Object get(Object key) {
          if (key == null) {
            return null;
          }
          return context.getAttribute(key.toString());
        }
      };
    }
    return moduleScope;
  }

  private Map getRequestScope() {
    if (requestScope == null) {
      requestScope = new MapFacade() {
        public Object get(Object key) {
          if (key == null) {
            return null;
          }
          return request.getAttribute(key.toString());
        }
      };
    }
    return requestScope;
  }

  private Map getSessionScope() {
    if (sessionScope == null) {
      sessionScope = new MapFacade() {
        HttpSession session = request.getSession(false);
        public Object get(Object key) {
          if (key == null) {
            return null;
          }
          return session == null ? null : session.getAttribute(key.toString());
        }
      };
    }
    return sessionScope;
  }

  private Map getParam() {
    if (param == null) {
      param = new MapFacade() {
        public Object get(Object key) {
          if (key == null) {
            return null;
          }
          return request.getParameter(key.toString());
        }
      };
    }
    return param;
  }

}
TOP

Related Classes of de.odysseus.calyxo.base.misc.CalyxoVariableResolver

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.