Author: tchemit Date: 2013-09-12 14:29:05 +0200 (Thu, 12 Sep 2013) New Revision: 2602 Url: http://nuiton.org/projects/nuiton-validator/repository/revisions/2602 Log: fixes #2847: Add method to set the sharedValueStack fixes #2848: Make possible to reuse the ActionContext when possible Modified: trunk/src/main/java/org/nuiton/validator/xwork2/XWork2ValidatorUtil.java Modified: trunk/src/main/java/org/nuiton/validator/xwork2/XWork2ValidatorUtil.java =================================================================== --- trunk/src/main/java/org/nuiton/validator/xwork2/XWork2ValidatorUtil.java 2013-09-11 15:24:03 UTC (rev 2601) +++ trunk/src/main/java/org/nuiton/validator/xwork2/XWork2ValidatorUtil.java 2013-09-12 12:29:05 UTC (rev 2602) @@ -67,8 +67,8 @@ private static final Log log = LogFactory.getLog(XWork2ValidatorUtil.class); /** - * a shared value stack to allow external operations on it (for example add - * some datas in stack to be usedby validators + * A shared value stack to allow external operations on it (for example add + * some datas in stack to be used by validators. */ static private ValueStack sharedValueStack; @@ -84,14 +84,45 @@ return sharedValueStack; } + /** + * Sets the given value stack as shared (can be null). + * + * @param sharedValueStack the new shared value stack to use (can be null). + * @since 3.0 + */ + public static void setSharedValueStack(ValueStack sharedValueStack) { + if (log.isDebugEnabled()) { + log.debug("set shared value stack " + sharedValueStack); + } + XWork2ValidatorUtil.sharedValueStack = sharedValueStack; + } + public static ValueStack createValuestack() { - ConfigurationManager confManager = new ConfigurationManager(); - Configuration conf = confManager.getConfiguration(); - Container container = conf.getContainer(); - ValueStackFactory stackFactory = container.getInstance(ValueStackFactory.class); - ValueStack vs = stackFactory.createValueStack(); - return vs; + ValueStack result; + + ActionContext context = ActionContext.getContext(); + if (context == null) { + + // no action context, create a value stack from scratch + ConfigurationManager confManager = new ConfigurationManager(); + Configuration conf = confManager.getConfiguration(); + Container container = conf.getContainer(); + ValueStackFactory stackFactory = container.getInstance(ValueStackFactory.class); + result = stackFactory.createValueStack(); + } else { + + // there is an action context, try to use his value stack + result = context.getValueStack(); + + if (result == null) { + + // no value stack, create then a new one + ValueStackFactory stackFactory = context.getInstance(ValueStackFactory.class); + result = stackFactory.createValueStack(); + } + } + return result; } public static <O> XWork2ScopeValidator<O> newXWorkScopeValidator(Class<O> beanClass, String contextName, @@ -124,39 +155,46 @@ } } - ActionContext context = new ActionContext(vs.getContext()); + ActionContext context = ActionContext.getContext(); - // must set the action context otherwise can't obtain after validators - // with the method validator.getValidators(XXX) - // Later in the code, we could not having reference to the ValueStack - // before using a validator... Must be cleaned... - ActionContext.setContext(context); + if (context == null) { + context = new ActionContext(vs.getContext()); + // must set the action context otherwise can't obtain after validators + // with the method validator.getValidators(XXX) + // Later in the code, we could not having reference to the ValueStack + // before using a validator... Must be cleaned... + ActionContext.setContext(context); + + } Container container = context.getContainer(); - // We need sometimes a ActionInvocation (see http://nuiton.org/issues/2837) - Configuration configuration = container.getInstance(Configuration.class); + if (context.getActionInvocation() == null) { - UnknownHandlerConfig unknownHandlerStack = new UnknownHandlerConfig("nuiton"); - List<UnknownHandlerConfig> unknownHandlerStackList = configuration.getUnknownHandlerStack(); + // We need sometimes a ActionInvocation (see http://nuiton.org/issues/2837) + Configuration configuration = container.getInstance(Configuration.class); - if (unknownHandlerStackList == null) { - unknownHandlerStackList = Lists.newArrayList(); - configuration.setUnknownHandlerStack(unknownHandlerStackList); - } - unknownHandlerStackList.add(0, unknownHandlerStack); + UnknownHandlerConfig unknownHandlerStack = new UnknownHandlerConfig("nuiton"); + List<UnknownHandlerConfig> unknownHandlerStackList = configuration.getUnknownHandlerStack(); - Map<String, Object> extraContext = Maps.newHashMap(); - extraContext.put(ActionContext.VALUE_STACK, vs); - DefaultActionInvocation invocation = new DefaultActionInvocation(extraContext, false); - invocation.setObjectFactory(container.getInstance(ObjectFactory.class)); - invocation.setContainer(container); + if (unknownHandlerStackList == null) { + unknownHandlerStackList = Lists.newArrayList(); + configuration.setUnknownHandlerStack(unknownHandlerStackList); + } + unknownHandlerStackList.add(0, unknownHandlerStack); - ActionProxyFactory actionProxyFactory = context.getInstance(ActionProxyFactory.class); - ActionProxy actionProxy = actionProxyFactory.createActionProxy(invocation, "java.lang", "java.lang.Object", "nuiton-validation", false, false); - invocation.init(actionProxy); - context.setActionInvocation(invocation); + Map<String, Object> extraContext = Maps.newHashMap(); + extraContext.put(ActionContext.VALUE_STACK, vs); + DefaultActionInvocation invocation = new DefaultActionInvocation(extraContext, false); + invocation.setObjectFactory(container.getInstance(ObjectFactory.class)); + invocation.setContainer(container); + ActionProxyFactory actionProxyFactory = context.getInstance(ActionProxyFactory.class); + ActionProxy actionProxy = actionProxyFactory.createActionProxy(invocation, "java.lang", "java.lang.Object", "nuiton-validation", false, false); + invocation.init(actionProxy); + context.setActionInvocation(invocation); + + } // init validator ActionValidatorManager validatorManager =
participants (1)
-
tchemit@users.nuiton.org