Anda di halaman 1dari 26

Spring Dynamic

Modules for
OSGi
From Hello World to Real World

About you...

• By show of hands...
• Java 6? Java 5? Java 1.4? Java 1.3? Java 1.2-?

• C#? Ruby? Groovy? Scala? Erlang? Python?

• Spring 1.x? Spring 2.0.x? Spring 2.5.x?

• OSGi? Spring-DM?

E-mail: craig@habuma.com Blog: http://www.springloaded.info Source Code: svn://svn.geekisp.com/SiA svn://svn.geekisp.com/habuma


About me...

• Professionally developing software for almost 14 years


• Java developer for most of that time

• Telecom, finance, retail, education, software

• Now at Semantra, Inc. developing natural language


business intelligence solution

• Author of Spring in Action and XDoclet in Action

• Spring fanatic

E-mail: craig@habuma.com Blog: http://www.springloaded.info Source Code: svn://svn.geekisp.com/SiA svn://svn.geekisp.com/habuma

Agenda

• Modularity and Java

• The OSGi Solution to Modularity

• The OSGi Problem

• The Spring-DM Solution

• Building Web-Apps with Spring-DM

• What about S2AP?

• Q&A

E-mail: craig@habuma.com Blog: http://www.springloaded.info Source Code: svn://svn.geekisp.com/SiA svn://svn.geekisp.com/habuma


Disclaimer

• I only have a little over an hour

• There’s a *lot* of ground to cover

• There’s more to it than what you’ll see tonight

• There simply isn’t enough time

E-mail: craig@habuma.com Blog: http://www.springloaded.info Source Code: svn://svn.geekisp.com/SiA svn://svn.geekisp.com/habuma

Modularity
and
Java
What is Modularity?

• Module: Self-contained component of a larger


system

• Key attributes of a module...


• High cohesion - The internal measurement of how
focused a module is

• Low coupling - The external measure of how a module


relates to other modules

E-mail: craig@habuma.com Blog: http://www.springloaded.info Source Code: svn://svn.geekisp.com/SiA svn://svn.geekisp.com/habuma

Why Modularity?

• Key benefits are...


• Changeability - “It enables us to change our minds faster.”

• Comprehensibility - It’s easier to understand a large


system if you first understand its individual pieces

• Parallel Development - Task breakdown can take place


along module boundaries

• Testability - Modules can be tested in isolation

• Reuse - Highly-cohesive/loosely-coupled modules are


more reusable

E-mail: craig@habuma.com Blog: http://www.springloaded.info Source Code: svn://svn.geekisp.com/SiA svn://svn.geekisp.com/habuma


Benefits of Modularity

"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."
--Richard Gauthier and Stephen Pont
Designing Systems Programs, Prentice Hall 1970

E-mail: craig@habuma.com Blog: http://www.springloaded.info Source Code: svn://svn.geekisp.com/SiA svn://svn.geekisp.com/habuma

Modularity in Java?

• Methods

• Classes

• Packages

• JAR files

E-mail: craig@habuma.com Blog: http://www.springloaded.info Source Code: svn://svn.geekisp.com/SiA svn://svn.geekisp.com/habuma


I’m Already Modular!
• You might say...
• I use Spring and interfaces to decouple my application
classes
• I use Maven’s multimodule support to break my app into
several modules
• I’m careful to not use private implementations directly

• John Wells (formerly BEA) says...


• “Many developers say things like: ‘My code is very
modular’, or ‘My code doesn’t depend on very much’, or
‘No one uses any of my classes except from the Foo
package.’ Unless they are already using OSGi, they are
wrong. Until modularity is enforced, it is not there.”
E-mail: craig@habuma.com Blog: http://www.springloaded.info Source Code: svn://svn.geekisp.com/SiA svn://svn.geekisp.com/habuma

Are you really modular?

• If so, then why do you still deploy your web


applications in one big monolithic WAR file?

E-mail: craig@habuma.com Blog: http://www.springloaded.info Source Code: svn://svn.geekisp.com/SiA svn://svn.geekisp.com/habuma


