Skip to content

Commit f25b0f3

Browse files
committed
ZOOKEEPER-3938: Upgrade JLine to 3.30.6
JLine API has been changed between 2x and 3.x quite much so JLineZNodeCompleter and ZooKeeperMain had to be changed quite much.
1 parent d8e5217 commit f25b0f3

File tree

7 files changed

+34
-38
lines changed

7 files changed

+34
-38
lines changed

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,7 @@
562562
<netty.version>4.1.127.Final</netty.version>
563563
<jetty.version>9.4.57.v20241219</jetty.version>
564564
<jackson.version>2.15.2</jackson.version>
565-
<jline.version>2.14.6</jline.version>
565+
<jline.version>3.25.1</jline.version>
566566
<snappy.version>1.1.10.5</snappy.version>
567567
<kerby.version>2.0.0</kerby.version>
568568
<bouncycastle.version>1.78</bouncycastle.version>
@@ -748,7 +748,7 @@
748748
<version>${jackson.version}</version>
749749
</dependency>
750750
<dependency>
751-
<groupId>jline</groupId>
751+
<groupId>org.jline</groupId>
752752
<artifactId>jline</artifactId>
753753
<version>${jline.version}</version>
754754
</dependency>

zookeeper-assembly/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@
100100
<artifactId>jackson-databind</artifactId>
101101
</dependency>
102102
<dependency>
103-
<groupId>jline</groupId>
103+
<groupId>org.jline</groupId>
104104
<artifactId>jline</artifactId>
105105
</dependency>
106106
<dependency>

zookeeper-contrib/zookeeper-contrib-fatjar/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@
7979
<artifactId>jackson-databind</artifactId>
8080
</dependency>
8181
<dependency>
82-
<groupId>jline</groupId>
82+
<groupId>org.jline</groupId>
8383
<artifactId>jline</artifactId>
8484
</dependency>
8585
<dependency>

zookeeper-server/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@
116116
<scope>test</scope>
117117
</dependency>
118118
<dependency>
119-
<groupId>jline</groupId>
119+
<groupId>org.jline</groupId>
120120
<artifactId>jline</artifactId>
121121
<scope>provided</scope>
122122
</dependency>

zookeeper-server/src/main/java/org/apache/zookeeper/JLineZNodeCompleter.java

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,46 +18,44 @@
1818

1919
package org.apache.zookeeper;
2020

21+
import org.jline.reader.Candidate;
22+
import org.jline.reader.Completer;
23+
import org.jline.reader.LineReader;
24+
import org.jline.reader.ParsedLine;
25+
import org.jline.utils.AttributedString;
26+
2127
import java.util.Collections;
2228
import java.util.List;
23-
import jline.console.completer.Completer;
2429

