001/* 002 * (C) Copyright 2006-2016 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * bstefanescu 018 */ 019package org.nuxeo.ecm.automation.core.rendering; 020 021import static java.nio.charset.StandardCharsets.UTF_8; 022 023import java.io.IOException; 024import java.io.InputStream; 025import java.net.URL; 026import java.util.Collections; 027import java.util.LinkedHashMap; 028import java.util.Map; 029 030import org.apache.commons.io.IOUtils; 031import org.mvel2.templates.CompiledTemplate; 032import org.mvel2.templates.TemplateCompiler; 033import org.mvel2.templates.TemplateRuntime; 034import org.nuxeo.ecm.automation.OperationException; 035import org.nuxeo.runtime.api.Framework; 036import org.nuxeo.runtime.services.resource.ResourceService; 037 038/** 039 * MVEL rendering using a simple cache of compiled template. 040 * 041 * @author <a href="mailto:[email protected]">Bogdan Stefanescu</a> 042 */ 043public class MvelRender implements Renderer { 044 045 protected Map<String, CompiledTemplate> cache = Collections.synchronizedMap(new Cache()); 046 047 @Override 048 public String render(String uriOrContent, Map<String, Object> root) throws OperationException, IOException { 049 CompiledTemplate compiled; 050 String content; 051 if (uriOrContent.startsWith(Renderer.TEMPLATE_PREFIX)) { 052 String name = uriOrContent.substring(Renderer.TEMPLATE_PREFIX.length()); 053 compiled = cache.get(name); 054 if (compiled == null) { 055 URL url = Framework.getService(ResourceService.class).getResource(name); 056 if (url == null) { 057 throw new OperationException("Rendering resource not found: " + name); 058 } 059 try (InputStream in = url.openStream()) { 060 content = IOUtils.toString(in, UTF_8); 061 } 062 compiled = TemplateCompiler.compileTemplate(content); 063 cache.put(name, compiled); 064 } 065 } else { 066 content = uriOrContent; 067 compiled = TemplateCompiler.compileTemplate(content); 068 } 069 070 Object obj = TemplateRuntime.execute(compiled, root); 071 return obj == null ? "" : obj.toString(); 072 } 073 074 @SuppressWarnings("serial") 075 private static class Cache extends LinkedHashMap<String, CompiledTemplate> { 076 077 protected int maxCachedItems; 078 079 private Cache() { 080 this(128); 081 } 082 083 private Cache(int maxCachedItems) { 084 super(maxCachedItems, 1.0f, true); 085 this.maxCachedItems = maxCachedItems; 086 } 087 088 @Override 089 protected boolean removeEldestEntry(Map.Entry<String, CompiledTemplate> eldest) { 090 return size() > maxCachedItems; 091 } 092 093 } 094 095}