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