From e55f5b875efe58702c38364256f3321c35107b42 Mon Sep 17 00:00:00 2001
From: ys-kalyakin Action class.
The ModuleConfiguration with which we are
@@ -125,9 +126,7 @@ public void destroy() {
*/
public void init(ActionServlet servlet, ModuleConfig moduleConfig)
throws ServletException {
- synchronized (actions) {
- actions.clear();
- }
+ actions.clear();
this.servlet = servlet;
this.moduleConfig = moduleConfig;
@@ -198,9 +197,7 @@ public void process(HttpServletRequest request, HttpServletResponse response)
ActionForward forward = processException(request, response, e, form, mapping);
processForwardConfig(request, response, forward);
return;
- } catch (IOException e) {
- throw e;
- } catch (ServletException e) {
+ } catch (IOException | ServletException e) {
throw e;
}
@@ -241,9 +238,11 @@ public void process(HttpServletRequest request, HttpServletResponse response)
* the current request.
* @throws IOException if an input/output error occurs
*/
- protected Action processActionCreate(HttpServletRequest request,
- HttpServletResponse response, ActionMapping mapping)
- throws IOException {
+ protected Action processActionCreate(
+ HttpServletRequest request,
+ HttpServletResponse response,
+ ActionMapping mapping
+ ) throws IOException {
// Acquire the Action instance we will be using (if there is one)
String className = mapping.getType();
@@ -252,21 +251,11 @@ protected Action processActionCreate(HttpServletRequest request,
// If there were a mapping property indicating whether
// an Action were a singleton or not ([true]),
// could we just instantiate and return a new instance here?
- Action instance;
-
- synchronized (actions) {
- // Return any existing Action instance of this class
- instance = actions.get(className);
-
- if (instance != null) {
- log.trace(" Returning existing Action instance");
-
- return (instance);
- }
-
+ Action action = actions.computeIfAbsent(className, key -> {
// Create and return a new Action instance
log.trace(" Creating new Action instance");
+ Action instance;
try {
instance = (Action) RequestUtils.applicationInstance(className);
@@ -278,20 +267,24 @@ protected Action processActionCreate(HttpServletRequest request,
mapping.getPath(), mapping.toString()))
.setCause(e).log();
- response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
- getInternal().getMessage("actionCreate", mapping.getPath()));
-
return (null);
}
- actions.put(className, instance);
-
if (instance.getServlet() == null) {
instance.setServlet(this.servlet);
}
+
+ return instance;
+ });
+
+ if (action == null) {
+ response.sendError(
+ HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
+ getInternal().getMessage("actionCreate", mapping.getPath())
+ );
}
- return (instance);
+ return (action);
}
/**
diff --git a/core/src/main/java/org/apache/struts/chain/commands/servlet/CreateAction.java b/core/src/main/java/org/apache/struts/chain/commands/servlet/CreateAction.java
index 3719a4986..2d1780653 100644
--- a/core/src/main/java/org/apache/struts/chain/commands/servlet/CreateAction.java
+++ b/core/src/main/java/org/apache/struts/chain/commands/servlet/CreateAction.java
@@ -20,9 +20,6 @@
*/
package org.apache.struts.chain.commands.servlet;
-import java.util.HashMap;
-import java.util.Map;
-
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionServlet;
import org.apache.struts.chain.Constants;
@@ -31,9 +28,13 @@
import org.apache.struts.chain.contexts.ServletActionContext;
import org.apache.struts.config.ActionConfig;
import org.apache.struts.config.ModuleConfig;
+import org.apache.struts.util.Try;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
/**
*
Concrete implementation of AbstractCreateAction for use in
* a Servlet API chain. Expects that the ActionContext passed into it can
@@ -52,9 +53,11 @@ public class CreateAction
/* :TODO The Action class' dependency on having its "servlet" property set
* requires this API-dependent subclass of AbstractCreateAction.
*/
- protected synchronized Action getAction(ActionContext context, String type,
- ActionConfig actionConfig)
- throws Exception {
+ protected Action getAction(
+ ActionContext context,
+ String type,
+ ActionConfig actionConfig
+ ) throws Exception {
ServletActionContext saContext = (ServletActionContext) context;
ActionServlet actionServlet = saContext.getActionServlet();
@@ -62,24 +65,26 @@ protected synchronized Action getAction(ActionContext context, String type,
ModuleConfig moduleConfig = actionConfig.getModuleConfig();
String actionsKey = Constants.ACTIONS_KEY + moduleConfig.getPrefix();
@SuppressWarnings("unchecked")
- Map