1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.tools.visualizer.config; |
12 | |
|
13 | |
import org.mule.tools.visualizer.postrenderers.MuleDocPostRenderer; |
14 | |
import org.mule.tools.visualizer.util.VelocityLogger; |
15 | |
import org.mule.util.FileUtils; |
16 | |
import org.mule.util.IOUtils; |
17 | |
import org.mule.util.StringUtils; |
18 | |
|
19 | |
import java.io.File; |
20 | |
import java.io.IOException; |
21 | |
import java.net.URL; |
22 | |
|
23 | |
import org.apache.velocity.app.VelocityEngine; |
24 | |
import org.apache.velocity.runtime.log.LogSystem; |
25 | |
import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader; |
26 | |
|
27 | |
public abstract class VelocitySupport |
28 | |
{ |
29 | |
|
30 | |
public static final String FILE = "file"; |
31 | |
public static final String JAR = "jar"; |
32 | |
|
33 | |
|
34 | |
public static final String MAGIC_VELOCITY_RESOURCE_LOADER = "class.resource.loader.class"; |
35 | |
|
36 | |
private VelocityEngine ve; |
37 | |
|
38 | 12 | private GraphEnvironment env = null; |
39 | |
|
40 | |
private static LogSystem logSystem; |
41 | |
|
42 | |
protected VelocitySupport(GraphEnvironment env) throws Exception |
43 | 12 | { |
44 | 12 | this.setEnv(env); |
45 | 12 | logSystem = new VelocityLogger(env); |
46 | 12 | setVe(new VelocityEngine()); |
47 | 12 | getVe().setProperty(VelocityEngine.RUNTIME_LOG_LOGSYSTEM, logSystem); |
48 | 12 | inferVelocityLoaderPath(getVe()); |
49 | 12 | getVe().init(); |
50 | 12 | } |
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
private void inferVelocityLoaderPath(VelocityEngine ve) throws IOException |
58 | |
{ |
59 | 12 | String defaultPath = (String) ve.getProperty(VelocityEngine.FILE_RESOURCE_LOADER_PATH); |
60 | 12 | if (null == defaultPath || StringUtils.isEmpty(defaultPath)) |
61 | |
{ |
62 | 12 | URL url = IOUtils.getResourceAsUrl(MuleDocPostRenderer.DEFAULT_MULE_TEMPLATE, getClass()); |
63 | 12 | if (FILE.equals(url.getProtocol())) |
64 | |
{ |
65 | 12 | String path = FileUtils.getResourcePath(MuleDocPostRenderer.DEFAULT_MULE_TEMPLATE, getClass()); |
66 | 12 | if (!StringUtils.isEmpty(path)) |
67 | |
{ |
68 | 12 | File fullPath = new File(path); |
69 | 12 | File target = new File(MuleDocPostRenderer.DEFAULT_MULE_TEMPLATE); |
70 | |
|
71 | |
|
72 | 36 | while (null != target && !StringUtils.isEmpty(target.getPath())) |
73 | |
{ |
74 | 24 | env.log(fullPath.getPath() + " - " + target.getPath()); |
75 | 24 | target = target.getParentFile(); |
76 | 24 | fullPath = fullPath.getParentFile(); |
77 | |
} |
78 | |
|
79 | 12 | path = fullPath.getPath(); |
80 | 12 | if (path.endsWith("!")) |
81 | |
{ |
82 | 0 | path = path + File.separator; |
83 | |
} |
84 | 12 | getEnv().log(VelocityEngine.FILE_RESOURCE_LOADER_PATH + " = " + path); |
85 | 12 | ve.setProperty(VelocityEngine.FILE_RESOURCE_LOADER_PATH, path); |
86 | |
} |
87 | 12 | } |
88 | 0 | else if (JAR.equals(url.getProtocol())) |
89 | |
{ |
90 | 0 | env.log(url.toString()); |
91 | 0 | ve.setProperty(VelocityEngine.RESOURCE_LOADER, "class"); |
92 | 0 | ve.setProperty(MAGIC_VELOCITY_RESOURCE_LOADER, ClasspathResourceLoader.class.getName()); |
93 | |
} |
94 | |
} |
95 | 12 | } |
96 | |
|
97 | |
protected void setEnv(GraphEnvironment env) |
98 | |
{ |
99 | 20 | this.env = env; |
100 | 20 | } |
101 | |
|
102 | |
protected GraphEnvironment getEnv() |
103 | |
{ |
104 | 12 | return env; |
105 | |
} |
106 | |
|
107 | |
protected void setVe(VelocityEngine ve) |
108 | |
{ |
109 | 12 | this.ve = ve; |
110 | 12 | } |
111 | |
|
112 | |
protected VelocityEngine getVe() |
113 | |
{ |
114 | 48 | return ve; |
115 | |
} |
116 | |
|
117 | |
} |