1 18 package org.apache.beehive.netui.pageflow.internal; 19 20 import org.apache.beehive.netui.util.logging.Logger; 21 import org.apache.beehive.netui.util.config.ConfigUtil; 22 import org.apache.beehive.netui.util.config.bean.PageflowConfig; 23 import org.apache.beehive.netui.pageflow.PageFlowActionServlet; 24 import org.apache.beehive.netui.pageflow.PageFlowContextListener; 25 import org.apache.beehive.netui.pageflow.PageFlowConstants; 26 27 import javax.servlet.ServletContext ; 28 import java.io.Serializable ; 29 30 31 32 public class LegacySettings 33 implements Serializable , PageFlowConstants 34 { 35 private static final Logger _log = Logger.getInstance( LegacySettings.class ); 36 37 private static final String CONTEXT_ATTR = InternalConstants.ATTR_PREFIX + "_cache"; 38 private static final int DEFAULT_MAX_FORWARDS_PER_REQUEST = 25; 39 private static final int DEFAULT_MAX_NESTING_STACK_DEPTH = 10; 40 41 public static final int serialVersionUID = 1; 42 43 private boolean _secureForwards = false; 44 private int _forwardOverflowCount; 45 private int _nestingOverflowCount; 46 47 48 public static LegacySettings get( ServletContext servletContext ) 49 { 50 LegacySettings ls = ( LegacySettings ) servletContext.getAttribute( CONTEXT_ATTR ); 51 52 if ( ls == null ) 53 { 54 if ( _log.isErrorEnabled() ) 55 { 56 _log.error( "Page Flow ServletContext cache not initialized; either " 57 + PageFlowActionServlet.class.getName() + " must be the Struts action servlet, or " 58 + PageFlowContextListener.class.getName() + " must be registered as a listener in web.xml." ); 59 } 60 61 return init( servletContext ); 66 } 67 68 return ls; 69 } 70 71 public static LegacySettings init( ServletContext servletContext ) 72 { 73 assert servletContext.getAttribute( CONTEXT_ATTR ) == null : LegacySettings.class.getName() + " already initialized."; 74 LegacySettings cache = new LegacySettings( servletContext ); 75 servletContext.setAttribute( CONTEXT_ATTR, cache ); 76 return cache; 77 } 78 79 private void loadLegacySettings( ServletContext servletContext ) 80 { 81 PageflowConfig pageflowConfig = ConfigUtil.getConfig().getPageflowConfig(); 82 if ( pageflowConfig == null ) pageflowConfig = ConfigUtil.getConfig().addNewPageflowConfig(); 84 Integer forwardOverflowCount = 85 loadLegacyParam( FORWARD_OVERFLOW_COUNT_PARAM, servletContext, "max-forwards-per-request" ); 86 if ( forwardOverflowCount != null ) 87 { 88 _forwardOverflowCount = forwardOverflowCount.intValue(); 89 } 90 else 91 { 92 _forwardOverflowCount = pageflowConfig.isSetMaxForwardsPerRequest() 94 ? pageflowConfig.getMaxForwardsPerRequest() 95 : DEFAULT_MAX_FORWARDS_PER_REQUEST; 96 } 97 98 Integer nestingOverflowCount = 99 loadLegacyParam( NESTING_OVERFLOW_COUNT_PARAM, servletContext, "max-nesting-stack-depth" ); 100 if ( nestingOverflowCount != null ) 101 { 102 _nestingOverflowCount = nestingOverflowCount.intValue(); 103 } 104 else 105 { 106 _nestingOverflowCount = pageflowConfig.isSetMaxNestingStackDepth() 108 ? pageflowConfig.getMaxNestingStackDepth() 109 : DEFAULT_MAX_NESTING_STACK_DEPTH; 110 } 111 112 String doSecureForwards = servletContext.getInitParameter( SECURE_FORWARDS_PARAM ); 113 114 if ( doSecureForwards != null ) 115 { 116 _log.warn( "Servlet context-param " + SECURE_FORWARDS_PARAM + 117 " is deprecated; use the ensure-secure-forwards element within pageflow-config in " 118 + InternalConstants.NETUI_CONFIG_PATH ); 119 _secureForwards = Boolean.valueOf( doSecureForwards ).booleanValue(); 120 } 121 else 122 { 123 _secureForwards = pageflowConfig.getEnsureSecureForwards(); 124 } 125 126 127 } 128 private LegacySettings( ServletContext servletContext ) 129 { 130 loadLegacySettings( servletContext ); 135 136 } 137 138 public boolean shouldDoSecureForwards() 139 { 140 return _secureForwards; 141 } 142 143 public int getForwardOverflowCount() 144 { 145 return _forwardOverflowCount; 146 } 147 148 public int getNestingOverflowCount() 149 { 150 return _nestingOverflowCount; 151 } 152 153 private static Integer loadLegacyParam( String paramName, ServletContext servletContext, String configElementName ) 154 { 155 String strVal = servletContext.getInitParameter( paramName ); 156 157 if ( strVal != null ) 158 { 159 _log.warn( "Servlet context-param " + paramName + "is deprecated; use the " + configElementName 160 + " element within pageflow-config in " + InternalConstants.NETUI_CONFIG_PATH ); 161 162 try 163 { 164 return Integer.valueOf( strVal ); 165 } 166 catch ( NumberFormatException e ) 167 { 168 _log.error( "Could not parse integer value from context-param " + paramName + '.' ); 169 } 170 } 171 172 return null; 173 } 174 } 175 | Popular Tags |