java 9 modules: the duke yet lives that osgi shall depose

181
Java 9 Modules. The Duke Yet Lives That OSGi Shall Depose Nikita Lipsky Excelsior LLC 1

Upload: nikita-lipsky

Post on 21-Jan-2018

487 views

Category:

Software


3 download

TRANSCRIPT

Page 1: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Java 9 Modules. The Duke Yet Lives That OSGi Shall Depose

Nikita Lipsky

Excelsior LLC

1

Page 2: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

2

Page 3: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Modules in IDE

3

Page 4: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Modules in Mavencom.foo

app1.0

com.fooparse-api

2.0

com.foopersist-api

3.0

org.apachecommons

2.1

org.apachecommons-io

3.14

Page 5: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Modules in Mavencom.foo

app1.0

com.fooparse-api

2.0

com.foopersist-api

3.0

org.apachecommons

2.1

org.apachecommons

3.15

Page 6: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

OSGi

6

Page 7: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Why Jigsaw is not OSGi?

Mark Reinhold (the Chief Architect of the Java Platform Group):

“…As it (OSGi) stands, moreover, it’s useful for library and application modules but, since it’s built strictly on top of the Java SE Platform, it can’t be used to modularize the Platform itself”

7

Page 8: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Why Jigsaw is not OSGi?

Question: why the presence of j.l.Object in Java Language Specification, implemented in Java in turn, does not lead to a bootstrap problem?

Факт: Существует реализация Java SE, где OSGi поддерживается на уровне JVM (на уровне платформы).

8

Page 9: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Why Jigsaw is not OSGi?

Question: why the presence of j.l.Object in Java Language Specification, implemented in Java in turn, does not lead to a bootstrap problem?

Fact: There is a Java SE implementation that supports OSGi at the JVM level (at the platform level).

9

Page 10: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Nikita Lipsky• 20+ years in software development

• Excelsior JET project initiator– 16+ years of contributions– compiler engineer – team lead– product lead– etc.

• Twitter: @pjBooms

10

Page 11: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Excelsior JET?• AOT-centric Java SE implementation

– certified as Java Compatible since 2005

• AOT compiler + Java Runtime– mixed compilation: AOT + JIT– AOT support for custom classloaders (Eclipse RCP, Tomcat)

• Toolkit– Startup Optimizer– Deployment

11

Page 12: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Agenda

• Why OSGi

• How OSGi does it

• Why NOT OSGi

• Why Jigsaw is not OSGi

• Jigsaw mantra

• Jigsaw problems

12

Page 13: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Where OSGi?

Standardized by OSGi Alliance (IBM, Adobe, Bosch, Huawei, NTT, Oraсle)

Implementations:

• Equinox – Eclipse IDE, Eclipse RCP, IBM Websphere

• Apache Felix– Oracle WebLogic, Glassfish, Netbeans

13

Page 14: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Why OSGi?

14

Page 15: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Why OSGi?• Modularity

– Reduced complexity– Hide internals (encapsulation)– Dependency management and easy deployment

• Dynamic updates• Versioning• Lazy• Simple, fast, small, secure, etc.https://www.osgi.org/developer/benefits-of-using-osgi/

15

Page 16: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Модульная система OSGi

16

Page 17: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

OSGi Module SystemOSGi module – Bundle:• Jar or directory• Import/export is defined in META-INF/MANIFEST.MF:

Manifest-Version: 1.0 Bundle-ManifestVersion: 2

Bundle-SymbolicName: B

Bundle-Version: 1

Bundle-Name: B Bundle

Export-Package: org.bar

Import-Package: org.foo;version=“[1,2)”

17

Page 18: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

OSGi Module System

• OSGi bundle imports/exports

– packages (Import-Package/Export-Package)

– services (Import-Service/Export-Service).

• May import other bundles directly

– Require-Bundle directive

– However it is not the best practice (less flexible)

18

Page 19: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

OSGi Module System

19

Bundle A

Class ABundle B

Class B

Bundle C

Class CExport-Package: packageCImport-Package: packageA

