001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.webbeans.context;
031:
032: import com.caucho.server.dispatch.ServletInvocation;
033: import com.caucho.webbeans.component.ComponentImpl;
034:
035: import javax.servlet.*;
036: import javax.webbeans.*;
037:
038: /**
039: * Configuration for the xml web bean component.
040: */
041: public class RequestScope extends ScopeContext {
042: public <T> T get(ComponentFactory<T> component, boolean create) {
043: ServletRequest request = ServletInvocation.getContextRequest();
044:
045: if (request != null) {
046: ComponentImpl comp = (ComponentImpl) component;
047:
048: Object value = request.getAttribute(comp.getScopeId());
049:
050: if (value == null && create) {
051: value = component.create();
052:
053: request.setAttribute(comp.getScopeId(), value);
054: }
055:
056: return (T) value;
057: } else
058: return null;
059: }
060:
061: public <T> void put(ComponentFactory<T> component, T value) {
062: ServletRequest request = ServletInvocation.getContextRequest();
063:
064: if (request != null) {
065: ComponentImpl comp = (ComponentImpl) component;
066:
067: request.setAttribute(comp.getScopeId(), value);
068: }
069: }
070:
071: /**
072: * Removes the scope value for the given component.
073: */
074: public <T> void remove(ComponentFactory<T> component) {
075: ServletRequest request = ServletInvocation.getContextRequest();
076:
077: if (request != null) {
078: ComponentImpl comp = (ComponentImpl) component;
079:
080: request.removeAttribute(comp.getScopeId());
081: }
082: }
083:
084: @Override
085: public boolean canInject(ScopeContext scope) {
086: return (scope instanceof SingletonScope
087: || scope instanceof ApplicationScope
088: || scope instanceof SessionScope
089: || scope instanceof ConversationScope || scope instanceof RequestScope);
090: }
091:
092: public void addDestructor(ComponentImpl comp, Object value) {
093: ServletRequest request = ServletInvocation.getContextRequest();
094:
095: if (request != null) {
096: DestructionListener listener = (DestructionListener) request
097: .getAttribute("caucho.destroy");
098:
099: if (listener == null) {
100: listener = new DestructionListener();
101: request.setAttribute("caucho.destroy", listener);
102: }
103:
104: listener.addValue(comp, value);
105: }
106: }
107: }
|