3

The documentation

promises there would be an option to have a customer resource loader. The corresponding documentation link

is broken as of 2015-04-15. An issue for this has been filed as

reverse engineering the situation leads to some sources like:

and some bug reports like:

Question: Where is a working simple example for a custom ResourceLoader available?

What is the minimum to be implemented - especially if resources are not loaded from a filesystem but from another source e.g. via RESTFul web calls?

Below are some codesnippets of what an example could look like - is this a valid example? The codesnippets assume that custom templates use a 'namespace' denoted by the prefix "custom." so @custom.test would try to load a template "test" via this custom ResourceLoader. The Rythmsuffix, codetype and stuff is simply ignored for simplicity.

CustomTemplateResourceLoader:

package org.rythm.custom;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.rythmengine.RythmEngine;
import org.rythmengine.extension.ICodeType;
import org.rythmengine.extension.ITemplateResourceLoader;
import org.rythmengine.internal.compiler.TemplateClass;
import org.rythmengine.resource.ITemplateResource;
import org.rythmengine.resource.TemplateResourceManager;
import org.rythmengine.template.ITemplate;

/**
 * a custom TemplateResourceLoader for the Rythm template engine
 */
public class CustomResourceLoader implements ITemplateResourceLoader {
 protected static Logger LOGGER = Logger.getLogger("org.rythm.custom");
 private RythmEngine engine;
 private Map<String,CustomTemplateResource> resources=new LinkedHashMap<String,CustomTemplateResource>();
 @Override
  public String getResourceLoaderRoot() {
    LOGGER.log(Level.WARNING, "getResourceLoaderRoot not implemented");
    String result = "";
    return result;
  }

/**
 * get the resource for the given path
 * @param path
 * @return
 */
 public CustomTemplateResource getResource(String path) {
   if (resources.containsKey(path)) {
     return resources.get(path);
   }
   if (path.startsWith("custom.")) {
     try {
        CustomTemplateResource template = new CustomTemplateResource(this,path);
        resources.put(path,template);
        return template;
      } catch (Exception e) {
         LOGGER.log(Level.SEVERE,e.getMessage());
      }
    }
    return null;  
 }

  @Override
  public ITemplateResource load(String path) {
    LOGGER.log(Level.WARNING, "load '" + path + "' not implemented");
    return getResource(path);
  }

  @Override
  public TemplateClass tryLoadTemplate(String tmplName, RythmEngine engine,
      TemplateClass callerClass, ICodeType codeType) {
    if (engine.templateRegistered(tmplName))
      return null;
    if (!tmplName.startsWith("custom."))
      return null;
    String rythmSuffix = engine.conf().resourceNameSuffix();
    String key = tmplName;
    TemplateClass tc = engine.classes().getByTemplate(key);
    if (tc == null) {
      try {
        CustomTemplateResource customTemplate =getResource(key);
        tc = new TemplateClass(customTemplate, engine);
        ITemplate template = tc.asTemplate(engine);
        if (null != template) {
           engine.registerTemplate(key, template);
        }
      } catch (Exception e) {
         LOGGER.log(Level.SEVERE,e.getMessage()):
      }
    }
    return tc;
  }

  @Override
  public void scan(TemplateResourceManager manager) {
    LOGGER.log(Level.WARN,"scan not implemented");
  }


  @Override
  public void setEngine(RythmEngine engine) {
    this.engine = engine;
  }

  @Override
  public RythmEngine getEngine() {
    return this.engine;
  }

}

CustomTemplateResourceLoader:

package org.rythm.custom;

import java.util.logging.Level;
import java.util.logging.Logger;

import org.rythmengine.resource.ITemplateResource;
import org.rythmengine.resource.TemplateResourceBase;

/**
 * 
 * @author wf
 *
 */
public class CustomTemplateResource extends TemplateResourceBase implements ITemplateResource{
  /**
   * 
   */
  private static final long serialVersionUID = 67049021377257267L;
  protected static Logger LOGGER = Logger.getLogger("org.rythm.custom");

  String key;

  /**
   * 
   * @param customTemplateResourceLoader 
   * @param tmplName
   * @throws Exception 
   */
  public CustomTemplateResource(CustomTemplateResourceLoader wikiTemplateResourceLoader, String tmplName) throws Exception {
    super(customTemplateResourceLoader);
    key=tmplName;
  }

  @Override
  public Object getKey() {
    return key;
  }

  @Override
  public boolean isValid() {
     return true;
  }

  @Override
  protected long defCheckInterval() {
    // 5 minutes?
    long result=1000 * 300;
    return result;
  }

  @Override
  protected long lastModified() {
    return 0;
  }

  @Override
  protected String reload() {
    String pageContent=null;
    ... get the pageContent with the key here 
    return pageContent;
  }

}
4

0 回答 0