Modularity of the Java Platform (OSGi, Jigsaw and Penrose)

78
Modularity of the Java Platform Martin Toshev

description

Seminar "Modularity of the Java Platform" of the Bulgarian Java User Group. Topics of the seminar: Modularity 101 Modularity on top of the platform: OSGi Modularity of the platform: Jigsaw OSGi and Jigsaw interoperability: Penrose

Transcript of Modularity of the Java Platform (OSGi, Jigsaw and Penrose)

Page 1: Modularity of the Java Platform (OSGi, Jigsaw and Penrose)

Modularity of the Java Platform

Martin Toshev

Page 2: Modularity of the Java Platform (OSGi, Jigsaw and Penrose)

BG JUG mailing list:https://groups.google.com/forum/#!forum/bg-jug

Page 3: Modularity of the Java Platform (OSGi, Jigsaw and Penrose)

Agenda

• Modularity 101

• Modularity on top of the platform: OSGi

• Modularity of the platform: Jigsaw

• OSGi and Jigsaw interoperability: Penrose

Page 4: Modularity of the Java Platform (OSGi, Jigsaw and Penrose)

Modularity 101

Page 5: Modularity of the Java Platform (OSGi, Jigsaw and Penrose)

Modularity 101

• Standard Java libraries are modules - Hibernate, log4j and any library you can basically think of …

• Build systems like Maven provide transparent management of modules

Page 6: Modularity of the Java Platform (OSGi, Jigsaw and Penrose)

Modularity 101

• Benefits of modularization:

– smaller modules are typically tested easier than a monolithic application

– allows for easier evolution of the system - modules evolve independently

Page 7: Modularity of the Java Platform (OSGi, Jigsaw and Penrose)

Modularity 101

• Benefits of modularization:

– development of the system can be split easier between teams/developers

– increased maintainability of separate modules

Page 8: Modularity of the Java Platform (OSGi, Jigsaw and Penrose)

Modularity 101

• The dependency mechanism used by the JDK introduces a number of problems that modular systems aim to solve:

– The "JAR hell" problem caused by shortcomings of the classloading process

Page 9: Modularity of the Java Platform (OSGi, Jigsaw and Penrose)

Modularity 101

• The dependency mechanism used by the JDK introduces a number of problems that modular systems aim to solve:

– The lack of dynamicity in managing dependent modules

– The lack of loose coupling between modules

Page 10: Modularity of the Java Platform (OSGi, Jigsaw and Penrose)

Modularity 101

• Module systems aim to solve the mentioned problems and typically provide:

– module management– module deployment– versioning– dependency management– module repositories– configuration management

Page 11: Modularity of the Java Platform (OSGi, Jigsaw and Penrose)

Modularity on top of the platform: OSGi

Page 12: Modularity of the Java Platform (OSGi, Jigsaw and Penrose)

Modularity on top of the platform: OSGi

• OSGi (Open Service Gateway initiave) provides a specification for module systems implemented in Java

• It is introduced as JSR 8 and JSR 291 to the Java platform

Page 13: Modularity of the Java Platform (OSGi, Jigsaw and Penrose)

Modularity on top of the platform: OSGi

Q: So what is an OSGi runtime ?

Page 14: Modularity of the Java Platform (OSGi, Jigsaw and Penrose)

Modularity on top of the platform: OSGi

Q: So what is an OSGi runtime ?A: An OSGi runtime (module system) makes use of the Java classloading mechanism in order to implement a container for modular units (bundles) and is based on the OSGi spec - a series of standards by the OSGi Alliance. Many application servers are implemented using OSGi as a basis - it is also used in systems from a diversity of areas

Page 15: Modularity of the Java Platform (OSGi, Jigsaw and Penrose)

Modularity on top of the platform: OSGi

Q: So what is an OSGi runtime ?A: An OSGi bundle is a just a JAR file that contains source code, bundle metadata and resources. A bundle may provide various services and components to the OSGi runtime. An OSGi runtime allows for bundles to be installed, started, stopped, updated and uninstalled without requiring a reboot

Page 16: Modularity of the Java Platform (OSGi, Jigsaw and Penrose)

Modularity on top of the platform: OSGi

