Repository: struts Updated Branches: refs/heads/master 843693fdd -> 93dba5bce
WW-4758 Uses ObjectFactory to create Tiles' ViewPrepare Project: http://git-wip-us.apache.org/repos/asf/struts/repo Commit: http://git-wip-us.apache.org/repos/asf/struts/commit/93dba5bc Tree: http://git-wip-us.apache.org/repos/asf/struts/tree/93dba5bc Diff: http://git-wip-us.apache.org/repos/asf/struts/diff/93dba5bc Branch: refs/heads/master Commit: 93dba5bce22e9e7e539e3e631166d6510efca023 Parents: 216c520 Author: Lukasz Lenart <[email protected]> Authored: Sun Jun 18 11:11:11 2017 +0200 Committer: Lukasz Lenart <[email protected]> Committed: Sun Jun 18 11:11:25 2017 +0200 ---------------------------------------------------------------------- .../struts2/tiles/StrutsPreparerFactory.java | 53 ++++++++++++++++++++ .../tiles/StrutsTilesContainerFactory.java | 6 +++ 2 files changed, 59 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/struts/blob/93dba5bc/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsPreparerFactory.java ---------------------------------------------------------------------- diff --git a/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsPreparerFactory.java b/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsPreparerFactory.java new file mode 100644 index 0000000..d231786 --- /dev/null +++ b/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsPreparerFactory.java @@ -0,0 +1,53 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.struts2.tiles; + +import com.opensymphony.xwork2.ActionContext; +import com.opensymphony.xwork2.ObjectFactory; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.apache.logging.log4j.message.ParameterizedMessage; +import org.apache.tiles.preparer.ViewPreparer; +import org.apache.tiles.preparer.factory.BasicPreparerFactory; + +/** + * This is a basic ViewPreparer factory that uses {@link ObjectFactory} to create the ViewPreparer + */ +public class StrutsPreparerFactory extends BasicPreparerFactory { + + private static final Logger LOG = LogManager.getLogger(StrutsPreparerFactory.class); + + @Override + protected ViewPreparer createPreparer(String name) { + ActionContext actionContext = ActionContext.getContext(); + if (actionContext == null) { + LOG.warn("Action context not initialised, request has omitted an action? Fallback to super.createPreparer!"); + return super.createPreparer(name); + } + + try { + ObjectFactory factory = actionContext.getContainer().getInstance(ObjectFactory.class); + return (ViewPreparer) factory.buildBean(name, ActionContext.getContext().getContextMap()); + } catch (Exception e) { + LOG.error(new ParameterizedMessage("Cannot create a ViewPreparer [{}], fallback to super.createPreparer!", name), e); + return super.createPreparer(name); + } + } +} http://git-wip-us.apache.org/repos/asf/struts/blob/93dba5bc/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsTilesContainerFactory.java ---------------------------------------------------------------------- diff --git a/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsTilesContainerFactory.java b/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsTilesContainerFactory.java index b7564a9..99a259d 100644 --- a/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsTilesContainerFactory.java +++ b/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsTilesContainerFactory.java @@ -52,6 +52,7 @@ import org.apache.tiles.ognl.PropertyAccessorDelegateFactory; import org.apache.tiles.ognl.ScopePropertyAccessor; import org.apache.tiles.ognl.TilesApplicationContextNestedObjectExtractor; import org.apache.tiles.ognl.TilesContextPropertyAccessorDelegateFactory; +import org.apache.tiles.preparer.factory.PreparerFactory; import org.apache.tiles.request.ApplicationContext; import org.apache.tiles.request.ApplicationResource; import org.apache.tiles.request.Request; @@ -204,6 +205,11 @@ public class StrutsTilesContainerFactory extends BasicTilesContainerFactory { return filteredResources; } + @Override + protected PreparerFactory createPreparerFactory(ApplicationContext applicationContext) { + return new StrutsPreparerFactory(); + } + protected Set<String> getTilesDefinitions(Map<String, String> params) { if (params.containsKey(DefinitionsFactory.DEFINITIONS_CONFIG)) { return TextParseUtil.commaDelimitedStringToSet(params.get(DefinitionsFactory.DEFINITIONS_CONFIG));
