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.webapp.WebApp;
033: import com.caucho.webbeans.component.ComponentImpl;
034:
035: import java.util.*;
036:
037: import javax.servlet.*;
038: import javax.servlet.http.*;
039:
040: /**
041: * The application scope value
042: */
043: public class DependentScope {
044: private static final ThreadLocal<DependentScope> _threadScope = new ThreadLocal<DependentScope>();
045:
046: private ComponentImpl _owner;
047: private Object _value;
048: private ScopeContext _scope;
049:
050: private IdentityHashMap<ComponentImpl, Object> _map;
051:
052: public DependentScope() {
053: }
054:
055: public DependentScope(ComponentImpl owner, Object value,
056: ScopeContext scope) {
057: _owner = owner;
058: _value = value;
059:
060: _scope = scope;
061: }
062:
063: public DependentScope(ScopeContext scope) {
064: _scope = scope;
065: }
066:
067: /**
068: * Returns the current dependent scope.
069: */
070: public static DependentScope getCurrent() {
071: return _threadScope.get();
072: }
073:
074: /**
075: * Begins a new instanceof the dependent scope
076: */
077: public static DependentScope begin(ScopeContext ownerScope) {
078: throw new UnsupportedOperationException();
079:
080: //DependentScope scope = new DependentScope(ownerScope);
081:
082: //_threadScope.set(scope);
083:
084: //return scope;
085: }
086:
087: /**
088: * Closes the scope
089: */
090: public static void end(DependentScope oldScope) {
091: _threadScope.set(oldScope);
092: }
093:
094: /**
095: * Returns the object with the given name.
096: */
097: public Object get(ComponentImpl comp) {
098: if (comp == _owner)
099: return _value;
100: else if (_map != null)
101: return _map.get(comp);
102: else
103: return null;
104: }
105:
106: /**
107: * Sets the object with the given name.
108: */
109: public void put(ComponentImpl comp, Object value) {
110: if (_map == null)
111: _map = new IdentityHashMap<ComponentImpl, Object>(8);
112:
113: _map.put(comp, value);
114: }
115:
116: public boolean canInject(ScopeContext scope) {
117: if (scope == null)
118: return true;
119: else if (_scope == null)
120: return scope instanceof SingletonScope;
121: else
122: return _scope.canInject(scope);
123: }
124:
125: /**
126: * Adds a @PreDestroy destructor
127: */
128: public void addDestructor(ComponentImpl comp, Object value) {
129: if (_scope != null)
130: _scope.addDestructor(comp, value);
131: else {
132: // add to env?
133: }
134: }
135:
136: public String toString() {
137: return getClass().getSimpleName() + "[" + _owner + "," + _scope
138: + "]";
139: }
140: }
|