Skip to content

Commit f684fb1

Browse files
fanstekoentsje
authored andcommitted
HBX-3165: Support zip and jar archives as TEMPLATE_PATH
1 parent 7f03b6b commit f684fb1

File tree

1 file changed

+24
-8
lines changed

1 file changed

+24
-8
lines changed

orm/src/main/java/org/hibernate/tool/internal/export/common/TemplateHelper.java

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
import java.io.Reader;
2525
import java.io.StringReader;
2626
import java.io.Writer;
27+
import java.net.MalformedURLException;
28+
import java.net.URL;
29+
import java.net.URLClassLoader;
2730
import java.util.ArrayList;
2831
import java.util.Date;
2932
import java.util.List;
@@ -34,6 +37,7 @@
3437
import freemarker.cache.ClassTemplateLoader;
3538
import freemarker.cache.FileTemplateLoader;
3639
import freemarker.cache.MultiTemplateLoader;
40+
import freemarker.cache.URLTemplateLoader;
3741
import freemarker.cache.TemplateLoader;
3842
import freemarker.ext.beans.BeansWrapperBuilder;
3943
import freemarker.template.Configuration;
@@ -79,15 +83,27 @@ public void init(File outputDirectory, String[] templatePaths) {
7983

8084
for (int i = 0; i < templatePaths.length; i++) {
8185
File file = new File(templatePaths[i]);
82-
if(file.exists() && file.isDirectory()) {
83-
try {
84-
loaders.add(new FileTemplateLoader(file));
85-
}
86-
catch (IOException e) {
87-
throw new RuntimeException("Problems with templatepath " + file, e);
88-
}
86+
if(file.exists()) {
87+
if (file.isDirectory()) {
88+
try {
89+
loaders.add(new FileTemplateLoader(file));
90+
} catch (IOException e) {
91+
throw new RuntimeException("Problems with templatepath " + file, e);
92+
}
93+
} else if (file.getName().endsWith(".zip") || file.getName().endsWith(".jar")) {
94+
final URLClassLoader classLoaderForZip;
95+
try {
96+
classLoaderForZip = new URLClassLoader(new URL[]{ file.toURI().toURL() }, null);
97+
} catch (MalformedURLException e) {
98+
throw new RuntimeException("template path " + file + " is not a valid zip file", e);
99+
}
100+
101+
loaders.add(new ClassTemplateLoader(classLoaderForZip, "/"));
102+
} else {
103+
log.warn("template path " + file + " is not a directory");
104+
}
89105
} else {
90-
log.warn("template path" + file + " either does not exist or is not a directory");
106+
log.warn("template path " + file + " does not exist");
91107
}
92108
}
93109
loaders.add(new ClassTemplateLoader(this.getClass(),"/")); // the template names are like pojo/Somewhere so have to be a rooted classpathloader

0 commit comments

Comments
 (0)