KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > pageflow > internal > LegacySettings


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * $Header:$
17  */

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 JavaDoc;
28 import java.io.Serializable JavaDoc;
29
30
31
32 public class LegacySettings
33         implements Serializable JavaDoc, PageFlowConstants
34 {
35     private static final Logger _log = Logger.getInstance( LegacySettings.class );
36     
37     private static final String JavaDoc 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 JavaDoc 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             //
62
// We can initialize it now, but it's not good because many requests could conceivably be in this
63
// code at the same time.
64
//
65
return init( servletContext );
66         }
67         
68         return ls;
69     }
70     
71     public static LegacySettings init( ServletContext JavaDoc 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 JavaDoc servletContext )
80     {
81         PageflowConfig pageflowConfig = ConfigUtil.getConfig().getPageflowConfig();
82         if ( pageflowConfig == null ) pageflowConfig = ConfigUtil.getConfig().addNewPageflowConfig(); // for defaults
83

84         Integer JavaDoc 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             // Why can't we read the default value from the XmlObjext?
93
_forwardOverflowCount = pageflowConfig.isSetMaxForwardsPerRequest()
94                                     ? pageflowConfig.getMaxForwardsPerRequest()
95                                     : DEFAULT_MAX_FORWARDS_PER_REQUEST;
96         }
97         
98         Integer JavaDoc 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             // Why can't we read the default value from the XmlObjext?
107
_nestingOverflowCount = pageflowConfig.isSetMaxNestingStackDepth()
108                                     ? pageflowConfig.getMaxNestingStackDepth()
109                                     : DEFAULT_MAX_NESTING_STACK_DEPTH;
110         }
111         
112         String JavaDoc 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 JavaDoc servletContext )
129     {
130         //
131
// Try loading some settings (max-forwards-per-requst, max-nesting-stack-depth, ensure-secure-forwards) from
132
// the deprecated locations first, then fall back to netui-config.xml.
133
//
134
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 JavaDoc loadLegacyParam( String JavaDoc paramName, ServletContext JavaDoc servletContext, String JavaDoc configElementName )
154     {
155         String JavaDoc 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 JavaDoc 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