Q: So what is an OSGi runtime ?A: The OSGi Core spec defines a layered architecture that determines what is supported by the runtime – each layer defines a particular functionality supported by the runtime and the bundles

OSGi logical units: bundles services services registry life-cycle modules security execution environment

OSGi logical layers:

Page 17: Modularity of the Java Platform (OSGi, Jigsaw and Penrose)

Modularity on top of the platform: OSGi

Q: So what is an OSGi runtime ?A: Bundles may export packages for use by other bundles or import packages exported by other bundles - this dependency mechanism is referred to as wire protocol and is provided by the Module layer of OSGi. Bundles may publish services to the runtime and use already published services from the runtime – this dependency mechanism is provided by the Service layer of OSGI.

Page 18: Modularity of the Java Platform (OSGi, Jigsaw and Penrose)

Modularity on top of the platform: OSGi

Q: So what is an OSGi runtime ?A: The MANIFEST.MF file of the bundle’s JAR file describes the metadata of the bundle

Manifest-Version: 1.0Bundle-ManifestVersion: 2Bundle-Name: SampleBundle-SymbolicName: com.sampleBundle-Version: 1.0.0.qualifierBundle-Activator: sample.ActivatorBundle-Vendor: QIVICONRequire-Bundle: org.eclipse.smarthome.core, com.qivicon.extensionsBundle-RequiredExecutionEnvironment: JavaSE-1.7Service-Component: OSGI-INF/service.xmlImport-Package: com.qivicon.services.hdm;version="3.0.0“Export-Package: com.sample.utils

Page 19: Modularity of the Java Platform (OSGi, Jigsaw and Penrose)

Modularity on top of the platform: OSGi

Q: So what is an OSGi runtime ?A: The runtime may implement extensions based on the OSGi Compendium spec that extends the OSGi Core spec. These could be:

remote services

log service

HTTP service

device access service

configuration admin

metatype service

preferences service

user admin

wire admin

DMT admin service

IO connector service

provisioning service

UPnP device service

configuration admin

declarative services

event admin service

deployment admin

XML parser service

monitoring service

others

Page 20: Modularity of the Java Platform (OSGi, Jigsaw and Penrose)

Modularity on top of the platform: OSGi

Q: What about Maven support for OSGi bundles ?

Page 21: Modularity of the Java Platform (OSGi, Jigsaw and Penrose)

Modularity on top of the platform: OSGi

Q: What about Maven support for OSGi bundles ?A: Such a support is provided by the Tycho Maven plug-ins that provide support for packaging types, target platform definitions, interoperability with the Maven dependency mechanism and so on …

Page 22: Modularity of the Java Platform (OSGi, Jigsaw and Penrose)

Modularity on top of the platform: OSGi

• OSGi continues to evolve …

Page 23: Modularity of the Java Platform (OSGi, Jigsaw and Penrose)

Live DemoModularity on top of the platform: OSGi

Page 24: Modularity of the Java Platform (OSGi, Jigsaw and Penrose)

Modularity of the platform: Jigsaw

Page 25: Modularity of the Java Platform (OSGi, Jigsaw and Penrose)

Modularity of the platform: Jigsaw

• When speaking of modularity we should also consider the entire runtime (rt.jar) and the JDK core libraries …

• … and built-in support for improved "OSGi-like" modules in the Java platform

Page 26: Modularity of the Java Platform (OSGi, Jigsaw and Penrose)

Modularity of the platform: Jigsaw

• The JDK is monolithic …

Page 27: Modularity of the Java Platform (OSGi, Jigsaw and Penrose)

Modularity of the platform: Jigsaw

• JDK 8 compact profiles provide smaller versions … (javac -profile <profile_name> or make profiles for an OpenJDK build)

compact 1 compact 2 compact 3

Page 28: Modularity of the Java Platform (OSGi, Jigsaw and Penrose)

Modularity of the platform: Jigsaw

• The aim of project Jigsaw is to provide a module system for the Java platform

• Although deferred to JDK 9 some additional effort such as Compact Profiles and removed/ deprecated inter-library dependencies have been introduced in JDK8 as an intermediate solution

Page 29: Modularity of the Java Platform (OSGi, Jigsaw and Penrose)

Modularity of the platform: Jigsaw

