VelocityPlugin.java
上传用户:qing5858
上传日期:2015-10-27
资源大小:6056k
文件大小:7k
源码类别:

搜索引擎

开发平台:

Java

  1. package net.javacoding.jspider.mod.plugin.velocity;
  2. import net.javacoding.jspider.api.event.JSpiderEvent;
  3. import net.javacoding.jspider.api.event.engine.SpideringStoppedEvent;
  4. import net.javacoding.jspider.core.logging.Log;
  5. import net.javacoding.jspider.core.logging.LogFactory;
  6. import net.javacoding.jspider.core.util.config.*;
  7. import net.javacoding.jspider.spi.Plugin;
  8. import org.apache.velocity.Template;
  9. import org.apache.velocity.VelocityContext;
  10. import org.apache.velocity.app.VelocityEngine;
  11. import org.apache.velocity.context.Context;
  12. import java.io.*;
  13. import java.util.*;
  14. /**
  15.  *
  16.  * $Id: VelocityPlugin.java,v 1.7 2003/04/09 17:08:14 vanrogu Exp $
  17.  *
  18.  * @author G黱ther Van Roey
  19.  */
  20. public class VelocityPlugin implements Plugin {
  21.     public static final String MODULE_NAME = "Velocity Template JSpider module";
  22.     public static final String MODULE_VERSION = "v1.0";
  23.     public static final String MODULE_DESCRIPTION = "A JSpider module that renders output via velocity templates";
  24.     public static final String MODULE_VENDOR = "http://www.javacoding.net";
  25.     public static final String TRACE_FILENAME = "trace.filename";
  26.     public static final String DUMP_FILENAME = "dump.filename";
  27.     public static final String TRACE_WRITE = "trace.write";
  28.     public static final String DUMP_WRITE = "dump.write";
  29.     public static final String TEMPLATEFOLDER = "templatefolder";
  30.     public static final String DEFAULT_TRACE_FILENAME = "velocity-trace.out";
  31.     public static final String DEFAULT_DUMP_FILENAME = "velocity-dump.out";
  32.     public static final boolean DEFAULT_TRACE_WRITE = true;
  33.     public static final boolean DEFAULT_DUMP_WRITE = false;
  34.     public static final String DEFAULT_TEMPLATEFOLDER = "velocity";
  35.     protected String name;
  36.     protected PropertySet config;
  37.     protected Map templates;
  38.     protected Writer traceWriter;
  39.     protected Writer dumpWriter;
  40.     protected VelocityEngine velocityEngine;
  41.     protected boolean writeTrace;
  42.     protected boolean writeDump;
  43.     protected Log log;
  44.     public VelocityPlugin(String name, PropertySet config) {
  45.         this.name = name;
  46.         this.config = config;
  47.         log = LogFactory.getLog(VelocityPlugin.class);
  48.     }
  49.     public String getName() {
  50.         return MODULE_NAME;
  51.     }
  52.     public String getVersion() {
  53.         return MODULE_VERSION;
  54.     }
  55.     public String getDescription() {
  56.         return MODULE_DESCRIPTION;
  57.     }
  58.     public String getVendor() {
  59.         return MODULE_VENDOR;
  60.     }
  61.     public void initialize() {
  62.         String traceFileName = config.getString(TRACE_FILENAME, DEFAULT_TRACE_FILENAME);
  63.         String dumpFileName = config.getString(DUMP_FILENAME, DEFAULT_DUMP_FILENAME);
  64.         writeTrace = config.getBoolean(TRACE_WRITE, DEFAULT_TRACE_WRITE);
  65.         writeDump = config.getBoolean(DUMP_WRITE, DEFAULT_DUMP_WRITE);
  66.         log.info("writing trace file: " + writeTrace);
  67.         log.info("writing dump file: " + writeDump);
  68.         String templateFolderName = config.getString(TEMPLATEFOLDER, DEFAULT_TEMPLATEFOLDER);
  69.         log.info("Velocity template folder : " + templateFolderName);
  70.         try {
  71.             JSpiderConfiguration jspiderConfig = ConfigurationFactory.getConfiguration();
  72.             File folder = jspiderConfig.getPluginConfigurationFolder(templateFolderName);
  73.             velocityEngine = new VelocityEngine();
  74.             Vector paths = new Vector();
  75.             paths.add(folder.getAbsolutePath());
  76.             velocityEngine.setProperty("file.resource.loader.path", paths);
  77.             log.debug("file.resource.loader.path set to " + folder.getAbsolutePath());
  78.             velocityEngine.init();
  79.             log.debug("velocity.init() done");
  80.             if (writeTrace) {
  81.                 traceWriter = new FileWriter(new File(jspiderConfig.getDefaultOutputFolder(), traceFileName));
  82.                 log.debug("opened trace file '" + traceFileName + "'");
  83.                 log.info("Writing to trace file: " + traceFileName);
  84.             }
  85.             if (writeDump) {
  86.                 dumpWriter = new FileWriter(new File(jspiderConfig.getDefaultOutputFolder(), dumpFileName));
  87.                 log.debug("opened dump file '" + dumpFileName + "'");
  88.                 log.info("Writing to dump file: " + dumpFileName);
  89.             }
  90.         } catch (IOException e) {
  91.             log.error("i/o exception", e);
  92.         } catch (Exception e) {
  93.             log.error("exception", e);
  94.         }
  95.         templates = new HashMap();
  96.     }
  97.     public void shutdown() {
  98.         templates = null;
  99.         if (writeTrace) {
  100.             log.debug("closing trace file...");
  101.             try {
  102.                 traceWriter.flush();
  103.                 traceWriter.close();
  104.             } catch (IOException e) {
  105.                 log.error("i/o exception closing trace file", e);
  106.             }
  107.         }
  108.         if (writeDump) {
  109.             log.debug("closing dump file...");
  110.             try {
  111.                 dumpWriter.flush();
  112.                 dumpWriter.close();
  113.             } catch (IOException e) {
  114.                 log.error("i/o exception closing dump file", e);
  115.             }
  116.         }
  117.         log.debug("shutdown.");
  118.     }
  119.     public void notify(JSpiderEvent event) {
  120.         if (event instanceof SpideringStoppedEvent) {
  121.             if (writeDump) {
  122.                 try {
  123.                     SpideringStoppedEvent ste = (SpideringStoppedEvent) event;
  124.                     Template template = getTemplate("dump");
  125.                     if (template == null) {
  126.                         log.error("couldn't load 'dump' template");
  127.                     } else {
  128.                         log.info("writing dump - this could take a while");
  129.                         Context ctx = new VelocityContext();
  130.                         ctx.put("resources", ste.getResources());
  131.                         ctx.put("sites", ste.getSites());
  132.                         template.merge(ctx, dumpWriter);
  133.                         log.debug("dump written");
  134.                     }
  135.                 } catch (Exception e) {
  136.                     log.error("exception while merging template", e);
  137.                 }
  138.             }
  139.         }
  140.         if (writeTrace) {
  141.             try {
  142.                 String eventName = event.getName();
  143.                 Template template = getTemplate(eventName);
  144.                 if (template == null) {
  145.                     template = getTemplate("default");
  146.                 }
  147.                 Context ctx = new VelocityContext();
  148.                 ctx.put("eventName", eventName);
  149.                 ctx.put("event", event);
  150.                 template.merge(ctx, traceWriter);
  151.             } catch (Exception e) {
  152.                 log.error("exception while merging template", e);
  153.             }
  154.         }
  155.     }
  156.     protected Template getTemplate(String eventName) {
  157.         Template template = (Template) templates.get(eventName);
  158.         if (template == null) {
  159.             String templateName = "";
  160.             try {
  161.                 templateName = eventName.replace('.', '/') + ".vm";
  162.                 log.debug("loading velocity template '" + templateName);
  163.                 template = velocityEngine.getTemplate(templateName);
  164.                 log.debug("loaded velocity template '" + templateName);
  165.                 templates.put(eventName, template);
  166.             } catch (Exception e) {
  167.                 log.error("exception while loading template " + templateName, e);
  168.             }
  169.         }
  170.         return template;
  171.     }
  172. }