001/*
002 * (C) Copyright 2006-2011 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 java.io.IOException;
022import java.io.InputStream;
023import java.net.URL;
024import java.util.Collections;
025import java.util.LinkedHashMap;
026import java.util.Map;
027
028import org.mvel2.templates.CompiledTemplate;
029import org.mvel2.templates.TemplateCompiler;
030import org.mvel2.templates.TemplateRuntime;
031import org.nuxeo.common.utils.FileUtils;
032import org.nuxeo.ecm.automation.OperationException;
033import org.nuxeo.runtime.api.Framework;
034import org.nuxeo.runtime.services.resource.ResourceService;
035
036/**
037 * MVEL rendering using a simple cache of compiled template.
038 *
039 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
040 */
041public class MvelRender implements Renderer {
042
043    protected Map<String, CompiledTemplate> cache = Collections.synchronizedMap(new Cache());
044
045    @Override
046    public String render(String uriOrContent, Map<String, Object> root) throws OperationException, IOException {
047        CompiledTemplate compiled = null;
048        String content = null;
049        if (uriOrContent.startsWith(Renderer.TEMPLATE_PREFIX)) {
050            String name = uriOrContent.substring(Renderer.TEMPLATE_PREFIX.length());
051            compiled = cache.get(name);
052            if (compiled == null) {
053                URL url = Framework.getService(ResourceService.class).getResource(name);
054                if (url == null) {
055                    throw new OperationException("Rendering resource not found: " + name);
056                }
057                InputStream in = url.openStream();
058                try {
059                    content = FileUtils.read(in);
060                } finally {
061                    in.close();
062                }
063                compiled = TemplateCompiler.compileTemplate(content);
064                cache.put(name, compiled);
065            }
066        } else {
067            content = uriOrContent;
068            compiled = TemplateCompiler.compileTemplate(content);
069        }
070
071        Object obj = TemplateRuntime.execute(compiled, root);
072        return obj == null ? "" : obj.toString();
073    }
074
075    @SuppressWarnings("serial")
076    private static class Cache extends LinkedHashMap<String, CompiledTemplate> {
077
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}