• Modularization of the Java platform is a significant change that impacts the entire ecosystem - may even break existing projects

Page 30: Modularity of the Java Platform (OSGi, Jigsaw and Penrose)

Modularity of the platform: Jigsaw

Q: So what is exactly project Jigsaw ?

Page 31: Modularity of the Java Platform (OSGi, Jigsaw and Penrose)

Modularity of the platform: Jigsaw

Q: So what is exactly project Jigsaw ?A: Jigsaw will provide the basis for a Java Module System JSR

Page 32: Modularity of the Java Platform (OSGi, Jigsaw and Penrose)

Modularity of the platform: Jigsaw

Q: So what is exactly project Jigsaw ?A: Project Jigsaw provides a modularized version of JDK along with additional tools and language support for creating Jigsaw modules. Currently early-access builds provide two types of JDK: JDK modules image - all components are preinstalled as

modules JDK base image + jmod packages - base JDK installation along

with additional Jigsaw modules of the JDK that can be installed on-demand using the jmod tool

Page 33: Modularity of the Java Platform (OSGi, Jigsaw and Penrose)

Modularity of the platform: Jigsaw

Q: So what is exactly project Jigsaw ?A: No jre directory exists anymore in the JDK installation, rt.jar and tools.jar no longer exist. Modular JDK must be compatible with existing applications to a greater extend. Legacy applications are expected to run on a Jigsaw JDK if:

– they don't depend upon the internal structure of the JDK/JRE

– they use only supported APIs

Page 34: Modularity of the Java Platform (OSGi, Jigsaw and Penrose)

Modularity of the platform: Jigsaw

Q: What is a Jigsaw module ?

Page 35: Modularity of the Java Platform (OSGi, Jigsaw and Penrose)

Modularity of the platform: Jigsaw

Q: What is a Jigsaw module ?A: A collection of Java classes, native libraries and other resources along with metadata that provides name and version of the module and dependencies on other modules

Page 36: Modularity of the Java Platform (OSGi, Jigsaw and Penrose)

Modularity of the platform: Jigsaw

Q: What is a Jigsaw module ?A: Jigsaw resolves modules during build and installation.Jigsaw has no dynamics, no module lifecycle. The module system assumes the existence of a foundational module named java.base

Page 37: Modularity of the Java Platform (OSGi, Jigsaw and Penrose)

Modularity of the platform: Jigsaw

Q: What is a Jigsaw module ?A: Module can use ("require") other modules and additionally specify version or version ranges for the module dependency - modules are loaded with different module classloaders

module org.bgjug.A @ 1.0 { requires org.bgjug.B @ [2.0, 3.0);

}

module org.bgjug.A { requires org.bgjug.B @ >= 1.0; requires org.bgjug.C @ < 2.0;}

Page 38: Modularity of the Java Platform (OSGi, Jigsaw and Penrose)

Modularity of the platform: Jigsaw

Q: What is a Jigsaw module ?A: Module versions are compared using a similar approach as the one used for Debian package versions …

Page 39: Modularity of the Java Platform (OSGi, Jigsaw and Penrose)

Modularity of the platform: Jigsaw

Q: What is a Jigsaw module ?A: Modules can also "require" optional modules - meaning that compilation succeeds even if the required module is missing

module org.bgjug.A { requires optional jdk-corba@8-ea;}

Page 40: Modularity of the Java Platform (OSGi, Jigsaw and Penrose)

Modularity of the platform: Jigsaw

Q: What is a Jigsaw module ?A: Modules can also "require" local modules - meaning that the local module is a kind of a "mixin" - it is loaded in the same classloader as the requiring module

module org.bgjug.A @ 1.0 { requires local org.bgjug.B @ [2.0,

3.0);}

Page 41: Modularity of the Java Platform (OSGi, Jigsaw and Penrose)

Modularity of the platform: Jigsaw

Q: What is a Jigsaw module ?A: A module may export packages and classes

module org.bgjug.A @ 1.0 { requires org.bgjug.B @ [2.0, 3.0);exports org.bgjug.A.seminar.Sample;exports org.bgjug.A.seminar.samples.*;

}

Page 42: Modularity of the Java Platform (OSGi, Jigsaw and Penrose)

Modularity of the platform: Jigsaw

