Lyndsey has posted 139 posts at DZone. View Full User Profile

Book Giveaway & Exclusive Chapter: Modular Java

09.15.2009
| 8603 views |
  • submit to reddit

DZone and Pragmatic Bookshelf have partnered to bring you this exclusive chapter from 'Modular Java' (by Craig Walls). This excerpt was extracted from Modular Java, published in July 2009 by Pragmatic Bookshelf. It is being reproduced here by permission from Pragmatic Bookshelf. For more information or to purchase a paperback or PDF copy, visit the Modular Java homepage.

To get started with Spring-DM, we’ll need to add these bundles to our project:

dwmjs% pax-import-bundle -g org.springframework.osgi -a spring-osgi-extender \
? -v 1.2.0 -- -DimportTransitive -DwidenScope
[INFO] Scanning for projects...
...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 8 seconds
[INFO] Finished at: Fri Mar 20 15:33:34 CDT 2009
[INFO] Final Memory: 9M/18M
[INFO] ------------------------------------------------------------------------
dwmjs%

Here we’ve asked Pax Construct to add version 1.2.0 of the Spring-DM extender bundle (identified with a group ID of org.springframework.osgi and an artifact ID of org.springframework.osgi.extender) to the project. In
addition to the Spring-DM extender bundle itself, we’ve also asked that pax-import-bundle also pull in transitive dependencies (-DimportTransitive) and to consider all compile and runtime dependencies as potential bundles
(-DwidenScope).



The Spring-DM bundles are now in place and are ready to help us declaratively publish the index service.

Declaring Services

The first step in declaring a service in Spring-DM is to wire a bean in the Spring application context. In Spring, a bean is any object (not necessarily a JavaBean) that is instantiated and managed by the Spring Framework. A common way of configuring the beans that Spring creates is to define a Spring application context in an XML file. For example, consider this Spring configuration XML (index-context.xml) that we’ll use to define an application context for the index service bundle:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:compass="http://www.compass-project.org/schema/spring-core-config"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.compass-project.org/schema/spring-core-config
http://www.compass-project.org/schema/spring-compass-core-config-2.0.xsd">
<bean id="indexService"
class="dwmj.index.internal.IndexServiceImpl" >
<constructor-arg ref="compass" />
</bean>
<compass:compass name="compass" >
<compass:connection>
<compass:file path="/tmp/dudeindex" />
</compass:connection>
<compass:mappings>
<compass:class name="dwmj.domain.JarFile"/>
</compass:mappings>
</compass:compass>

<compass:session id="compassSession" />
</beans>

Here we’ve declared two beans. The first is defined with the <bean> element. This bean tells Spring to create an instance of IndexServiceImpl and to give it an ID of indexService. What’s especially interesting about this bean is that we’re telling Spring to instantiate it by calling a singleargument constructor and passing in a reference to another bean. Specifically, Spring should construct IndexServiceImpl with a reference to a bean whose ID is compass.

That brings us to the second bean. Instead of using a generic <bean> element to declare the compass bean, we’re using elements from a Compass-specific configuration namespace provided as part of the
Compass library. Ultimately, this declaration creates a bean of type org.compass.core.Compass, suitable for the first argument of the IndexServiceImpl constructor.

As mentioned before, Spring-DM creates an application context by reading all XML files in the META-INF/spring directory. Since we’re building the bundle using Maven, we’ll need to place index-context.xml in the src/main/resources/META-INF/spring directory of the index bundle project. But it won’t be alone. In addition to the core Spring configuration file, we’ll also create a separate Spring configuration file (index-osgi.xml) that tells Spring-DM to publish the indexService bean to the OSGi service registry:

<beans:beans xmlns="http://www.springframework.org/schema/osgi"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/osgi
http://www.springframework.org/schema/osgi/spring-osgi.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<service ref="indexService"
interface="dwmj.index.IndexService" />

 
Click here to download the entire exerpt.


Copyright © 2009 Craig Walls. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form, or by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior consent of the publisher.
AttachmentSize
DeclaringServices.pdf149.49 KB
bookcontest-modjava-sidways.jpg29.37 KB
Published at DZone with permission of its author, Lyndsey Clevesy.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Stevi Deter replied on Tue, 2009/09/15 - 10:52am

I expect this will be another great book from the Pragmatic Bookshelf, explaining how to use Spring DM and OSGI for dynamic, modular applications. Great topic!

Sudhakar Ramasamy replied on Tue, 2009/09/15 - 12:16pm

I'm currently reading this book borrowed from my library at work. And I like the content and Craig Walls's style. It feels like taking an easy walk through the garden taking in one thing at a time till the whole picture comes alive. It'd be great if I won this contest and got my own free copy of the book!

Mike P(Okidoky) replied on Tue, 2009/09/15 - 12:41pm

In order to win this book, you have to comment on your experiences with this book. What's the point of winning this book if you already have this book?

Slava Ustovitskiy replied on Tue, 2009/09/15 - 10:09pm

Looks like it is great book! Looking forward to get one copy of it.

Neil Shannon replied on Wed, 2009/09/16 - 8:16am

I've enjoyed other selections from the Pragmatic Bookshelf and look forward to exploring Spring DM and OSGi further.  Most of my work lately has been focused on rapidly changing legacy web-apps with redeployments every two weeks or so--adding modular capability to eliminate service interruptions would be ideal.

Lyndsey Clevesy replied on Wed, 2009/09/16 - 10:25am in response to: Mike P(Okidoky)

Hi Mike,

You may comment on your experiences with this book, or you can comment on your experiences with the topic of the book. Or you may comment on this particular chapter.

Thanks!

Lyndsey

Jose Fernandez replied on Wed, 2009/09/16 - 10:48am

When I first heard about OSGi the concept excited me. Unfortunately it is a topic that I have not been able to invest any time researching thus far. This book has been on my Amazon wish list for a while as one of the few available resources for learning OSGi. I own several other Pragmatic Programmer books and found them to be consistently concise and informational. I have several personal projects in the pipeline and several of them were going to be based on the same technology, so I am really curious as to how Spring/OSGi - and this book - can help me achieve the degree of code re-use that I desire.

Dan Dromereschi replied on Thu, 2009/09/17 - 1:23am

We were developing a framework for geophysical applications based on a home-grown technology. The project was growing nice and we were adding a lot of features, the code base was growing. However the framework lacked the most important piece of functionality: it was not easy to write plugins / applications based on it. We started to evaluate OSGi and we were amazed by its cool features: bundles, declarative services, extensions. OSGi is definitely a good choice in order to achieve modular Java projects. And in order to start working with an OSGi implementation, a book is required. Regards, Dan

Paul Cowan replied on Thu, 2009/09/17 - 3:46pm

Looks like an interesting book. I'm another one of those people who has to deal a lot with upgrading production systems, dealing with dependencies between modules, trying to keep a system maintainable, and uptime + reliability issues. All of that stuff makes it sound like I'm the perfect candidate for stuff like OSGi, but like most Java developers OSGi just kind of seems like black magic to me. It's a really poorly-understood part of the Java world, and I think most developers are in the same boat as me. I look at something like Equinox and go 'yeah, ok, this is amazing, but I still don't get it'. Definitely need to learn more about it -- any system which might even remotely help alleviate the fortunately rare but still very painful Java medical condition known as 'classloader hell' would be welcome!

Expert Writer replied on Sat, 2009/09/19 - 1:38am

Hi,

Nice work! I personally like the Pragmatic bookshelf. I will be looking for more information and details 

<a href="http://www.topcustomessays.co.uk"> Essays</a>

Christian Voller replied on Thu, 2009/09/24 - 5:50am

OSGI and Spring DM are really powerful frameworks. I am really interested in these Frameworks and looking forward to get a copy :-))!

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.