Skip to content

Commit 8dcc6ee

Browse files
committed
Migration of jooby-banner from 1.x. TODO: test cases.
1 parent 73b6984 commit 8dcc6ee

File tree

170 files changed

+134003
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

170 files changed

+134003
-0
lines changed

modules/jooby-banner/pom.xml

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4.0.0.xsd">
5+
6+
<parent>
7+
<groupId>io.jooby</groupId>
8+
<artifactId>modules</artifactId>
9+
<version>2.0.6-SNAPSHOT</version>
10+
</parent>
11+
12+
<modelVersion>4.0.0</modelVersion>
13+
<artifactId>jooby-banner</artifactId>
14+
15+
<dependencies>
16+
<dependency>
17+
<groupId>com.google.code.findbugs</groupId>
18+
<artifactId>jsr305</artifactId>
19+
<scope>provided</scope>
20+
</dependency>
21+
22+
<dependency>
23+
<groupId>io.jooby</groupId>
24+
<artifactId>jooby</artifactId>
25+
<version>${jooby.version}</version>
26+
</dependency>
27+
28+
<!-- jfiglet -->
29+
<dependency>
30+
<groupId>com.github.lalyos</groupId>
31+
<artifactId>jfiglet</artifactId>
32+
</dependency>
33+
34+
<!-- Test dependencies -->
35+
<dependency>
36+
<groupId>org.junit.jupiter</groupId>
37+
<artifactId>junit-jupiter-engine</artifactId>
38+
<scope>test</scope>
39+
</dependency>
40+
41+
<dependency>
42+
<groupId>org.jacoco</groupId>
43+
<artifactId>org.jacoco.agent</artifactId>
44+
<classifier>runtime</classifier>
45+
<scope>test</scope>
46+
</dependency>
47+
48+
<dependency>
49+
<groupId>org.mockito</groupId>
50+
<artifactId>mockito-core</artifactId>
51+
</dependency>
52+
</dependencies>
53+
</project>
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
/**
2+
* Jooby https://jooby.io
3+
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
4+
* Copyright 2014 Edgar Espina
5+
*/
6+
package io.jooby.banner;
7+
8+
import com.typesafe.config.Config;
9+
import io.jooby.Extension;
10+
import io.jooby.Jooby;
11+
import org.slf4j.Logger;
12+
13+
import javax.annotation.Nonnull;
14+
import javax.inject.Provider;
15+
import java.util.Optional;
16+
17+
import static com.github.lalyos.jfiglet.FigletFont.convertOneLine;
18+
import static io.jooby.ServiceKey.key;
19+
import static java.util.Objects.requireNonNull;
20+
21+
/**
22+
* <h1>banner</h1>
23+
* <p>
24+
* Prints out an ASCII art banner on startup using
25+
* <a href="https://github.com/lalyos/jfiglet">jfiglet</a>.
26+
* </p>
27+
*
28+
* <h2>usage</h2>
29+
*
30+
* <pre>{@code
31+
* package com.myapp;
32+
*
33+
* {
34+
* use(new Banner());
35+
* }
36+
* }</pre>
37+
*
38+
* <p>
39+
* Prints out the value of <code>application.name</code> which here is <code>myapp</code>. Or you
40+
* can specify the text to prints out:
41+
* </p>
42+
*
43+
* <pre>{@code
44+
* package com.myapp;
45+
*
46+
* {
47+
* use(new Banner("my awesome app"));
48+
* }
49+
* }</pre>
50+
*
51+
* <h2>font</h2>
52+
* <p>
53+
* You can pick and use the font of your choice via {@link #font(String)} option:
54+
* </p>
55+
*
56+
* <pre>{@code
57+
* package com.myapp;
58+
*
59+
* {
60+
* use(new Banner("my awesome app").font("slant"));
61+
* }
62+
* }</pre>
63+
*
64+
* <p>
65+
* Font are distributed within the library inside the <code>/flf</code> classpath folder. A full
66+
* list of fonts is available <a href="http://patorjk.com/software/taag">here</a>.
67+
* </p>
68+
*
69+
* @author edgar
70+
* @since 2.0.0
71+
*/
72+
public class BannerModule implements Extension {
73+
74+
private static final String FONT = "classpath:/flf/%s.flf";
75+
76+
private String font = "speed";
77+
78+
private final Optional<String> text;
79+
80+
/**
81+
* Creates a new {@link BannerModule} with the given text.
82+
*
83+
* @param text Text to display.
84+
*/
85+
public BannerModule(final String text) {
86+
this.text = Optional.of(text);
87+
}
88+
89+
/**
90+
* Default banner, defined by <code>application.name</code>.
91+
*/
92+
public BannerModule() {
93+
this.text = Optional.empty();
94+
}
95+
96+
@Override
97+
public void install(@Nonnull Jooby application) throws Exception {
98+
Logger log = application.getLog();
99+
Config conf = application.getConfig();
100+
String name = conf.getString("application.name");
101+
String version = conf.getString("application.version");
102+
String text = this.text.orElse(name);
103+
104+
Provider<String> ascii = () -> {
105+
try {
106+
return rtrim(convertOneLine(fontPath(font), text));
107+
} catch (Throwable t) {
108+
return text;
109+
}
110+
};
111+
112+
application.getServices().put(key(String.class, "application.banner"), ascii);
113+
114+
application.onStarting(() -> log.info("\n{} v{}\n", ascii.get(), version));
115+
}
116+
117+
/**
118+
* Set/change default font (speed).
119+
*
120+
* @param font A font's name.
121+
* @return This module.
122+
*/
123+
public BannerModule font(final String font) {
124+
this.font = requireNonNull(font, "Font is required.");
125+
return this;
126+
}
127+
128+
static String fontPath(String font) {
129+
return String.format(FONT, font);
130+
}
131+
132+
static String rtrim(String s) {
133+
int i = s.length() - 1;
134+
while (i >= 0 && Character.isWhitespace(s.charAt(i))) {
135+
i--;
136+
}
137+
return s.substring(0, i + 1);
138+
}
139+
}

0 commit comments

Comments
 (0)