Q: What is a Jigsaw module ?A: Modules can explicitly specify which other modules can "require" them

module org.bgjug.A @ 2.0 { exports org.bgjug.A.seminar;permits org.bgjug.B;

}

Page 43: Modularity of the Java Platform (OSGi, Jigsaw and Penrose)

Modularity of the platform: Jigsaw

Q: What is a Jigsaw module ?A: Modules can logically provide other module names (aliases):

module com.bgjug.A @ 1.0 {provides com.bgjug.First @ 2.0;

}

Page 44: Modularity of the Java Platform (OSGi, Jigsaw and Penrose)

Modularity of the platform: Jigsaw

Q: What is a Jigsaw module ?A: Modules can have a single entry point

the main() method org.bgjug.A.Main is called when invoking:java -m org.bgjug.A

module org.bgjug.A @ 1.0 {permits org.bgjug.B;class org.bgjug.A.Main;

}

Page 45: Modularity of the Java Platform (OSGi, Jigsaw and Penrose)

Modularity of the platform: Jigsaw

Q: What is a Jigsaw module ?A: Modules can define multiple views

the main() method org.bgjug.A.Main is called when invoking:java -m org.bgjug.A

module org.bgjug.A {exports org.bgjug.A.seminar;view org.bgjug.internal.view {

permits org.bgjug.B}

}

Page 46: Modularity of the Java Platform (OSGi, Jigsaw and Penrose)

Modularity of the platform: Jigsaw

Q: What is a Jigsaw module ?A: A module can declare that it provides a service

module org.bgjug.A { provides service org.bgjug.A.external.TestService with org.bgjug.A.external.TestServiceImpl;}

Page 47: Modularity of the Java Platform (OSGi, Jigsaw and Penrose)

Modularity of the platform: Jigsaw

Q: What is a Jigsaw module ?A: A module can require a service

module org.bgjug.B { requires service org.bgjug.A.external.TestService }

Page 48: Modularity of the Java Platform (OSGi, Jigsaw and Penrose)

Modularity of the platform: Jigsaw

Q: What is a Jigsaw module ?A: A service can also be required optionally

module org.bgjug.B{ requires optional service org.bgjug.A.TestService;}

Page 49: Modularity of the Java Platform (OSGi, Jigsaw and Penrose)

Modularity of the platform: Jigsaw

Q: How are modules compiled ?

Page 50: Modularity of the Java Platform (OSGi, Jigsaw and Penrose)

Modularity of the platform: Jigsaw

Q: How are modules compiled ?A: The javac compiler is extended with a ModulePath - the equivalent of ClassPath but for module dependencies(-modulepath option)For javac the -d option (that specifies the output directory):

– outputs to legacy single-module structure if ClassPath is set

– outputs to multi-module structure if ModulePath is set

Page 51: Modularity of the Java Platform (OSGi, Jigsaw and Penrose)

Modularity of the platform: Jigsaw

Q: How are modules managed ?

Page 52: Modularity of the Java Platform (OSGi, Jigsaw and Penrose)

Modularity of the platform: Jigsaw

Q: How are modules managed ?A: Modules can be added to module libraries. Various tools are added to support creation of modules, module libraries and module repositories and packaging of artifacts:

– jmod - for managing modules and module libraries– jpkg - for packaging modules (uses pack200 compression)

Page 53: Modularity of the Java Platform (OSGi, Jigsaw and Penrose)

Modularity of the platform: Jigsaw

Q: So why not adopt OSGi (at least the module layer) for modularization of the Java platform ?

Page 54: Modularity of the Java Platform (OSGi, Jigsaw and Penrose)

Modularity of the platform: Jigsaw

Q: So why not adopt OSGi (at least the module layer) for modularization of the Java platform ?A: As chief architect of the Java platform Mark Reinhold states: The OSGI module layer is not operative at compile time - it addresses modularity only during packaging, deployment and execution. It is also strictly build on top of the platform so it cannot be used to modularize it.For compile-time resolution of OSGi dependencies during development IDEs (such as Eclipse) and build tooling (such as Maven) provide a mechanism for specifying a target platform which is collection of bundles used to provide compile-time dependencies

Page 55: Modularity of the Java Platform (OSGi, Jigsaw and Penrose)