The OSGi
Solution to
Modularity
It doesn’t like to be called “OSS-GEE”

History of OSGi

• OSGi Alliance formed in 1999


• Today includes members from Ericcson, IBM, Motorola,
Sun, SpringSource, and others

• Originally targeted at home automation systems

• Desktop (Eclipse 3.0)

• Moving into the server-side

E-mail: craig@habuma.com Blog: http://www.springloaded.info Source Code: svn://svn.geekisp.com/SiA svn://svn.geekisp.com/habuma


Key Elements of OSGi

• Deployment model for


>((6*8")*:+?@5+$6&0

.&#3*8&0

.&85#*)/
Java modules .&#3*8&-=&,*0)#/

• Bundle lifecycle ;*<&8/86&

management 7:$56&0

• Services (SOA in a JVM) 2"3"-4*#)5"6-7"89*+&

• Core and compendium '(&#")*+,-./0)&1

!"#$%"#&
services
• Security

E-mail: craig@habuma.com Blog: http://www.springloaded.info Source Code: svn://svn.geekisp.com/SiA svn://svn.geekisp.com/habuma

What does OSGi offer?

• Content hiding

• Service registry

• Parallel deployment of bundle versions

• Strong naming

E-mail: craig@habuma.com Blog: http://www.springloaded.info Source Code: svn://svn.geekisp.com/SiA svn://svn.geekisp.com/habuma


What’s an OSGi Bundle?

• Just a JAR file


• With special entries in META-INF/MANIFEST.MF

• May export packages for other bundles to use


• May import packages exposed by other bundles

• May expose services to be used by other bundles


• May consume services exposed by other bundles

• May be extended by fragments

E-mail: craig@habuma.com Blog: http://www.springloaded.info Source Code: svn://svn.geekisp.com/SiA svn://svn.geekisp.com/habuma

Bundle Lifecycle
!"#$%&&

'()%$*
!"#$%&&'(

&& +*
$% #,
!"# &-*
'"

'"!"#$%&&
)"!"#$%&&'( *'#+&,'(
$
%+
#$

#$%*$!"- #$+..!"-
,(
#$

%/$!,'

E-mail: craig@habuma.com Blog: http://www.springloaded.info Source Code: svn://svn.geekisp.com/SiA svn://svn.geekisp.com/habuma


OSGi Containers

• Where bundles live


• Manages bundle lifecycle

• Several open-source containers...


• Equinox (Eclipse)

• Knopflerfish

• Felix (formerly known as Oscar)

• Concierge

E-mail: craig@habuma.com Blog: http://www.springloaded.info Source Code: svn://svn.geekisp.com/SiA svn://svn.geekisp.com/habuma

OSGi Myths

• OSGi is complex

• OSGi is heavyweight

E-mail: craig@habuma.com Blog: http://www.springloaded.info Source Code: svn://svn.geekisp.com/SiA svn://svn.geekisp.com/habuma


What about JSR-277?

• JAM vs. JAR

• Language constructs (Java 7 only?)

• No dynamic loading/unloading

• No module isolation - inconsistent classpath

• Module-to-module dependencies vs. module-to-


package dependencies

• Odd versioning scheme vs. commonly understood


versioning scheme

E-mail: craig@habuma.com Blog: http://www.springloaded.info Source Code: svn://svn.geekisp.com/SiA svn://svn.geekisp.com/habuma

Example:
Hello World 1
Example: Hello World

!"##$%&$'()*"+%,)'-#" !"##$%."+/01"%,)'-#"

!"##$ !"##$
&#0"'3 ."+/01"

1+"43"( +"50(3"+(

2130/43$+ 2130/43$+

E-mail: craig@habuma.com Blog: http://www.springloaded.info Source Code: svn://svn.geekisp.com/SiA svn://svn.geekisp.com/habuma

The Problem
with
OSGi
import org.osgi.*

• The OSGi model is a bit invasive

• Normally confined to OSGi-specific classes


• May leak into business classes

E-mail: craig@habuma.com Blog: http://www.springloaded.info Source Code: svn://svn.geekisp.com/SiA svn://svn.geekisp.com/habuma

Managing the Manifest


• Handling a bundle’s imports and exports can be
tedious
• It isn’t that bad, though...
• Smaller, more cohesive bundles need less stuff in the
manifest

• Tools help a lot


• BND - http://www.aqute.biz/Code/Bnd
• Maven Bundle Plugin - http://felix.apache.org/site/
maven-bundle-plugin-bnd.html
• Pax Construct - http://www.ops4j.org/projects/pax/
construct
E-mail: craig@habuma.com Blog: http://www.springloaded.info Source Code: svn://svn.geekisp.com/SiA svn://svn.geekisp.com/habuma
Spring and OSGi

Two great tastes that taste great


together

Spring does...

• Loose-coupling through dependency injection

• Promotes POJO-based development

• Rich framework of services and abstractions

E-mail: craig@habuma.com Blog: http://www.springloaded.info Source Code: svn://svn.geekisp.com/SiA svn://svn.geekisp.com/habuma


Spring-DM

• Spring Dynamic Modules for OSGi


• aka, Spring-OSGi or Spring-DM

• Spring and OSGi together


• Replaces programmatic OSGi service with declarative
services

• http://www.springframework.org/osgi

E-mail: craig@habuma.com Blog: http://www.springloaded.info Source Code: svn://svn.geekisp.com/SiA svn://svn.geekisp.com/habuma

Spring OSGi Extender

• Automatic creation of Spring context


• Watches for bundles to be deployed and started

• Looks for Spring context to create

• Transparent publishing of POJOs as services

• Transparent consumption of services within a


Spring context

E-mail: craig@habuma.com Blog: http://www.springloaded.info Source Code: svn://svn.geekisp.com/SiA svn://svn.geekisp.com/habuma


Example:
Hello World 2

Hello Service and Consumer


• Service and consumer are just beans in Spring

• Spring-DM exposes service bean as an OSGi


service...

• ...and references bean to be wired into client

E-mail: craig@habuma.com Blog: http://www.springloaded.info Source Code: svn://svn.geekisp.com/SiA svn://svn.geekisp.com/habuma


Spring-DM Context

• /META-INF/spring/*.xml
• Assumed to be Spring context definitions

• Spring-DM extender automatically creates Spring


application context

E-mail: craig@habuma.com Blog: http://www.springloaded.info Source Code: svn://svn.geekisp.com/SiA svn://svn.geekisp.com/habuma

Spring-DM Namespace

• Two key elements:


• <osgi:service> - Publishes a Spring <bean> as an OSGi
service.

• <osgi:reference> - Consumes an OSGi service...makes it


available as a bean in the Spring context.

E-mail: craig@habuma.com Blog: http://www.springloaded.info Source Code: svn://svn.geekisp.com/SiA svn://svn.geekisp.com/habuma


Building Web-
Apps with Spring
and OSGi
Let’s get real

A typical Java web app


• Logically partitioned into layers

• But probably deployed as one monolithic WAR

E-mail: craig@habuma.com Blog: http://www.springloaded.info Source Code: svn://svn.geekisp.com/SiA svn://svn.geekisp.com/habuma


Web App With OSGi
• Layers are deployed as independent bundles
• But must interact directly with container to interact with
each other

E-mail: craig@habuma.com Blog: http://www.springloaded.info Source Code: svn://svn.geekisp.com/SiA svn://svn.geekisp.com/habuma

Web App with Spring-DM


• Spring injects services transparently across
bundles
• No OSGi-specific Java code required

E-mail: craig@habuma.com Blog: http://www.springloaded.info Source Code: svn://svn.geekisp.com/SiA svn://svn.geekisp.com/habuma


What to do about web layer?

• This is where it gets tricky

• How do you deploy a WAR file?

• How do you wire OSGi services into Spring MVC


controllers?

E-mail: craig@habuma.com Blog: http://www.springloaded.info Source Code: svn://svn.geekisp.com/SiA svn://svn.geekisp.com/habuma

Deploying WAR bundles

• Pax Web Extender


• Watches for WAR-like bundles (or is it OSGi-like WARs?)

• Parses WEB-INF/web.xml

• Automatically deploys WAR contents to HTTP service

• Spring-DM Web Extender


• Bridges OSGi world with web container (Tomcat or Jetty)

• Full web.xml support

E-mail: craig@habuma.com Blog: http://www.springloaded.info Source Code: svn://svn.geekisp.com/SiA svn://svn.geekisp.com/habuma


Spring-DM web extender

• Uses Tomcat by default


• Can be configured to use Jetty

• Bridges gap between OSGi and web container

• Automatically registers WAR files with web


container

E-mail: craig@habuma.com Blog: http://www.springloaded.info Source Code: svn://svn.geekisp.com/SiA svn://svn.geekisp.com/habuma

What we’d like to see

E-mail: craig@habuma.com Blog: http://www.springloaded.info Source Code: svn://svn.geekisp.com/SiA svn://svn.geekisp.com/habuma


What we get

E-mail: craig@habuma.com Blog: http://www.springloaded.info Source Code: svn://svn.geekisp.com/SiA svn://svn.geekisp.com/habuma

Spring MVC support

• Released with Spring-DM 1.1.0

• Important...even if you’re not using Spring MVC


• Integration between Spring and other web frameworks
typically involves ContextLoaderListener...part of Spring
MVC

• Supplies OSGi-savvy web application context


• Bridges the gap between Spring MVC-created context
and Spring-DM-created context

E-mail: craig@habuma.com Blog: http://www.springloaded.info Source Code: svn://svn.geekisp.com/SiA svn://svn.geekisp.com/habuma


Example:
Dude, Where’s
My JAR?

Dude, Where’s My JAR?

• Search engine for Java developers

• JarHoo + mvnrepository.com + a bit of special


magic

• Search for JAR files and OSGi bundles based on


their content

E-mail: craig@habuma.com Blog: http://www.springloaded.info Source Code: svn://svn.geekisp.com/SiA svn://svn.geekisp.com/habuma


DWMJ: Bundle Design

/$01 !"#$%&' 7583)"


2..-)*34)5"
+,"#-$
/26

9$3'*;9 &'

!"#$%&'
# $%
!"

!"#$%
3##91:269 &.)#$'
&$'()*$
+,"#-$
+,"#-$

E-mail: craig@habuma.com Blog: http://www.springloaded.info Source Code: svn://svn.geekisp.com/SiA svn://svn.geekisp.com/habuma

What’s the deal


with the
SpringSource
Application
Platform?
What is S2AP?

• Spring-DM + Equinox + Tomcat

• Next-generation application server platform

• A glimpse into Java EE 6? (JSR-316)


• All about extensibility and profiles

E-mail: craig@habuma.com Blog: http://www.springloaded.info Source Code: svn://svn.geekisp.com/SiA svn://svn.geekisp.com/habuma

S2AP Deployment Models

• Standard WAR

• Shared library WAR

• Shared services WAR

• Web Modules

• PAR

E-mail: craig@habuma.com Blog: http://www.springloaded.info Source Code: svn://svn.geekisp.com/SiA svn://svn.geekisp.com/habuma


Q&A

Resources

• These Slides: http://www.habuma.com/spring/SpringDM.pdf


• Spring-DM: http://www.springframework.org/osgi
• S2AP: http://www.springframework.com/products/suite/applicationplatform
• OSGi Alliance: http://www.osgi.org
• BND: http://www.aqute.biz/Code/Bnd
• OPS4J (PAX): http://www.ops4j.org
• Peter Kriens’ blog: http://www.aqute.biz/Blog
• Neil Bartlett’s blog: http://neilbartlett.name/blog
• Equinox: http://www.eclipse.org/equinox
• Felix: http://felix.apache.org
• Knopflerfish: http://www.knopflerfish.org

E-mail: craig@habuma.com Blog: http://www.springloaded.info Source Code: svn://svn.geekisp.com/SiA svn://svn.geekisp.com/habuma

Anda mungkin juga menyukai