Export-Package: packageAI

Import-Package: packageA,packageC

Page 20: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

OSGi Runtime

• Resolves Import/Export of OSGi bundles(wiring)

• Defines bundle life cycle

– May start (activate) bundles lazily

– Enables on-the-fly bundle updates without system restart (aka hot redeploy).

20

Page 21: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Jar/Classpath HellApp1.0

LibraryFoo2.0

LibraryBar3.0

LibraryBaz2.1

LibraryBaz3.1

21

Page 22: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Versioning in OSGi

• OSGI resolves JAR hell:

– import/export is qualified by version

– If two bundles require a library bundle of two different versions, both versions will be loaded

22

Page 23: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Why OSGi?So, OSGi promises:• Modularity

– explicit dependencies– encapsulation

• JAR Hell problem resolution– via versioning

• Hot ReDeploy– via ability to update a separate bundle dynamically

23

Page 24: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Why OSGi?So, OSGi promises:• Modularity

– explicit dependencies– encapsulation

• JAR Hell problem resolution– via versioning

• Hot ReDeploy– via ability to update a separate bundle dynamically

24

Page 25: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Why OSGi?So, OSGi promises:• Modularity

– explicit dependencies– encapsulation

• JAR Hell problem resolution– via versioning

• Hot ReDeploy– via ability to update a separate bundle dynamically

25

Page 26: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Why OSGi?So, OSGi promises:• Modularity

– explicit dependencies– encapsulation

• JAR Hell problem resolution– via versioning

• Hot ReDeploy– via ability to update a separate bundle dynamically

26

Page 27: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

27

Page 28: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

How OSGi?

28

Page 29: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Versioning in OSGi

Question: How to implement versioning?

Task: For given A module importing Lib (v1) library, and B module importing Lib (v2), it is required that both versions of Lib working without conflicts.

Solution: load the versions of the library by different classloaders.

29

Page 30: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Versioning in OSGi

Question: How to implement versioning?

Task: For a given module A importing library Lib (v1), and module B importing Lib (v2), it is required that both versions of Lib work without conflicts.

Solution: load the versions of the library by different classloaders.

30

Page 31: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Versioning in OSGi

Question: How to implement versioning?

Task: For a given module A importing library Lib (v1), and module B importing Lib (v2), it is required that both versions of Lib work without conflicts.

Solution: load versions of the library by different classloaders.

31

Page 32: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Versioning in OSGi

Thus each OSGi bundle is loaded by its own classloader:

• Subtype of java.lang.ClassLoader

• Unique class namespace

• No conflicts with classes of other bundles

32

Page 33: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Versioning in OSGi

33

com.fooApp1.0

com.fooparse-api

2.0

com.foopersist-api

3.0

org.apachecommons

2.1

org.apachecommons

3.1

CL

CL

CL

CL

CL

Different versions of apache commons may simultaneously present in the JVM

Each bundle hasits own classloader

Page 34: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Encapsulation in OSGi

34

Page 35: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Encapsulation in OSGisrc/com/foo/exported/A.java:

package com.foo.exported;

import com.foo.internal.B;

public class A {B useB;

}

src/com/foo/internal/B.java:

package com.foo.internal;

public class B {

}

How to make the class B inaccessible from outside?

35

Page 36: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Encapsulation in OSGi

Question: how to make an internal class of a module declared public inaccessible outside?

36

Page 37: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Encapsulation in OSGi

Question: how to make an internal class of a module declared public inaccessible outside?

Answer: classloaders (!) may hide internal classes.

37

Page 38: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Dynamic Updates

Task: update one (!) changed bundle in a running program without stopping it

38

Page 39: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Dynamic Updates

Task: update one (!) changed bundle in a running program without stopping it

Solution: CLASSLOADERS!

(unload old bundle, load new bundle by a new classloader)

39

Page 40: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Bundle life cycle

40

Page 41: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Lazy start

Start of bundles in OSGi is implemented via the bundle activators feature:• Each bundle may have an activator

– Bundle-Activator manifest directive– Implementation of an interface with methods start(), stop()

