Skip to content
This repository has been archived by the owner on Aug 29, 2024. It is now read-only.

Java module support #15

Open
olivier-duval opened this issue Feb 23, 2023 · 5 comments
Open

Java module support #15

olivier-duval opened this issue Feb 23, 2023 · 5 comments
Assignees

Comments

@olivier-duval
Copy link

Hello

The library can't be used currently in a java-module environment (java 9+).
It's missing the module-info.java file.

@fdechelle
Copy link
Contributor

True, the module-info.java file is missing.
However, I am not 100% sure that it would be completely useful, as the java-idmef-library has transitive dependencies that do not provide a module-info.java (for instance the json schema validator https://github.com/networknt/json-schema-validator). Can you confirm (or not) this?

@fdechelle fdechelle self-assigned this Feb 23, 2023
@olivier-duval
Copy link
Author

Yeah, json-schema-validator is not module-compatible neither...

So there's 3 ways forward

  • opening an issue about that on the https://github.com/networknt/json-schema-validator ... but it might take some time
  • forking the json-schema-validator to implement this functionality ... should be the easiest (and then maybe giving this functionality to the main project)
  • using another library compatible with modules ... but the API may change and this will have some impacts

What do you think ?

@fdechelle
Copy link
Contributor

Thank you for your helpful comments.
Points 1 and 2 may be considered, probably point 2 is the fastest way, but may require some time even if json-schema-validator has little dependencies (which in turn must be made java modules compliant).
Point 3 is not to be considered, it will require a significant rewriting of the library.
BTW I'm curious about the fact that the library can't be used: it is fully functional even without java modules, using gradle (which is supported under eclipse, intellij, visual studio code...)

@olivier-duval
Copy link
Author

Well, the library is completly function WITHOUT module but "almost functional" under java with module... and a slight modification may do the trick.

The problem is that, when a non module JAR is used, java tries to generate a module name from the jar name. However, module names can't have a dash ("-") in their name... so it doesn't work with "java-idmef-library".

A simple workaround is to define an "Automatic-Module-Name" property in the MANIFEST.MF:

Manifest-Version: 1.0
Automatic-Module-Name: org.idmefv2

for this, you need to add to your build.gradle the following lines:

jar {
    manifest {
        attributes(
                'Automatic-Module-Name': 'org.idmefv2'
        )
    }
}

After that, when you use the library from a module in java, you will only need to declare it in your "module-info.java" like:

module fr.doap.idmefv2module {
	requires javafx.controls;
	requires javafx.fxml;

	requires org.idmefv2;
	
	opens fr.doap.idmefv2 to javafx.fxml;
	exports fr.doap.idmefv2;
}

and it seem to work OK on your example:

		IDMEFObject msg = new IDMEFObject();
		msg.put("Version", "2.0.3");
		msg.put("ID", "09db946e-673e-49af-b4b2-a8cd9da58de6");
		msg.put("CreateTime", "2021-11-22T14:42:51.881033Z");
		
		IDMEFObject analyzer = new IDMEFObject();
		analyzer.put("IP", "127.0.0.1");
		analyzer.put("Name", "foobar");
		analyzer.put("Model", "generic");
		analyzer.put("Category", new String[]{"LOG"});
		analyzer.put("Data", new String[]{"Log"});
		analyzer.put("Method", new String[]{"Monitor"});
		
		msg.put("Analyzer", analyzer);

Maybe a simple update for 1.0.3 ;-)

@olivier-duval
Copy link
Author

As a reference, in case there is no update to the library, it's possible to use this "non modular" library from a modular java (ex. with JavaFX) using https://github.com/gradlex-org/extra-java-module-info to automatically add the 'Automatic-Module-Name': 'org.idmefv2' in the manifest of the calling application.

In the application, edit your build.gradle and add at the beginning of the file (before the plugins):

buildscript {
    repositories {
        maven {
            url "https://plugins.gradle.org/m2/"
        }
    }
    dependencies {
        classpath "org.gradlex:extra-java-module-info:1.3"
    }
}

then, in the build.gradle add the following lines:

extraJavaModuleInfo {
    automaticModule("java-idmef-library-1.0.2.jar", "org.idmefv2")
}

if you're using the JAR file (with COMPILED CLASSES, see #16) locally, so if your files contains :

dependencies {
    implementation files("C:\\Users\\MisterM\\Downloads\\java-idmef-library-1.0.2.jar")
}

or

extraJavaModuleInfo {
    automaticModule("com.github.teclib-idmef:java-idmef-library", "org.idmefv2")
}

if you're using the JAR file (with COMPILED CLASSES, see #16) from a maven repository :

dependencies {
    implementation("com.github.teclib-idmef:java-idmef-library:V1.0.2")
}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

When branches are created from issues, their pull requests are automatically linked.

2 participants