Skip to content

Commit

Permalink
Runnable GF Embedded: Cleanup based on review comments
Browse files Browse the repository at this point in the history
Turned WordWraper into a direct collector WordWrapCollector.
Moved the man page into the proper module and location.
INFO level for output from admin commands
  • Loading branch information
OndroMih committed Sep 17, 2024
1 parent 5713d41 commit a66fb8c
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ private void executeCommandFromString(String stringCommand) {
? commandRunner.run(command) : commandRunner.run(command, commandParams);
switch (result.getExitStatus()) {
case SUCCESS:
logger.log(FINE, () -> "SUCCESS: " + result.getOutput());
logger.log(INFO, () -> "SUCCESS: " + result.getOutput());
break;
default:
if (result.getFailureCause() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.stream.Stream;

import org.glassfish.embeddable.GlassFishProperties;
import org.glassfish.runnablejar.UberMain;

/**
*
Expand Down Expand Up @@ -189,27 +190,25 @@ public void printHelp(Consumer<String> lineConsumer) {
}

private String replaceArguments(String line) {
final WordWrapper wordWrapper = new WordWrapper(HELP_LINE_LENGTH, 40, HELP_LINE_INDENT);
final WordWrapCollector wordWrapper = new WordWrapCollector(HELP_LINE_LENGTH, 40, HELP_LINE_INDENT);

final String arguments = Stream.concat(
Arrays.stream(Option.values())
.map(option -> "[" + option.getUsage() + "]"),
Stream.of("[applications or admin commands...]")
)
.map(wordWrapper::map)
.collect(Collectors.joining(" "));
.collect(wordWrapper);
return line.replace("${ARGUMENTS}", arguments);
}

private String replaceOptions(String line) {
final String options = Arrays.stream(Option.values())
.map(option -> {
final WordWrapper wordWrapper = new WordWrapper(HELP_LINE_LENGTH, HELP_LINE_INDENT.length(), HELP_LINE_INDENT);
final WordWrapCollector wordWrapper = new WordWrapCollector(HELP_LINE_LENGTH, HELP_LINE_INDENT.length(), HELP_LINE_INDENT);
return HELP_FIRST_LINE_INDENT + option.getUsage() + "\n"
+ HELP_LINE_INDENT
+ getStreamOfWords(option.getHelpText())
.map(wordWrapper::map)
.collect(Collectors.joining(" "));
.collect(wordWrapper);
})
.collect(Collectors.joining("\n\n"));
return line.replace(
Expand All @@ -221,7 +220,8 @@ protected Stream<String> getStreamOfWords(String helpText) {
}

private BufferedReader openManPageReader() {
final InputStream manPageInputStream = this.getClass().getClassLoader().getResourceAsStream("manpages/glassfish-embedded.1");
final InputStream manPageInputStream = this.getClass().getClassLoader().getResourceAsStream(
UberMain.class.getPackageName().replace(".", "/") + "/glassfish-embedded.1");
return new BufferedReader(new InputStreamReader(manPageInputStream, StandardCharsets.UTF_8));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,18 @@
*/
public class UnknownPropertyException extends Exception {

private String key;
private String value;
private final String key;
private final String value;

public UnknownPropertyException(String key, String value) {
this.key = key;
this.value = value;
}

public String getKey() {
return key;
}

public String getValue() {
return value;
}

@Override
public String getMessage() {
return "Unknown property " + key + "=" + value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,75 +26,53 @@
*
* @author Ondro Mihalyi
*/
public class WordWrapper {
public final class WordWrapCollector implements Collector<String, StringBuilder, String>{

final int MAX_LINE_LENGTH;
final String HELP_LINE_INDENT;
int characterCount;
private final int MAX_LINE_LENGTH;
private final String HELP_LINE_INDENT;
private int characterCount;

public WordWrapper(int maxLineLength, int initialIndent, String otherLinesIndentText) {
public WordWrapCollector(int maxLineLength, int initialIndent, String otherLinesIndentText) {
this.MAX_LINE_LENGTH = maxLineLength;
this.HELP_LINE_INDENT = otherLinesIndentText;
this.characterCount = initialIndent;
}

public String map(String optionText) {
String[] optionTextLines = optionText.split("\n", -1);
StringBuilder sb = new StringBuilder();
boolean first = true;
for (String optionTextLine : optionTextLines) {
if (!first) {
sb.append("\n" + HELP_LINE_INDENT);
characterCount = HELP_LINE_INDENT.length();
}
characterCount += optionTextLine.length() + 1;
if (characterCount > MAX_LINE_LENGTH) {
final String newOptionText = HELP_LINE_INDENT + optionTextLine;
characterCount = newOptionText.length();
sb.append("\n").append(newOptionText);
} else {
sb.append(optionTextLine);
}
first = false;
}
return sb.toString();
@Override
public Supplier<StringBuilder> supplier() {
return StringBuilder::new;
}

public Collector<String, StringBuilder, String> collector() {
return new Collector<String, StringBuilder, String>() {
@Override
public Supplier<StringBuilder> supplier() {
return StringBuilder::new;
}

@Override
public BiConsumer<StringBuilder, String> accumulator() {
return (assembly, item) -> appendItem(assembly, item);
@Override
public BiConsumer<StringBuilder, String> accumulator() {
return (assembly, item) -> {
for (String word : item.split(" ")) {
appendItem(assembly, word);
}
};
}

@Override
public BinaryOperator<StringBuilder> combiner() {
return (sb1, sb2) -> sb1.append(sb2);
}
@Override
public BinaryOperator<StringBuilder> combiner() {
return (sb1, sb2) -> sb1.append(sb2);
}

@Override
public Function<StringBuilder, String> finisher() {
return StringBuilder::toString;
}
@Override
public Function<StringBuilder, String> finisher() {
return StringBuilder::toString;
}

@Override
public Set<Collector.Characteristics> characteristics() {
return Set.of();
}
};
@Override
public Set<Collector.Characteristics> characteristics() {
return Set.of();
}

protected static char lastChar(StringBuilder assembly) {
private static char lastChar(StringBuilder assembly) {
return assembly.charAt(assembly.length() - 1);
}

protected void appendItem(StringBuilder assembly, String item) {
final String modifiedItem = map(item);
private void appendItem(StringBuilder assembly, String item) {
final String modifiedItem = wrapWord(item);
if (assembly.length() == 0
|| Set.of('\n', ' ').contains(lastChar(assembly))
|| modifiedItem.startsWith("\n")
Expand All @@ -105,4 +83,28 @@ protected void appendItem(StringBuilder assembly, String item) {
}
assembly.append(modifiedItem);
}

// move word to the next row if needs to be wrapped
private String wrapWord(String word) {
String[] lines = word.split("\n", -1);
StringBuilder sb = new StringBuilder();
boolean first = true;
for (String line : lines) {
if (!first) {
sb.append("\n").append(HELP_LINE_INDENT);
characterCount = HELP_LINE_INDENT.length();
}
characterCount += line.length() + 1;
if (characterCount > MAX_LINE_LENGTH) {
final String modifiedLine = HELP_LINE_INDENT + line;
characterCount = modifiedLine.length();
sb.append("\n").append(modifiedLine);
} else {
sb.append(line);
}
first = false;
}
return sb.toString();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ NAME
glassfish-embedded - Start GlassFish Embedded as an executable JAR

SYNOPSIS
java -jar glassfish-embedded-all.jar ${ARGUMENTS}
java -jar glassfish-embedded.jar ${ARGUMENTS}

DESCRIPTION
GlassFish Embedded can be executed as an executable JAR file. This starts
Expand Down Expand Up @@ -35,14 +35,15 @@ ${OPTIONS}

Any argument that doesn't start with a hyphen (-), is treated as follows:

- If it's a file or directory, it's deployed at startup as an application. If it's
the only application deployed at startup, it's deployed under the root context '/'.
Otherwise it's deployed under the context root derived from the name of the file
or deployment descriptors.
- In all other cases, the argument is executed as a GlassFish admin command.
Individual commands must be enclosed in quotes if they contain spaces. GlassFish
admin commands are the same commands supported by GlassFish Server's "asadmin"
command line tool or by the "CommandRunner" Java class in GlassFish Simple Public API.
- If it's a file or directory, it's deployed at startup as an application.
If only application is deployed at startup, it's deployed under the root
context '/'. Otherwise it's deployed under the context root derived from
the name of the file or deployment descriptors.
- In all other cases, the argument is executed as a GlassFish admin
command. Individual commands must be enclosed in quotes if they contain
spaces. GlassFish admin commands are the same commands supported by
GlassFish Server's "asadmin" command line tool or by the "CommandRunner"
Java class in GlassFish Simple Public API.

DEFAULT CONFIGURATION

Expand All @@ -55,23 +56,23 @@ EXAMPLES
Example 1: Run an application from command line
On port 8080 and root context by default

java -jar glassfish-embedded-all.jar app.war
java -jar glassfish-embedded.jar app.war

Example 2: Run an app on a different port

java -jar glassfish-embedded-all.jar --httpPort=8090 app.war
java -jar glassfish-embedded.jar --httpPort=8090 app.war

Example 3: Run a custom deploy command on startup
Sets a custom root context. Custom commands need to be enclosed in quotes
if they contain spaces.
Sets a custom root context. Custom commands need to be enclosed in
quotes if they contain spaces.

java -jar glassfish-embedded-all.jar "deploy --contextroot=/app app.war"
java -jar glassfish-embedded.jar "deploy --contextroot=/app app.war"

Example 4: Run 2 applications from command line
Deploys applications on different context roots, based on the file name
or info in the application descriptors.

java -jar glassfish-embedded-all.jar app1.war app2.war
java -jar glassfish-embedded.jar app1.war app2.war

EXIT STATUS
0
Expand All @@ -83,4 +84,4 @@ EXIT STATUS
SEE ALSO
deploy(1), asadmin(1)

Jakarta EE 10 13 Sep 2024 glassfish-embedded(1)
Jakarta EE 10 13 Sep 2024 glassfish-embedded(1)
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@ public class WordWrapperTest {
@Test
public void testWrapper() {
final int MAX_LINE_LENGTH = 80;
final WordWrapper wordWrapper = new WordWrapper(MAX_LINE_LENGTH, 8, " ");
final WordWrapCollector wordWrapper = new WordWrapCollector(MAX_LINE_LENGTH, 8, " ");
String message = " "
+ Arrays.stream(Option.PROPERTIES.getHelpText().split(" "))
.collect(wordWrapper.collector());
+ Arrays.stream(Option.values())
.map(Option::getHelpText)
.collect(wordWrapper);
final List<String> linesEndingWithSpace = message.lines()
.filter(line -> line.endsWith(" "))
.collect(toList());
Expand Down

0 comments on commit a66fb8c

Please sign in to comment.