Skip to content
This repository was archived by the owner on Mar 3, 2026. It is now read-only.

Commit efcc2ae

Browse files
feffijknack
authored andcommitted
cleanup jooby-crash (jooby-project#1157)
* fixed minor code issues * removed unused import
1 parent 8254df2 commit efcc2ae

File tree

4 files changed

+86
-88
lines changed

4 files changed

+86
-88
lines changed

modules/jooby-crash/src/main/java/org/jooby/crash/CrashBootstrap.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ class CrashBootstrap extends PluginLifeCycle {
232232

233233
private static final Predicate<Path> ACCEPT = endsWith(".class").negate();
234234

235-
private List<CrashFSDriver> drivers = new ArrayList<>();
235+
private final List<CrashFSDriver> drivers = new ArrayList<>();
236236

237237
public PluginContext start(final ClassLoader loader, final Properties props,
238238
final Map<String, Object> attributes, final Set<CRaSHPlugin<?>> plugins) throws IOException {
@@ -280,7 +280,7 @@ private static ScheduledExecutorService scanner(final String name) {
280280

281281
private static ExecutorService executor(final String name) {
282282
AtomicInteger next = new AtomicInteger(0);
283-
return new ThreadPoolExecutor(0, 10, 60L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>(),
283+
return new ThreadPoolExecutor(0, 10, 60L, TimeUnit.SECONDS, new SynchronousQueue<>(),
284284
r -> {
285285
Thread thread = Executors.defaultThreadFactory().newThread(r);
286286
thread.setName(name + "-" + next.incrementAndGet());

modules/jooby-crash/src/main/java/org/jooby/crash/CrashFSDriver.java

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -231,20 +231,20 @@ class CrashFSDriver extends AbstractFSDriver<Path> implements AutoCloseable {
231231

232232
private static final String LOGIN = "login.groovy";
233233

234-
private static Predicate<Path> FLOGIN = fname(LOGIN);
234+
private static final Predicate<Path> FLOGIN = fname(LOGIN);
235235

236236
private static final String JAR = ".jar!";
237237

238238
/** The logging system. */
239239
private final Logger log = LoggerFactory.getLogger(getClass());
240240

241-
private Path root;
241+
private final Path root;
242242

243243
private FileSystem fs;
244244

245245
private Predicate<Path> filter;
246246

247-
private URI src;
247+
private final URI src;
248248

249249
private CrashFSDriver(final URI src, final FileSystem fs, final Predicate<Path> filter) {
250250
this.src = src;
@@ -263,43 +263,40 @@ private CrashFSDriver(final URI src, final Path root, final Predicate<Path> filt
263263
}
264264

265265
@Override
266-
public Path root() throws IOException {
266+
public Path root() {
267267
return root;
268268
}
269269

270270
@Override
271-
public String name(final Path handle) throws IOException {
271+
public String name(final Path handle) {
272272
return Optional.ofNullable(handle.getFileName()).orElse(handle).toString().replaceAll("/", "");
273273
}
274274

275275
@Override
276-
public boolean isDir(final Path handle) throws IOException {
276+
public boolean isDir(final Path handle) {
277277
return Files.isDirectory(handle);
278278
}
279279

280280
@Override
281281
public Iterable<Path> children(final Path handle) throws IOException {
282282
try (Stream<Path> walk = Files.walk(handle)) {
283-
List<Path> children = walk
283+
return walk
284284
.skip(1)
285285
.filter(filter)
286286
.collect(Collectors.toList());
287-
return children;
288287
}
289288
}
290289

291290
public boolean exists(final Predicate<Path> filter) {
292291
try (Stream<Path> walk = Try.apply(() -> Files.walk(root)).orElse(Stream.empty())) {
293292
return walk
294293
.skip(1)
295-
.filter(filter)
296-
.findFirst()
297-
.isPresent();
294+
.anyMatch(filter);
298295
}
299296
}
300297

301298
@Override
302-
public long getLastModified(final Path handle) throws IOException {
299+
public long getLastModified(final Path handle) {
303300
return Try.apply(() -> Files.getLastModifiedTime(handle).toMillis()).orElse(-1L);
304301
}
305302

@@ -354,12 +351,10 @@ private static List<URI> expandPath(final ClassLoader loader, final String patte
354351
if (file.exists()) {
355352
result.add(file.toURI());
356353
}
357-
Try.run(() -> {
358-
Collections.list(loader.getResources(pattern))
359-
.stream()
360-
.map(it -> Try.apply(() -> it.toURI()).get())
361-
.forEach(result::add);
362-
});
354+
Try.run(() -> Collections.list(loader.getResources(pattern))
355+
.stream()
356+
.map(it -> Try.apply(it::toURI).get())
357+
.forEach(result::add));
363358
return result;
364359
}
365360

modules/jooby-crash/src/main/java/org/jooby/crash/SimpleProcessContext.java

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -211,18 +211,17 @@
211211
import org.jooby.Results;
212212
import org.jooby.Status;
213213

214-
import java.io.IOException;
215214
import java.util.function.Consumer;
216215

217216
class SimpleProcessContext implements ShellProcessContext {
218217

219-
private StringBuilder buff = new StringBuilder();
218+
private final StringBuilder buff = new StringBuilder();
220219

221-
private Consumer<Result> deferred;
220+
private final Consumer<Result> deferred;
222221

223-
private int width;
222+
private final int width;
224223

225-
private int height;
224+
private final int height;
226225

227226
public SimpleProcessContext(final Consumer<Result> deferred) {
228227
this(deferred, 204, 48);
@@ -236,12 +235,12 @@ public SimpleProcessContext(final Consumer<Result> deferred, final int width,
236235
}
237236

238237
@Override
239-
public boolean takeAlternateBuffer() throws IOException {
238+
public boolean takeAlternateBuffer() {
240239
return false;
241240
}
242241

243242
@Override
244-
public boolean releaseAlternateBuffer() throws IOException {
243+
public boolean releaseAlternateBuffer() {
245244
return false;
246245
}
247246

@@ -252,7 +251,7 @@ public String getProperty(final String propertyName) {
252251

253252
@Override
254253
public String readLine(final String msg, final boolean echo)
255-
throws IOException, InterruptedException, IllegalStateException {
254+
throws IllegalStateException {
256255
return null;
257256
}
258257

@@ -267,34 +266,33 @@ public int getHeight() {
267266
}
268267

269268
@Override
270-
public void flush() throws IOException {
269+
public void flush() {
271270
}
272271

273272
@Override
274-
public Screenable append(final Style style) throws IOException {
273+
public Screenable append(final Style style) {
275274
return this;
276275
}
277276

278277
@Override
279-
public Screenable cls() throws IOException {
278+
public Screenable cls() {
280279
return this;
281280
}
282281

283282
@Override
284-
public Appendable append(final CharSequence csq) throws IOException {
283+
public Appendable append(final CharSequence csq) {
285284
buff.append(csq);
286285
return this;
287286
}
288287

289288
@Override
290-
public Appendable append(final CharSequence csq, final int start, final int end)
291-
throws IOException {
289+
public Appendable append(final CharSequence csq, final int start, final int end) {
292290
buff.append(csq, start, end);
293291
return this;
294292
}
295293

296294
@Override
297-
public Appendable append(final char c) throws IOException {
295+
public Appendable append(final char c) {
298296
buff.append(c);
299297
return this;
300298
}

modules/jooby-crash/src/main/java/org/jooby/crash/WebShellHandler.java

Lines changed: 58 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -235,71 +235,76 @@ public void onOpen(final Request req, final WebSocket ws) throws Exception {
235235
ShellFactory factory = ctx.getPlugin(ShellFactory.class);
236236
Shell shell = factory.create(null);
237237

238-
AtomicReference<ShellProcess> process = new AtomicReference<ShellProcess>();
238+
AtomicReference<ShellProcess> process = new AtomicReference<>();
239239

240240
ws.onMessage(msg -> {
241241
Map event = msg.to(Map.class);
242242
String type = (String) event.get("type");
243-
if (type.equals("welcome")) {
244-
log.debug("sending welcome + prompt");
245-
ws.send(event("print", shell.getWelcome()));
246-
ws.send(event("prompt", shell.getPrompt()));
247-
} else if (type.equals("execute")) {
248-
String command = (String) event.get("command");
249-
Integer width = (Integer) event.get("width");
250-
Integer height = (Integer) event.get("height");
251-
process.set(shell.createProcess(command));
252-
SimpleProcessContext context = new SimpleProcessContext(r -> {
253-
Try.run(() -> {
254-
// reset process
255-
process.set(null);
243+
switch (type) {
244+
case "welcome":
245+
log.debug("sending welcome + prompt");
246+
ws.send(event("print", shell.getWelcome()));
247+
ws.send(event("prompt", shell.getPrompt()));
248+
break;
249+
case "execute":
250+
String command = (String) event.get("command");
251+
Integer width = (Integer) event.get("width");
252+
Integer height = (Integer) event.get("height");
253+
process.set(shell.createProcess(command));
254+
SimpleProcessContext context = new SimpleProcessContext(r -> {
255+
Try.run(() -> {
256+
// reset process
257+
process.set(null);
256258

257-
ws.send(event("print", r.get()));
258-
ws.send(event("prompt", shell.getPrompt()));
259-
ws.send(event("end"));
260-
}).onFailure(x -> log.error("error found while sending output", x));
259+
ws.send(event("print", r.get()));
260+
ws.send(event("prompt", shell.getPrompt()));
261+
ws.send(event("end"));
262+
}).onFailure(x -> log.error("error found while sending output", x));
261263

262-
if ("bye".equals(command)) {
263-
ws.close(WebSocket.NORMAL);
264-
}
265-
}, width, height);
266-
log.debug("executing {}", command);
267-
process.get().execute(context);
268-
} else if (type.equals("cancel")) {
269-
ShellProcess p = process.get();
270-
if (p != null) {
271-
log.info("cancelling {}", p);
272-
p.cancel();
273-
}
274-
} else if (type.equals("complete")) {
275-
String prefix = (String) event.get("prefix");
276-
CompletionMatch completion = shell.complete(prefix);
277-
Completion completions = completion.getValue();
278-
Delimiter delimiter = completion.getDelimiter();
279-
StringBuilder sb = new StringBuilder();
280-
List<String> values = new ArrayList<String>();
281-
if (completions.getSize() == 1) {
282-
String value = completions.getValues().iterator().next();
283-
delimiter.escape(value, sb);
284-
if (completions.get(value)) {
285-
sb.append(delimiter.getValue());
264+
if ("bye".equals(command)) {
265+
ws.close(WebSocket.NORMAL);
266+
}
267+
}, width, height);
268+
log.debug("executing {}", command);
269+
process.get().execute(context);
270+
break;
271+
case "cancel":
272+
ShellProcess p = process.get();
273+
if (p != null) {
274+
log.info("cancelling {}", p);
275+
p.cancel();
286276
}
287-
values.add(sb.toString());
288-
} else {
289-
String commonCompletion = Utils.findLongestCommonPrefix(completions.getValues());
290-
if (commonCompletion.length() > 0) {
291-
delimiter.escape(commonCompletion, sb);
277+
break;
278+
case "complete":
279+
String prefix = (String) event.get("prefix");
280+
CompletionMatch completion = shell.complete(prefix);
281+
Completion completions = completion.getValue();
282+
Delimiter delimiter = completion.getDelimiter();
283+
StringBuilder sb = new StringBuilder();
284+
List<String> values = new ArrayList<>();
285+
if (completions.getSize() == 1) {
286+
String value = completions.getValues().iterator().next();
287+
delimiter.escape(value, sb);
288+
if (completions.get(value)) {
289+
sb.append(delimiter.getValue());
290+
}
292291
values.add(sb.toString());
293292
} else {
294-
for (Map.Entry<String, Boolean> entry : completions) {
295-
delimiter.escape(entry.getKey(), sb);
293+
String commonCompletion = Utils.findLongestCommonPrefix(completions.getValues());
294+
if (commonCompletion.length() > 0) {
295+
delimiter.escape(commonCompletion, sb);
296296
values.add(sb.toString());
297-
sb.setLength(0);
297+
} else {
298+
for (Map.Entry<String, Boolean> entry : completions) {
299+
delimiter.escape(entry.getKey(), sb);
300+
values.add(sb.toString());
301+
sb.setLength(0);
302+
}
298303
}
299304
}
300-
}
301-
log.debug("completing {} with {}", prefix, values);
302-
ws.send(event("complete", values));
305+
log.debug("completing {} with {}", prefix, values);
306+
ws.send(event("complete", values));
307+
break;
303308
}
304309
});
305310

0 commit comments

Comments
 (0)