Modularity of the platform: Jigsaw

Q: So why not adopt OSGi (at least the module layer) for modularization of the Java platform ?A: However - according to Peter Kriens (former technical director at the OSGi Alliance and one of the key drivers behind the OSGi spec) Jigsaw is not typically needed an will introduce burden to the platform and modules can be introduced much easier:The only thing we need to add to the existing system is versioning information on the package and record this version in the class file

Page 56: Modularity of the Java Platform (OSGi, Jigsaw and Penrose)

Modularity of the platform: Jigsaw

Q: What about IDE support for Jigsaw module development ?

Page 57: Modularity of the Java Platform (OSGi, Jigsaw and Penrose)

Modularity of the platform: Jigsaw

Q: What about IDE support for Jigsaw module development ?A: It is still an early stage to consider possible IDE support - IDE vendors outline use cases for Jigsaw usage in order to consider how to extend their IDEs with Jigsaw support (valid mostly for NetBeans - Eclipse and IntelliJ are still idle).However - according to Peter Kriens the structure of a JDK module (especially the module-info.java file) will increase effort needed to support modules in the various IDEs and build tools

Page 58: Modularity of the Java Platform (OSGi, Jigsaw and Penrose)

Modularity of the platform: Jigsaw

Q: What about Maven support for Jigsaw module development ?

Page 59: Modularity of the Java Platform (OSGi, Jigsaw and Penrose)

Modularity of the platform: Jigsaw

Q: What about Maven support for Jigsaw module development ?A: Still at a very early stage of discussion - but most probably Maven will provide build-in support for Jigsaw modules and module dependency resolution from upstream Jigsaw repositories

Page 60: Modularity of the Java Platform (OSGi, Jigsaw and Penrose)

Modularity of the platform: Jigsaw

Q: What about Maven support for Jigsaw module development ?A: According the Peter Kriens:And not to forget the build tools, they will start having to interpret the module-info file and link to the appropriate module system to find their class path. Today, a build tool tells the compiler its class path, in the future it would first have to compile or interpret the Java file. This alone will probably kill 90% of the ant scripts because the class path is used in other places then compiling. Also maven will have to start to interact with this

Page 61: Modularity of the Java Platform (OSGi, Jigsaw and Penrose)

Live DemoModularity of the platform: Jigsaw

Page 62: Modularity of the Java Platform (OSGi, Jigsaw and Penrose)

OSGi and Jigsaw interoperability: Penrose

Page 63: Modularity of the Java Platform (OSGi, Jigsaw and Penrose)

OSGi and Jigsaw interoperability: Penrose

• When speaking of JDK modules we should consider interoperability with existing module systems such as OSGi

• The purpose of project Penrose is to explore interoperability between OSGi and Jigsaw

Page 64: Modularity of the Java Platform (OSGi, Jigsaw and Penrose)

OSGi and Jigsaw interoperability: Penrose

• Penrose is still in early stage of development …

Page 65: Modularity of the Java Platform (OSGi, Jigsaw and Penrose)

OSGi and Jigsaw interoperability: Penrose

• Penrose goals:

– ensuring OSGi frameworks run unmodified in an Jigsaw-enabled runtime

– create modules/bundles that have both OSGi and Jigsaw metadata in them

– Jigsaw metadata can be extended with OSGi concepts– extend OSGi to read Jigsaw module info

Page 66: Modularity of the Java Platform (OSGi, Jigsaw and Penrose)

OSGi and Jigsaw interoperability: Penrose

• Penrose goals:

– mapping Jigsaw metadata to OSGi metadata– resolve Jigsaw modules in an OSGi runtime– enhance OSGi to use Jigsaw repositories and APIs– more cross delegation between the two systems …

Page 67: Modularity of the Java Platform (OSGi, Jigsaw and Penrose)

OSGi and Jigsaw interoperability: Penrose

• Since module-info.java is compiled to module-info.class this implies that class file must be parsed in order to read metadata

• Penrose proposes a simpler JSON format (module-info.json) that can be used to generate both Jigsaw and OSGi metadata

Page 68: Modularity of the Java Platform (OSGi, Jigsaw and Penrose)

OSGi and Jigsaw interoperability: Penrose

