001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: * $Header:$
018: */
019: package org.apache.beehive.netui.pageflow.internal;
020:
021: import org.apache.beehive.netui.util.logging.Logger;
022: import org.apache.beehive.netui.util.config.ConfigUtil;
023: import org.apache.beehive.netui.util.config.bean.PageFlowConfig;
024: import org.apache.beehive.netui.pageflow.PageFlowActionServlet;
025: import org.apache.beehive.netui.pageflow.PageFlowContextListener;
026: import org.apache.beehive.netui.pageflow.PageFlowConstants;
027:
028: import javax.servlet.ServletContext;
029: import java.io.Serializable;
030:
031: public class LegacySettings implements Serializable, PageFlowConstants {
032: private static final Logger _log = Logger
033: .getInstance(LegacySettings.class);
034:
035: private static final String CONTEXT_ATTR = InternalConstants.ATTR_PREFIX
036: + "_cache";
037:
038: public static final int serialVersionUID = 1;
039:
040: private boolean _secureForwards = false;
041: private int _forwardOverflowCount;
042: private int _nestingOverflowCount;
043:
044: public static LegacySettings get(ServletContext servletContext) {
045: LegacySettings ls = (LegacySettings) servletContext
046: .getAttribute(CONTEXT_ATTR);
047:
048: if (ls == null) {
049: if (_log.isErrorEnabled()) {
050: _log
051: .error("Page Flow ServletContext cache not initialized; either "
052: + PageFlowActionServlet.class.getName()
053: + " must be the Struts action servlet, or "
054: + PageFlowContextListener.class
055: .getName()
056: + " must be registered as a listener in web.xml.");
057: }
058:
059: //
060: // We can initialize it now, but it's not good because many requests could conceivably be in this
061: // code at the same time.
062: //
063: return init(servletContext);
064: }
065:
066: return ls;
067: }
068:
069: public static LegacySettings init(ServletContext servletContext) {
070: LegacySettings cache = new LegacySettings(servletContext);
071: servletContext.setAttribute(CONTEXT_ATTR, cache);
072: return cache;
073: }
074:
075: private void loadLegacySettings(ServletContext servletContext) {
076: PageFlowConfig pageflowConfig = ConfigUtil.getConfig()
077: .getPageFlowConfig();
078: assert pageflowConfig != null : "Received an invalid PageFlowConfig object";
079:
080: Integer forwardOverflowCount = loadLegacyParam(
081: FORWARD_OVERFLOW_COUNT_PARAM, servletContext,
082: "max-forwards-per-request");
083: if (forwardOverflowCount != null) {
084: _forwardOverflowCount = forwardOverflowCount.intValue();
085: } else {
086: // Why can't we read the default value from the XmlObjext?
087: _forwardOverflowCount = pageflowConfig
088: .getMaxForwardsPerRequest();
089: }
090:
091: Integer nestingOverflowCount = loadLegacyParam(
092: NESTING_OVERFLOW_COUNT_PARAM, servletContext,
093: "max-nesting-stack-depth");
094: if (nestingOverflowCount != null) {
095: _nestingOverflowCount = nestingOverflowCount.intValue();
096: } else {
097: // Why can't we read the default value from the XmlObjext?
098: _nestingOverflowCount = pageflowConfig
099: .getMaxNestingStackDepth();
100: }
101:
102: String doSecureForwards = servletContext
103: .getInitParameter(SECURE_FORWARDS_PARAM);
104:
105: if (doSecureForwards != null) {
106: _log
107: .warn("Servlet context-param "
108: + SECURE_FORWARDS_PARAM
109: + " is deprecated; use the ensure-secure-forwards element within pageflow-config in "
110: + InternalConstants.NETUI_CONFIG_PATH);
111: _secureForwards = Boolean.valueOf(doSecureForwards)
112: .booleanValue();
113: } else {
114: _secureForwards = pageflowConfig.isEnsureSecureForwards();
115: }
116:
117: }
118:
119: private LegacySettings(ServletContext servletContext) {
120: //
121: // Try loading some settings (max-forwards-per-requst, max-nesting-stack-depth, ensure-secure-forwards) from
122: // the deprecated locations first, then fall back to netui-config.xml.
123: //
124: loadLegacySettings(servletContext);
125:
126: }
127:
128: public boolean shouldDoSecureForwards() {
129: return _secureForwards;
130: }
131:
132: public int getForwardOverflowCount() {
133: return _forwardOverflowCount;
134: }
135:
136: public int getNestingOverflowCount() {
137: return _nestingOverflowCount;
138: }
139:
140: private static Integer loadLegacyParam(String paramName,
141: ServletContext servletContext, String configElementName) {
142: String strVal = servletContext.getInitParameter(paramName);
143:
144: if (strVal != null) {
145: _log.warn("Servlet context-param " + paramName
146: + "is deprecated; use the " + configElementName
147: + " element within pageflow-config in "
148: + InternalConstants.NETUI_CONFIG_PATH);
149:
150: try {
151: return Integer.valueOf(strVal);
152: } catch (NumberFormatException e) {
153: _log
154: .error("Could not parse integer value from context-param "
155: + paramName + '.');
156: }
157: }
158:
159: return null;
160: }
161: }
|