Skip to content
This repository has been archived by the owner on Sep 28, 2022. It is now read-only.

FIX 496 : Add support for CodeSource URL for GoloClassLoader #497

Closed
wants to merge 2 commits into from
Closed

Conversation

utybo
Copy link

@utybo utybo commented Aug 28, 2017

This PR aims to fix #496 where it is impossible to get a .golo file's location at runtime. This uses the same mechanism as regular .jar and .classes in Java, which is registering a ProtectionDomain and CodeSource.

This PR adds a load method to GoloClassLoader that takes one argument more than the already present load method, which is a URL. This URL represents the original location of the .golo file.

Both the CLI "golo golo" and "golosh" were changed to use the new method. The old method was kept to ensure backwards-compatibility and still provide a way to load golo files without providing its source. This could also be useful in the future if you guys want to use Certificates or other security features of ProtectionDomain with GoloClassLoader.

@yloiseau
Copy link
Contributor

yloiseau commented Sep 6, 2017

I think we should take the opportunity of this PR to refactor code loading in the commands. Indeed, almost every commands scan some directory for golo files and load/compile them, each in its own way.
We should refactor all this to have a single source scanning method, that yield Path objects, or even CodeSource object.

The GoloClassLoader::load can be refactored to take only a single CodeSource argument, and derive the goloSourceFilename and sourceCodeInputStream from its URL.

* @return the class matching the Golo module defined in the source.
* @throws GoloCompilationException if either of the compilation phase failed.
*/
public synchronized Class<?> load(String goloSourceFilename, InputStream sourceCodeInputStream, URL sourceCodeLocation) throws GoloCompilationException {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method is copy/past from the previous one. You should refactor the previous one to call this one with a null location.
Or even better, as stated in the main comments, refactor the method to only take a CodeSource and derive the name and input stream from it, and remove the old one.

@@ -78,7 +78,7 @@ public void execute() throws Throwable {
}
} else if (file.getName().endsWith(".golo")) {
try (FileInputStream in = new FileInputStream(file)) {
Class<?> loadedClass = loader.load(file.getName(), in);
Class<?> loadedClass = loader.load(file.getName(), in, file.toURI().toURL());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Build the CodeSource instance here (see the proposed load refactoring). This will allows to construct a Certificate[] from command line options if we ever need to.
BTW, the file scanning facility should be extracted.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yloiseau what do you mean by "extracted"?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should be a mode generic method that scan (recursively) a directory and return golo files, maybe in the form of a CodeSource object, or Path with a method to convert a Path into a CodeSource

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see the "extract method" refactoring...

@@ -78,7 +78,7 @@ private void loadOtherGoloFiles(GoloClassLoader loader, Path basedir, Path scrip
try (InputStream is = Files.newInputStream(path)) {
Path filename = path.getFileName();
if (filename != null) {
return loader.load(filename.toString(), is);
return loader.load(filename.toString(), is, path.toUri().toURL());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

idem

@utybo
Copy link
Author

utybo commented Sep 6, 2017

Alright, I've seen your comments but am not sure of when I'll be able to work on this, though it seems to be pretty easy, so if anyone wants to do this instead of me feel free to do so.

@yloiseau
Copy link
Contributor

yloiseau commented Sep 7, 2017

Since it's not a critical bug/feature, you can take some times to implement it 😉

@utybo utybo closed this Dec 16, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

How to get the main golo file's location?
2 participants