MANIFEST.MF (OSGi)

module-info.java (Jigsaw)

module org.bgjug @ 1.0 {exports org.bgjug.external;

}

Manifest-Version: 1.0Bundle-ManifestVersion: 2Bundle-Name: org.bgjugBundle-SymbolicName: org.bgjugBundle-RequiredExecutionEnvironment: JavaSE-1.7Export-Package: org.bgjug.external;version="1.0.0"

module-info.json (Penrose)"module" : { "name" : "org.bgjug", "version": 1.0, "exports" : [{

"name" : "org.bgjug.external", "org.osgi.version" :

"1.0.0" }]

}

module-info.json (Penrose)

Page 69: Modularity of the Java Platform (OSGi, Jigsaw and Penrose)

Summary

Q: Projects Jigsaw/Penrose - when ?

Page 70: Modularity of the Java Platform (OSGi, Jigsaw and Penrose)

Summary

Q: Projects Jigsaw/Penrose - when ?A: Maybe when the Armagedon comes …

Page 71: Modularity of the Java Platform (OSGi, Jigsaw and Penrose)

Summary

Q: Projects Jigsaw/Penrose - when ?A: Unless you …… discuss:http://mail.openjdk.java.net/mailman/listinfo/penrose-devhttp://mail.openjdk.java.net/mailman/listinfo/jigsaw-devhttp://mail.openjdk.java.net/mailman/listinfo/penrose-discuss… and code:http://hg.openjdk.java.net/jigsawhttp://hg.openjdk.java.net/penrose

Page 72: Modularity of the Java Platform (OSGi, Jigsaw and Penrose)

Q&A

Thank you

Bulgarian JUG mailing list:https://groups.google.com/forum/#!forum/bg-jug

Page 73: Modularity of the Java Platform (OSGi, Jigsaw and Penrose)

References

OSGi Alliancehttp://www.osgi.org/Main/HomePage

Jigsaw Projecthttp://openjdk.java.net/projects/jigsaw/

Penrose Projecthttp://openjdk.java.net/projects/penrose/

Page 74: Modularity of the Java Platform (OSGi, Jigsaw and Penrose)

References

Modularity - what is it ?http://www.infoq.com/articles/modular-java-what-is-it/

Java modularity - why ?http://java.dzone.com/articles/java-modularity-2-why

Java JAR hell problemhttp://en.wikipedia.org/wiki/Java_Classloader#JAR_hell

Page 75: Modularity of the Java Platform (OSGi, Jigsaw and Penrose)

References

Java Module System Requirementshttp://openjdk.java.net/projects/jigsaw/doc/draft-java-module-system-requirements-12

Project Jigsaw: The Big Picturehttp://cr.openjdk.java.net/~mr/jigsaw/notes/jigsaw-big-picture-01

Java 8 Modules Jigsaw and OSGihttp://www.slideshare.net/mfrancis/java-8-modules-jigsaw-and-osgi-neil-bartlett

Page 76: Modularity of the Java Platform (OSGi, Jigsaw and Penrose)

References

Project Jigsaw: Late for the train http://mreinhold.org/blog/late-for-the-train-qa

Unbearable lightness of Jigsaw http://blog.osgi.org/2011/05/unbearable-lightness-of-jigsaw.html

Netbeans discussion on Jigsawhttp://wiki.netbeans.org/Jigsaw

Page 77: Modularity of the Java Platform (OSGi, Jigsaw and Penrose)

References

Java Modularity - OSGi and Project Jigsawhttp://techdistrict.kirkk.com/2009/06/12/java-modularity-osgi-and-project-jigsaw/

The Modular Java Platform & Project Jigsaw http://www.jfokus.se/jfokus14/preso/Jigsaw.pdf

JAX 2013: A Project Jigsaw primerhttp://jaxenter.com/a-project-jigsaw-primer-50029.html

Page 78: Modularity of the Java Platform (OSGi, Jigsaw and Penrose)

References

JavaOne 2013: The Modular Java Platform and Project Jigsaw http://parleys.com/play/52549d02e4b0a43ac12124be/about

OpenJDK Penrose JavaOne 2012http://

www.slideshare.net/bosschaert/open-jdk-penrose-javaone-2012