mirror of
https://github.com/apache/zeppelin
synced 2026-05-24 09:38:26 +00:00
[ZEPPELIN-1999] get interpreter property with replaced context parameters
This commit is contained in:
parent
5144050f99
commit
b5424b9998
3 changed files with 75 additions and 3 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
|
@ -95,6 +95,8 @@ Thumbs.db
|
|||
# intelliJ IDEA project files
|
||||
.idea/
|
||||
*.iml
|
||||
*.iws
|
||||
*.ipr
|
||||
|
||||
# maven target files
|
||||
target/
|
||||
|
|
|
|||
|
|
@ -18,15 +18,19 @@
|
|||
package org.apache.zeppelin.interpreter;
|
||||
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.net.URL;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.apache.zeppelin.annotation.ZeppelinApi;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.apache.commons.lang.reflect.FieldUtils;
|
||||
import org.apache.zeppelin.annotation.Experimental;
|
||||
import org.apache.zeppelin.annotation.ZeppelinApi;
|
||||
import org.apache.zeppelin.interpreter.thrift.InterpreterCompletion;
|
||||
import org.apache.zeppelin.scheduler.Scheduler;
|
||||
import org.apache.zeppelin.scheduler.SchedulerFactory;
|
||||
|
|
@ -157,6 +161,8 @@ public abstract class Interpreter {
|
|||
}
|
||||
}
|
||||
|
||||
replaceContextParameters(p);
|
||||
|
||||
return p;
|
||||
}
|
||||
|
||||
|
|
@ -296,6 +302,38 @@ public abstract class Interpreter {
|
|||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Replace markers #{contextFieldName} by values from {@link InterpreterContext} fields
|
||||
* with same name and marker #{user}. If value == null then replace by empty string.
|
||||
*/
|
||||
private void replaceContextParameters(Properties properties) {
|
||||
InterpreterContext interpreterContext = InterpreterContext.get();
|
||||
if (interpreterContext != null) {
|
||||
String markerTemplate = "#\\{%s\\}";
|
||||
List typesToProcess = Arrays.asList(String.class, Double.class, Float.class, Short.class,
|
||||
Byte.class, Character.class, Boolean.class, Integer.class, Long.class);
|
||||
for (String key : properties.stringPropertyNames()) {
|
||||
String p = properties.getProperty(key);
|
||||
if (StringUtils.isNotEmpty(p)) {
|
||||
for (Field field : InterpreterContext.class.getDeclaredFields()) {
|
||||
Class clazz = field.getType();
|
||||
if (typesToProcess.contains(clazz) || clazz.isPrimitive()) {
|
||||
try {
|
||||
Object value = FieldUtils.readField(field, interpreterContext, true);
|
||||
p = p.replaceAll(String.format(markerTemplate, field.getName()),
|
||||
value != null ? value.toString() : StringUtils.EMPTY);
|
||||
} catch (Exception e) {
|
||||
logger.error("Cannot replace context parameter", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
p = p.replaceAll(String.format(markerTemplate, "user"),
|
||||
StringUtils.defaultString(userName, StringUtils.EMPTY));
|
||||
properties.setProperty(key, p);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Type of interpreter.
|
||||
|
|
|
|||
|
|
@ -17,13 +17,14 @@
|
|||
|
||||
package org.apache.zeppelin.interpreter;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
import org.apache.zeppelin.interpreter.remote.mock.MockInterpreterA;
|
||||
import org.apache.zeppelin.user.AuthenticationInfo;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class InterpreterTest {
|
||||
|
||||
@Test
|
||||
|
|
@ -60,4 +61,35 @@ public class InterpreterTest {
|
|||
assertEquals("v2", intp.getProperty("p2"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPropertyWithReplacedContextFields() {
|
||||
String noteId = "testNoteId";
|
||||
String paragraphTitle = "testParagraphTitle";
|
||||
String user = "username";
|
||||
InterpreterContext.set(new InterpreterContext(noteId,
|
||||
null,
|
||||
null,
|
||||
paragraphTitle,
|
||||
null,
|
||||
new AuthenticationInfo("testUser", "testTicket"),
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null));
|
||||
Properties p = new Properties();
|
||||
p.put("p1", "paragraphTitle #{noteId}, #{paragraphTitle}, #{replName}, #{noteId}, #{user}," +
|
||||
" #{authenticationInfo}");
|
||||
MockInterpreterA intp = new MockInterpreterA(p);
|
||||
intp.setUserName(user);
|
||||
String actual = intp.getProperty("p1");
|
||||
InterpreterContext.remove();
|
||||
|
||||
assertEquals(
|
||||
String.format("paragraphTitle %s, %s, , %s, %s, #{authenticationInfo}", noteId,
|
||||
paragraphTitle, noteId, user),
|
||||
actual
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue