OSGi & Spring combined Using Spring together with Eclipse Equinox Bernd Kolb b.kolb@kolbware.de http://www.kolbware.de Gerd Wütherich gerd@wuetherich.de http://www.wuetherich.de Martin Lippert lippert@acm.org Copyright 2007 by Bernd Kolb; -1-made available under the 2007 EPL Bernd v1.0 Kolb
C O N T E N T S What is OSGi Where does OSGi come from What is Spring Why this combination Problems An Example Conclusion -2-
Woher kommt OSGi OSGi-Alliance: http://www.osgi.org/ org/ Developed since 1999 Originally for Embedded-Systems Now: Also used for enterprise and server-side side systems Applications based on a OSGi-Framework: Eclipse, IBM-Websphere Application-Server, Lotus, Adobe BEA Oracle -3-
C O N T E N T S What is OSGi Where does OSGi come from What is Spring Why this combination Problems An Example Conclusion -4-
What is OSGi - I A dynamic, service oriented module system for Java Source: OSGi.org A module system for Java which defines Modules (aka Bundles ), Visibility of Packages (public-api vs. private-api) Dependencies between Bundles Version of a Bundle. -5-
What is OSGi - II A dynamic, service oriented module system for Java Source: OSGi.org Bundles can be dynamically installed, started, stopped uninstalled and refreshed during runtime. -6-
What is OSGi - III A dynamic, service oriented module system for Java Source: OSGi.org Bundles can dynamically publish services Bundles can search for services in a global service repository and use them The runtime provides support for (de-)registration of services while the system is up and running -7-
C O N T E N T S What is OSGi Where does OSGi come from What is Spring Why this combination Problems An Example Conclusion -8-
Was ist Spring de-facto standard for Java EE Applications Simple programming model (POJOs) Inversion of Control / Dependency Injection AOP using different implementations Many technology abstractions and helpers Simple and technology independent d development of enterprise applications -9-
C O N T E N T S What is OSGi Where does OSGi come from What is Spring Why this combination Problems An Example Conclusion -10-
Why this combination - I OSGi == Embedded? - Spring == Enterprise?? Goal: Using the benefits of the OSGi Service Platform without loosing the benefits of Spring Splitting up your application in Bundles to use Dependency Management Visibility Management Versions-Management Dynamic behavior Deploying Spring as Bundles -11-
Why this combination - II Boundary conditions Complexity It must still be as easy to use as Spring is POJO programming model Integration with the OSGi-Service-Model OSGi-Services as Beans and vice versa Testing Must be possible without an OSGi-Container If possible, no dependencies to the OSGi-APIs The enterprise features of Spring must still be available -12-
C O N T E N T S What is OSGi Where does OSGi come from What is Spring Why this combination Problems An Example Conclusion -13-
Problems in general Class loading Many 3rd-party libraries expect to see and work on a linear Java Class path No linear Class path in OSGi Using strategies to load Classes Buddy Class loading ContextClassLoader Dynamic behavior What to do if a part of my application is not available?? How to handle the different versions? Which 3rd-party libraries can I use? -14-
Problems in particular It is still pretty new stuff Broken builds Bugs Missing features Examples: Persistence layer: Class loading problems Problems with OSGi s dynamic behavior Web Not yet implemented Also some open questions -15-
C O N T E N T S What is OSGi Where does OSGi come from What is Spring Why this combination Problems An Example Conclusion -16-
Example I OSGi Spring Application(s) -17-
Example II Bundles and Spring Normal Spring-Apps: 1 ApplicationContext Spring-OSGi: One App == several Bundles One ApplicationContext per Bundle: Spring-OSGi creates and destroys the Context automatically whenever a bundle was (de-)activated Defining the ApplicationContext in META-INF/spring -18-
Example III Beans as OSGi Service Spring-Beans can be exported as an OSGi-ServiceService OSGi-Services are visible for all bundles <beans> <bean name="customerservice" class="de.example.customerservice.internal.customerserviceimpl" /> <osgi:service i id="customerserviceosgi" i i" ref="customerservice" interface="de.example.customerservice.customerservice" /> </beans> -19-
Example IV OSGi Service as Beans Using existing OSGi-Services within your Spring-Context <beans> <osgi:reference id="customerserviceosgi" i i" interface="de.example.customerservice.customerservice"/> <bean id="orderservice" class="de.example.orderservice.internal.orderserviceimpl"> <property name="customerservice"> <ref local="customerserviceosgi"/> </property> </bean> </beans> -20-
Example V jpetstore jpetstore is a Spring example 3 Layers ibatis RMI, HTTP, Hessian and Burlap for remoting JSPs and Spring- WebMVC -21-
Example VI jpetstore Architecture I Bundles and their dependencies Export:...dataaccess Internal:...internal.dataaccess...internal.dataaccess.ibatis...internal.dataaccess.ibatis.maps dataaccess import...dataaccess Export:...domain Export:...domain.logic...domain.logic.validator Internal:...domain.internal.logic domain.logic import...domain domain import...domain.logic...domain.logic.validator Export: Internal:...internal.web...internal.web.spring web -22-
Example VII jpetstore Architecture II Domain model and Database mapping Account mapped to Account 1 -account mapped to Order mapped to LineItem Order 1..* -items LineItem -quantity : int 1 -item mapped to Product CartItem -item 1 Item 0..* -cartitems -items 0..* Cart -product 1 Product -category 1 Category mapped to Item mapped to Category -23-
Example VIII jpetstore Architecture III Services dataaccess DataSource <<uses>> PropertyPlaceholder <<uses>> AccountDao CategoryDao ProductDao ItemDao OrderDao TransactionManager domain.logic <<uses>> <<AOP>> <<uses>> TransactionAdvice OrderValidator AccountValidator PetStore <<uses>> exporter web servlets <<uses>> httppetstore client client rmipetstore rmipetstore...controller -24-
C O N T E N T S What is OSGi Where does OSGi come from What is Spring Why this combination Problems An Example Conclusion -25-
Conclusion No OSGi-API used Visibility could be defined not only on package-level but also for Spring beans Dependency Injection Many other possibilities Service cardinalities Service-Listener Property-Placeholder -26-
Thank you for your attention Questions? -27-