2530
class JLineZNodeCompleter implements Completer {
2631

27-
private ZooKeeper zk;
32+
private final ZooKeeper zk;
2833

2934
public JLineZNodeCompleter(ZooKeeper zk) {
3035
this.zk = zk;
3136
}
3237

33-
@SuppressWarnings({"unchecked", "rawtypes"})
34-
public int complete(String buffer, int cursor, List candidates) {
38+
@Override
39+
public void complete(LineReader lineReader, ParsedLine commandLine, List<Candidate> candidates) {
3540
// Guarantee that the final token is the one we're expanding
36-
buffer = buffer.substring(0, cursor);
37-
String token = "";
38-
if (!buffer.endsWith(" ")) {
39-
String[] tokens = buffer.split(" ");
40-
if (tokens.length != 0) {
41-
token = tokens[tokens.length - 1];
42-
}
43-
}
41+
String token = commandLine.words().get(commandLine.words().size() - 1);
4442

4543
if (token.startsWith("/")) {
46-
return completeZNode(buffer, token, candidates);
44+
completeZNode(token, candidates);
45+
} else {
46+
completeCommand(token, candidates);
4747
}
48-
return completeCommand(buffer, token, candidates);
4948
}
5049

51-
private int completeCommand(String buffer, String token, List<String> candidates) {
50+
private void completeCommand(String token, List<Candidate> candidates) {
5251
for (String cmd : ZooKeeperMain.getCommands()) {
5352
if (cmd.startsWith(token)) {
54-
candidates.add(cmd);
53+
candidates.add(createCandidate(cmd));
5554
}
5655
}
57-
return buffer.lastIndexOf(" ") + 1;
5856
}
5957

60-
private int completeZNode(String buffer, String token, List<String> candidates) {
58+
private void completeZNode(String token, List<Candidate> candidates) {
6159
String path = token;
6260
int idx = path.lastIndexOf("/") + 1;
6361
String prefix = path.substring(idx);
@@ -67,16 +65,17 @@ private int completeZNode(String buffer, String token, List<String> candidates)
6765
List<String> children = zk.getChildren(dir, false);
6866
for (String child : children) {
6967
if (child.startsWith(prefix)) {
70-
candidates.add(child);
68+
String zNode = dir + (idx == 1 ? "" : "/") + child;
69+
candidates.add(createCandidate(zNode));
7170
}
7271
}
73-
} catch (InterruptedException e) {
74-
return 0;
75-
} catch (KeeperException e) {
76-
return 0;
72+
} catch (InterruptedException | KeeperException e) {
73+
return;
7774
}
7875
Collections.sort(candidates);
79-
return candidates.size() == 0 ? buffer.length() : buffer.lastIndexOf("/") + 1;
8076
}
8177

78+
private static Candidate createCandidate(String cmd) {
79+
return new Candidate(AttributedString.stripAnsi(cmd), cmd, null, null, null, null, true);
80+
}
8281
}

zookeeper-server/src/main/java/org/apache/zookeeper/ZooKeeperMain.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -311,20 +311,18 @@ void run() throws IOException, InterruptedException {
311311
boolean jlinemissing = false;
312312
// only use jline if it's in the classpath
313313
try {
314-
Class<?> consoleC = Class.forName("jline.console.ConsoleReader");
314+
Class<?> readerC = Class.forName("org.jline.reader.LineReader");
315315
Class<?> completerC = Class.forName("org.apache.zookeeper.JLineZNodeCompleter");
316316

317317
System.out.println("JLine support is enabled");
318318

319-
Object console = consoleC.getConstructor().newInstance();
320-
321319
Object completer = completerC.getConstructor(ZooKeeper.class).newInstance(zk);
322-
Method addCompleter = consoleC.getMethod("addCompleter", Class.forName("jline.console.completer.Completer"));
323-
addCompleter.invoke(console, completer);
320+
Object terminal = createTerminal();
321+
Object reader = createLineReader(terminal, completer);
324322

325323
String line;
326-
Method readLine = consoleC.getMethod("readLine", String.class);
327-
while ((line = (String) readLine.invoke(console, getPrompt())) != null) {
324+
Method readLine = readerC.getMethod("readLine", String.class);
325+
while ((line = (String) readLine.invoke(reader, getPrompt())) != null) {
328326
executeLine(line);
329327
}
330328
} catch (ClassNotFoundException

zookeeper-server/src/test/java/org/apache/zookeeper/server/ClientSSLReloadTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import java.util.Properties;
2626
import java.util.concurrent.CountDownLatch;
2727
import java.util.concurrent.TimeUnit;
28-
import jline.internal.Log;
2928
import org.apache.commons.io.FileUtils;
3029
import org.apache.zookeeper.PortAssignment;
3130
import org.apache.zookeeper.WatchedEvent;
@@ -144,7 +143,7 @@ public void certificateReloadTest() throws Exception {
144143
assertTrue(l.await(10, TimeUnit.SECONDS));
145144
}
146145

147-
Log.info("Updating keyStore & trustStore files !!!!");
146+
LOG.info("Updating keyStore & trustStore files !!!!");
148147
// Update the keyStoreFile1 and trustStoreFile1 files in the filesystem with keyStoreFile2 & trustStoreFile2
149148
FileUtils.writeStringToFile(keyStoreFile1, FileUtils.readFileToString(keyStoreFile2, StandardCharsets.US_ASCII), StandardCharsets.US_ASCII, false);
150149
FileUtils.writeStringToFile(trustStoreFile1, FileUtils.readFileToString(trustStoreFile2, StandardCharsets.US_ASCII), StandardCharsets.US_ASCII, false);

0 commit comments

Comments
 (0)