Skip to content

Commit 2bb8aa3

Browse files
committed
Add base module write sample applications quickly
1 parent 361525e commit 2bb8aa3

File tree

17 files changed

+248
-133
lines changed

17 files changed

+248
-133
lines changed

allocations/pom.xml

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,19 @@
2929
<packaging>jar</packaging>
3030
<name>allocations</name>
3131

32-
<properties>
33-
<fully.qualified.main.class>com.github.chrishantha.sample.allocations.App</fully.qualified.main.class>
34-
</properties>
32+
<dependencies>
33+
<dependency>
34+
<groupId>com.github.chrishantha.sample</groupId>
35+
<artifactId>base</artifactId>
36+
</dependency>
37+
</dependencies>
38+
39+
<build>
40+
<plugins>
41+
<plugin>
42+
<groupId>org.apache.maven.plugins</groupId>
43+
<artifactId>maven-shade-plugin</artifactId>
44+
</plugin>
45+
</plugins>
46+
</build>
3547
</project>

allocations/src/main/java/com/github/chrishantha/sample/allocations/App.java renamed to allocations/src/main/java/com/github/chrishantha/sample/allocations/AllocationsApplication.java

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,33 +15,14 @@
1515
*/
1616
package com.github.chrishantha.sample.allocations;
1717

18-
import com.beust.jcommander.JCommander;
1918
import com.beust.jcommander.Parameter;
19+
import com.github.chrishantha.sample.base.SampleApplication;
2020