– Static class initializer analogue for bundles

41

Page 42: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Lazy start

Start of bundles in OSGi is implemented via the bundle activators feature:

public interface BundleActivator {void start(BundleContext context) throws Exception;void stop(BundleContext context) throws Exception;

}

42

Page 43: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Lazy start

• Bundle start can be defined by OSGi configuration

• Otherwise the bundle starts when it is required by other started bundles

43

Page 44: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Lazy start

Task: start (load) bundles only when they become needed for program execution

44

Page 45: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Lazy start

Solution: CLASSLOADERS (again!) in OSGi invoke the start()method of the bundle activator right before loading of the first class of a bundle

Since classloading in the JVM is lazy, bundle activation becomes lazy AUTOMATICALLY

45

Page 46: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Lazy start

Solution: CLASSLOADERS (again!) in OSGi invoke the start()method of the bundle activator right before loading of the first class of a bundle

Since classloading in the JVM is lazy, bundle activation becomes lazy AUTOMATICALLY

46

Page 47: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Lazy start

Solution: CLASSLOADERS (again!) in OSGi invoke the start()method of the bundle activator right before loading of the first class of a bundle

Since classloading in the JVM is lazy, bundle activation becomes lazy AUTOMATICALLY

47

Page 48: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

How OSGi?

All solved in one fell swoop!48

Page 49: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

49

Page 50: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Why Not OSGi?

50

Page 51: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Modularity?Bundle A

Bundle B

51

Page 52: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Modularity?Bundle A

Bundle B Bundle C

52

Page 53: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Modularity?Bundle A

Bundle B Bundle C

Bundle D

53

Page 54: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Modularity?Bundle A

Bundle B Bundle C

Bundle D

54

Page 55: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Modularity?Bundle A

Bundle B Bundle C

Bundle D

55

Page 56: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Modularity?Bundle A

Bundle B Bundle C

Bundle D

56

Page 57: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Modularity?

OSGI allows cycles in

the dependency graph

Exercise: understand, why

that is bad

A

B C

D

57

Page 58: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

On the fly updates?

58

Page 59: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

On the fly updates?

Bundle ABundle B

59

Page 60: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

On the fly updates?

Bundle ABundle B

Let’s update bundle B

60

Page 61: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

On the fly updates?

Bundle ABundle B

61

Page 62: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

On the fly updates?

Bundle A

Bundle B

62

Page 63: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

On the fly updates?

Bundle A

NEWBundle B

63

Page 64: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

On the fly updates?

Bundle ANEWBundle B

64

Page 65: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

On the fly updates?

Bundle ABundle B

Does it work for bundle A?

65

Page 66: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

On the fly updates?

Bundle B

Class B

Bundle A

Class А

66

Page 67: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

On the fly updates?

Bundle B

Class B

Bundle A

Class А

If bundle B imports bundle А then there is a class from B that references a class from A

67

Page 68: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Symbolic references resolution

B.java:class B {

A useA;int f1 = A.f;int f2 = A.foo();

}

A.java:class A {

static int f;static int foo(){};

}68

Page 69: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Symbolic references resolution

69

B.class…

CONSTANT_Class: A

CONSTANT_FieldRef: A.f@int

CONSTANT_MethodRef: A.foo()I

A.class…

Field: f@int

Method: foo()I

Page 70: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Symbolic references resolution

• A class references other classes and their fields/methods symbolically

70

Page 71: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Symbolic references resolution

• A class references other classes and their fields/methods symbolically

• JVM resolves those symbolic references with real values at runtime

71

Page 72: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Symbolic references resolution

• A class references other classes and their fields/methods symbolically

• JVM resolves those symbolic references with real values at runtime

• Once a reference is resolved, the value of the reference is never changed!

72

Page 73: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

On the fly updates?

• If a reference from a class B to a class A is resolved then the class А cannot be unloadedfrom the JVM without unloading the class B

73

Page 74: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

On the fly updates?

• If a reference from a class B to a class A is resolved then the class А cannot be unloadedfrom the JVM without unloading the class B

