Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
A concept of "app name" for help purposes #93
* a better algorithm that detects jars
- Loading branch information
Showing
3 changed files
with
92 additions
and
53 deletions.
There are no files selected for viewing
85 changes: 85 additions & 0 deletions
85
bootique/src/main/java/io/bootique/cli/meta/ApplicationIntrospector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package io.bootique.cli.meta; | ||
|
||
import io.bootique.Bootique; | ||
|
||
import java.net.URL; | ||
import java.util.Map; | ||
|
||
/** | ||
* A helper class to determine the app main class and user-freindly name. | ||
*/ | ||
public class ApplicationIntrospector { | ||
|
||
/** | ||
* Returns application name that is the name of the main class derived from runtime stack. | ||
* | ||
* @return application name that is the name of the main class derived from runtime stack. | ||
*/ | ||
static String appNameFromRuntime() { | ||
Class<?> main = mainClass(); | ||
String name = appNameFromJar(main); | ||
return name != null ? name : appNameFromClassName(main); | ||
} | ||
|
||
private static String appNameFromClassName(Class<?> mainClass) { | ||
// use full class name as app name... | ||
return mainClass.getName(); | ||
} | ||
|
||
private static String appNameFromJar(Class<?> mainClass) { | ||
|
||
URL url; | ||
try { | ||
url = mainClass.getProtectionDomain().getCodeSource().getLocation(); | ||
} catch (Exception e) { | ||
return null; | ||
} | ||
|
||
String urlString = url.toExternalForm(); | ||
|
||
// e.g. the app is started from an unpacked .jar | ||
if (!urlString.endsWith(".jar")) { | ||
return null; | ||
} | ||
|
||
int slash = urlString.lastIndexOf('/'); | ||
return slash < 0 && slash >= urlString.length() - 1 ? urlString : urlString.substring(slash + 1); | ||
} | ||
|
||
|
||
/** | ||
* Returns the name of the app main class. If it can't be found, return 'io.bootique.Bootique'. | ||
* | ||
* @return the name of the app main class. | ||
*/ | ||
static Class<?> mainClass() { | ||
|
||
for (Map.Entry<Thread, StackTraceElement[]> stackEntry : Thread.getAllStackTraces().entrySet()) { | ||
|
||
// thread must be called "main" | ||
if ("main".equals(stackEntry.getKey().getName())) { | ||
|
||
StackTraceElement[] stack = stackEntry.getValue(); | ||
StackTraceElement bottom = stack[stack.length - 1]; | ||
|
||
// method must be called main | ||
if ("main".equals(bottom.getMethodName())) { | ||
|
||
try { | ||
return Class.forName(bottom.getClassName()); | ||
} catch (ClassNotFoundException e) { | ||
// can't load class for some reason... | ||
return Bootique.class; | ||
} | ||
|
||
} else { | ||
// no other ideas where else to look for main... | ||
return Bootique.class; | ||
} | ||
} | ||
} | ||
|
||
// failover... | ||
return Bootique.class; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters