-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Early work on Teeny Auto configuration
- Loading branch information
Showing
21 changed files
with
922 additions
and
155 deletions.
There are no files selected for viewing
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,5 @@ | ||
http://localhost:80/todos2/todos | ||
|
||
### | ||
|
||
http://localhost:80/todos/todos |
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 |
---|---|---|
|
@@ -4,4 +4,5 @@ | |
exports net.jonathangiles.tools.teenyhttpd.annotations; | ||
|
||
requires java.logging; | ||
requires java.base; | ||
} |
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
14 changes: 14 additions & 0 deletions
14
src/main/java/net/jonathangiles/tools/teenyhttpd/annotations/Inject.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,14 @@ | ||
package net.jonathangiles.tools.teenyhttpd.annotations; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target({ElementType.METHOD, ElementType.FIELD}) | ||
public @interface Inject { | ||
|
||
String name() default ""; | ||
|
||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/net/jonathangiles/tools/teenyhttpd/annotations/PostConstruct.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,11 @@ | ||
package net.jonathangiles.tools.teenyhttpd.annotations; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(ElementType.METHOD) | ||
public @interface PostConstruct { | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/net/jonathangiles/tools/teenyhttpd/annotations/Resource.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,14 @@ | ||
package net.jonathangiles.tools.teenyhttpd.annotations; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target({ElementType.METHOD, ElementType.TYPE}) | ||
public @interface Resource { | ||
|
||
String name() default ""; | ||
|
||
} |
168 changes: 168 additions & 0 deletions
168
src/main/java/net/jonathangiles/tools/teenyhttpd/implementation/AnnotationScanner.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,168 @@ | ||
package net.jonathangiles.tools.teenyhttpd.implementation; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.lang.reflect.Method; | ||
import java.net.URL; | ||
import java.util.*; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
|
||
import net.jonathangiles.tools.teenyhttpd.annotations.*; | ||
|
||
public class AnnotationScanner { | ||
|
||
public static Set<Class<?>> scan(Class<?> target) { | ||
|
||
Set<Class<?>> classList = new HashSet<>(); | ||
|
||
String packageName = target.getPackageName(); | ||
|
||
List<String> packages = getPackages(packageName); | ||
packages.add(packageName); | ||
|
||
for (String aPackage : packages) { | ||
try { | ||
List<Class<?>> classes = getClasses(aPackage); | ||
|
||
for (Class<?> clazz : classes) { | ||
if (hasAnnotations(clazz)) { | ||
classList.add(clazz); | ||
} | ||
} | ||
|
||
} catch (ClassNotFoundException | IOException e) { | ||
Logger.getLogger(AnnotationScanner.class.getName()) | ||
.log(Level.SEVERE, null, e); | ||
} | ||
} | ||
|
||
if (hasEndpoints(target)) { | ||
classList.add(target); | ||
} | ||
|
||
return classList; | ||
} | ||
|
||
private static boolean hasAnnotations(Class<?> clazz) { | ||
|
||
if (clazz.isInterface()) return false; | ||
|
||
if (clazz.isAnnotationPresent(Resource.class)) { | ||
return true; | ||
} | ||
|
||
if (clazz.isAnnotationPresent(Configuration.class)) { | ||
return true; | ||
} | ||
|
||
if (!clazz.isAnnotationPresent(Path.class)) { | ||
return false; | ||
} | ||
|
||
return hasEndpoints(clazz); | ||
} | ||
|
||
private static boolean hasEndpoints(Class<?> clazz) { | ||
|
||
|
||
for (Method method : clazz.getDeclaredMethods()) { | ||
|
||
if (method.isAnnotationPresent(ServerEvent.class)) { | ||
return true; | ||
} | ||
|
||
if (isEndpoint(method)) { | ||
return true; | ||
} | ||
|
||
} | ||
|
||
return false; | ||
} | ||
|
||
public static boolean isEndpoint(Method method) { | ||
|
||
|
||
if (method.isAnnotationPresent(Get.class)) { | ||
return true; | ||
} | ||
|
||
if (method.isAnnotationPresent(Delete.class)) { | ||
return true; | ||
} | ||
|
||
if (method.isAnnotationPresent(Put.class)) { | ||
return true; | ||
} | ||
|
||
if (method.isAnnotationPresent(Post.class)) { | ||
return true; | ||
} | ||
|
||
return method.isAnnotationPresent(Patch.class); | ||
} | ||
|
||
|
||
private static List<String> getPackages(String packageName) { | ||
List<String> packages = new ArrayList<>(); | ||
try { | ||
ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); | ||
String path = packageName.replace('.', '/'); | ||
Enumeration<URL> resources = classLoader.getResources(path); | ||
while (resources.hasMoreElements()) { | ||
URL resource = resources.nextElement(); | ||
File directory = new File(resource.getFile()); | ||
File[] files = directory.listFiles(); | ||
if (files != null) { | ||
for (File file : files) { | ||
if (file.isDirectory()) { | ||
packages.add(packageName + "." + file.getName()); | ||
} | ||
} | ||
} | ||
} | ||
} catch (Exception e) { | ||
Logger.getLogger(AnnotationScanner.class.getName()) | ||
.log(Level.SEVERE, null, e); | ||
} | ||
return packages; | ||
} | ||
|
||
private static List<Class<?>> getClasses(String packageName) throws ClassNotFoundException, IOException { | ||
ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); | ||
assert classLoader != null; | ||
String path = packageName.replace('.', '/'); | ||
Enumeration<URL> resources = classLoader.getResources(path); | ||
List<File> dirs = new ArrayList<>(); | ||
while (resources.hasMoreElements()) { | ||
URL resource = resources.nextElement(); | ||
dirs.add(new File(resource.getFile())); | ||
} | ||
List<Class<?>> classes = new ArrayList<>(); | ||
|
||
for (File directory : dirs) { | ||
classes.addAll(findClasses(directory, packageName)); | ||
} | ||
|
||
return classes; | ||
} | ||
|
||
private static List<Class<?>> findClasses(File directory, String packageName) throws ClassNotFoundException { | ||
List<Class<?>> classes = new ArrayList<>(); | ||
if (!directory.exists()) { | ||
return classes; | ||
} | ||
File[] files = directory.listFiles(); | ||
assert files != null; | ||
for (File file : files) { | ||
if (file.isDirectory()) { | ||
assert !file.getName().contains("."); | ||
classes.addAll(findClasses(file, packageName + "." + file.getName())); | ||
} else if (file.getName().endsWith(".class")) { | ||
classes.add(Class.forName(packageName + '.' + file.getName().substring(0, file.getName().length() - 6))); | ||
} | ||
} | ||
return classes; | ||
} | ||
} |
Oops, something went wrong.