001: /*
002: * Copyright 2004, 2005, 2006 Odysseus Software GmbH
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package de.odysseus.calyxo.base.access;
017:
018: import javax.servlet.http.HttpServletRequest;
019: import javax.servlet.http.HttpSession;
020:
021: import de.odysseus.calyxo.base.ModuleContext;
022:
023: /**
024: * This accessor serves an attribute value.
025: *
026: * @author Christoph Beck
027: */
028: public class AttributeAccessor implements Accessor {
029:
030: /**
031: * Request attribute scope
032: */
033: public static final int REQUEST_SCOPE = 1;
034:
035: /**
036: * Session attribute scope
037: */
038: public static final int SESSION_SCOPE = 2;
039:
040: /**
041: * Module attribute scope
042: */
043: public static final int MODULE_SCOPE = 4;
044:
045: /**
046: * Application attribute scope
047: */
048: public static final int APPLICATION_SCOPE = 8;
049:
050: /**
051: * Search all scopes
052: */
053: public static final int ALL_SCOPES = 15;
054:
055: private int scope;
056: private String name;
057: private boolean cachable;
058: private ModuleContext context;
059:
060: /**
061: * Constructor.
062: * @param context The module context we're in
063: * @param name attribute name
064: * @param scope attribute scope
065: * @param cachable true iff attribute value cannot change within request
066: */
067: public AttributeAccessor(ModuleContext context, String name,
068: int scope, boolean cachable) {
069: this .context = context;
070: this .scope = scope;
071: this .name = name;
072: this .cachable = cachable;
073: }
074:
075: /**
076: * Constructor (not cachable).
077: * @param context The module context we're in
078: * @param name attribute name
079: * @param scope attribute scope
080: */
081: public AttributeAccessor(ModuleContext context, String name,
082: int scope) {
083: this (context, name, scope, false);
084: }
085:
086: /**
087: * Constructor (search all scopes).
088: * @param context The module context we're in
089: * @param name attribute name
090: * @param cachable true iff attribute value cannot change within request
091: */
092: public AttributeAccessor(ModuleContext context, String name,
093: boolean cachable) {
094: this (context, name, ALL_SCOPES, cachable);
095: }
096:
097: /**
098: * Constructor (not cachable, search all scopes).
099: * @param context The module context we're in
100: * @param name attribute name
101: */
102: public AttributeAccessor(ModuleContext context, String name) {
103: this (context, name, false);
104: }
105:
106: /**
107: * Get attribute value according to name and scope settings;
108: */
109: public Object get(HttpServletRequest request) {
110: HttpSession session = null;
111: switch (scope) {
112: case REQUEST_SCOPE:
113: return request.getAttribute(name);
114:
115: case SESSION_SCOPE:
116: session = request.getSession(false);
117: if (session == null) {
118: return null;
119: }
120: return session.getAttribute(name);
121:
122: case MODULE_SCOPE:
123: return context.getAttribute(name);
124:
125: case APPLICATION_SCOPE:
126: return context.getServletContext().getAttribute(name);
127:
128: case ALL_SCOPES:
129: Object result = request.getAttribute(name);
130: if (result != null) {
131: return result;
132: }
133: session = request.getSession(false);
134: result = session.getAttribute(name);
135: if (result != null) {
136: return result;
137: }
138: result = context.getAttribute(name);
139: if (result != null) {
140: return result;
141: }
142: return context.getServletContext().getAttribute(name);
143: }
144: return null;
145: }
146:
147: /**
148: * Get cachable
149: */
150: public boolean isCacheable() {
151: return cachable;
152: }
153: }
|