• Hence if a bundle В imports a bundle А then the bundle А cannot be unloaded without also unloading the bundle B

74

Page 75: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

On the fly updates?

Bundle ABundle B

Let’s update bundle A

75

Page 76: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

On the fly updates?

Bundle ABundle B

76

Page 77: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

On the fly updates?

Bundle A

Bundle B

77

Page 78: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

On the fly updates?

Бандл ABundle B

78

Page 79: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

On the fly updates?

Now let’s remember

cyclic dependencies

Exercise: try to update

SWT bundle in running Eclipse

A

B C

D

79

Page 80: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

80

Page 81: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

On the fly updates?

• On the fly updates in OSGi work, more or less, only for leaf bundles that are not imported by other bundles – plugins

81

Page 82: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

On the fly updates?

• On the fly updates in OSGi work, more or less, only for leaf bundles that are not imported by other bundles – plugins

• There are much simpler ways than OSGi to implement plugins

82

Page 83: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

On the fly updates?

Even leaf bundles are not so easy to unload from the JVM:

• The classes from leaf bundles can live in the JVM after their unloading

– Known problem: Classloaders Memory Leak

83

Page 84: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

One does not simply

unload a class from the JVM84

Page 85: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Versioning?

85

Page 86: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Versioning?App1.0

Foo2.0

Bar3.0

Baz2.1

Baz3.1

86

Page 87: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Versioning?App1.0

Foo2.0

Bar3.0

Baz2.1

Baz3.1

87

Page 88: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Versioning?App1.0

Foo2.0

Bar3.0

Baz2.1

Baz3.1

A

88

Page 89: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Versioning?App1.0

Foo2.0

Bar3.0

Baz2.1

Baz3.1

A A

89

Page 90: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Versioning?App1.0

Foo2.0

Bar3.0

Baz2.1

Baz3.1

A

A

90

Page 91: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Versioning?App1.0

Foo2.0

Bar3.0

Baz2.1

Baz3.1

A A

91

Page 92: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Versioning?App1.0

Foo2.0

Bar3.0

Baz2.1

Baz3.1

A A

92

Page 93: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Versioning?App1.0

Foo2.0

Bar3.0

Baz2.1

Baz3.1

A A

I’m “A”!

93

Page 94: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Versioning?App1.0

Foo2.0

Bar3.0

Baz2.1

Baz3.1

A A

I’m “A”!No, I’m

“A”!

94

Page 95: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Versioning?App1.0

Foo2.0

Bar3.0

Baz2.1

Baz3.1

AAAAA!AAAAA!

AA

95

Page 96: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

96

Page 97: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Loading constraints

• Loading constraints prohibit two classes with the same fully qualified name to appear in the namespace of another class

97

Page 98: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Loading constraints

B.java:class B {

T1 f1 = A.f;int f2 = A.m(t2);

}

