You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openwebbeans.apache.org by st...@apache.org on 2015/05/15 12:13:26 UTC
svn commit: r1679531 - in /openwebbeans/trunk:
webbeans-impl/src/main/java/org/apache/webbeans/config/
webbeans-impl/src/main/resources/META-INF/openwebbeans/
webbeans-web/src/main/java/org/apache/webbeans/web/context/
Author: struberg
Date: Fri May 15 10:13:25 2015
New Revision: 1679531
URL: http://svn.apache.org/r1679531
Log:
OWB-1070 implement configurable eager session initalisation behaviour
Modified:
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/config/OpenWebBeansConfiguration.java
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/config/PropertyLoader.java
openwebbeans/trunk/webbeans-impl/src/main/resources/META-INF/openwebbeans/openwebbeans.properties
openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/web/context/WebContextsService.java
Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/config/OpenWebBeansConfiguration.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/config/OpenWebBeansConfiguration.java?rev=1679531&r1=1679530&r2=1679531&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/config/OpenWebBeansConfiguration.java (original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/config/OpenWebBeansConfiguration.java Fri May 15 10:13:25 2015
@@ -130,6 +130,21 @@ public class OpenWebBeansConfiguration
*/
public static final String IGNORED_INTERFACES = "org.apache.webbeans.ignoredDecoratorInterfaces";
+ /**
+ * By default we do _not_ force session creation in our WebBeansConfigurationListener. We only create the
+ * Session if we really need the SessionContext. E.g. when we create a Contextual Instance in it.
+ * Sometimes this creates a problem as the HttpSession can only be created BEFORE anything got written back
+ * to the client.
+ * With this configuration you can choose between 3 settings
+ * <ul>
+ * <li>"true" the Session will <u>always</u> eagerly be created at the begin of a request</li>
+ * <li>"false" the Session will <u>never</u> eagerly be created but only lazily when the first @SessionScoped bean gets used</li>
+ * <li>any other value will be interpreted as Java regular expression for request URIs which need eager Session initialization</li>
+ * </ul>
+ */
+ public static final String EAGER_SESSION_INITIALISATION = "org.apache.webbeans.web.eagerSessionInitialisation";
+
+
private Set<String> ignoredInterfaces;
/**
Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/config/PropertyLoader.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/config/PropertyLoader.java?rev=1679531&r1=1679530&r2=1679531&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/config/PropertyLoader.java (original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/config/PropertyLoader.java Fri May 15 10:13:25 2015
@@ -210,14 +210,7 @@ public class PropertyLoader
Properties mergedProperties = new Properties();
for (Properties p : sortedProperties)
{
- for (Map.Entry<?,?> entry : p.entrySet())
- {
- String key = (String) entry.getKey();
- String value = (String) entry.getValue();
-
- // simply overwrite the old properties with the new ones.
- mergedProperties.setProperty(key, value);
- }
+ mergedProperties.putAll(p);
}
return mergedProperties;
Modified: openwebbeans/trunk/webbeans-impl/src/main/resources/META-INF/openwebbeans/openwebbeans.properties
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/resources/META-INF/openwebbeans/openwebbeans.properties?rev=1679531&r1=1679530&r2=1679531&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/resources/META-INF/openwebbeans/openwebbeans.properties (original)
+++ openwebbeans/trunk/webbeans-impl/src/main/resources/META-INF/openwebbeans/openwebbeans.properties Fri May 15 10:13:25 2015
@@ -117,3 +117,15 @@ org.apache.webbeans.useBDABeansXMLScanne
# org.apache.webbeans.proxy.mapping.javax.enterprise.context.RequestScoped=org.apache.webbeans.intercept.NormalScopedBeanInterceptorHandler
org.apache.webbeans.proxy.mapping.javax.enterprise.context.ApplicationScoped=org.apache.webbeans.intercept.ApplicationScopedBeanInterceptorHandler
################################################################################################
+
+############################ Eager Session Initialisation ######################################
+# By default we do _not_ force session creation in our WebBeansConfigurationListener. We only create the
+# Session if we really need the SessionContext. E.g. when we create a Contextual Instance in it.
+# Sometimes this creates a problem as the HttpSession can only be created BEFORE anything got written back
+# to the client.
+# With this configuration you can choose between 3 settings
+# * true: the Session will _always_ eagerly be created at the begin of a request
+# * false: the Session will _never_ eagerly be created but only lazily when the first @SessionScoped bean gets used
+# * any other value will be interpreted as Java regular expression for request URIs which need eager Session initialization
+org.apache.webbeans.web.eagerSessionInitialisation=false
+################################################################################################
Modified: openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/web/context/WebContextsService.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/web/context/WebContextsService.java?rev=1679531&r1=1679530&r2=1679531&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/web/context/WebContextsService.java (original)
+++ openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/web/context/WebContextsService.java Fri May 15 10:13:25 2015
@@ -21,6 +21,7 @@ package org.apache.webbeans.web.context;
import org.apache.webbeans.annotation.DestroyedLiteral;
import org.apache.webbeans.annotation.InitializedLiteral;
import org.apache.webbeans.config.OWBLogConst;
+import org.apache.webbeans.config.OpenWebBeansConfiguration;
import org.apache.webbeans.config.WebBeansContext;
import org.apache.webbeans.context.AbstractContextsService;
import org.apache.webbeans.context.ApplicationContext;
@@ -50,6 +51,8 @@ import java.lang.annotation.Annotation;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
/**
* Web container {@link org.apache.webbeans.spi.ContextsService}
@@ -89,6 +92,9 @@ public class WebContextsService extends
/**Conversation context manager*/
protected final ConversationManager conversationManager;
+ protected Boolean eagerSessionInitialisation = null;
+ protected Pattern eagerSessionPattern = null;
+
/**Initialize thread locals*/
@@ -113,6 +119,28 @@ public class WebContextsService extends
applicationContext = new ApplicationContext();
applicationContext.setActive(true);
+
+ configureEagerSessionInitialisation(webBeansContext);
+ }
+
+ protected void configureEagerSessionInitialisation(WebBeansContext webBeansContext)
+ {
+ String val = webBeansContext.getOpenWebBeansConfiguration().getProperty(OpenWebBeansConfiguration.EAGER_SESSION_INITIALISATION);
+ if ("false".equalsIgnoreCase(val))
+ {
+ eagerSessionInitialisation = Boolean.FALSE;
+ logger.fine("EagerSessionInitialisation is configured to FALSE (Session will get created lazily on first use)");
+ }
+ else if ("true".equalsIgnoreCase(val))
+ {
+ eagerSessionInitialisation = Boolean.TRUE;
+ logger.fine("EagerSessionInitialisation is configured to TRUE (Session will get created at the beginning of a request)");
+ }
+ else
+ {
+ logger.fine("EagerSessionInitialisation used for all URIs with RegExp " + val);
+ eagerSessionPattern = Pattern.compile(val);
+ }
}
/**
@@ -328,11 +356,27 @@ public class WebContextsService extends
if (request != null)
{
payload = request;
+
+ if (shouldEagerlyInitializeSession(request))
+ {
+ request.getSession(true);
+ }
}
}
webBeansContext.getBeanManagerImpl().fireEvent(payload != null ? payload : new Object(), InitializedLiteral.INSTANCE_REQUEST_SCOPED);
}
-
+
+ protected boolean shouldEagerlyInitializeSession(HttpServletRequest request)
+ {
+ if (eagerSessionPattern != null)
+ {
+ String requestURI = request.getRequestURI();
+ Matcher matcher = eagerSessionPattern.matcher(requestURI);
+ return matcher.matches();
+ }
+ return eagerSessionInitialisation;
+ }
+
/**
* Destroys the request context and all of its components.
* @param endObject http servlet request object or other payload