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;
017:
018: import java.util.Collections;
019: import java.util.HashMap;
020: import java.util.Set;
021:
022: import javax.servlet.ServletContext;
023: import javax.servlet.ServletException;
024: import javax.servlet.http.HttpServletRequest;
025: import javax.servlet.jsp.PageContext;
026:
027: /**
028: * Module support class. There is one instance per servlet
029: * context, which may be retreived via the <code>getInstance()</code>
030: * methods.
031: * An application consists of several modules, each represented
032: * by a context.
033: * The application's module support instance is used to lookup module
034: * contexts by name and request.
035: *
036: * @see de.odysseus.calyxo.base.ModuleContext
037: * @see de.odysseus.calyxo.base.ModuleGroup
038: * @author Christoph Beck
039: */
040: public class ModuleSupport implements ModuleSelector {
041:
042: /**
043: * Application scope key where the module support instance is stored.
044: */
045: static final String MODULE_SUPPORT_KEY = "de.odysseus.calyxo.base.modules";
046:
047: /**
048: * Convenience method.
049: * @see #getInstance(ServletContext)
050: */
051: public static final ModuleSupport getInstance(
052: HttpServletRequest request) {
053: ModuleSupport support = (ModuleSupport) request
054: .getAttribute(MODULE_SUPPORT_KEY);
055: if (support == null) {
056: support = getInstance(request.getSession()
057: .getServletContext());
058: request.setAttribute(MODULE_SUPPORT_KEY, support);
059: }
060: return support;
061: }
062:
063: /**
064: * Convenience method.
065: * @see #getInstance(ServletContext)
066: */
067: public static final ModuleSupport getInstance(
068: PageContext pageContext) {
069: return getInstance((HttpServletRequest) pageContext
070: .getRequest());
071: }
072:
073: /**
074: * Loookup module support instance
075: */
076: public static final ModuleSupport getInstance(ServletContext context) {
077: ModuleSupport result = (ModuleSupport) context
078: .getAttribute(MODULE_SUPPORT_KEY);
079: if (result == null) {
080: context.setAttribute(MODULE_SUPPORT_KEY,
081: result = new ModuleSupport());
082: }
083: return result;
084: }
085:
086: private HashMap modules = new HashMap();
087: private ModuleSelector selector;
088:
089: /**
090: * Constructor.
091: */
092: protected ModuleSupport() {
093: this (new ModuleSelector() {
094: public ModuleContext getModuleContext(
095: HttpServletRequest request) {
096: return null;
097: }
098: });
099: }
100:
101: protected ModuleSupport(ModuleSelector selector) {
102: this .selector = selector;
103: }
104:
105: final ModuleSelector getModuleSelector() {
106: return selector;
107: }
108:
109: final void setModuleSelector(ModuleSelector value) {
110: selector = value;
111: }
112:
113: /**
114: * Get the module context for the given request.
115: */
116: public final ModuleContext getModuleContext(
117: HttpServletRequest request) {
118: return selector.getModuleContext(request);
119: }
120:
121: /**
122: * Convenience method.
123: * @see #getModuleContext(HttpServletRequest)
124: */
125: public final ModuleContext getModuleContext(PageContext pageContext) {
126: return getModuleContext((HttpServletRequest) pageContext
127: .getRequest());
128: }
129:
130: /**
131: * Lookup a module context by name.
132: */
133: public final ModuleContext getModuleContext(String name) {
134: return (ModuleContext) modules.get(name);
135: }
136:
137: /**
138: * Answer <code>true</code> iff there are no modules
139: */
140: public final boolean isEmpty() {
141: return modules.isEmpty();
142: }
143:
144: /**
145: * Answer an unmodifiable set containing the module names
146: */
147: public final Set getModuleNames() {
148: return Collections.unmodifiableSet(modules.keySet());
149: }
150:
151: /**
152: * Add a module context
153: */
154: final void add(ModuleContext context) throws ServletException {
155: String name = context.getName();
156: if (modules.containsKey(name)) {
157: throw new ServletException("Module '" + name
158: + "' alredy exists!");
159: }
160: modules.put(context.getName(), context);
161: }
162:
163: /**
164: * Remove a module context
165: */
166: final void remove(ModuleContext context) {
167: modules.remove(context.getName());
168: }
169: }
|