A.java:class A {

static T1 f;static int m(T2 t)

If B is loaded by L1 classloader and A is loaded by L2 then the JVM will check that (T1, L1) = (T1, L2) and (T2, L1) = (T2, L2)

==

98

Page 99: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Versioning?

• JVM throws java.lang.LinkageError on loading constraints violation

99

Page 100: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Versioning?

• JVM throws java.lang.LinkageError on loading constraints violation

• OSGi DOES NOT help developers to avoid loading constraints violation

– Just google for “OSGI” and “LinkageError” to estimate the scale of the problem

100

Page 101: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Versioning?

There are bundles in the latest Eclipse versions with potential loading constraints violations and nobody cares!

101

Page 102: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Versioning?

Conclusion: OSGi does not solve JAR Hell but raises it to a new, more sophisticated level.

102

Page 103: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Versioning?

Conclusion: Do not use two different versions of the same library in one application!

103

Page 104: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Encapsulation?

Question: well, at least the encapsulation problem is solved by OSGi, right?

104

Page 105: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Reflection – universal countermeasure

against encapsulation in Java

Encapsulation?

setAccessible(true)

105

Page 106: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Encapsulation?

Question: well, at least the encapsulation problem is solved by OSGi, right?

Answer: OSGi does not protect from unauthorized access via Reflection

106

Page 107: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Lazy start?

107

Page 108: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Lazy start?

108

Page 109: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Lazy start?Bundle A

Bundle B Bundle C

Question: in what order the bundles will be activated?

109

Page 110: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Lazy start?

Bundle activation order in OSGi depends on classloading order directly

– A bundle is started from loadClass() of the bundle classloader

110

Page 111: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Lazy start?

Bundle activation order in OSGi depends on classloading order directly

– A bundle is started from loadClass() of the bundle classloader

However, class loading order is not defined by the JVM specification!

111

Page 112: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Symbolic references resolutionA class may reference other classes and their fields/methods symbolically. A JVM may resolve references:

• Lazily– Each reference is resolved on first access

• Eagerly– All references are resolved at the time of class loading

112

Page 113: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Lazy start?

The classloading order depends on the class reference resolution scheme in the given JVM implementation: lazy, less lazy, eager.

113

Page 114: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Lazy start?

Bundle B activator:class B implements BundleActivator {

public void start() {assert A.f!= null;

}

Bundle A activator:class A implements BundleActivator {

static T f;public void start() {

f = new T();}

Typical case: В thinks that А is already activated, but in fact А can be activated after B, so assertion fails

114

Page 115: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Lazy start?

• Bundle activation scheme in OSGi is a time bomb:

– If the JVM starts to resolve class references less lazily, practically all OSGi applications will stop working

115

Page 116: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Why NOT OSGi?

• Modularity with cycles

• Hot Redeploy works for leaf bundles only

• No protection from loading constraints violation

• No protection of implementation details from reflective access

• Bundle activation order depends substantially on class reference resolution scheme

116

Page 117: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Jigsaw

117

Page 118: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Jigsaw

118

Page 119: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Jigsaw vs. OSGi

OSGi is dynamic essentially

– modules appear at run time only

119

Page 120: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Jigsaw vs. OSGi

Jigsaw was thought up as static from the beginning.

Practically all JDK tools know about modules:– javac: respects modules visibility rules

– jdeps: analyses dependencies

– jar, jmod: pack modules

– jlink: prepares final image for deployment

– java: there are modules in runtime as well

120

Page 121: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Module example// src/java.sql/module-info.javamodule java.sql {

requires transitive java.logging;requires transitive java.xml;

exports java.sql;exports javax.sql;exports javax.transaction.xa;uses java.sql.Driver;

}

121

Page 122: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Jigsaw vs. OSGi

Jigsaw prohibits (explicit) cycles

in the dependency graph

Modules import modules

and not packages.

122

Page 123: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Versioning

There was versioning in the first drafts of Jigsaw (similar to OSGi).

123

Page 124: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Versioning

There was versioning in the first drafts of Jigsaw (similar to OSGi).

However versioning was removed later …

124

Page 125: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Versioning

There was versioning in the first drafts of Jigsaw (similar to OSGi).

However versioning was removed later …

Why?

125

Page 126: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Versioning

Versioning immediately means:

1 module 1 classloader

126

Page 127: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Versioning

Versioning immediately means:

1 module 1 classloader

It was exactly so in the first Jigsaw drafts!

127

Page 128: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Jigsaw and classloaders

Jigsaw is not just modules, but also the Java SE platform split into modules.

128

Page 129: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Jigsaw and classloaders

Backward compatibility problem:

According to the specification

getClassloader() == nullfor core platform classes.то противоречит

That precludes splitting of the platform into modules, with each module loaded by its loader

129

Page 130: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Jigsaw and classloaders

Backward compatibility problem:

According to the specification

getClassloader() == nullfor core platform classes.то противоречит

That precludes splitting of the platform into modules, with each module loaded by its loader

130

Page 131: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Jigsaw and classloaders

Problem 2: How to protect developers from loading constraints violation?

131

Page 132: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Versioning

Another detail: import in early Jigsaw versions (as in OSGi) was qualified by not a single version but by а version range:

– A module may declare that it can work with a dependency of “from” version to “to” version

132

Page 133: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Versioning

Problem 3: Resolving dependencies (wiring modules) from version ranges is an NP-complete problem!

– Reduced to 3-SAT

133

Page 134: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Versioning

… after that versioning in JPMS breathed its last.

134

Page 135: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Versioning

… after that versioning in JPMS breathed its last.

No versioning – no classloaders for modules.

135

Page 136: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Jigsaw

• No dynamic updates

• No versioning

136

Page 137: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Jigsaw

• No dynamic updates

• No versioning

So what does it have?

137

Page 138: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Jigsaw Mantra

Reliable Configuration

Strong Encapsulation

138

Page 139: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Reliable Configuration

Такая ситуация в Jigsaw просто запрещена!

App1.0

Foo2.0

Bar3.0

Baz2.1

Baz3.1

139

Page 140: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Reliable Configuration

Jigsaw simply prohibits this situation!

App1.0

Foo2.0

Bar3.0

Baz2.1

Baz3.1

140

Page 141: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Reliable Configuration

Right (reliable) configuration:

App1.0

Foo2.5

Bar3.0

Baz3.1

141

Page 142: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Reliable Configuration

Reliable configuration:

• All module dependencies are resolved

• No cyclic dependencies

• No modules containing the same packages (split packages)

These properties are checked at startup (wiring)

142

Page 143: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Strong Encapsulation

setAccessible(true)

143

Page 144: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Strong Encapsulation

Java 9 modules are first class citizens

• Define visibility access rules

– via declared export

• There is no access to non-exported functionality outside of the module even via reflection

– Even setAccessible(true) does not work

144

Page 145: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Strong Encapsulation

Java 9 modules are first class citizens

• Define visibility access rules

– via declared export

• There is no access to non-exported functionality outside of the module even via reflection

– Even setAccessible(true) does not work

145

Page 146: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Strong Encapsulation

Java 9 modules are first class citizens

• Define visibility access rules

– via declared export

• There is no access to non-exported functionality outside of the module even via reflection

– Even setAccessible(true) does not work

146

Page 147: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

So Jigsaw does not have problems?

147

Page 148: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

148

Page 149: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Reliable Configuration?

• Reflective access was prohibited between modules without explicit dependencies in early Jigsaw drafts

• However it had to be relaxed when classloaders had gone from JPMS– because Class.forName() has to work

backward compatible

149

Page 150: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Reliable Configuration?

• Reflective access was prohibited between modules without explicit dependencies in early Jigsaw drafts

• Had to be relaxed when classloaders had gone from JPMS– because Class.forName() had to remain

backward compatible

150

Page 151: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Reliable Configuration?

However if you may have reflective dependencies that are not explicitly declared, where is the guarantee that the resulting configuration is reliable?

151

Page 152: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Reliable Configuration?

Split packages are prohibited, but what about application containers (Tomcat, Java EE)?

152

Page 153: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Jigsaw Layers *

To solve the application containers problem

Layers feature was introduced in JPMS:

• Local module system for each application in a container

• Two modules containing the same package have to belong to different layers

153

Page 154: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Jigsaw Layers *

* The picture is from Alex Buckley’s presentation: Project Jigsaw Under the hood154

Page 155: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Strong Encapsulation?

155

Page 156: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Strong Encapsulation?

The platform is split into modules:

• Which means that private APIs become really private

156

Page 157: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Strong Encapsulation?

The platform is split into modules:

• Which means that private APIs become really private

• But what about

sun.misc.Unsafe ?

157

Page 158: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Strong Encapsulation?

Ok, Java community (temporarily)

defended sun.misc.Unsafe (in an unequal fight)!

158

Page 159: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Strong Encapsulation?

But what about Dependency Injection?

159

Page 160: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Strong Encapsulation?

DI frameworks essentially depend on:

• reflective access to the code in which they inject dependencies

• including non-exported code

160

Page 161: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Strong Encapsulation?

module my.module {exports my.module.pack;

}

161

Page 162: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Strong Encapsulation?

module my.module {exports my.module.pack;exports my.module.internal.object.orgy;

}

162

Page 163: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Strong Encapsulation?

module my.moduleO{

exports my.module.pack;}

163

Page 164: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Strong Encapsulation?

open module my.module {

exports my.module.pack;}

164

Page 165: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Strong Encapsulation?

Open modules were introduced to solve the DI frameworks problem:

• An open module allows reflective access to its non-exported functionality

165

Page 166: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Strong Encapsulation?

Open modules provide not-so-strong encapsulation, but it is better than nothing.

166

Page 167: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Jigsaw

Well, but what benefits me in Jigsaw at last?

167

Page 168: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Jigsaw benefitsIf all your dependencies are on the classpath now, migrating to the modulepath would improve the architecture of your application by eliminating:• cycles in the dependencies• split packages (jar hell)• unsound access into implementation details of

other modules• dependencies to JDK private API

168

Page 169: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Jigsaw benefitsIf all your dependencies are on the classpath now, migrating to the modulepath would improve the architecture of your application by eliminating:• cycles in the dependency graph• split packages (jar hell)• unsound access into implementation details of

other modules• dependencies to JDK private API

169

Page 170: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Jigsaw benefitsIf all your dependencies are on the classpath now, migrating to the modulepath would improve the architecture of your application by eliminating:• cycles in the dependency graph• split packages (jar hell)• unsound access into implementation details of

other modules• dependencies to JDK private API

170

Page 171: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Jigsaw benefitsIf all your dependencies are on the classpath now, migrating to the modulepath would improve the architecture of your application by eliminating:• cycles in the dependency graph• split packages (jar hell)• unsound reliance upon implementation details of

other modules• dependencies to JDK private API

171

Page 172: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Jigsaw benefitsIf all your dependencies are on the classpath now, migrating to the modulepath would improve the architecture of your application by eliminating:• cycles in the dependency graph• split packages (jar hell)• unsound reliance upon implementation details of

other modules• dependencies on JDK private APIs

172

Page 173: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Jigsaw benefits

Jigsaw introduces a migration path to the modulepath:

• Old classpath forms Unnamed Modulе

• Jars from classpath may be temporally moved as is to modulepath as Auto Modules

• Module declaration can be added to auto modules later

173

Page 174: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Jigsaw benefits

Jigsaw introduces a migration path to the modulepath:

• Old classpath forms Unnamed Module

• Jars from classpath may be temporally moved as is to modulepath as Auto Modules

• Module declaration can be added to auto modules later

174

Page 175: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Jigsaw benefits

Jigsaw introduces a migration path to the modulepath:

• Old classpath forms Unnamed Modulе

• Jars from classpath may be temporarily moved “as-is” to modulepath as Auto Modules

• Module declaration can be added to auto modules later

175

Page 176: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Jigsaw benefits

Jigsaw introduces a migration path to the modulepath:

• Old classpath forms Unnamed Modulе

• Jars from classpath may be temporarily moved “as-is” to modulepath as Auto Modules

• Module declaration can be added to auto modules later

176

Page 177: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Jigsaw benefitsUnfortunately, most Java developers won’t benefit from JPMS immediately:• Java EE standards do not define how they will

interoperate with JPMS yet• Even servlet containers standard knows nothing

about modules so far– Dependencies in war файлах are essentially old plain

classpath!

177

Page 178: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Jigsaw benefitsUnfortunately, most Java developers won’t benefit from JPMS immediately:• Java EE standards do not define how they will

interoperate with JPMS yet• Even the servlet container standard knows

nothing about modules so far– Dependencies in war files are old plain classpath in

fact!

178

Page 179: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

The birth is inevitable!

179

Page 180: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Conclusion• OSGi is a nice attempt to give modules to Java

developers– but OSGi has many problems unfortunately– including fundamental

• Jigsaw is a carefully designed system without visible fundamental problems– but with a system of checks and balances– there are community acceptance problems

180

Page 181: Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose

Q & A

Nikita Lipsky,Excelsior

[email protected]: @pjBooms

181