-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from Tapeline/master
Quail v2.0-RC-4
- Loading branch information
Showing
113 changed files
with
5,548 additions
and
79 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
16 changes: 16 additions & 0 deletions
16
...eEnvironment/src/main/java/me/tapeline/quailj/addons/AddonAlreadyRegisteredException.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,16 @@ | ||
package me.tapeline.quailj.addons; | ||
|
||
public class AddonAlreadyRegisteredException extends Exception { | ||
|
||
protected QuailAddon addon; | ||
|
||
public AddonAlreadyRegisteredException(QuailAddon addon) { | ||
super("Addon " + addon + " already registered"); | ||
this.addon = addon; | ||
} | ||
|
||
public QuailAddon getAddon() { | ||
return addon; | ||
} | ||
|
||
} |
23 changes: 23 additions & 0 deletions
23
QuailRuntimeEnvironment/src/main/java/me/tapeline/quailj/addons/AddonLoadException.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,23 @@ | ||
package me.tapeline.quailj.addons; | ||
|
||
import java.io.File; | ||
|
||
public class AddonLoadException extends Exception { | ||
|
||
protected File file; | ||
|
||
public AddonLoadException(String message, File file) { | ||
super(message + "\nFile: " + file); | ||
this.file = file; | ||
} | ||
|
||
public AddonLoadException(Throwable cause, File file) { | ||
super("Cannot load addon " + file + " due to " + cause, cause); | ||
this.file = file; | ||
} | ||
|
||
public File getFile() { | ||
return file; | ||
} | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
...ntimeEnvironment/src/main/java/me/tapeline/quailj/addons/AddonNotRegisteredException.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,16 @@ | ||
package me.tapeline.quailj.addons; | ||
|
||
public class AddonNotRegisteredException extends Exception { | ||
|
||
protected String addon; | ||
|
||
public AddonNotRegisteredException(String addon) { | ||
super("Addon " + addon + " is not registered"); | ||
this.addon = addon; | ||
} | ||
|
||
public String getAddon() { | ||
return addon; | ||
} | ||
|
||
} |
18 changes: 18 additions & 0 deletions
18
QuailRuntimeEnvironment/src/main/java/me/tapeline/quailj/addons/QuailAddon.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,18 @@ | ||
package me.tapeline.quailj.addons; | ||
|
||
import me.tapeline.quailj.parsing.annotation.Annotation; | ||
import me.tapeline.quailj.preprocessing.directives.AbstractDirective; | ||
import me.tapeline.quailj.runtime.Runtime; | ||
import me.tapeline.quailj.runtime.librarymanagement.BuiltinLibrary; | ||
|
||
import java.util.List; | ||
|
||
public abstract class QuailAddon { | ||
|
||
public abstract String getName(); | ||
public abstract List<AbstractDirective> providedDirectives(); | ||
public abstract List<Annotation> providedAnnotations(); | ||
public abstract List<BuiltinLibrary> providedLibraries(); | ||
public void customizeRuntime(Runtime runtime) {} | ||
|
||
} |
82 changes: 82 additions & 0 deletions
82
QuailRuntimeEnvironment/src/main/java/me/tapeline/quailj/addons/QuailAddonLoader.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,82 @@ | ||
package me.tapeline.quailj.addons; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.net.MalformedURLException; | ||
import java.net.URL; | ||
import java.net.URLClassLoader; | ||
import java.util.Enumeration; | ||
import java.util.jar.JarEntry; | ||
import java.util.jar.JarFile; | ||
|
||
public class QuailAddonLoader { | ||
|
||
public static QuailAddon loadAddonFromJar(File file) throws | ||
UnresolvedAddonMainClassException, AddonLoadException { | ||
JarFile jarFile; | ||
try { | ||
jarFile = new JarFile(file); | ||
} catch (IOException e) { | ||
throw new AddonLoadException(e, file); | ||
} | ||
Enumeration<JarEntry> e = jarFile.entries(); | ||
String mainClass = null; | ||
while (e.hasMoreElements()) { | ||
JarEntry entry = e.nextElement(); | ||
if (entry.getName().equals("addonMainClass")) { | ||
BufferedReader reader; | ||
try { | ||
reader = new BufferedReader(new InputStreamReader(jarFile.getInputStream(entry))); | ||
} catch (IOException ex) { | ||
throw new AddonLoadException(ex, file); | ||
} | ||
try { | ||
String line = reader.readLine(); | ||
if (line == null) throw new UnresolvedAddonMainClassException(file); | ||
mainClass = line.trim(); | ||
break; | ||
} catch (IOException exception) { | ||
throw new UnresolvedAddonMainClassException(file); | ||
} | ||
} | ||
} | ||
|
||
URL[] urls; | ||
try { | ||
urls = new URL[]{ new URL("jar:file:" + file + "!/") }; | ||
} catch (MalformedURLException ex) { | ||
throw new AddonLoadException(ex, file); | ||
} | ||
URLClassLoader cl = URLClassLoader.newInstance(urls); | ||
|
||
while (e.hasMoreElements()) { | ||
JarEntry je = e.nextElement(); | ||
if(je.isDirectory() || !je.getName().endsWith(".class")) { | ||
continue; | ||
} | ||
// -6 because of .class | ||
String className = je.getName().substring(0, je.getName().length() - 6); | ||
className = className.replace('/', '.'); | ||
try { | ||
Class<?> c = cl.loadClass(className); | ||
if (className.equals(mainClass)) { | ||
return (QuailAddon) c.newInstance(); | ||
} | ||
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException exception) { | ||
throw new AddonLoadException(exception, file); | ||
} | ||
} | ||
|
||
throw new UnresolvedAddonMainClassException(file); | ||
} | ||
|
||
public static QuailAddon loadAndRegisterFromJar(File file) throws UnresolvedAddonMainClassException, | ||
AddonLoadException, AddonAlreadyRegisteredException { | ||
QuailAddon addon = loadAddonFromJar(file); | ||
QuailAddonRegistry.registerAddon(addon); | ||
return addon; | ||
} | ||
|
||
} |
35 changes: 35 additions & 0 deletions
35
QuailRuntimeEnvironment/src/main/java/me/tapeline/quailj/addons/QuailAddonRegistry.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,35 @@ | ||
package me.tapeline.quailj.addons; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
|
||
public class QuailAddonRegistry { | ||
|
||
protected static HashMap<String, QuailAddon> registeredAddons = new HashMap<>(); | ||
|
||
public static void registerAddon(QuailAddon addon) throws AddonAlreadyRegisteredException { | ||
if (registeredAddons.containsKey(addon.getName())) | ||
throw new AddonAlreadyRegisteredException(addon); | ||
registeredAddons.put(addon.getName(), addon); | ||
} | ||
|
||
public static QuailAddon getAddonByName(String name) { | ||
return registeredAddons.get(name); | ||
} | ||
|
||
public static List<QuailAddon> getAddons() { | ||
return new ArrayList<>(registeredAddons.values()); | ||
} | ||
|
||
public static void unregisterAddon(String name) throws AddonNotRegisteredException { | ||
if (!registeredAddons.containsKey(name)) | ||
throw new AddonNotRegisteredException(name); | ||
registeredAddons.remove(name); | ||
} | ||
|
||
public static boolean addonRegistered(String name) { | ||
return registeredAddons.containsKey(name); | ||
} | ||
|
||
} |
18 changes: 18 additions & 0 deletions
18
...nvironment/src/main/java/me/tapeline/quailj/addons/UnresolvedAddonMainClassException.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,18 @@ | ||
package me.tapeline.quailj.addons; | ||
|
||
import java.io.File; | ||
|
||
public class UnresolvedAddonMainClassException extends Exception { | ||
|
||
protected File file; | ||
|
||
public UnresolvedAddonMainClassException(File file) { | ||
super("Cannot resolve main class in addon " + file); | ||
this.file = file; | ||
} | ||
|
||
public File getFile() { | ||
return file; | ||
} | ||
|
||
} |
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
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
Oops, something went wrong.