Taher Alkhateeb <[email protected]> writes:
> I didn't get into it before, but my first gut reaction is .. delete
> that thing! :)
:-)
> Anyway, it seems to have to many references, so I would advise
> investigating how it is constructed to understand what is being pulled
> in. Just begin from the container loader and work your way down with a
> debugger until you hit exactly the reason why this check was in place.
I have put a breakpoint inside the if branch of the overidden ‘get’
method and started to randomly navigate OFBiz. I have found some
callers of this “feature” which can be triggered for example via the
<https://localhost:8443/facility/control/EditFacility?facilityId=WebStoreWarehouse>
URI.
The reason is that some Freemarker template files are referencing the
‘context’ map explicitly. I have no experience writing “.ftl” files so
I am not sure if this is a good thing or not. It seems required since
my attempts to replace those ‘context’ references by globals have
failed.
As shown by next patch, I would like to replace the hardcoded special
case in ‘MapStack’ by explicitly adding the ‘context’ Map in the
FreeMarker model at rendering time.
diff --git framework/base/src/main/java/org/apache/ofbiz/base/util/collections/MapStack.java framework/base/src/main/java/org/apache/ofbiz/base/util/collections/MapStack.java
index 547e9942a3..347d6cbd3e 100644
--- framework/base/src/main/java/org/apache/ofbiz/base/util/collections/MapStack.java
+++ framework/base/src/main/java/org/apache/ofbiz/base/util/collections/MapStack.java
@@ -84,42 +84,4 @@ public class MapStack<K> extends MapContext<K, Object> {
standAloneChild.push();
return standAloneChild;
}
-
- /* (non-Javadoc)
- * @see java.util.Map#get(java.lang.Object)
- */
- @Override
- public Object get(Object key) {
- if ("context".equals(key)) {
- return this;
- }
-
- return super.get(key);
- }
-
- /* (non-Javadoc)
- * @see org.apache.ofbiz.base.util.collections.LocalizedMap#get(java.lang.String, java.util.Locale)
- */
- @Override
- public Object get(String name, Locale locale) {
- if ("context".equals(name)) {
- return this;
- }
-
- return super.get(name, locale);
- }
-
- /* (non-Javadoc)
- * @see java.util.Map#put(java.lang.Object, java.lang.Object)
- */
- @Override
- public Object put(K key, Object value) {
- if ("context".equals(key)) {
- if (value == null || this != value) {
- Debug.logWarning("Putting a value in a MapStack with key [context] that is not this MapStack, will be hidden by the current MapStack self-reference: " + value, module);
- }
- }
-
- return super.put(key, value);
- }
}
diff --git framework/base/src/main/java/org/apache/ofbiz/base/util/template/FreeMarkerWorker.java framework/base/src/main/java/org/apache/ofbiz/base/util/template/FreeMarkerWorker.java
index 129bcaf799..1f53fd9fba 100644
--- framework/base/src/main/java/org/apache/ofbiz/base/util/template/FreeMarkerWorker.java
+++ framework/base/src/main/java/org/apache/ofbiz/base/util/template/FreeMarkerWorker.java
@@ -199,6 +199,8 @@ public final class FreeMarkerWorker {
public static Environment renderTemplate(Template template, Map<String, Object> context, Appendable outWriter) throws TemplateException, IOException {
// make sure there is no "null" string in there as FreeMarker will try to use it
context.remove("null");
+ // Allow the template to refer to the context explicitly.
+ context.put("context", context);
// Since the template cache keeps a single instance of a Template that is shared among users,
// and since that Template instance is immutable, we need to create an Environment instance and
// use it to process the template with the user's settings.
WDYT?
--
Mathieu Lirzin
GPG: F2A3 8D7E EB2B 6640 5761 070D 0ADE E100 9460 4D37