Package de.odysseus.calyxo.struts.base

Source Code of de.odysseus.calyxo.struts.base.StrutsModuleContext

/*
* 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.struts.base;

import javax.servlet.ServletContext;
import javax.servlet.jsp.el.ExpressionEvaluator;

import org.apache.commons.el.ExpressionEvaluatorImpl;
import org.apache.struts.Globals;
import org.apache.struts.action.ActionServlet;
import org.apache.struts.config.ModuleConfig;
import org.apache.struts.util.RequestUtils;

import de.odysseus.calyxo.base.ModuleContext;
import de.odysseus.calyxo.base.conf.ConfigException;

/**
*
* @author Christoph Beck
*/
public class StrutsModuleContext implements ModuleContext {
  private final ExpressionEvaluator evaluator = new ExpressionEvaluatorImpl();
  private final ActionServlet servlet;
  private final String name;
  private final String prefix;
  private final String path;
  private final String extension;

  private ClassLoader loader;

 
  /**
   * Constructor
   */
  public StrutsModuleContext(ActionServlet servlet, ModuleConfig config, String name) {   
    this.servlet = servlet;
    this.prefix = config.getPrefix();
    this.name = name;

    String mapping = (String)servlet.getServletContext().getAttribute(Globals.SERVLET_KEY);
    if (mapping != null && mapping.startsWith("*.")) {
      this.path = prefix;
      this.extension = mapping.substring(1);
    } else if (mapping != null && mapping.endsWith("/*")) {
      this.path = prefix + mapping.substring(0, mapping.length() - 2);
      this.extension = null;
    } else {
      this.path = prefix;
      this.extension = null;
    }
  }

  /* (non-Javadoc)
   * @see de.odysseus.calyxo.base.ModuleContext#getName()
   */
  public String getName() {
    return name;
  }

  /**
   * Get the struts module prefix
   */
  public String getPrefix() {
    return prefix;
  }
 
  /* (non-Javadoc)
   * @see de.odysseus.calyxo.base.ModuleContext#getServletContext()
   */
  public ServletContext getServletContext() {
    return servlet.getServletContext();
  }

  /* (non-Javadoc)
   * @see de.odysseus.calyxo.base.ModuleContext#getPath(java.lang.String)
   */
  public String getPath(String action) {
    StringBuffer s = new StringBuffer();
    s.append(path);
    if (!action.startsWith("/")) {
      s.append("/");
    }
    if (extension != null) {
      int query = action.indexOf("?");
      if (query > 0) {
        // insert extension before query string
        s.append(action.substring(0, query));
        s.append(extension);
        s.append(action.substring(query));
      } else {
        int anchor = action.indexOf("#");
        if (anchor > 0) {
          // insert extension before anchor
          s.append(action.substring(0, anchor));         
          s.append(extension);
          s.append(action.substring(anchor));
        } else {
          s.append(action);
          s.append(extension);
        }
      }
    } else {
      s.append(action);
    }
    return s.toString();
  }

  /* (non-Javadoc)
   * @see de.odysseus.calyxo.base.ModuleContext#getAttribute(java.lang.String)
   */
  public Object getAttribute(String name) {
    return servlet.getServletContext().getAttribute(name + prefix);
  }

  /* (non-Javadoc)
   * @see de.odysseus.calyxo.base.ModuleContext#setAttribute(java.lang.String, java.lang.Object)
   */
  public void setAttribute(String name, Object value) {
    servlet.getServletContext().setAttribute(name + prefix, value);
  }

  /* (non-Javadoc)
   * @see de.odysseus.calyxo.base.ModuleContext#removeAttribute(java.lang.String)
   */
  public void removeAttribute(String name) {
    servlet.getServletContext().removeAttribute(name + prefix);
  }
 
  /* (non-Javadoc)
   * @see de.odysseus.calyxo.base.ModuleContext#getInitParameter(java.lang.String)
   */
  public String getInitParameter(String name) {
    return servlet.getServletConfig().getInitParameter(name);
  }

  /**
   * If the class loader has not been explicitely set via <code>setClassLoader()</code>,
   * a class loader instance is computed as in Struts: first choice is
   * <code>Thread.currentThread().getContextClassLoader()</code>;
   * if this is <code>null</code>, answer <code>RequestUtils.class.getClassLoader()</code>.
   * @see de.odysseus.calyxo.base.ModuleContext#getClassLoader()
   */
  public ClassLoader getClassLoader() {
    if (loader == null) {
          ClassLoader result = Thread.currentThread().getContextClassLoader();
          if (result == null) {
            result = RequestUtils.class.getClassLoader();
          }
          return result;
    }
    return loader;
  }

  /**
   * Set module class loader.
   * The class loader may be set once via this method. Invoking this method after
   * a class loader has already been set will throw an exception.
   * @param loader
   * @throws ConfigException
   */
  public void setClassLoader(ClassLoader loader) throws ConfigException {
    if (this.loader != null) {
      throw new ConfigException("Module class loader must not be set more than once!");
    }
    this.loader = loader;
  }

  /*
   * (non-Javadoc)
   * @see de.odysseus.calyxo.base.ModuleContext#getExpressionEvaluator()
   */
  public ExpressionEvaluator getExpressionEvaluator() {
    return evaluator;
  }
}
TOP

Related Classes of de.odysseus.calyxo.struts.base.StrutsModuleContext

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.