Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (C) 2009 eXo Platform SAS. This is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this software; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 
 
 package org.exoplatform.groovyscript.text;
 
 
 
Created by The eXo Platform SAS Dec 26, 2005
 
 @NameTemplate({ @Property(key = "view", value = "portal"), @Property(key = "service", value = "management"),
         @Property(key = "type", value = "template") })
 @ManagedDescription("Template management service")
 @RESTEndpoint(path = "templateservice")
 public class TemplateService {
 
     private GroovyTemplateEngine engine_;
 
 
 
     private boolean cacheTemplate_ = true;
 
         public GroovyTemplate retrieve(ResourceResolver contextResourceKey keythrows Exception {
             byte[] bytes;
             InputStream is = context.getInputStream(key.getURL());
             try {
                 bytes = IOUtil.getStreamContentAsBytes(is);
                 is.close();
             } finally {
                 IOTools.safeClose(is);
             }
 
             // The template class name
             int pos = key.getURL().lastIndexOf('/');
             if (pos == -1) {
                 pos = 0;
             }
             String name = key.getURL().substring(pos);
 
             // Julien: it's a bit dangerious here, with respect to the file encoding...
             String text = new String(bytes);
 
             // Finally do the expensive template creation
             return .createTemplate(key.getURL(), nametext);
         }
     };
 
.
 
     private final Logger log = LoggerFactory.getLogger(TemplateService.class);
 
    public TemplateService(TemplateStatisticService statisticServiceCacheService cservicethrows Exception {
        this. = new GroovyTemplateEngine();
        this. = statisticService;
        this. = cservice.getCacheInstance(TemplateService.class.getSimpleName());
    }
    public void merge(String nameBindingContext contextthrows Exception {
        long startTime = System.currentTimeMillis();
        GroovyTemplate template = getTemplate(namecontext.getResourceResolver());
        context.put("_ctx"context);
        context.setGroovyTemplateService(this);
        template.render(context.getWriter(), context, (Localecontext.get("locale"));
        long endTime = System.currentTimeMillis();
        TemplateStatistic templateStatistic = .getTemplateStatistic(name);
        templateStatistic.setTime(endTime - startTime);
        templateStatistic.setResolver(context.getResourceResolver());
    }
    @Deprecated
    public void merge(Template templateBindingContext contextthrows Exception {
        context.put("_ctx"context);
        context.setGroovyTemplateService(this);
        Writable writable = template.make(context);
        writable.writeTo(context.getWriter());
    }
    public void include(String nameBindingContext contextthrows Exception {
        if (context == null)
            throw new Exception("Binding cannot be null");
        context.put("_ctx"context);
        GroovyTemplate template = getTemplate(namecontext.getResourceResolver());
        template.render(context.getWriter(), context, (Localecontext.get("locale"));
    }
    public final GroovyTemplate getTemplate(String nameResourceResolver resolverthrows Exception {
        return getTemplate(nameresolver);
    }
    public final GroovyTemplate getTemplate(String urlResourceResolver resolverboolean cacheablethrows Exception {
        GroovyTemplate template;
        ResourceKey resourceId = resolver.createResourceKey(url);
        if (cacheable) {
            template = .get(resolverresourceId);
        } else {
            template = .retrieve(resolverresourceId);
        }
        //
        return template;
    }
    public final void invalidateTemplate(String nameResourceResolver resolver) {
        ResourceKey resourceKey = resolver.createResourceKey(name);
        getTemplatesCache().remove(resourceKey);
    }
        return ;
    }
    /*
     * Clear the templates cache
     */
    @Managed
    @ManagedDescription("Clear the template cache")
    public void reloadTemplates() {
        try {
            .clearCache();
        } catch (Exception e) {
            .error(e.getMessage(), e);
        }
    }
    /*
     * Clear the template cache by name
     */
    @Managed
    @ManagedDescription("Clear the template cache for a specified template identifier")
    public void reloadTemplate(@ManagedDescription("The template id") @ManagedName("templateId"String name) {
        if (app != null) {
            ResourceResolver resolver = app.getResolver();
            .remove(resolver.createResourceKey(name));
        }
    }
    @Managed
    @ManagedDescription("List the identifiers of the cached templates")
    public String[] listCachedTemplates() {
        try {
            ArrayList<Stringlist = new ArrayList<String>();
            for (GroovyTemplate template : .getCachedObjects()) {
                list.add(template.getId());
            }
            return list.toArray(new String[list.size()]);
        } catch (Exception e) {
            .error(e.getMessage(), e);
            return null;
        }
    }
New to GrepCode? Check out our FAQ X