2. Who Am I?
E-mail: craig@habuma.com Blog: http://www.springinaction.com Twitter: @habuma Slides: http://www.slideshare.net/habuma
3. Who Am I?
Java, Spring, and OSGi fanatic
Principal Consultant with .
Author
XDoclet in Action (Manning)
Spring in Action (Manning)
Modular Java (Pragmatic Bookshelf)
E-mail: craig@habuma.com Blog: http://www.springinaction.com Twitter: @habuma Slides: http://www.slideshare.net/habuma
5. OSGi is...
A modularity specification for Java
An intra-VM SOA
E-mail: craig@habuma.com Blog: http://www.springinaction.com Twitter: @habuma Slides: http://www.slideshare.net/habuma
6. OSGi is NOT...
New
Heavyweight
Difficult
Only for Eclipse
EJB all over again
An acronym
Named very cool
E-mail: craig@habuma.com Blog: http://www.springinaction.com Twitter: @habuma Slides: http://www.slideshare.net/habuma
7. Modularity is...
• High cohesion
• Modules are focused in purpose
• Low coupling
• Modules have minimal/no direct dependency on
each other
• Not a new idea...
“A well-defined segmentation of the project effort ensures system modularity.
Each task forms a separate, distinct program module. At implementation time
each module and its inputs and outputs are well-defined, there is no confusion
in the intended interface with other system modules. At checkout time the integrity
of the module is tested independently; there are few scheduling problems in
synchronizing the completion of several tasks before checkout can begin. Finally,
the system is maintained in modular fashion; system errors and deficiencies can
be traced to specific system modules, thus limiting the scope of detailed error
searching.”
Designing Systems Programs, Richard Gauthier and Stephen Pont, 1970.
E-mail: craig@habuma.com Blog: http://www.springinaction.com Twitter: @habuma Slides: http://www.slideshare.net/habuma
19. Modularity in Java?
• Java lacks some essential features to
support modular development
E-mail: craig@habuma.com Blog: http://www.springinaction.com Twitter: @habuma Slides: http://www.slideshare.net/habuma
20. Modularity in Java?
• Java lacks some essential features to
support modular development
• Classes encapsulate data and functionality
E-mail: craig@habuma.com Blog: http://www.springinaction.com Twitter: @habuma Slides: http://www.slideshare.net/habuma
21. Modularity in Java?
• Java lacks some essential features to
support modular development
• Classes encapsulate data and functionality
• They’re too fine-grained for practical modularity
E-mail: craig@habuma.com Blog: http://www.springinaction.com Twitter: @habuma Slides: http://www.slideshare.net/habuma
22. Modularity in Java?
• Java lacks some essential features to
support modular development
• Classes encapsulate data and functionality
• They’re too fine-grained for practical modularity
• Packages only contain classes
E-mail: craig@habuma.com Blog: http://www.springinaction.com Twitter: @habuma Slides: http://www.slideshare.net/habuma
23. Modularity in Java?
• Java lacks some essential features to
support modular development
• Classes encapsulate data and functionality
• They’re too fine-grained for practical modularity
• Packages only contain classes
• They’re only an organizational mechanism
E-mail: craig@habuma.com Blog: http://www.springinaction.com Twitter: @habuma Slides: http://www.slideshare.net/habuma
24. Modularity in Java?
• Java lacks some essential features to
support modular development
• Classes encapsulate data and functionality
• They’re too fine-grained for practical modularity
• Packages only contain classes
• They’re only an organizational mechanism
• JAR files only contain packages/classes
E-mail: craig@habuma.com Blog: http://www.springinaction.com Twitter: @habuma Slides: http://www.slideshare.net/habuma
25. JAR Files: A false sense of modularity
E-mail: craig@habuma.com Blog: http://www.springinaction.com Twitter: @habuma Slides: http://www.slideshare.net/habuma
27. Where’s the modularity?
public class MyAwesomeClass {
public void doSomething() {
FooInternal foo = new FooInternal();
// ...
}
}
E-mail: craig@habuma.com Blog: http://www.springinaction.com Twitter: @habuma Slides: http://www.slideshare.net/habuma
28. Where’s the modularity?
public class MyAwesomeClass {
public void doSomething() {
FooInternal foo = new FooInternal();
// ...
}
}
/**
* Intended for INTERNAL USE ONLY!!!
* Do not use directly!!!
*/
class FooInternal { ... }
E-mail: craig@habuma.com Blog: http://www.springinaction.com Twitter: @habuma Slides: http://www.slideshare.net/habuma
38. Only import service API
E-mail: craig@habuma.com Blog: http://www.springinaction.com Twitter: @habuma Slides: http://www.slideshare.net/habuma
39. Only import service API
OSGi!
E-mail: craig@habuma.com Blog: http://www.springinaction.com Twitter: @habuma Slides: http://www.slideshare.net/habuma
40. The solution...OSGi
• Dynamic Runtime
• Modules can be installed, started, stopped,
updated, and uninstalled...in a live program
• Ends JAR Hell
• Multiple versions of a class can reside in OSGi
simultaneously
E-mail: craig@habuma.com Blog: http://www.springinaction.com Twitter: @habuma Slides: http://www.slideshare.net/habuma
46. Bundles
• Just JAR files
E-mail: craig@habuma.com Blog: http://www.springinaction.com Twitter: @habuma Slides: http://www.slideshare.net/habuma
47. Bundles
• Just JAR files
• Contain special metadata in MANIFEST.MF
E-mail: craig@habuma.com Blog: http://www.springinaction.com Twitter: @habuma Slides: http://www.slideshare.net/habuma
48. Bundles
• Just JAR files
• Contain special metadata in MANIFEST.MF
• All content is private by default
• May export packages to be imported by other
bundles
• May publish services
E-mail: craig@habuma.com Blog: http://www.springinaction.com Twitter: @habuma Slides: http://www.slideshare.net/habuma
49. Bundles
• Just JAR files
• Contain special metadata in MANIFEST.MF
• All content is private by default
• May export packages to be imported by other
bundles
• May publish services
• May be versioned
E-mail: craig@habuma.com Blog: http://www.springinaction.com Twitter: @habuma Slides: http://www.slideshare.net/habuma
52. Fragments
• Just JAR files, like bundles, but...
E-mail: craig@habuma.com Blog: http://www.springinaction.com Twitter: @habuma Slides: http://www.slideshare.net/habuma
53. Fragments
• Just JAR files, like bundles, but...
• Must be hosted by another bundle
E-mail: craig@habuma.com Blog: http://www.springinaction.com Twitter: @habuma Slides: http://www.slideshare.net/habuma
54. Fragments
• Just JAR files, like bundles, but...
• Must be hosted by another bundle
• Physically separate, logically united
E-mail: craig@habuma.com Blog: http://www.springinaction.com Twitter: @habuma Slides: http://www.slideshare.net/habuma
55. Fragments
• Just JAR files, like bundles, but...
• Must be hosted by another bundle
• Physically separate, logically united
• Used to add content (classes, resources,
etc) to a hosting bundle
E-mail: craig@habuma.com Blog: http://www.springinaction.com Twitter: @habuma Slides: http://www.slideshare.net/habuma
56. OSGi Manifest
• Defines the content of a bundle
Manifest-Version: 1.0
Built-By: wallsc
Created-By: Apache Maven Bundle Plugin
Bundle-Activator: com.osgiknowhow.hello.consumer.internal.HelloConsumerActivator
Import-Package: com.osgiknowhow.hello.service;version="1.0.0.SNAPSHOT",
org.osgi.framework,org.osgi.util.tracker
Bnd-LastModified: 1236686261405
Bundle-Version: 1.0.0.SNAPSHOT
Ignore-Package: com.osgiknowhow.hello.consumer.internal
Bundle-Name: com.osgiknowhow.hello.consumer
Bundle-Description: Generated using Pax-Construct
Build-Jdk: 1.5.0_16
Private-Package: com.osgiknowhow.hello.consumer.internal
Bundle-ManifestVersion: 2
Bundle-SymbolicName: com.osgiknowhow.hello.consumer
Tool: Bnd-0.0.255
• Don’t ever write this file yourself
• Generate it (see Bnd, Bundlor, etc)
E-mail: craig@habuma.com Blog: http://www.springinaction.com Twitter: @habuma Slides: http://www.slideshare.net/habuma
59. Versioning
• Bundles and packages can be versioned
• Can even be versioned independent from each
other
E-mail: craig@habuma.com Blog: http://www.springinaction.com Twitter: @habuma Slides: http://www.slideshare.net/habuma
60. Versioning
• Bundles and packages can be versioned
• Can even be versioned independent from each
other
• Multiple versions can be available
simultaneously
E-mail: craig@habuma.com Blog: http://www.springinaction.com Twitter: @habuma Slides: http://www.slideshare.net/habuma
61. Versioning
• Bundles and packages can be versioned
• Can even be versioned independent from each
other
• Multiple versions can be available
simultaneously
• Packages can be imported by specifying a
specific version, a version range, or no
version at all (implying an infinite version
range)
E-mail: craig@habuma.com Blog: http://www.springinaction.com Twitter: @habuma Slides: http://www.slideshare.net/habuma
67. OSGi Tools Disclaimer
• My OSGi toolbox includes
• Maven
• Pax Construct
• BND
• TextMate (and Eclipse)
• You don’t have to use what I use
• There are a lot of options to choose from
• Ant with BND also works fine
E-mail: craig@habuma.com Blog: http://www.springinaction.com Twitter: @habuma Slides: http://www.slideshare.net/habuma
68. Just enough Maven...
• For the most part, you’ll only ever need...
• mvn install
• Occasionally you may need...
• mvn test
• mvn clean
E-mail: craig@habuma.com Blog: http://www.springinaction.com Twitter: @habuma Slides: http://www.slideshare.net/habuma
70. BND
• A giant in a small JAR
• Can be used as an Ant task
• Can be used as an Eclipse plugin
• Can be used at the command line
• Automatically infers much of a manifest
• Can be coerced, if necessary
• BND instructions can take wildcards!
• It’s the magic underneath the Felix Maven
Bundle Plugin
• Which is some of the magic underneath Pax
Construct
E-mail: craig@habuma.com Blog: http://www.springinaction.com Twitter: @habuma Slides: http://www.slideshare.net/habuma
78. Blueprint vs. Spring-DM
Spring-DM implements Blueprint Services
Spring-DM isn’t the only implementation
E-mail: craig@habuma.com Blog: http://www.springinaction.com Twitter: @habuma Slides: http://www.slideshare.net/habuma
79. Blueprint vs. Spring-DM
Spring-DM implements Blueprint Services
Spring-DM isn’t the only implementation
If you want/need the power of Spring, then
use Spring-DM
E-mail: craig@habuma.com Blog: http://www.springinaction.com Twitter: @habuma Slides: http://www.slideshare.net/habuma
80. Blueprint vs. Spring-DM
Spring-DM implements Blueprint Services
Spring-DM isn’t the only implementation
If you want/need the power of Spring, then
use Spring-DM
Otherwise, any implementation of Blueprint
Services will do
E-mail: craig@habuma.com Blog: http://www.springinaction.com Twitter: @habuma Slides: http://www.slideshare.net/habuma
86. Bundle Testing
• Does the bundle export what it’s supposed
to?
E-mail: craig@habuma.com Blog: http://www.springinaction.com Twitter: @habuma Slides: http://www.slideshare.net/habuma
87. Bundle Testing
• Does the bundle export what it’s supposed
to?
• Does the bundle publish the services that
it’s supposed to?
E-mail: craig@habuma.com Blog: http://www.springinaction.com Twitter: @habuma Slides: http://www.slideshare.net/habuma
88. Bundle Testing
• Does the bundle export what it’s supposed
to?
• Does the bundle publish the services that
it’s supposed to?
• Does the bundle’s service(s) behave like
they’re supposed to?
E-mail: craig@habuma.com Blog: http://www.springinaction.com Twitter: @habuma Slides: http://www.slideshare.net/habuma
89. Bundle Testing
• Does the bundle export what it’s supposed
to?
• Does the bundle publish the services that
it’s supposed to?
• Does the bundle’s service(s) behave like
they’re supposed to?
• Does the bundle get along with other
bundles?
E-mail: craig@habuma.com Blog: http://www.springinaction.com Twitter: @habuma Slides: http://www.slideshare.net/habuma
95. Pax-Exam/Spring-DM Testing
Written as JUnit Test
E-mail: craig@habuma.com Blog: http://www.springinaction.com Twitter: @habuma Slides: http://www.slideshare.net/habuma
96. Pax-Exam/Spring-DM Testing
Written as JUnit Test
JUnit 3 for Spring-DM; JUnit 4 for Pax Exam
E-mail: craig@habuma.com Blog: http://www.springinaction.com Twitter: @habuma Slides: http://www.slideshare.net/habuma
97. Pax-Exam/Spring-DM Testing
Written as JUnit Test
JUnit 3 for Spring-DM; JUnit 4 for Pax Exam
Test setup identifies one or more bundles and
an OSGi framework to deploy to
E-mail: craig@habuma.com Blog: http://www.springinaction.com Twitter: @habuma Slides: http://www.slideshare.net/habuma
98. Pax-Exam/Spring-DM Testing
Written as JUnit Test
JUnit 3 for Spring-DM; JUnit 4 for Pax Exam
Test setup identifies one or more bundles and
an OSGi framework to deploy to
Test methods make assertions against
bundles and services
E-mail: craig@habuma.com Blog: http://www.springinaction.com Twitter: @habuma Slides: http://www.slideshare.net/habuma
99. Pax-Exam/Spring-DM Testing
Written as JUnit Test
JUnit 3 for Spring-DM; JUnit 4 for Pax Exam
Test setup identifies one or more bundles and
an OSGi framework to deploy to
Test methods make assertions against
bundles and services
When run, test becomes an on-the-fly bundle
running within the framework
E-mail: craig@habuma.com Blog: http://www.springinaction.com Twitter: @habuma Slides: http://www.slideshare.net/habuma
103. Strategy
1. Convert full app into one large bundle
a. Embed JARs (Use Bundle-ClassPath)
b. Add Bundle-SymbolicName
E-mail: craig@habuma.com Blog: http://www.springinaction.com Twitter: @habuma Slides: http://www.slideshare.net/habuma
104. Strategy
1. Convert full app into one large bundle
a. Embed JARs (Use Bundle-ClassPath)
b. Add Bundle-SymbolicName
2. Peel off dependencies one-by-one
a. Start with 3rd party libraries
b. Pull out proprietary modules
E-mail: craig@habuma.com Blog: http://www.springinaction.com Twitter: @habuma Slides: http://www.slideshare.net/habuma