Java 8 modules, Jigsaw and OSGi - Neil Bartlett
-
Upload
mfrancis -
Category
Technology
-
view
12.766 -
download
0
description
Transcript of Java 8 modules, Jigsaw and OSGi - Neil Bartlett
![Page 1: Java 8 modules, Jigsaw and OSGi - Neil Bartlett](https://reader033.fdocuments.in/reader033/viewer/2022052619/5552f4ceb4c90584028b4d2a/html5/thumbnails/1.jpg)
JAVA 8 MODULES, JIGSAW AND OSGi
Neil Bartlett with Tim Ellison
![Page 2: Java 8 modules, Jigsaw and OSGi - Neil Bartlett](https://reader033.fdocuments.in/reader033/viewer/2022052619/5552f4ceb4c90584028b4d2a/html5/thumbnails/2.jpg)
MOTIVATION
![Page 3: Java 8 modules, Jigsaw and OSGi - Neil Bartlett](https://reader033.fdocuments.in/reader033/viewer/2022052619/5552f4ceb4c90584028b4d2a/html5/thumbnails/3.jpg)
WHY MODULARISE THE JDK?
• The JRE is monolithic
• Download time and start-up time are directly affected by number of types available at runtime
• Start-up time includes a linear search through the class path to find system and application code
• Oracle 1.7 Windows boot classpath nearly 20k classes
• rt.jar index alone is approx 1Mb
![Page 4: Java 8 modules, Jigsaw and OSGi - Neil Bartlett](https://reader033.fdocuments.in/reader033/viewer/2022052619/5552f4ceb4c90584028b4d2a/html5/thumbnails/4.jpg)
OSGi
• If only Java had a module system that could help with that!
• As we all know, OSGi will not be used for JDK modularity
![Page 5: Java 8 modules, Jigsaw and OSGi - Neil Bartlett](https://reader033.fdocuments.in/reader033/viewer/2022052619/5552f4ceb4c90584028b4d2a/html5/thumbnails/5.jpg)
JIGSAW MOTIVATION
• JRE libraries evolved organically and haphazardly over 13+ years
• Many cyclic dependencies
• Many weird/unexpected dependencies
• E.g.: java.util is an incoherent mess
• Splitting packages is unavoidable
![Page 6: Java 8 modules, Jigsaw and OSGi - Neil Bartlett](https://reader033.fdocuments.in/reader033/viewer/2022052619/5552f4ceb4c90584028b4d2a/html5/thumbnails/6.jpg)
Good news, everyone!
OSGi supports split packages, using Require-
Bundle.
![Page 7: Java 8 modules, Jigsaw and OSGi - Neil Bartlett](https://reader033.fdocuments.in/reader033/viewer/2022052619/5552f4ceb4c90584028b4d2a/html5/thumbnails/7.jpg)
SINGLE CLASSLOADER
• Yeah but there’s the single classloader assumption...
• Many parts of the JRE assume a single boot classloader
• Package-private (“default”) accessibility requires whole packages in single classloader
• Need to split packages across modules but not across classloaders
• Break the one-to-one mapping of modules to classloaders
![Page 8: Java 8 modules, Jigsaw and OSGi - Neil Bartlett](https://reader033.fdocuments.in/reader033/viewer/2022052619/5552f4ceb4c90584028b4d2a/html5/thumbnails/8.jpg)
OSGi supports shared classloaders, using
Fragments.
![Page 9: Java 8 modules, Jigsaw and OSGi - Neil Bartlett](https://reader033.fdocuments.in/reader033/viewer/2022052619/5552f4ceb4c90584028b4d2a/html5/thumbnails/9.jpg)
DEPENDENCY DIRECTION
• Yeah, but the dependency goes in the “wrong” direction (fragment depends on host).
• Jigsaw wants the “host” to depend on the “fragment”: host shouldn’t resolve if fragment unavailable.
![Page 10: Java 8 modules, Jigsaw and OSGi - Neil Bartlett](https://reader033.fdocuments.in/reader033/viewer/2022052619/5552f4ceb4c90584028b4d2a/html5/thumbnails/10.jpg)
OSGi R4.3 supports that too using generic
requirements/capabilities.
![Page 11: Java 8 modules, Jigsaw and OSGi - Neil Bartlett](https://reader033.fdocuments.in/reader033/viewer/2022052619/5552f4ceb4c90584028b4d2a/html5/thumbnails/11.jpg)
REUSE
• Yeah but the fragment still depends on the host and cannot be used by other hosts.
![Page 12: Java 8 modules, Jigsaw and OSGi - Neil Bartlett](https://reader033.fdocuments.in/reader033/viewer/2022052619/5552f4ceb4c90584028b4d2a/html5/thumbnails/12.jpg)
...
![Page 13: Java 8 modules, Jigsaw and OSGi - Neil Bartlett](https://reader033.fdocuments.in/reader033/viewer/2022052619/5552f4ceb4c90584028b4d2a/html5/thumbnails/13.jpg)
Pretty sure we could have supported this, if only you
had talked to us.
![Page 14: Java 8 modules, Jigsaw and OSGi - Neil Bartlett](https://reader033.fdocuments.in/reader033/viewer/2022052619/5552f4ceb4c90584028b4d2a/html5/thumbnails/14.jpg)
REFACTORING
• Usual and best solution: move classes to the correct, most logical place
• Obviously impossible in the JRE.
• 9 million Java developers (Sun estimate, 2009) and billions of apps depend on this library.
![Page 15: Java 8 modules, Jigsaw and OSGi - Neil Bartlett](https://reader033.fdocuments.in/reader033/viewer/2022052619/5552f4ceb4c90584028b4d2a/html5/thumbnails/15.jpg)
JIGSAW
![Page 16: Java 8 modules, Jigsaw and OSGi - Neil Bartlett](https://reader033.fdocuments.in/reader033/viewer/2022052619/5552f4ceb4c90584028b4d2a/html5/thumbnails/16.jpg)
MODULES
• Module declared in module-info.java (module-info.class)
• All new keywords are “scoped”
module B @ 1.0 { ...}
![Page 17: Java 8 modules, Jigsaw and OSGi - Neil Bartlett](https://reader033.fdocuments.in/reader033/viewer/2022052619/5552f4ceb4c90584028b4d2a/html5/thumbnails/17.jpg)
REQUIRES
• Modules require other modules by name (and optionally, version)
module B @ 1.0 { require A @ [2.0,3.0);}
![Page 18: Java 8 modules, Jigsaw and OSGi - Neil Bartlett](https://reader033.fdocuments.in/reader033/viewer/2022052619/5552f4ceb4c90584028b4d2a/html5/thumbnails/18.jpg)
LOCAL
• Requirements can be marked “local”
• Target module is loaded in same classloader
module B @ 1.0 { require local A @ [2.0,3.0);}
![Page 19: Java 8 modules, Jigsaw and OSGi - Neil Bartlett](https://reader033.fdocuments.in/reader033/viewer/2022052619/5552f4ceb4c90584028b4d2a/html5/thumbnails/19.jpg)
EXPORTS
• Modules list their exports, at package and type level
• May include re-exported contents of required modules
module B @ 1.0 { require A @ [2.0,3.0); export org.foo.ClassFoo; export org.bar.*;}
![Page 20: Java 8 modules, Jigsaw and OSGi - Neil Bartlett](https://reader033.fdocuments.in/reader033/viewer/2022052619/5552f4ceb4c90584028b4d2a/html5/thumbnails/20.jpg)
FRIENDS
• Modules can control which other modules require them
• Compare with “friend” classes in C++
• N.B. permit clause is not versioned.
module A @ 2.0 { permit B;}
![Page 21: Java 8 modules, Jigsaw and OSGi - Neil Bartlett](https://reader033.fdocuments.in/reader033/viewer/2022052619/5552f4ceb4c90584028b4d2a/html5/thumbnails/21.jpg)
PROVIDES
• Modules can logically “provide” other modules names
• Compare with “virtual packages” in Debian
• Supports substitution, but not refactoring (splits or joins)
module com.ibm.stax @ 1.0 { provide jdk.stax @ 2.0;}
![Page 22: Java 8 modules, Jigsaw and OSGi - Neil Bartlett](https://reader033.fdocuments.in/reader033/viewer/2022052619/5552f4ceb4c90584028b4d2a/html5/thumbnails/22.jpg)
ENTRY POINT
• Modules can have a single entry-point class
• Compare with Main-Class header in Jar manifests.
module A @ 2.0 { permit B; class org.foo.Main;}
![Page 23: Java 8 modules, Jigsaw and OSGi - Neil Bartlett](https://reader033.fdocuments.in/reader033/viewer/2022052619/5552f4ceb4c90584028b4d2a/html5/thumbnails/23.jpg)
VERSIONS
• Modules will be versioned
• Requirements use exact version or a range
• No version semantics beyond ordering
![Page 24: Java 8 modules, Jigsaw and OSGi - Neil Bartlett](https://reader033.fdocuments.in/reader033/viewer/2022052619/5552f4ceb4c90584028b4d2a/html5/thumbnails/24.jpg)
COMPARISONS
![Page 25: Java 8 modules, Jigsaw and OSGi - Neil Bartlett](https://reader033.fdocuments.in/reader033/viewer/2022052619/5552f4ceb4c90584028b4d2a/html5/thumbnails/25.jpg)
LIFECYCLE
• Resolver solves dependencies in the face of multiple valid alternatives
• OSGi resolver finds best fit for currently installed bundles at runtime
• Jigsaw resolves during build and installation
• Jigsaw has no dynamics, no module lifecycle
![Page 26: Java 8 modules, Jigsaw and OSGi - Neil Bartlett](https://reader033.fdocuments.in/reader033/viewer/2022052619/5552f4ceb4c90584028b4d2a/html5/thumbnails/26.jpg)
METADATA
• Tools required to inspect module-info.class
• OSGi uses kind-of readable MANIFEST.MF
![Page 27: Java 8 modules, Jigsaw and OSGi - Neil Bartlett](https://reader033.fdocuments.in/reader033/viewer/2022052619/5552f4ceb4c90584028b4d2a/html5/thumbnails/27.jpg)
COMPARISON
• Whole-module dependencies and split packages are the biggest differences
• Jigsaw will suffer all the same problems seen in OSGi with Require-Bundle (e.g. Eclipse Core Runtime refactoring mess)
• While Jigsaw will technically achieve JDK modularity it will increase developer maintenance burden
![Page 28: Java 8 modules, Jigsaw and OSGi - Neil Bartlett](https://reader033.fdocuments.in/reader033/viewer/2022052619/5552f4ceb4c90584028b4d2a/html5/thumbnails/28.jpg)
INTEROP
![Page 29: Java 8 modules, Jigsaw and OSGi - Neil Bartlett](https://reader033.fdocuments.in/reader033/viewer/2022052619/5552f4ceb4c90584028b4d2a/html5/thumbnails/29.jpg)
INTEROP
• Both are here to stay... try to get the best of both worlds!
• OSGi is established and will continue to be used widely
• Jigsaw is underway and a key component of Java 8
• It need not be a zero-sum game
![Page 30: Java 8 modules, Jigsaw and OSGi - Neil Bartlett](https://reader033.fdocuments.in/reader033/viewer/2022052619/5552f4ceb4c90584028b4d2a/html5/thumbnails/30.jpg)
JAVA 8 REQUIREMENTS
• “It must be demonstrated by prototype to be feasible to modify an OSGi micro-kernel such that OSGi bundles running in that kernel can depend upon Java modules. The kernel must be able to load Java modules directly and resolve them using its own resolver, except for core system modules. Core system modules can only be loaded using the module system’s reification API.”
• http://openjdk.java.net/projects/jigsaw/doc/draft-java-module-system-requirements-12
![Page 31: Java 8 modules, Jigsaw and OSGi - Neil Bartlett](https://reader033.fdocuments.in/reader033/viewer/2022052619/5552f4ceb4c90584028b4d2a/html5/thumbnails/31.jpg)
PROJECT PENROSE
• OpenJDK-hosted project to work on Jigsaw/OSGi interop
• Project lead is Tim Ellison
![Page 32: Java 8 modules, Jigsaw and OSGi - Neil Bartlett](https://reader033.fdocuments.in/reader033/viewer/2022052619/5552f4ceb4c90584028b4d2a/html5/thumbnails/32.jpg)
LEVELS
0
1
2
3+
Tolerate
Understand
Exploit
Cooperate
![Page 33: Java 8 modules, Jigsaw and OSGi - Neil Bartlett](https://reader033.fdocuments.in/reader033/viewer/2022052619/5552f4ceb4c90584028b4d2a/html5/thumbnails/33.jpg)
LEVEL 0: TOLERATE
• Ensure that OSGi frameworks continue to run unmodified on Jigsaw-enabled runtime.
• Creating modules/bundles that have both Jigsaw and OSGi metadata on the same JAR.
![Page 34: Java 8 modules, Jigsaw and OSGi - Neil Bartlett](https://reader033.fdocuments.in/reader033/viewer/2022052619/5552f4ceb4c90584028b4d2a/html5/thumbnails/34.jpg)
LEVEL 1: UNDERSTAND
• Teach OSGi to read Jigsaw module info
• Mapping Jigsaw metadata into OSGi concepts e.g. requires = Require-Bundle, exports = Export-Package.
• Resolve Jigsaw modules using the OSGi resolver.
![Page 35: Java 8 modules, Jigsaw and OSGi - Neil Bartlett](https://reader033.fdocuments.in/reader033/viewer/2022052619/5552f4ceb4c90584028b4d2a/html5/thumbnails/35.jpg)
LEVEL 2: EXPLOIT
• OSGi implementation exploits Jigsaw modularity
• E.g. use Jigsaw publication repositories.
![Page 36: Java 8 modules, Jigsaw and OSGi - Neil Bartlett](https://reader033.fdocuments.in/reader033/viewer/2022052619/5552f4ceb4c90584028b4d2a/html5/thumbnails/36.jpg)
LEVEL 3+: COOPERATE
• A blend of OSGi and Jigsaw cross-delegation on module phases.
![Page 37: Java 8 modules, Jigsaw and OSGi - Neil Bartlett](https://reader033.fdocuments.in/reader033/viewer/2022052619/5552f4ceb4c90584028b4d2a/html5/thumbnails/37.jpg)
ACHIEVEMENT 1
• Passed the OSGi tests on a Jigsaw-enabled runtime
• Equinox 3.7, the OSGi Reference Implementation.
• OSGi R4.3 Compliance Tests
![Page 38: Java 8 modules, Jigsaw and OSGi - Neil Bartlett](https://reader033.fdocuments.in/reader033/viewer/2022052619/5552f4ceb4c90584028b4d2a/html5/thumbnails/38.jpg)
ACHIEVEMENT 2
• Run a Java application as either OSGi or Jigsaw modules
• Took a Java 2D demo
• Broke into multiple functional units, run demo as either OSGi bundles or Jigsaw modules
![Page 39: Java 8 modules, Jigsaw and OSGi - Neil Bartlett](https://reader033.fdocuments.in/reader033/viewer/2022052619/5552f4ceb4c90584028b4d2a/html5/thumbnails/39.jpg)
GOAL 1
• Map Jigsaw module metadata to OSGi equivalents
• Discussion: how to interpret Jigsaw directives
![Page 40: Java 8 modules, Jigsaw and OSGi - Neil Bartlett](https://reader033.fdocuments.in/reader033/viewer/2022052619/5552f4ceb4c90584028b4d2a/html5/thumbnails/40.jpg)
GOAL 2
• Modify OSGi (Equinox) to use Jigsaw reification APIs
• Load a 3rd-party module from the Jigsaw repository
• Resolve it using the OSGi resolver
![Page 41: Java 8 modules, Jigsaw and OSGi - Neil Bartlett](https://reader033.fdocuments.in/reader033/viewer/2022052619/5552f4ceb4c90584028b4d2a/html5/thumbnails/41.jpg)
TIMELINE
• Java 8: summer 2013? Code freeze early 2013.
• Jigsaw seems to be basically “done”, with little recent activity
• No JSR yet, or perhaps ever.
• If we need changes in Jigsaw, we need to push them soon.
![Page 42: Java 8 modules, Jigsaw and OSGi - Neil Bartlett](https://reader033.fdocuments.in/reader033/viewer/2022052619/5552f4ceb4c90584028b4d2a/html5/thumbnails/42.jpg)
GET INVOLVED
• Penrose home & mailing list:
• http://openjdk.java.net/projects/penrose/
• Hg repository:
• http://hg.openjdk.java.net/penrose/jigsaw/
![Page 43: Java 8 modules, Jigsaw and OSGi - Neil Bartlett](https://reader033.fdocuments.in/reader033/viewer/2022052619/5552f4ceb4c90584028b4d2a/html5/thumbnails/43.jpg)