001    package com.khubla.pragmatach.plugin.thymeleaf;
002    
003    import java.io.InputStream;
004    
005    import javax.servlet.ServletContext;
006    
007    import org.thymeleaf.TemplateProcessingParameters;
008    import org.thymeleaf.resourceresolver.IResourceResolver;
009    
010    import com.khubla.pragmatach.framework.resourceloader.DefaultResourceLoaderImpl;
011    import com.khubla.pragmatach.framework.resourceloader.ResourceLoader;
012    
013    /**
014     * @author tome
015     */
016    public class PragmatachResourceResolver implements IResourceResolver {
017       /**
018        * ServletContext
019        */
020       private final ServletContext servletContext;
021    
022       public PragmatachResourceResolver(ServletContext servletContext) {
023          this.servletContext = servletContext;
024       }
025    
026       @Override
027       public String getName() {
028          return this.getClass().getSimpleName();
029       }
030    
031       @Override
032       public InputStream getResourceAsStream(TemplateProcessingParameters templateProcessingParameters, String resourceName) {
033          try {
034             final ResourceLoader resourceLoader = new DefaultResourceLoaderImpl(servletContext);
035             final InputStream is = resourceLoader.getResource(resourceName);
036             return is;
037          } catch (final Exception e) {
038             throw new RuntimeException("Cannot open resource '" + resourceName + "'", e);
039          }
040       }
041    }