Package org.jibeframework.core

Source Code of org.jibeframework.core.Context

/**
* Copyright (C) 2008 Kodeks d.o.o .
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
* or see <http://www.gnu.org/licenses/>
*
* As a special exception to the terms and conditions of version 2.0 of
* the GPL, you may redistribute this Program in connection with Free/Libre
* and Open Source Software ("FLOSS") applications as described in Jibe
* FLOSS exception.  You should have recieved a copy of the text describing
* the FLOSS exception, and it is also available here:
* http://www.jibeframework.org
*/
package org.jibeframework.core;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jibeframework.core.app.Application;
import org.jibeframework.core.app.conversation.Conversation;
import org.jibeframework.core.app.conversation.ConversationScope;
import org.jibeframework.core.app.method.ArgumentsResolver;
import org.jibeframework.core.util.Content;
import org.jibeframework.core.util.GroovyBeanFactory;
import org.springframework.context.ApplicationContext;

/**
* Instance of Context is data object which is passed through request handling
* lifecycle.
*
* @author dhalupa
*
*/
public class Context {

  private static ThreadLocal<Context> instance = new ThreadLocal<Context>();

  private List<Object> functions = new ArrayList<Object>();
  private HttpServletResponse servletResponse;
  private Map<String, Object> params = new HashMap<String, Object>();
  private Content uploadedContent = null;
  private HttpServletRequest servletRequest;
  private String extensionPath, serviceContextPath;

  private static final Log logger = LogFactory.getLog(Context.class);

  private HttpSession httpSession;

  public Context() {
    instance.set(this);
  }

  public Locale getLocale() {
    if (servletRequest.isRequestedSessionIdValid()) {
      return (Locale) httpSession.getAttribute("locale");
    }
    return null;
  }

  public static Context getCurrentContext() {
    Context context = instance.get();
    return context;
  }

  public final HttpSession getSession() {
    return httpSession;
  }

  public void setHttpSession(HttpSession httpSession) {
    this.httpSession = httpSession;
  }

  public final Conversation getConversation() {
    return ConversationScope.getCurrentConversation();
  }

  public void addAsParameter(Object object) {
    ArgumentsResolver.addToArgsCache(object);
  }

  public void addAsParameter(Class<?> genericType, Class<?>[] actualTypes, Object object) {
    ArgumentsResolver.addToArgsCache(genericType, actualTypes, object);
  }

  public Map<String, Object> getParams() {
    return params;
  }

  public void setParams(Map<String, Object> params) {
    this.params = params;
  }

  public Content getUploadedContent() {
    return uploadedContent;
  }

  public void setUploadedContent(Content uploadedContent) {
    this.uploadedContent = uploadedContent;
  }

  public HttpServletRequest getServletRequest() {
    return servletRequest;
  }

  public void setServletRequest(HttpServletRequest servletRequest) {
    this.servletRequest = servletRequest;
  }

  public HttpServletResponse getServletResponse() {
    return servletResponse;
  }

  public void setServletResponse(HttpServletResponse servletResponse) {
    this.servletResponse = servletResponse;
  }

  public List<Object> getFunctions() {
    return functions;
  }

  public String getExtensionPath() {
    return extensionPath;
  }

  public void setExtensionPath(String extensionPath) {
    this.extensionPath = extensionPath;
  }

  public String getServiceContextPath() {
    return serviceContextPath;
  }

  public void setServiceContextPath(String serviceContextPath) {
    this.serviceContextPath = serviceContextPath;
  }

  public boolean containsViewBean(String name) {
    return GroovyBeanFactory.getInstance().getBeans().contains(name);
  }

  public Object getViewBean(String name) {
    Object bean = null;
    synchronized (getConversation()) {
      Map<String, Object> registry = getViewRegistry();
      if (!registry.containsKey(name)) {
        if (GroovyBeanFactory.getInstance().getBeans().contains(name)) {
          ApplicationContext applicationContext = Application.getApplicationContext();
          registry.put(name, applicationContext.getBean(name));
        }
      }
      bean = registry.get(name);
    }
    return bean;
  }

  public void startModelBuilding(String modelId) {
    logger.debug("Starting model building:" + modelId);
    Conversation conversation = getConversation();
    conversation.setAttribute("__currentModelId", modelId);
    getModelRegistry().put(modelId, new LinkedHashMap<String, Object>());
  }

  public void addModelEntry(String name, Map<String, Object> entry) {
    logger.debug("Adding model entry:" + name);
    getCurrentModel().put(name, entry);
  }

  @SuppressWarnings("unchecked")
  public Map<String, Object> getCurrentModel() {
    String currentModelId = getCurrentModelId();
    Map<String, Object> model = (Map<String, Object>) getModelRegistry().get(currentModelId);
    return model;
  }

  @SuppressWarnings("unchecked")
  public Map<String, Object> getModel(String modelId) {
    Map<String, Object> model = (Map<String, Object>) getModelRegistry().get(modelId);
    return model;
  }

  @SuppressWarnings("unchecked")
  private Map<String, Object> getViewRegistry() {
    Conversation conversation = getConversation();
    Map<String, Object> registry = (Map<String, Object>) conversation.getAttribute("__viewRegistry");
    if (registry == null) {
      registry = new HashMap<String, Object>();
      conversation.setAttribute("__viewRegistry", registry);
    }
    return registry;
  }

  @SuppressWarnings("unchecked")
  private Map<String, Object> getModelRegistry() {
    Conversation conversation = getConversation();
    Map<String, Object> registry = (Map<String, Object>) conversation.getAttribute("__modelRegistry");
    if (registry == null) {
      registry = new HashMap<String, Object>();
      conversation.setAttribute("__modelRegistry", registry);
    }
    return registry;
  }

  public String getCurrentModelId() {
    Conversation conversation = getConversation();
    String modelId = (String) conversation.getAttribute("__currentModelId");
    if (modelId != null) {
      return modelId;
    }
    throw new IllegalStateException("Current model id has not been set in the conversation");
  }

}
TOP

Related Classes of org.jibeframework.core.Context

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.