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:bs@nuxeo.com">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        private static final long serialVersionUID = 1L;
078        protected int maxCachedItems;
079
080        private Cache() {
081            this(128);
082        }
083
084        private Cache(int maxCachedItems) {
085            super(maxCachedItems, 1.0f, true);
086            this.maxCachedItems = maxCachedItems;
087        }
088
089        @Override
090        protected boolean removeEldestEntry(Map.Entry<String, CompiledTemplate> eldest) {
091            return size() > maxCachedItems;
092        }
093
094    }
095
096}