A patch from Sumit Pandit "Issue in rendering Freemarker Template from String." (https://issues.apache.org/jira/browse/OFBIZ-3782) - OFBIZ-3782

With the help of Bob Morley (it was more a cache issue than what Sumit thought)

git-svn-id: https://svn.apache.org/repos/asf/ofbiz/trunk@950866 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Jacques Le Roux
2010-06-03 04:11:32 +00:00
parent 013853ee4f
commit e57c2599a9
5 changed files with 41 additions and 1 deletions
+1
View File
@@ -28,6 +28,7 @@ under the License.
<classpath type="jar" location="lib/uno/*"/>
<classpath type="jar" location="build/lib/*"/>
<entity-resource type="model" reader-name="main" loader="main" location="entitydef/entitymodel.xml"/>
<entity-resource type="eca" reader-name="main" loader="main" location="entitydef/eecas.xml"/>
<entity-resource type="data" reader-name="seed" loader="main" location="data/ContentTypeData.xml"/>
<entity-resource type="data" reader-name="seed" loader="main" location="data/ContentOperationData.xml"/>
<entity-resource type="data" reader-name="seed" loader="main" location="data/MimeTypeData.xml"/>
@@ -252,4 +252,11 @@
<attribute mode="IN" name="rootDir" optional="true" type="String"/>
</service>
-->
<!-- Cleared associated DataResource rendered cache from UtilCache. -->
<service name="clearAssociatedRenderCache" engine="java" auth="true" default-entity-name="DataResource"
location="org.ofbiz.content.data.DataServices" invoke="clearAssociatedRenderCache">
<auto-attributes include="pk" mode="IN" />
</service>
</services>
@@ -546,6 +546,20 @@ public class DataResourceWorker implements org.ofbiz.widget.DataResourceWorkerI
// DataResource rendering methods
// -------------------------------------
public static void clearAssociatedRenderCache(Delegator delegator, String dataResourceId) throws GeneralException {
if (dataResourceId == null) {
throw new GeneralException("Cannot clear dataResource related cache for a null dataResourceId");
}
GenericValue dataResource = delegator.findByPrimaryKeyCache("DataResource", UtilMisc.toMap("dataResourceId", dataResourceId));
if (dataResource != null) {
String dataTemplateTypeId = dataResource.getString("dataTemplateTypeId");
if ("FTL".equals(dataTemplateTypeId)) {
FreeMarkerWorker.clearTemplateFromCache("DataResource:" + dataResourceId);
}
}
}
public static String renderDataResourceAsText(Delegator delegator, String dataResourceId, Map<String, Object> templateContext,
Locale locale, String targetMimeTypeId, boolean cache) throws GeneralException, IOException {
Writer writer = new StringWriter();
@@ -54,6 +54,19 @@ public class DataServices {
public static final String module = DataServices.class.getName();
public static Map<String, Object> clearAssociatedRenderCache(DispatchContext dctx, Map<String, Object> context) {
Delegator delegator = dctx.getDelegator();
String dataResourceId = (String) context.get("dataResourceId");
try {
DataResourceWorker.clearAssociatedRenderCache(delegator, dataResourceId);
} catch (GeneralException e) {
String errMsg = "Unable to clear associated render cache with dataResourceId=" + dataResourceId;
Debug.logError(e, errMsg, module);
return ServiceUtil.returnError(errMsg);
}
return ServiceUtil.returnSuccess();
}
/**
* A top-level service for creating a DataResource and ElectronicText together.
*/
@@ -173,6 +173,12 @@ public class FreeMarkerWorker {
renderTemplate(template, context, outWriter);
}
public static void clearTemplateFromCache(String templateLocation) {
synchronized (cachedTemplates) {
cachedTemplates.remove(templateLocation);
}
}
public static Environment renderTemplateFromString(String templateString, String templateLocation, Map<String, Object> context, Appendable outWriter) throws TemplateException, IOException {
Template template = cachedTemplates.get(templateLocation);
if (template == null) {
@@ -186,7 +192,6 @@ public class FreeMarkerWorker {
}
}
}
return renderTemplate(template, context, outWriter);
}