21-
public class App {
21+
public class AllocationsApplication implements SampleApplication {
2222

2323
@Parameter(names = "--max", description = "Max Numbers")
2424
private long max = 10_000_000L;
2525

26-
@Parameter(names = "--help", description = "Display Help", help = true)
27-
private boolean help;
28-
29-
public static void main(String[] args) {
30-
App app = new App();
31-
final JCommander jcmdr = new JCommander(app);
32-
jcmdr.setProgramName(App.class.getSimpleName());
33-
jcmdr.parse(args);
34-
35-
System.out.println(app);
36-
37-
if (app.help) {
38-
jcmdr.usage();
39-
return;
40-
}
41-
42-
app.start();
43-
}
44-
4526
boolean isPrime(Long n) {
4627
//check if n is a multiple of 2
4728
if (n % 2 == 0) return false;
@@ -53,7 +34,8 @@ boolean isPrime(Long n) {
5334
return true;
5435
}
5536

56-
private void start() {
37+
@Override
38+
public void start() {
5739
Long primeCount = 0L;
5840
for (long i = 0; i < max; i++) {
5941
if (isPrime(i)) {
@@ -66,7 +48,7 @@ private void start() {
6648
@Override
6749
public String toString() {
6850
StringBuilder builder = new StringBuilder();
69-
builder.append("App [max=");
51+
builder.append("AllocationsApplication [max=");
7052
builder.append(max);
7153
builder.append("]");
7254
return builder.toString();
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
com.github.chrishantha.sample.allocations.AllocationsApplication

base/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Base module for Sample Applications
2+
===================================
3+
4+
This module helps to write sample applications quickly by implementing
5+
`com.github.chrishantha.sample.base.SampleApplication` interface and registering the implementation class as a service
6+
in `META-INF/services/com.github.chrishantha.sample.base.SampleApplication`

base/pom.xml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
# Copyright 2018 M. Isuru Tharanga Chrishantha Perera
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
-->
17+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
18+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
19+
20+
<parent>
21+
<groupId>com.github.chrishantha.sample</groupId>
22+
<artifactId>java-samples</artifactId>
23+
<version>0.0.2-SNAPSHOT</version>
24+
<relativePath>../pom.xml</relativePath>
25+
</parent>
26+
27+
<modelVersion>4.0.0</modelVersion>
28+
<artifactId>base</artifactId>
29+
<packaging>jar</packaging>
30+
<name>base</name>
31+
32+
<dependencies>
33+
<dependency>
34+
<groupId>com.beust</groupId>
35+
<artifactId>jcommander</artifactId>
36+
</dependency>
37+
</dependencies>
38+
</project>
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright 2018 M. Isuru Tharanga Chrishantha Perera
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.github.chrishantha.sample.base;
17+
18+
import com.beust.jcommander.JCommander;
19+
import com.beust.jcommander.Parameter;
20+
import com.beust.jcommander.ParameterException;
21+
22+
import java.util.Iterator;
23+
import java.util.ServiceLoader;
24+
25+
public class App {
26+
27+
private static class CommonArgs {
28+
@Parameter(names = "--help", description = "Display Help", help = true)
29+
private boolean help;
30+
}
31+
32+
public static void main(String[] args) {
33+
// There should be only one application
34+
Iterator<SampleApplication> applicationIterator = ServiceLoader.load(SampleApplication.class).iterator();
35+
if (!applicationIterator.hasNext()) {
36+
throw new IllegalStateException("Could not load Sample Application");
37+
}
38+
SampleApplication sampleApplication = applicationIterator.next();
39+
CommonArgs commonArgs = new CommonArgs();
40+
final JCommander jcmdr = JCommander.newBuilder()
41+
.programName(sampleApplication.getClass().getSimpleName())
42+
.addObject(sampleApplication)
43+
.addObject(commonArgs)
44+
.build();
45+
46+
try {
47+
jcmdr.parse(args);
48+
} catch (ParameterException ex) {
49+
System.err.println(ex.getMessage());
50+
return;
51+
}
52+
53+
if (commonArgs.help) {
54+
jcmdr.usage();
55+
return;
56+
}
57+
58+
System.out.println(sampleApplication);
59+
sampleApplication.start();
60+
}
61+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright 2018 M. Isuru Tharanga Chrishantha Perera
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.github.chrishantha.sample.base;
17+
18+
public interface SampleApplication {
19+
20+
void start();
21+
22+
String toString();
23+
}

highcpu/pom.xml

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,19 @@
2929
<packaging>jar</packaging>
3030
<name>highcpu</name>
3131

32-
<properties>
33-
<fully.qualified.main.class>com.github.chrishantha.sample.highcpu.App</fully.qualified.main.class>
34-
</properties>
32+
<dependencies>
33+
<dependency>
34+
<groupId>com.github.chrishantha.sample</groupId>
35+
<artifactId>base</artifactId>
36+
</dependency>
37+
</dependencies>
38+
39+
<build>
40+
<plugins>
41+
<plugin>
42+
<groupId>org.apache.maven.plugins</groupId>
43+
<artifactId>maven-shade-plugin</artifactId>
44+
</plugin>
45+
</plugins>
46+
</build>
3547
</project>

highcpu/src/main/java/com/github/chrishantha/sample/highcpu/App.java renamed to highcpu/src/main/java/com/github/chrishantha/sample/highcpu/HighCpuApplication.java

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@
1515
*/
1616
package com.github.chrishantha.sample.highcpu;
1717

18-
import com.beust.jcommander.JCommander;
1918
import com.beust.jcommander.Parameter;
19+
import com.github.chrishantha.sample.base.SampleApplication;
2020

2121
import java.util.Timer;
2222
import java.util.TimerTask;
2323

24-
public class App {
24+
public class HighCpuApplication implements SampleApplication {
2525

2626
@Parameter(names = "--run-hashing", description = "Run Hashing Worker", arity = 1)
2727
private boolean runHashing = true;
@@ -47,29 +47,11 @@ public class App {
4747
@Parameter(names = "--hashing-algo", description = "Hashing Algorithm")
4848
private String hashingAlgorithm = "SHA-1";
4949

50-
@Parameter(names = "--help", description = "Display Help", help = true)
51-
private boolean help;
52-
5350
@Parameter(names = "--exit-timeout", description = "Exit Timeout in Seconds (Default 2 minutes. Use 0 to run forever)")
5451
private int exitTimeoutInSeconds = 2 * 60;
5552

56-
public static void main(String[] args) {
57-
App app = new App();
58-
final JCommander jcmdr = new JCommander(app);
59-
jcmdr.setProgramName(App.class.getSimpleName());
60-
jcmdr.parse(args);
61-
62-
System.out.println(app);
63-
64-
if (app.help) {
65-
jcmdr.usage();
66-
return;
67-
}
68-
69-
app.start();
70-
}
71-
72-
private void start() {
53+
@Override
54+
public void start() {
7355
System.out.println("Starting Application...");
7456
if (exitTimeoutInSeconds > 0) {
7557
final Timer timer = new Timer();
@@ -106,7 +88,7 @@ private void startThread(int i, String name, Runnable worker) {
10688
@Override
10789
public String toString() {
10890
StringBuilder builder = new StringBuilder();
109-
builder.append("App [runHashing=");
91+
builder.append("HighCpuApplication [runHashing=");
11092
builder.append(runHashing);
11193
builder.append(", runSleeping=");
11294
builder.append(runSleeping);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
com.github.chrishantha.sample.highcpu.HighCpuApplication

0 commit comments

Comments
 (0)