Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import org.apache.cayenne.map.QueryDescriptor;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.app.VelocityEngine;
import org.slf4j.Logger;

Expand Down Expand Up @@ -290,6 +291,8 @@ private Template getTemplate(TemplateType type) {
VelocityEngine velocityEngine = new VelocityEngine();
velocityEngine.init(props);

Velocity.setProperty("cayenne.cgen.rootpath", cgenConfiguration.getDataMap().getConfigurationSource());

template = velocityEngine.getTemplate(templateName);
templateCache.put(templateName, template);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.nio.file.Paths;

import org.apache.cayenne.resource.Resource;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.exception.ResourceNotFoundException;
import org.apache.velocity.runtime.resource.loader.FileResourceLoader;

Expand All @@ -40,6 +45,8 @@
// instantiated via reflection by Velocity
public class ClassGeneratorResourceLoader extends FileResourceLoader {

private static final String CGEN_ROOT_PATH ="cayenne.cgen.rootpath";

/**
* Returns resource as InputStream. First calls super implementation. If resource
* wasn't found, it attempts to load it from current directory or as an absolute path.
Expand All @@ -48,38 +55,41 @@ public class ClassGeneratorResourceLoader extends FileResourceLoader {
public synchronized Reader getResourceReader(String name, String charset)
throws ResourceNotFoundException {

Reader stream = loadFromRelativePath(name, charset);
Reader stream;
stream = loadFromThreadClassLoader(name);
if (stream != null) {
return stream;
}

stream = loadFromAbsPath(name);
stream = loadFromThisClassLoader(name);
if (stream != null) {
return stream;
}

stream = loadFromThreadClassLoader(name);
stream= loadFromRelativePath(name);
if (stream != null) {
return stream;
}

stream = loadFromThisClassLoader(name);
stream = loadFromAbsPath(name);
if (stream != null) {
return stream;
}

throw new ResourceNotFoundException("Couldn't find resource '"
+ name
+ "'. Searched filesystem path and classpath");
}

protected Reader loadFromRelativePath(String name, String charset) {
protected Reader loadFromRelativePath(String name) {
Resource rootPath = (Resource) Velocity.getProperty(CGEN_ROOT_PATH);
Path datamapPath;
try {
return super.getResourceReader(name, charset);
}
catch (ResourceNotFoundException rnfex) {
datamapPath = Paths.get(rootPath.getURL().toURI()).getParent();
} catch (URISyntaxException use) {
return null;
}
Path absolutePath = datamapPath.resolve(name).normalize();
return loadFromAbsPath(absolutePath.toString());
}

protected Reader loadFromAbsPath(String name) {
Expand Down