KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > javacoding > jspider > mod > plugin > velocity > VelocityPlugin


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 /**
20  *
21  * $Id: VelocityPlugin.java,v 1.7 2003/04/09 17:08:14 vanrogu Exp $
22  *
23  * @author Günther Van Roey
24  */

25 public class VelocityPlugin implements Plugin {
26
27     public static final String JavaDoc MODULE_NAME = "Velocity Template JSpider module";
28     public static final String JavaDoc MODULE_VERSION = "v1.0";
29     public static final String JavaDoc MODULE_DESCRIPTION = "A JSpider module that renders output via velocity templates";
30     public static final String JavaDoc MODULE_VENDOR = "http://www.javacoding.net";
31
32     public static final String JavaDoc TRACE_FILENAME = "trace.filename";
33     public static final String JavaDoc DUMP_FILENAME = "dump.filename";
34     public static final String JavaDoc TRACE_WRITE = "trace.write";
35     public static final String JavaDoc DUMP_WRITE = "dump.write";
36     public static final String JavaDoc TEMPLATEFOLDER = "templatefolder";
37
38     public static final String JavaDoc DEFAULT_TRACE_FILENAME = "velocity-trace.out";
39     public static final String JavaDoc 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 JavaDoc DEFAULT_TEMPLATEFOLDER = "velocity";
43
44     protected String JavaDoc 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 JavaDoc name, PropertySet config) {
57         this.name = name;
58         this.config = config;
59         log = LogFactory.getLog(VelocityPlugin.class);
60     }
61
62     public String JavaDoc getName() {
63         return MODULE_NAME;
64     }
65
66     public String JavaDoc getVersion() {
67         return MODULE_VERSION;
68     }
69
70     public String JavaDoc getDescription() {
71         return MODULE_DESCRIPTION;
72     }
73
74     public String JavaDoc getVendor() {
75         return MODULE_VENDOR;
76     }
77
78     public void initialize() {
79         String JavaDoc traceFileName = config.getString(TRACE_FILENAME, DEFAULT_TRACE_FILENAME);
80         String JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc e) {
160                     log.error("exception while merging template", e);
161                 }
162             }
163         }
164
165         if (writeTrace) {
166             try {
167                 String JavaDoc 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 JavaDoc e) {
177                 log.error("exception while merging template", e);
178             }
179         }
180     }
181
182     protected Template getTemplate(String JavaDoc eventName) {
183         Template template = (Template) templates.get(eventName);
184         if (template == null) {
185             String JavaDoc 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 JavaDoc e) {
193                 log.error("exception while loading template " + templateName, e);
194             }
195         }
196         return template;
197     }
198
199 }
200
Popular Tags