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.jsp.PageContext;
020:
021: import de.odysseus.calyxo.base.ModuleContext;
022: import de.odysseus.calyxo.base.ModuleSupport;
023:
024: /**
025: * Access support class. There is one instance per module,
026: * which may be retreived via the <code>getInstance()</code>
027: * methods.
028: * This class encapsulates an accessor Map, which may be accessed and
029: * extended during module initialization.
030: *
031: * @author Christoph Beck
032: */
033: public class AccessSupport {
034:
035: /**
036: * Module scope key where the access support instance is stored.
037: */
038: public static final String ACCESS_SUPPORT_KEY = "de.calyxo.base.access";
039:
040: /**
041: * Lookup access support for module determined by specified request
042: */
043: public static final AccessSupport getInstance(
044: HttpServletRequest request) {
045: ModuleContext context = ModuleSupport.getInstance(request)
046: .getModuleContext(request);
047: return (AccessSupport) context.getAttribute(ACCESS_SUPPORT_KEY);
048: }
049:
050: /**
051: * Convenience method.
052: * @see #getInstance(HttpServletRequest)
053: */
054: public static final AccessSupport getInstance(
055: PageContext pageContext) {
056: return getInstance((HttpServletRequest) pageContext
057: .getRequest());
058: }
059:
060: /**
061: * Lookup access support for specified module
062: */
063: public static final AccessSupport getInstance(ModuleContext context) {
064: return (AccessSupport) context.getAttribute(ACCESS_SUPPORT_KEY);
065: }
066:
067: private AccessorMap accessors;
068:
069: /**
070: * Default constructor.
071: * Uses an empty <code>AccessorMap</code>.
072: */
073: public AccessSupport() {
074: this (new AccessorMap());
075: }
076:
077: /**
078: * Constructor.
079: * The passed map may already contain some accessors.
080: *
081: * @param accessors the map of accessors.
082: */
083: public AccessSupport(AccessorMap accessors) {
084: super ();
085:
086: this .accessors = accessors;
087: }
088:
089: /**
090: * Get accessor for specified key
091: */
092: public Accessor get(String key) {
093: return accessors.get(key);
094: }
095:
096: /**
097: * Put accessor for specified key
098: */
099: public void put(String key, Accessor value) {
100: accessors.put(key, value);
101: }
102:
103: /**
104: * Create object (map) from our encapsulated accessor map.
105: */
106: public Object create(HttpServletRequest request) {
107: return accessors.get(request);
108: }
109: }
|