Better way to find resource dir for InterpreterOutput watcher

This commit is contained in:
Lee moon soo 2016-04-13 13:29:34 +01:00
parent 024d7fc2c5
commit 71f814dce4
3 changed files with 35 additions and 19 deletions

View file

@ -22,8 +22,10 @@ import org.slf4j.LoggerFactory;
import java.io.*;
import java.net.URL;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
/**
* InterpreterOutput is OutputStream that supposed to print content on notebook
@ -32,6 +34,7 @@ import java.util.List;
public class InterpreterOutput extends OutputStream {
Logger logger = LoggerFactory.getLogger(InterpreterOutput.class);
private final int NEW_LINE_CHAR = '\n';
private List<String> resourceSearchPaths = Collections.synchronizedList(new LinkedList<String>());
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
@ -157,26 +160,32 @@ public class InterpreterOutput extends OutputStream {
}
}
public void writeResource(String resourceName) throws IOException {
// search file under resource dir first for dev mode
File mainResource = new File("./src/main/resources/" + resourceName);
File testResource = new File("./src/test/resources/" + resourceName);
if (mainResource.isFile()) {
write(mainResource);
} else if (testResource.isFile()) {
write(testResource);
} else {
// search from classpath
ClassLoader cl = Thread.currentThread().getContextClassLoader();
if (cl == null) {
cl = this.getClass().getClassLoader();
}
if (cl == null) {
cl = ClassLoader.getSystemClassLoader();
}
public void addResourceSearchPath(String path) {
resourceSearchPaths.add(path);
}
write(cl.getResource(resourceName));
public void writeResource(String resourceName) throws IOException {
// search file under provided paths first, for dev mode
for (String path : resourceSearchPaths) {
File res = new File(path + "/" + resourceName);
logger.info("path = " + path + ", res = " + resourceName);
logger.info("Search resource " + res.getAbsolutePath());
if (res.isFile()) {
write(res);
return;
}
}
// search from classpath
ClassLoader cl = Thread.currentThread().getContextClassLoader();
if (cl == null) {
cl = this.getClass().getClassLoader();
}
if (cl == null) {
cl = ClassLoader.getSystemClassLoader();
}
write(cl.getResource(resourceName));
}
public byte[] toByteArray() throws IOException {

View file

@ -19,6 +19,7 @@ package org.apache.zeppelin.interpreter.dev;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.net.URL;
import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.Level;
@ -75,6 +76,13 @@ public class ZeppelinApplicationDevServer extends ZeppelinDevServer {
Class<?> appClass = ClassLoader.getSystemClassLoader().loadClass(className);
Constructor<?> constructor = appClass.getConstructor(
ResourceSet.class, ApplicationContext.class);
// classPath will be ..../target/classes in dev mode most cases
String classPath = appClass.getProtectionDomain().getCodeSource().getLocation().getPath();
context.out.addResourceSearchPath(classPath + "../../src/main/resources/");
context.out.addResourceSearchPath(classPath + "../../src/test/resources/");
app = (Application) constructor.newInstance(resourceSet, getApplicationContext(context));
} catch (Exception e) {
logger.error(e.getMessage(), e);

View file

@ -38,7 +38,6 @@ public class ZeppelinDevServer extends
public static final int DEFAULT_TEST_INTERPRETER_PORT = 29914;
DevInterpreter interpreter = null;
private InterpreterEvent listener;
InterpreterOutput out;
public ZeppelinDevServer(int port) throws TException {
super(port);