<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>

<channel>
	<title>typed by Hans</title>
	<atom:link href="http://www.notehive.com/wp/feed/" rel="self" type="application/rss+xml" />
	<link>http://notehive.com/wp</link>
	<description></description>
	<pubDate>Fri, 28 May 2010 17:32:41 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5.1</generator>
	<language>en</language>
			<item>
		<title>Secrets of Success</title>
		<link>http://notehive.com/wp/2009/02/18/secrets-of-success/</link>
		<comments>http://notehive.com/wp/2009/02/18/secrets-of-success/#comments</comments>
		<pubDate>Wed, 18 Feb 2009 20:37:24 +0000</pubDate>
		<dc:creator>loedolff</dc:creator>
		
		<category><![CDATA[Life]]></category>

		<guid isPermaLink="false">http://notehive.com/wp/?p=59</guid>
		<description><![CDATA[I just watched Richard St. John: Secrets of success in 8 words, 3 minutes.  Inspired by a question that a teenager asked Richard - &#8220;What leads to success?&#8221; - and the fact that he wasn&#8217;t able to give her an answer, Richard started asking TED conference attendees what they think leads to success.  After talking [...]]]></description>
			<content:encoded><![CDATA[<p>I just watched <a href="http://www.ted.com/index.php/talks/richard_st_john_s_8_secrets_of_success.html" target="_blank">Richard St. John: Secrets of success in 8 words, 3 minutes</a>.  Inspired by a question that a teenager asked Richard - &#8220;What leads to success?&#8221; - and the fact that he wasn&#8217;t able to give her an answer, Richard started asking TED conference attendees what they think leads to success.  After talking to more than 500 people over 7 years, he summarizes the answers in this talk.  Richard says the following factors lead to success:</p>
<p><strong>Passion</strong><em> </em></p>
<p>Do what you do for love, not money.  He quotes Carol Coletta (radio producer of <a href="http://www.smartcityradio.com/smartcityradio/">Smart City</a>) as saying &#8220;I would pay someone to do what I do&#8221;.</p>
<p>Have you ever enjoyed your work so much that you couldn&#8217;t believe you were actually getting paid to do it?  The times in my life that I&#8217;ve felt like this were also the times during which I excelled.  A boring job makes you goof off and there is no way to be successful at something if you are not paying attention!</p>
<p><strong>Work</strong></p>
<p>Richard quotes Rupert Murdoch: &#8220;It&#8217;s all hard work.  Nothing comes easily.&#8221;</p>
<p>That&#8217;s good to know!  Successful people most often make it look so easy!  They tend to make me think &#8220;of course <em>they </em>can do it, it&#8217;s easy for <em>them</em>!&#8221;.  It&#8217;s good to know that&#8217;s not the norm!</p>
<p><strong>Excellence </strong></p>
<p>The word Richard used is &#8220;good&#8221;.  He quotes Alex Garden (a game developer) &#8220;To be successful, put your nose down in something and get damn good at it&#8221;).  Richard says: &#8220;There&#8217;s no magic - it&#8217;s practice-practice-practice&#8221;.</p>
<p>But how do you find out what you can become good at?  Is it only the things you have passion for?  How do you discover your passion?  Richard, I think the 3 minute talk was too short!  Could you add another minute and help answer this question?</p>
<p><strong>Focus</strong></p>
<p>I like it, but again, <em>what </em>should I focus on?</p>
<p><strong>Push </strong></p>
<p>Meaning &#8220;push yourself&#8221; (not other people!).  Perseverance.  I&#8217;ve heard that before.  But again, some things are better not pursuing.  How do you identify those before putting all your energy into it?</p>
<p><strong>Serve</strong></p>
<p>&#8220;Perform or provide a service&#8221; almost sounds like a charitable activity, but it does not have to be so.  You can get <em>rich </em>if you s<em>erve </em>people something of value!</p>
<p><strong>Ideas </strong></p>
<p>To get good ideas, Richard offers the string: &#8220;Listen, observe, be curious, question, problem solve, make connections&#8221;.  Good idea!</p>
<p><strong>Persist </strong></p>
<p>Persist through failure and &#8220;crap&#8221; (criticism, rejection, assholes, pressure).  Sounds good, but again I have to ask for more guidance.  Some criticism and rejection is valid (and sometimes unwittingly <em>you </em>are the asshole!)- how can you learn that a course of action should be abandoned?</p>
<p>Richard&#8217;s talk is inspiring and a &#8220;feel-good&#8221; talk, but on the flip side are people trying to do something crazy or impossible things (like trying to sell Hummers or believing in <em>clean </em>coal!)  What guidance can convince these poor souls to abandon their ill conceived causes?</p>
]]></content:encoded>
			<wfw:commentRss>http://notehive.com/wp/2009/02/18/secrets-of-success/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Too much choice</title>
		<link>http://notehive.com/wp/2008/08/13/too-much-choice/</link>
		<comments>http://notehive.com/wp/2008/08/13/too-much-choice/#comments</comments>
		<pubDate>Wed, 13 Aug 2008 20:33:44 +0000</pubDate>
		<dc:creator>loedolff</dc:creator>
		
		<category><![CDATA[Life]]></category>

		<guid isPermaLink="false">http://notehive.com/wp/?p=55</guid>
		<description><![CDATA[Barry Schwartz wrote the book The Paradox of Choice: Why More Is Less more and gave this TED talk on why too much choice can be a bad thing.
To summarize, too much choice is bad when:

It paralyzes decision making because you fear making the wrong choice.
You recalculate the benefit of your choice by subtracting the [...]]]></description>
			<content:encoded><![CDATA[<p>Barry Schwartz wrote the book <a href="http://www.amazon.com/Paradox-Choice-Why-More-Less/dp/0060005696/ref=pd_bbs_1?ie=UTF8&amp;s=books&amp;qid=1218657987&amp;sr=8-1" target="_blank">The Paradox of Choice: Why More Is Less more</a> and gave <a href="http://www.ted.com/index.php/talks/barry_schwartz_on_the_paradox_of_choice.html" target="_blank">this TED talk</a> on why too much choice can be a bad thing.</p>
<p>To summarize, too much choice is bad when:</p>
<ul>
<li>It paralyzes decision making because you fear making the wrong choice.</li>
<li>You recalculate the benefit of your choice by subtracting the benefits of the choices you didn&#8217;t make (i.e., subtracting the opportunity cost).</li>
<li>Choices leads to escalation of expectations.</li>
<li>You blame yourself for not making the right choice (if you didn&#8217;t have to choose then you couldn&#8217;t be blamed for making the wrong choice).</li>
</ul>
<p>A couple months ago I signed up to do the <a href="http://www.hoodtocoast.com/" target="_blank">Hood To Coast</a> relay run, which is next weekend.  I&#8217;m running with a really well organized and experienced team.  The team captains are taking care of all the organization, pretty much all I have to do is show up and run - so this is a fantastic opportunity!</p>
<p>However, since I signed up for the run I&#8217;ve had some regret because I&#8217;ve discovered I&#8217;m missing out on several other things (the company picnic, the first Chinese class of the new semester, an annual 100 mile cycling event, and watching the last couple of days of the Olympic games).  Why should having all these options available reduce the benefit of my decision to do the HTC run?!  Except for missing the class, none of the other options would even remotely be valid reasons for not doing the run!</p>
<p>Thanks to Barry, I now understand why I feel a little bit of regret, even though I shouldn&#8217;t.  However, in his TED talk Barry didn&#8217;t say what I should do about it&#8230;  it&#8217;s not unlike Daniel Gilbert, who explains in his book - <a href="http://www.amazon.com/Stumbling-Happiness-Daniel-Gilbert/dp/1400077427/ref=pd_sim_b_3" target="_blank">Stumbling on Happiness</a> - why we are often unhappy about our choices, without really explain what to do about it!</p>
<h2 class="entry-title"><a class="entry-title-link" href="http://feeds.feedburner.com/%7Er/TEDBlog/%7E3/363875702/archive_author.php" target="_blank"></a></h2>
]]></content:encoded>
			<wfw:commentRss>http://notehive.com/wp/2008/08/13/too-much-choice/feed/</wfw:commentRss>
		</item>
		<item>
		<title>My /dev directory layout and environment setup</title>
		<link>http://notehive.com/wp/2008/08/01/my-dev-directory-layout-and-environment-setup/</link>
		<comments>http://notehive.com/wp/2008/08/01/my-dev-directory-layout-and-environment-setup/#comments</comments>
		<pubDate>Sat, 02 Aug 2008 00:12:48 +0000</pubDate>
		<dc:creator>loedolff</dc:creator>
		
		<category><![CDATA[Tools]]></category>

		<guid isPermaLink="false">http://notehive.com/wp/?p=44</guid>
		<description><![CDATA[This article describes the directory structure and command shell environment configuration I use on my development machine to help manage tools, source code and dependent libraries.
Directory Layout
On Windows I keep all my development tools and source code under c:\dev.  On Linux I use the same layout under $HOME/dev/
Inside the &#8220;dev&#8221; folder I have the following [...]]]></description>
			<content:encoded><![CDATA[<p>This article describes the directory structure and command shell environment configuration I use on my development machine to help manage tools, source code and dependent libraries.</p>
<h3>Directory Layout</h3>
<p>On Windows I keep all my development tools and source code under c:\dev.  On Linux I use the same layout under $HOME/dev/</p>
<p>Inside the &#8220;dev&#8221; folder I have the following sub-directories:</p>
<p><strong>&#8230;/dev</strong></p>
<p style="padding-left: 30px;"><strong>/tools</strong> - contains executable application, tools and utilities, like Eclipse, DbVisualizer, Tomcat, Maven, HSQLDB, etc.  I only place tools here that you can install manually simply by unzipping them (for example, Ecipse, Maven, Tomcat).  For the tools or applications don&#8217;t ship in a zip file but only come with installers (for example, Java, Oracle, TortoiseSVN) - I let these tools install to their default locations (for example, under &#8220;C:\Program Files&#8221; or  &#8220;/usr/local/&#8221;).</p>
<p style="padding-left: 30px;"><strong>/lib </strong>- contains binary distributions of 3rd-party libraries and documentation, like GWT, JUnit, SpringFramework, Hibernate, etc.  These days this directory is actually pretty empty - since I now mostly rely on the Maven to download 3rd party libraries and store it in the local Maven repository.  However, it is still sometimes useful or necessary to have the documentation and examples handy.</p>
<p style="padding-left: 30px;"><strong>/os</strong> - contains source code for 3rd-party Open Source libraries and tools.  I also use it as a sandbox for unpacking and executing 3rd party sample code.  The entries here are usually transient.</p>
<p>For example, on Windows I have:</p>
<pre style="font-size: 10pt">    c:\dev\
            tools\
                    \apache-maven-2.0.9
                    \apache-tomcat-6.0.14
                    \eclipse-jee-ganymede-win32
                    …etc…
           lib\
                    \gwt-windows-1.4.62
                    \htmlunit-1.14
                    \spring-osgi-1.0.2
                    …etc…
           os\
                    \eclipse-equinox-SDK-3.3.2
                    \pax-samples
                    \servletbridge
                    …etc…
</pre>
<p>I also place my own source code projects directly under &#8220;dev&#8221;, for example:</p>
<pre style="font-size: 10pt">    c:\dev\
            apps\
            spikes\
            voluble\
            …etc…
</pre>
<p>BTW, &#8220;spikes&#8221; is a nice &#8220;catch all&#8221; place for trying new things out before moving them into &#8220;real&#8221; projects.  Even though &#8220;spikes&#8221; only contains throwaway or proof-of-concept code, I tend to commit the contents to a SVN repository.  Some people use the term &#8220;sandbox&#8221; instead of &#8220;spikes&#8221;.</p>
<h3>Shell Environment Configuration</h3>
<p>To set up my environment variables I create a command file in the &#8220;dev&#8221; folder - usually called &#8220;setenv.cmd&#8221; on Winows and &#8220;setenv.sh&#8221; on Linux.  The file sets up the path, environment variables, and on Linux it also sets up aliases.</p>
<p>On Windows I find it far more convenient to manage the environment settings through a command file rather than going through the Control Panel (System &gt; Advanced &gt; User Profiles &gt; Environment Variables) dialog.  It&#8217;s also very useful if to set your environment through batch files if you sometimes need to run a different versions of a tool (for example, if you use Java 6 for new projects but sometimes have to run Java 1.4 for legacy projects).</p>
<p>This is an example Windows setenv.cmd file:</p>
<pre style="font-size: 10pt">SET JAVA_HOME=c:\Progra~1\Java\jdk1.5.0_14
SET ANT_HOME=C:\dev\tools\apache-ant-1.9.0
SET CATALINA_HOME=C:\dev\tools\apache-tomcat-6.0.14
SET MAVEN_OPTS=-Xmx512m
SET DERBY_HOME=C:\dev\tools\db-derby-10.3.2.1-bin
SET SPRING_OSGI_LIB=C:\dev\os\spring-osgi-1.0-rc2\lib

@echo off

SET PATH=%JAVA_HOME%\bin
SET PATH=%PATH%;C:\WINDOWS
SET PATH=%PATH%;C:\dev\tools\apache-maven-2.0.9\bin
SET PATH=%PATH%;C:\dev\tools\putty
SET PATH=%PATH%;C:\dev\tools\apache-ant-1.7.0\bin
SET PATH=%PATH%;C:\WINDOWS\system32
SET PATH=%PATH%;C:\dev\tools\bnd-0.0.238
SET PATH=%PATH%;C:\dev\tools\db-derby-10.3.2.1-bin\bin
SET PATH=%PATH%;C:\progra~1\Subversion\bin

echo on
echo PATH=%PATH%</pre>
]]></content:encoded>
			<wfw:commentRss>http://notehive.com/wp/2008/08/01/my-dev-directory-layout-and-environment-setup/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Osgi Hibernate Spring-DM Sample</title>
		<link>http://notehive.com/wp/2008/07/23/osgi-hibernate-spring-dm-sample/</link>
		<comments>http://notehive.com/wp/2008/07/23/osgi-hibernate-spring-dm-sample/#comments</comments>
		<pubDate>Wed, 23 Jul 2008 22:08:46 +0000</pubDate>
		<dc:creator>loedolff</dc:creator>
		
		<category><![CDATA[OSGi]]></category>

		<category><![CDATA[hibernate]]></category>

		<category><![CDATA[maven]]></category>

		<category><![CDATA[spring]]></category>

		<category><![CDATA[spring-dm]]></category>

		<guid isPermaLink="false">http://notehive.com/wp/?p=53</guid>
		<description><![CDATA[Introduction
I have developed a small sample to demonstrate how to use Hibernate in OSGi with Spring Framework and Spring-DM.  The sample works in either Felix or Equinox. It builds with Maven 2 - the build process automatically downloads the Felix and Equinox jars so there is no need to install either framework separately.
Starting Hibernate in [...]]]></description>
			<content:encoded><![CDATA[<h2>Introduction</h2>
<p>I have developed a small sample to demonstrate how to use Hibernate in OSGi with Spring Framework and Spring-DM.  The sample works in either Felix or Equinox. It builds with Maven 2 - the build process automatically downloads the Felix and Equinox jars so there is no need to install either framework separately.</p>
<p>Starting Hibernate in OSGi with the normal Hibernate SessionFactory would allow you to use Hibernate in OSGi - but that would not address the dynamic behavior that an OSGi system is capable of. This sample demonstrates how a Hibernate SessionFactory can be updated dynamically in an OSGi environment. You can dynamically add and remove entity classes to the Hibernate configuration by stopping and starting bundles that contribute clasess to the Hibernate configuration.</p>
<p>This implementation contains a small Swing UI which allows you to see which entity classes are present in the Hibernate configuration. The Swing UI also allows you to issue rudimentary SQL and HQL queries.</p>
<p>The solution is based on this <a rel="nofollow" href="http://www.osgi.org/blog/labels/osgi%20hibernate%20felix%20equinox%20eclipse%20knopflerfish%20sql.html">OSGi and Hibernate blog entry</a> by Peter Kriens. The design uses an entry in the manifest file to declaratively add classes to a Hibernate session using an <em>extender model</em>.</p>
<h2>Get the code</h2>
<p>To get the source code for the hibernate sample, use SVN:</p>
<p><code>svn checkout http://voluble.googlecode.com/svn/trunk/osgi-samples/hibernate/ osgi-hibernate-sample</code></p>
<h2>Run the code</h2>
<p>Run &#8220;mvn install&#8221; in the new osgi-hibernate-sample folder and see all projects build successfully.</p>
<p>The 2nd last project that builds is &#8220;integration-tests&#8221;, which uses Spring DM to run an integration test suite in an OSGi container. The integration tests are run against an in-process HSQLDB server.</p>
<p><span id="more-53"></span></p>
<p>The very last project that builds is &#8220;deployment&#8221;. This projects assembles all our project bundles and dependencies in the deployment/target/classes/ folder. It also adds a Equinox configuration file (configuration/config.ini) and Unix and Windows batch files, which allow you to run equinox and automatically load the full set of bundles from this project in a standalone environment.</p>
<p>When you start the standalone Equinox server (by running either equinox.cmd or equinox.sh in the osgi-samples/hibernate/deployment/target/classes folder), a Swing UI is displayed. Click on the &#8220;Show Hibernate Config&#8221; button to see the classes in the configuration.</p>
<p>When you start the standalone OSGi server (by running either felix.cmd, equinox.cmd, felix.sh or equinox.sh in the osgi-samples/hibernate/deployment/target/classes folder), a Swing UI is displayed. Click on the &#8220;Show Hibernate Config&#8221; button to see the classes in the configuration.</p>
<p><strong>In Equinox</strong>: Type &#8220;ss&#8221; in the Equinox command window. You&#8217;ll see that bundles 28 and 29 are &#8220;resolved&#8221; but not active. Start one or either by executing &#8220;start 28&#8243; and/or &#8220;start 29&#8243;.</p>
<p><strong>In Felix</strong>: Type &#8220;ps&#8221; in the Felix command window. You&#8217;ll see that bundles 1 and 2 are &#8220;installed&#8221; but not active. Start one or either by executing &#8220;start 1&#8243; and/or &#8220;start 2&#8243;.</p>
<p>Now go back to the Swing UI and click on &#8220;Show Hibernate Config&#8221; again to see which classes are present in the Hibernate configuration.</p>
<p>You can also view data by typing the HQL &#8220;<tt>from A1</tt>&#8221; and clicking &#8220;Execute HQL&#8221; (note that &#8220;<tt>from A1</tt>&#8221; is roughly the HQL equivalent of the SQL statement &#8220;<tt>select * from A1</tt>&#8220;).  You should see &#8220;<tt>Rows returned: 0</tt>&#8220;.</p>
<p>To add data to the database, enter the SQL statement &#8220;<tt>insert into A1 values (1, &#8216;value A&#8217;,'value B&#8217;)</tt>&#8221; and press the &#8220;Execute SQL&#8221; button. Then run the HQL query &#8220;from A1&#8243; again to see your data being retrieved using Hibernate. (You can also connect to the Database from a different tool (like DbVisualizer) using using the URL &#8220;jdbc:hsqldb:hsql://localhost/osgi-hibernate-sample&#8221;. The username should be &#8220;sa&#8221; and the password should be empty).</p>
<h2>Understanding the code</h2>
<p>With an OSGi application you can dynamically add, start, stop and remove bundles from the system. If these bundles contain entity classes which Hibernate should be able to persist, then the Hibernate configuration should be updated dynamically when the classes are added to or removed from the system.</p>
<p>Hibernate itself does not allow classes to be added to or removed from a SessionFactory instance (it needs to know about all entity classes when it is instantiated). Consequently we need to create a new SessionFactory whenever the list of entity classes changes in the Hibernate configuration.</p>
<p>This example demonstrates how OSGi bundles can dynamically add new classes to a Hibernate configuration. We start with a bundle that provides a Hibernate SessionFactory - other bundles can grab and use the SessionFactory to access a database. However, if a new bundle is installed and it contains new entity classes that are not yet in the SessionFactory configuration, then the SessionFactory must be recreated to update the configuration with the new classes.</p>
<p>If you always use a <a rel="nofollow" href="http://www.hibernate.org/hib_docs/v3/reference/en/html/tutorial.html#tutorial-firstapp-helpers">HibernateUtil</a> helper class to get a fresh Hibernate SessionFactory right before using it (i.e., if you never store a local copy of the SessionFactory), then it&#8217;s straight forward to ensure that you are always working with an up-to-date SessionFactory. However, if you use Spring Framework to wire up your dao&#8217;s (data access objects) then it becomes more complicated to ensure that you get a reference to a most up-to-date SessionFactory.</p>
<p>In our sample, we access the SessionFactory via a proxy (called DynamicSessionFactory) which ensures that a SessionFactory with the most up-to-date configuration is always used. As such, the dao beans must be configured with a reference the dynamic session factory, instead of the usual Hibernate SessionFactory.</p>
<h2>Module List</h2>
<p>Here is a list of maven projects and modules to implement the solution:</p>
<table border="0">
<tbody>
<tr>
<td style="border: 1px solid #aaaaaa; padding: 5px;"><strong>provision</strong></td>
<td style="border: 1px solid #aaaaaa; padding: 5px;">This project downloads resources into the maven repository.  The project does not generate a new artifact.</td>
</tr>
<tr>
<td style="border: 1px solid #aaaaaa; padding: 5px;"><strong>hibernate-classes</strong></td>
<td style="border: 1px solid #aaaaaa; padding: 5px;">Assembles Hibernate classes into a new OSGi bundle</td>
</tr>
<tr>
<td style="border: 1px solid #aaaaaa; padding: 5px;"><strong>jta</strong></td>
<td style="border: 1px solid #aaaaaa; padding: 5px;">Assembles javax.transaction into a new OSGi bundle</td>
</tr>
<tr>
<td style="border: 1px solid #aaaaaa; padding: 5px;"><strong>hsqldb</strong></td>
<td style="border: 1px solid #aaaaaa; padding: 5px;">Assembles the HSQLDB jars a new OSGi bundle</td>
</tr>
<tr>
<td style="border: 1px solid #aaaaaa; padding: 5px;"><strong>hibernate-session</strong></td>
<td style="border: 1px solid #aaaaaa; padding: 5px;">Provides a Hibernate SessionFactory that can be reconfigured dynamically during runtime - classes can be added to, or removed from the Hibernate configuration when bundles are started and stopped. This bundle exports a service, DynamicConfiguration, which other bundles can use to register their contributions to the SessionFactory. The bundle also exports a DynamicSessionFactory which should be used to create Hibernate sessions with the most up to date Hibernate configuration.</td>
</tr>
<tr>
<td style="border: 1px solid #aaaaaa; padding: 5px;"><strong>model-a</strong></td>
<td style="border: 1px solid #aaaaaa; padding: 5px;">This bundle contributes an additional class (called &#8220;A1&#8243;) to the Hibernate configuration. It also provides a DAO service (called &#8220;A1Dao&#8221;) for this class.</td>
</tr>
<tr>
<td style="border: 1px solid #aaaaaa; padding: 5px;"><strong>model-b</strong></td>
<td style="border: 1px solid #aaaaaa; padding: 5px;">This bundle contributes an additional class (called &#8220;B1&#8243;) to the Hibernate configuration. It also provides a DAO service (called &#8220;B1Dao&#8221;) for this class.</td>
</tr>
<tr>
<td style="border: 1px solid #aaaaaa; padding: 5px;"><strong>integration-tests</strong></td>
<td style="border: 1px solid #aaaaaa; padding: 5px;">This bundle tests if a dynamically updated Hibernate Session can be used to create, read, update and delete objects of classes A1 and B1 in the database.</td>
</tr>
<tr>
<td style="border: 1px solid #aaaaaa; padding: 5px;"><strong>deployment</strong></td>
<td style="border: 1px solid #aaaaaa; padding: 5px;">This project assembles all dependent bundles along with Windows and Unix batch files (felix.cmd, equinox.cmd, felix.sh and equinox.sh) and configuration files so you can very easily start all the bundles in Equinox either Felix</td>
</tr>
<tr>
<td style="border: 1px solid #aaaaaa; padding: 5px;"><strong>application</strong></td>
<td style="border: 1px solid #aaaaaa; padding: 5px;">This bundle launches a Swing UI which allows you to execute HQL (Hibernate Query Language) commands and view the entities that the Hibernate session is currently aware of.</td>
</tr>
</tbody>
</table>
]]></content:encoded>
			<wfw:commentRss>http://notehive.com/wp/2008/07/23/osgi-hibernate-spring-dm-sample/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Introduction to OSGi</title>
		<link>http://notehive.com/wp/2008/03/23/introduction-to-osgi/</link>
		<comments>http://notehive.com/wp/2008/03/23/introduction-to-osgi/#comments</comments>
		<pubDate>Sun, 23 Mar 2008 23:51:36 +0000</pubDate>
		<dc:creator>loedolff</dc:creator>
		
		<category><![CDATA[OSGi]]></category>

		<category><![CDATA[maven]]></category>

		<category><![CDATA[spring]]></category>

		<guid isPermaLink="false">http://notehive.com/wp/?p=42</guid>
		<description><![CDATA[This is a quick and practical task-based introduction to OSGi.  We avoid any in depth discussion about the purpose or benefits of OSGi or even the tools referenced in this article.  Many high level introductions on the technology are available; for examples, please see the webcasts at Eclipse.  There&#8217;s also a good [...]]]></description>
			<content:encoded><![CDATA[<p>This is a quick and practical task-based introduction to OSGi.  We avoid any in depth discussion about the purpose or benefits of OSGi or even the tools referenced in this article.  Many high level introductions on the technology are available; for examples, please see the webcasts at <a href="http://www.eclipse.org/equinox-portal/getstarted.php">Eclipse</a>.  There&#8217;s also a good webcast on the <a href="http://www.parleys.com/display/PARLEYS/OSGi,+the+good+the+bad+the+ugly">OSGi efforts at Weblogic</a>.</p>
<p>With OSGi it&#8217;s possible to run multiple versions of the same classes in the same container.  We&#8217;ll start from scratch and build services that demonstrate this ability.</p>
<p>I used Apache Felix as an OSGi container.  The other two well known open source OSGi container implementations are <a href="http://www.eclipse.org/equinox/">Equinox</a> and <a href="http://www.knopflerfish.org/">Knopflerfish</a>.  Apache Felix actually seems to be the least popular of the three - I wanted to get some experience using the underdog!</p>
<p>Our immediate focus is to create and deploy applications in an OSGi server.  We’ll do the following:</p>
<ul>
<li> First we’ll set up a simple OSGi deployment environment.</li>
<li>We’ll create a simple OSGi bundle that will export classes and a service, making it available to other bundles.  We’ll call this Bundle A, version 1.  We’ll install and start the bundle in an OSGi container.</li>
<li>To demonstrate the power of OSGi, we’ll create a second version of the same bundle and install it alongside the first version.  We’ll call this Bundle A, version 2.  This will demonstrate OSGi technology’s ability to seamlessly manage multiple versions of the same classes at the same time, even though the class and package names of both versions will be identical.</li>
<li>We’ll create another set of bundles which will import and use the classes and service exported by the first set of bundles.  We’ll call them Bundle B, versions 1 and 2.  Bundle A, versions 1 and 2 will export services which will be imported by Bundle B versions 1 and 2 respectively.   This will demonstrate OSGi technology’s ability to dynamically manage modularized applications.</li>
</ul>
<p>We will use <a href="http://www.springframework.org/osgi">Spring Dynamic Modules for OSGi(tm) 1.0</a> (with dependencies), <a href="http://felix.apache.org/site/index.html">Apache Felix 1.0.3</a>, <a href="http://felix.apache.org/site/apache-felix-maven-bundle-plugin-bnd.html">Apache Felix Maven Bundle Plugin 1.2.0</a>, <a href="http://maven.apache.org/">Apache Maven 2.0.8</a> and <a href="http://java.sun.com/javase/downloads/index_jdk5.jsp">Java 1.5.14</a>.  Please refer to the individual project websites for more information and background on these projects.  We are avoiding discussion of these tools as we are only focusing on accomplishing the tasks outlined above.</p>
<p>I used Windows XP to create this tutorial.  When writing the development and deployment steps it seems much easier and less error prone to reference full path names, rather than relative names.  For example, I prefer referencing c:\dev\tools\felix-1.0.3, rather than “your Felix home directory”.  You will notice I use the following directory structure for my development environment.</p>
<pre style="font-size: 10pt">   c:\dev\
                  lib\   (Libraries)
                              \spring-framework-2.5.1
                              \spring-osgi-1.0
                              …etc…
                  tools\   (Executable tools)
                              \felix-1.0.3
                              \maven-2.0.7
                              …etc…</pre>
<p>You can change the steps in the tutorial to match your environment, or copy the environment I lay out here.  If you use a different layout, simply substitute path references to match your environment.  For example, substitute “c:\dev\tools\felix-1.0.3” with whatever you chose for “your Felix home directory”.</p>
<p>To start out I’ll detail where to get the components to build the OSGi deployment environment.  I will then use a command file, “setenv.cmd”, to configure my environment variables and system path when operating from a Windows command prompt.  I’ll provide a sample “setenv.cmd” file.<br />
<span id="more-42"></span></p>
<h3>Resources and Environment Setup</h3>
<p>Download and install the following components from to the locations shown below.</p>
<table style="font-size: 10pt" border="0">
<tbody>
<tr>
<th>Component</th>
<th>Get File</th>
<th>Extract or Install too</th>
</tr>
<tr>
<td><a href="http://felix.apache.org/site/downloads.cgi" target="_blank">Felix 1.0.3</a></td>
<td>felix-1.0.3.zip</td>
<td>C:\dev\tools\</td>
</tr>
<tr>
<td><a href="http://maven.apache.org/download.html" target="_blank">Maven 2.0.8</a></td>
<td>apache-maven-2.0.8-bin.zip</td>
<td>C:\dev\tools</td>
</tr>
<tr>
<td><a href="http://sourceforge.net/project/showfiles.php?group_id=73357&amp;package_id=227224">Spring Dynamic Modules 1.0</a></td>
<td>spring-osgi-1.0-with-dependencies.zip</td>
<td>C:\dev\lib\</td>
</tr>
<tr>
<td><a href="http://java.sun.com/javase/downloads/index_jdk5.jsp">JDK 5.0 Update 14</a></td>
<td></td>
<td>Default location: C:\Program Files\Java\jdk1.5.0_14</td>
</tr>
</tbody>
</table>
<p>After downloading and installing the listed components, you should have the following folders on your machine.</p>
<pre style="font-size: 10pt">    C:\dev\tools\felix-1.0.3\
                                    bin\
                                    bundle\
                                    …etc…
    C:\dev\tools\apache-maven-2.0.8\
                                    bin\
                                    boot\
                                    …etc…
    C:\dev\lib\spring-osgi-1.0\
                                    dist\
                                    docs\
                                    …etc…</pre>
<p>Create a file called “setenv.cmd” in c:\dev\.  The file should contain the following:</p>
<pre style="font-size: 10pt">  @echo off

  SET JAVA_HOME=c:\Progra~1\Java\jdk1.5.0_14

  SET PATH=%JAVA_HOME%\bin
  SET PATH=%PATH%;C:\dev\tools\apache-maven-2.0.8\bin
  SET PATH=%PATH%;C:\WINDOWS
  SET PATH=%PATH%;C:\WINDOWS\system32

  echo on
  @echo PATH=%PATH%</pre>
<p>The following several sections show in detail how to create and deploy OSGi bundles from scratch.</p>
<h2>Bundle A, version 1</h2>
<p>Open a command prompt and execute “c:\dev\setenv.cmd”.  In the same command prompt, create a directory c:\dev\osgi\ and change to this directory.</p>
<p>At the command prompt, create a new maven project using the spring bundle archetype.  The following command creates Bundle A, version 1.  It should be entered as a single command (on one line).  For your convenience, you can copy &amp; paste the tiny text below - it contains the same command, but on a single line with no line breaks.</p>
<pre style="font-size: 10pt">  c:\dev\osgi&gt;mvn archetype:create
    -DarchetypeGroupId=org.springframework.osgi
    -DarchetypeArtifactId=spring-osgi-bundle-archetype
    -DarchetypeVersion=1.0
    -DgroupId=com.example.osgi
    -DartifactId=bundle-a -Dversion=1.0.0</pre>
<pre style="font-size: 6pt">mvn archetype:create -DarchetypeGroupId=org.springframework.osgi -DarchetypeArtifactId=spring-osgi-bundle-archetype  -DarchetypeVersion=1.0  -DgroupId=com.example.osgi  -DartifactId=bundle-a -Dversion=1.0.0</pre>
<p>(Usage of the above command was gleaned from <a href="http://www.springframework.org/node/361">here</a>).</p>
<p>The above maven command creates a new folder, bundle-a, containing a new template project.  The group Id determines the package hierarchy.  We arbitrarily chose “com.example.osgi”.</p>
<p>To build the project, cd to bundle-a and run “mvn package”.  The maven-bundle-plugin in the pom.xml contains instructions for creating the OSGi bundle, bundle-a-1.0.0.jar, under the C:\dev\osgi\bundle-a\target folder.  The jar file contains the following files:</p>
<pre style="font-size: 10pt">   META-INF/MANIFEST.MF
   META-INF/maven/com.example.osgi/bundle-a/pom.properties
   META-INF/maven/com.example.osgi/bundle-a/pom.xml
   META-INF/spring/bundle-context-osgi.xml
   META-INF/spring/bundle-context.xml
   com/example/osgi/Bean.class
   com/example/osgi/impl/BeanImpl.class</pre>
<p>In addition to the class files from your project, the jar contains a META-INF/MANIFEST.MF file which is read by the OSGi server when we install the bundle.  The server reads entries such as Bundle-Name, Bundle-Version, Export-Package, etc. from the manifest file.  This gives the OSGi server information to resolve bundle dependencies, wire bundles together, etc.</p>
<p>The META-INF/spring/*.xml files are read by Spring OSGi support services.  In the next section we’ll see how to install and activate the Spring OSGi support in Apache Felix.  A Spring service monitors all new bundles loaded into the OSGi container looking for the META-INF/spring folder.  If this folder is found, then the xml files in the folder are used to initialize the new bundle’s Spring context.</p>
<p>The XML files also tell Spring how the new bundle wants to interact with other OSGi bundles.  For example, which OSGi services are referenced by the new bundle, which services in the bundle should be registered with the OSGi server, how the bundle should be notified of events, etc.</p>
<p>The jar also contains a META-INF/maven folder.  I don’t know why is included, or when it is used.  I presume it is packaged in the jar file in order to give detailed information on how it was created.</p>
<h3>Running Felix</h3>
<p>To run Felix, open a second command prompt, cd to c:\dev and execute “setenv.cmd”.  Then cd to tools\felix-1.0.3 and execute “java -jar bin\felix.jar” from the command prompt.</p>
<p>You will be prompted for a profile name.  Enter “test” and hit enter.  Felix creates a folder called “test” under &#8220;%HOME%\.felix\&#8221;.  All bundles installed into Felix while it is running under the “test” profile, will be copied to this folder.  You never need to manually manipulate this folder, although it is sometimes useful clean out the profile by deleting its folder.</p>
<p>At the Felix console prompt, type “ps” and hit enter.  Felix lists the bundles that were started by default:</p>
<pre style="font-size: 10pt">  ID   State         Level  Name
  [   0] [Active     ] [    0] System Bundle (1.0.3)
  [   1] [Active     ] [    1] Apache Felix Shell Service (1.0.0)
  [   2] [Active     ] [    1] Apache Felix Shell TUI (1.0.0)
  [   3] [Active     ] [    1] Apache Felix Bundle Repository (1.0.2)</pre>
<p>Looking in the “%HOME%\.felix\test” folder shows three corresponding folders (bundle0 through 3).</p>
<p>Type “ps -l” to list the bundles and the jar files that they were initially loaded from.  Type “help” to see a list of all commands.</p>
<p>Note that bundles 1 and 2 are Shell services.  They provide the Felix console interface you are using now.  If you stop either of these bundles, the Felix console will stop working – all other bundles will keep running, but you won’t be able to interact with them.  If you stop either of the shell services you can restart Felix to start them back up.</p>
<p>Felix knows which bundles to load at startup thanks to the C:\dev\tools\felix-1.0.3\conf\ config.properties file.  Let’s edit this file and modify it to load the Spring services every time that Felix is started.</p>
<p>Look for the line starting with “felix.auto.start.1=”.  This line is followed by three lines that start with file:bundle.  At the end of the 3rd line, append a backslash and then add following lines below it:</p>
<pre style="font-size: 10pt">file:${SPRING_OSGI}/lib/aopalliance.osgi-1.0-SNAPSHOT.jar \
file:${SPRING_OSGI}/lib/asm.osgi-2.2.3-SNAPSHOT.jar \
file:${SPRING_OSGI}/lib/backport-util-concurrent.osgi-3.1-SNAPSHOT.jar \
file:${SPRING_OSGI}/lib/framework-2.0.3.jar \
file:${SPRING_OSGI}/lib/jcl104-over-slf4j-1.4.3.jar \
file:${SPRING_OSGI}/lib/junit.osgi-3.8.2-SNAPSHOT.jar \
file:${SPRING_OSGI}/lib/log4j.osgi-1.2.15-SNAPSHOT.jar \
file:${SPRING_OSGI}/lib/org.apache.felix.main-1.0.1.jar \
file:${SPRING_OSGI}/lib/slf4j-api-1.4.3.jar \
file:${SPRING_OSGI}/lib/slf4j-log4j12-1.4.3.jar \
file:${SPRING_OSGI}/lib/spring-aop-2.5.1.jar \
file:${SPRING_OSGI}/lib/spring-beans-2.5.1.jar \
file:${SPRING_OSGI}/lib/spring-context-2.5.1.jar \
file:${SPRING_OSGI}/lib/spring-core-2.5.1.jar \
file:${SPRING_OSGI}/lib/spring-test-2.5.1.jar \
file:${SPRING_OSGI}/dist/spring-osgi-core-1.0.jar \
file:${SPRING_OSGI}/dist/spring-osgi-extender-1.0.jar \
file:${SPRING_OSGI}/dist/spring-osgi-io-1.0.jar \
file:${SPRING_OSGI}/dist/spring-osgi-mock-1.0.jar \
file:${SPRING_OSGI}/dist/spring-osgi-test-1.0.jar</pre>
<p>(I gleaned the above by combining what I learned from watching Pax Runner - Screencast - Spring OSGi with the Apache Felix configuration documentation.  It is also listed in Section 4.2 of the Spring Dynamic Modules reference guide - spring-dm-reference.pdf.</p>
<p>Some of the jars above may not actually be required for everything in this tutorial to work).While you are editing the config.properties file, replace the line “#felix.cache.profile=foo” with “felix.cache.profile=test” - be sure to remove the # at the start of the line.  This will avoid the need to type the profile name “test” every time that you restart Apache Felix.</p>
<p>Save and close the config.properties file.  We now need to define the SPRING_OSGI system variable referenced in the properties file.  Create a new file called system.properties in the C:\dev\tools\felix-1.0.3\conf folder.  Place this line in system.properties:</p>
<pre style="font-size: 10pt">SPRING_OSGI=C:/dev/lib/spring-osgi-1.0</pre>
<p>Now restart Apache Felix - in the Felix console, type “shutdown” and then type “java -jar bin\felix.jar” at the Windows command prompt.</p>
<p>When you restart Felix it should show a very long list of bundles wired ether.  Typing “ps” should show that 23 bundles are now installed and “Active”.  Amidst the startup message will also see a log4j warning:</p>
<pre style="font-size: 9pt">log4j:WARN No appenders could be found for logger (org.springframework.util.ClassUtils).
log4j:WARN Please initialize the log4j system properly.</pre>
<p>We will deal with that in a moment as it is important to get logging working so we can see any Spring related configuration errors.</p>
<p>First though, we will install Bundle A, Version 1.  In the Felix console, type:</p>
<pre style="font-size: 10pt">-&gt; install file:C:\dev\osgi\bundle-a\target\bundle-a-1.0.0.jar</pre>
<p>You should see a response:</p>
<pre style="font-size: 10pt">Bundle ID: 24</pre>
<p>You can type “headers 24” to see all the information that Felix read from the bundle’s Manifest file.</p>
<p>Running “ps” will show that bundle 24 is “Installed”. Start the bundle by running “start 24”.  The bundle starts up OK, but unfortunately a long list of warnings of the format:</p>
<pre style="font-size: 10pt">WARNING: *** Class 'org.springframework.aop.TargetSource' was not found because bundle 24 does not import 'org.springframework.aop' even though bundle 14 does export it. To resolve this issue, add an import for 'org.springframework.aop' to bundle 24.</pre>
<p>We’ll deal with these in a moment too.</p>
<p>Note that Felix has copied the bundle into the profile directory (under &#8220;%HOME%\.felix\test”).  If you shut down Felix and start it up again it will restart all the previously imported bundles, including Bundle A, version 1.</p>
<p>­</p>
<h2>Housekeeping</h2>
<p>In the following couple of sections we’ll deal with the warning messages we saw when running Felix.</p>
<h3>Configuring Log4J</h3>
<p>To remove the log4j warning messages (“log4j:WARN No appenders could be found for logger (org.springframework.util.ClassUtils) and “log4j:WARN Please initialize the log4j system properly.”), we need to create a log4j configuration file.</p>
<p>Create a file called “log4j.xml” in the C:\dev\tools\felix-1.0.3 folder.  Here’s a basic log configuration which you can use in this file:</p>
<pre style="font-size: 10pt">&lt;?xml version="1.0" encoding="UTF-8" ?&gt;
&lt;!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"&gt;
&lt;log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"&gt;
  &lt;appender name="console" class="org.apache.log4j.ConsoleAppender"&gt;
    &lt;param name="Target" value="System.out" /&gt;
    &lt;layout class="org.apache.log4j.PatternLayout"&gt;
      &lt;param name="ConversionPattern"
            value="%d{ISO8601}; %-5p [%t] %c{1}: %m%n&#8221; /&gt;
    &lt;/layout&gt;
  &lt;/appender&gt;
  &lt;root&gt;
    &lt;priority value=&#8221;info&#8221; /&gt;
    &lt;appender-ref ref=&#8221;console&#8221; /&gt;
  &lt;/root&gt;
&lt;/log4j:configuration&gt;</pre>
<p>When we restart Felix we must define the system variable “log4j.configuration” to tell log4j where to find its configuration file.  To do this, Start Felix with these options:</p>
<pre style="font-size: 10pt">java -Dlog4j.configuration=file:/dev/tools/felix-1.0.3/log4j.xml -jar bin\felix.jar</pre>
<p>Since that’s a lot to type every time, create a cmd file called “felix.cmd” in the “C:\dev\tools\felix-1.0.3” folder that contains the above line.</p>
<p>Restart Felix using the “felix.cmd” script. You should see the log4j warnings replaced with a properly formatted log4j message:</p>
<pre style="font-size: 10pt">2008-02-13 09:44:00,618; INFO  [FelixStartLevel] ContextLoaderListener: Starting org.springframework.osgi.extender bundle v.[1.0.0]</pre>
<p>Among many other messages, you will now also see messages from Spring as it initializes bundle A.</p>
<pre style="font-size: 10pt">2008-02-13 09:52:34,254; INFO  [SpringOsgiExtenderThread-9] XmlBeanDefinitionReader: Loading XML bean definitions from URL [bundle://24.0:0/META-INF/spring/bundle-context-osgi.xml]
2008-02-13 09:52:34,301; INFO  [SpringOsgiExtenderThread-9] XmlBeanDefinitionReader: Loading XML bean definitions from URL [bundle://24.0:0/META-INF/spring/bundle-context.xml]</pre>
<p>And further down:</p>
<pre style="font-size: 10pt">2008-02-14 09:55:54,162; INFO  [SpringOsgiExtenderThread-4] DefaultListableBeanFactory: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@8ab708: defining beans [myBean]; root of factory hierarchy
2008-02-14 09:55:54,162; INFO  [SpringOsgiExtenderThread-4] OsgiBundleXmlApplicationContext: Publishing application context with properties (org.springframework.context.service.name=com.example.osgi.bundle-a)</pre>
<h3>Define Bundle Imports</h3>
<p>To get rid of the bundle import warnings that have the form “WARNING: *** Class ‘&lt;classname&gt;&#8217; was not found because bundle 24 does not import &lt;’package name’&gt; even though bundle 15 does export it” we will make a small configuration change to bundle A and redeploy it.</p>
<p>The warnings are caused by a missing “Import-Package” specification in the bundle’s manifest file, which should list all the packages used by the bundle.  We are using the Apache Felix maven-bundle-plugin to generate the bundle and the manifest file entries.</p>
<p>Open C:\dev\osgi\bundle-a\pom.xml and find this line:</p>
<pre style="font-size: 10pt">&lt;Import-Package&gt;*&lt;/Import-Package&gt;</pre>
<p>This line is part of the maven-bundle-plugin configuration.  According to the plugin documentation, this should suffice to generate the Import-Package entry for all packages referred to in the project.  However, my understanding is, that the Spring extender bundle injects classes into our bundle at runtime (please help me out if you know that my understanding is skewed!).  During the packaging phase, the maven plugin does know that a dependency on Spring will be created at runtime and therefore cannot generate the correct import package list. We will manually specify the package list.</p>
<p>Replace the &lt;Import-Package&gt;*&lt;/Import-Package&gt; line with the full list of packages imported by our bundle:</p>
<pre style="font-size: 10pt">  &lt;Import-Package&gt;
    *,
    org.xml.sax,
    org.osgi.framework,
    org.springframework.aop,
    org.springframework.aop.framework,
    org.springframework.osgi.service.importer.support,
    org.springframework.beans.factory.xml,
    org.springframework.beans.propertyeditors,
  &lt;/Import-Package&gt;</pre>
<p>In the Windows command prompt, rebuild the bundle by changing to the “C:\dev\osgi\bundle-a” directory and executing “mvn packge” again (btw, you should keep two command prompts open – one to run the Felix console, and one to execute the maven commands.  I’ll refer to them as “the Felix console” and “the Windows command prompt).</p>
<p>Because of the new imports we added, you’ll see this warning during compilation:</p>
<pre style="font-size: 10pt">[WARNING] Warning building bundle com.example.osgi:bundle-a:bundle:1.0.0 : Importing packages that are never refered to by any class on the Bundle-Classpath[Jar:dot]: [org.osgi.framework, org.springframework.aop, org.springframework.aop.framework, org.springframework.beans.factory.xml, org.springframework.beans.propertyeditors, org.springframework.osgi.service.importer.support, org.xml.sax]</pre>
<p>We are faced with a tradeoff between getting some warnings during runtime and versus packaging time.  I prefer these package time errors over the barrage of runtime errors.</p>
<p>In the Apache Felix console, type “update 24” to reload Bundle A from its original URL (file:C:\dev\osgi\bundle-a\target\bundle-a-1.0.0.jar).</p>
<p>The bundle should reload, displaying several informational messages (mostly generated by Spring) but no warning messages should be displayed.</p>
<h2>Do Something!</h2>
<p>We now have a bundle activated in Apache Felix, but we still need to make the bundle do something.</p>
<p>We’ll keep things simple.  We’ll have the bundle (1) log a message to a logger and (2) expose an OSGi service.  Later we’ll create Bundle B, which will consume this service.</p>
<p>Let’s add a method to the com.example.osgi.Bean interface (in C:\dev\osgi\bundle-a\src\main\java\com\example\osgi\Bean.java):</p>
<pre style="font-size: 10pt">  String getName();</pre>
<p>Add the implementation to com.example.osgi.imp.BeanImpl (in C:\dev\osgi\bundle-a\src\main\java\com\example\osgi\impl\BeanImpl.java).</p>
<pre style="font-size: 10pt">    public String getName() {
        return "Bundle A, version 1";
    }</pre>
<p>Also add the following import to BeanImpl:</p>
<pre style="font-size: 10pt">    import java.util.Map;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;</pre>
<p>Slf4j is a logging façade which appears to be better suited for an OSGi environment.  Spring OSGi depends on Slf4j and the dependencies were automatically added to pom.xml when it was created.</p>
<p>Add the static logger variable at the top of the class:</p>
<pre style="font-size: 10pt">    private Logger logger = LoggerFactory.getLogger(BeanImpl.class);</pre>
<p>Add service registration listeners to the class:</p>
<pre style="font-size: 10pt">    public void serviceRegistered(BeanImpl serviceInstance,
                                            Map serviceProperties) {
        logger.info("serviceRegistered called in [" + serviceInstance.getName() + "]&#8220;);
    }

    public void serviceUnregistered(BeanImpl serviceInstance,
                                              Map serviceProperties) {
        logger.info(&#8221;serviceUnregistered called in [" + serviceInstance.getName() + "]&#8220;);
    }</pre>
<p>The entire class should look like this:</p>
<pre style="font-size: 8pt">package com.example.osgi.impl;

import com.example.osgi.Bean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Map;

public class BeanImpl implements Bean {

    private Logger logger = LoggerFactory.getLogger(BeanImpl.class);

    public boolean isABean() {
        return true;
    }

    public String getName() {
        return "Bundle A, version 1";
    }

    public void serviceRegistered(BeanImpl serviceInstance,
                                            Map serviceProperties) {
        logger.info("serviceRegistered called in [" + serviceInstance.getName() + "]&#8220;);
   }

    public void serviceUnregistered(BeanImpl serviceInstance,
                                              Map serviceProperties) {
        logger.info(&#8221;serviceUnregistered called in [" + serviceInstance.getName() + "]&#8220;);
    }
}</pre>
<p>We have to instruct Spring to register the BeanImpl and Bean as a service, and to call the serviceRegistered and serviceUnregistered methods.</p>
<p>Edit “C:\dev\osgi\bundle-a\src\main\resources\META-INF\spring\ bundle-context-osgi.xml” and add the following before the closing tag (&lt;/beans&gt;):</p>
<pre style="font-size: 10pt">    &lt;osgi:service id="myBeanInBundleA" ref="myBean"
              interface="com.example.osgi.Bean"&gt;
    &lt;osgi:registration-listener ref="myBean"
        registration-method="serviceRegistered"
        unregistration-method="serviceUnregistered" /&gt;
    &lt;/osgi:service&gt;</pre>
<p>This tells Spring to register the bean “myBean” as an OSGi service.  It also tells Spring to call certain methods when the service is registered or unregistered.  Note that “myBean” was already defined as a bean in the bundle-context.xml file.<br />
Rebuild the bundle by running “mvn package” in the Windows command prompt.  In the Felix console, enter “update 24” to load a new copy of Bundle A.  Among the log message you should see the line:</p>
<pre style="font-size: 10pt">2008-02-13 12:15:57,445; INFO  [SpringOsgiExtenderThread-50] BeanImpl:
serviceRegistered called in [Bundle A, version 1]</pre>
<p>Note with dismay though that more message of the form “WARNING: *** Class ‘&lt;classname&gt;&#8217; was not found…” is again being printed.  This time the warnings refer to “–BeanInfo” classes that do not exist.  I do not know how to work around these log messages and I have to ignore them.</p>
<p>Type “services 24” to show the services exported by the bundle.  The bundle exports two services – the Spring context created by the Spring extender bundle, and the new com.examlpe.osgi.Bean instance.</p>
<p>Let’s change the bundle name (Spring OSGi Bundle) to something more descriptive.  In the project pom.xml file (C:\dev\osgi\bundle-a\pom.xml), replace “&lt;name&gt;Spring OSGi Bundle&lt;/name&gt;” with ““&lt;name&gt;Bundle A&lt;/name&gt;”.</p>
<p>Since we know we are going to run multiple versions of Bundle A’s classes in Apache Felix, let’s make sure the different versions are well differentiated.  In the maven-bundle-plugin tag in the pom.xml, find the line containing “&lt;Export-Package&gt;com.example.osgi.*&lt;/Export-Package&gt;”.  Change it to “&lt;Export-Package&gt;com.example.osgi.*;version=1.0.0&lt;/Export-Package&gt;”.</p>
<p>We also prevent this bundle from importing its own packages from other packages (i.e., from other versions of itself).  Add the following line as the first line under &lt;Import-Package&gt; (just before the line containing just “*”):</p>
<pre style="font-size: 10pt">!com.example.osgi*,</pre>
<p>The exclamation mark instructs the maven-bundle-plugin to not import this package from other bundles in the OSGi container.  If you leave out this line, you’ll end up with Bundle A, version 1 importing packages from Bundle A, version 2.  This is undesirable for our exercise.</p>
<p>Run “mvn package” at the command line and “update 24” in the Felix console.  Type “services 24” again in Felix and note the new name for the bundle.</p>
<h3>Bundle A, version 2</h3>
<p>We’ll now create version 2 of Bundle A and deploy it alongside version 1.</p>
<p>Before creating version 2, install version 1 A into the maven repository by running “mvn install” at the command prompt in C:\dev\osgi\bundle-a\.  (We’ll reference Bundel A, version 1 in the maven repository later when we build Bundle B, version 1).</p>
<p>Now, in the C:\dev\osgi\bundle-a\pom.xml, change “&lt;version&gt;1.0.0&lt;/version&gt;” (right under &lt;name&gt;Bundle A&lt;/name&gt;) to “&lt;version&gt;2.0.0&lt;/version&gt;”.  Also change “&lt;Export-Package&gt;com.example.osgi.*;version=1.0.0&lt;/Export-Package&gt;” to “&lt;Export-Package&gt;com.example.osgi.*;version=2.0.0&lt;/Export-Package&gt;”.   Finally, edit com.example.osgi.impl.BeanImpl (in C:\dev\osgi\bundle-a\src\main\java\com\example\osgi\impl\BeanImpl.java) and change the getName method so it returns &#8220;Bundle A, version 2&#8243; (instead of “Bundle A, version 1”).</p>
<p>In the Windows command prompt, run “mvn package”.  Note that a new jar file is created in the target folder: “bundle-a-2.0.0.jar”.  At the Felix console, install the new bundle by running:</p>
<pre style="font-size: 10pt">install file:C:\dev\osgi\bundle-a\target\bundle-a-2.0.0.jar</pre>
<p>You should see the response:</p>
<pre style="font-size: 10pt">Bundle ID: 25</pre>
<p>Start the new bundle by running “start 25”.  You should now see the message: “2008-02-14 10:14:20,339; INFO  [SpringOsgiExtenderThread-10] BeanImpl: serviceRegistered called in [Bundle A, version 2]”.</p>
<p>You can stop and start bundles 24 and 25 by running “stop 24 25” and “start 24 25” in the Felix console.  You should see the massages from both versions 1 and 2.  Also run “services 24 25” to see versions 1 and 2 of the com.example.osgi.Bean.</p>
<h3>Bundle B, version 1</h3>
<p>Here are the steps for creating a new bundle which will import the service exported by Bundle A.</p>
<p>First, run “mvn install” in the “C:\dev\spikes\spring-osgi\bundle-a” directory to install the latest version of Bundle A in the maven repository.</p>
<p>At the windows command prompt, change to the “C:\dev\osgi” directory and enter the following on a single line (you can cut ‘n paste the small text below the command):</p>
<pre style="font-size: 10pt">mvn archetype:create
-DarchetypeGroupId=org.springframework.osgi
-DarchetypeArtifactId=spring-osgi-bundle-archetype
-DarchetypeVersion=1.0
-DgroupId=com.example.bundleb
-DartifactId=bundle-b -Dversion=1.0.0</pre>
<pre style="font-size: 4pt">mvn archetype:create -DarchetypeGroupId=org.springframework.osgi -DarchetypeArtifactId=spring-osgi-bundle-archetype  -DarchetypeVersion=1.0  -DgroupId=com.example.bundleb -DartifactId=bundle-b -Dversion=1.0.0</pre>
<p>This creates the new project in “C:\dev\osgi\bundle-b”.</p>
<p>Edit the file “C:\dev\osgi\bundle-b\pom.xml” and change “&lt;name&gt;Spring OSGi Bundle&lt;/name&gt;” to “&lt;name&gt;Bundle B&lt;/name&gt;”.</p>
<p>In the pom.xml’s “&lt;dependencies&gt;” section (around line 16), add the following dependency on Bundle A:</p>
<pre style="font-size: 10pt">&lt;dependency&gt;
&lt;groupId&gt;com.example.osgi&lt;/groupId&gt;
&lt;artifactId&gt;bundle-a&lt;/artifactId&gt;
&lt;version&gt;1.0.0&lt;/version&gt;
&lt;/dependency&gt;</pre>
<p>In the maven-bundle-plugin, change the line “&lt;Export-Package&gt;com.example.bundleb*&lt;/Export-Package&gt;” to “&lt;Export-Package&gt; com.example.bundleb*;version=${project.version}&lt;/Export-Package&gt;”.</p>
<p>Replace “&lt;Import-Package&gt;*&lt;/Import-Package&gt;” with the following:</p>
<pre style="font-size: 10pt">&lt;Import-Package&gt;
!com.example.bundleb*,
com.example.osgi;version="[1,2)",
*,
org.xml.sax,
org.osgi.framework,
org.springframework.aop,
org.springframework.aop.framework,
org.springframework.osgi.service.importer.support,
org.springframework.beans.factory.xml,
org.springframework.beans.propertyeditors,
org.springframework.osgi.config,
org.springframework.osgi.service.exporter.support    &lt;/Import-Package&gt;</pre>
<p>The first line in the Import-Package tag - !com.example.bundleb* - similar to what we did in Bundle A.  It prevents Bundle B from importing its own packages from other versions of the same bundle.</p>
<p>The second Import-Package line - com.example.osgi;version="[1,2)" - imports the packages we’ll use from Bundle A.  Specifically, it ensures that we get packages from Bundle A that has at least version 1, but also no more than version 1.  Square brackets are inclusive, round brackets are exclusive.  Specifying a single version, for example “com.example.osgi;version=1.0.0”, would be synonymous to asking for any version &gt;= 1.0.0, in which case we might end up with a reference to version 2.0.0.  For this tutorial we want to explicitly avoid that.</p>
<p>In C:\dev\osgi\bundle-b\src\main\java\com\example\bundleb\, create a new class file BeanClient.java containing the following:</p>
<pre style="font-size: 8pt">package com.example.bundleb;

import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.example.osgi.Bean;

public class BeanClient {

    private Logger logger = LoggerFactory.getLogger(BeanClient.class);
    private Bean bean;

    // set the bean from bundle a
    public void setBean(Bean bean) {
        this.bean = bean;
    }

    public void clientInitialized() {
        logger.info("This is Bundle B, version 1. " +
                       "I have a reference to [" +
                       bean.getName() + "]");
        }
}</pre>
<p>Add the following before &lt;/beans&gt; in the file C:\dev\osgi\bundle-b\src\main\resources\META-INF\spring\bundle-context-osgi.xml:<br />
­<br />
&lt;osgi:reference id=&#8221;beanFromBundleA&#8221;<br />
interface=&#8221;com.example.osgi.Bean&#8221;/&gt;</p>
<p>The “osgi:reference” tag above tells the OSGi container (via Spring) that this bundle wants a reference to the bean exported by bundle A.</p>
<p>In C:\dev\osgi\bundle-b\src\main\resources\META-INF\spring\bundle-context.xml, add the following bean definition:</p>
<pre style="font-size: 10pt">    &lt;bean name="myBeanClient" class="com.example.bundleb.BeanClient"
              init-method="clientInitialized"&gt;
        &lt;property name="bean" ref="beanFromBundleA"/&gt;
    &lt;/bean&gt;</pre>
<p>The above line tells Spring where to wire up the reference to the bean from Bundle A.  We also define an init method so the bundle gets a chance to do something (like write a message to the log file).</p>
<p>The “beanFromBundle” will be provided by the OSGi container during runtime. However, we also need a reference to this been when running the unit tests.  We can accomplish this as follows:</p>
<p>Create a new file, C:\dev\osgi\bundle-b\src\test\resources\test-context.xml, with the following content:</p>
<pre style="font-size: 10pt">&lt;?xml version="1.0" encoding="UTF-8"?&gt;
    &lt;beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd"&gt;

    &lt;bean id="beanFromBundleA" class="com.example.osgi.impl.BeanImpl"/&gt;
    &lt;/beans&gt;</pre>
<p>Edit BeanIntegrationTest.java (in “C:\dev\osgi\bundle-b\src\test\java\com\example\bundleb\impl\ “) and change the line:return new String[] {&#8221;META-INF/spring/bundle-context.xml&#8221; };</p>
<p>to include the new test context as follows:</p>
<pre style="font-size: 10pt">return new String[] {&#8221;META-INF/spring/bundle-context.xml&#8221;,
&#8220;test-context.xml&#8221;};</pre>
<p>At the Windows command prompt, run “mvn package” to package bundle B.</p>
<p>In the Felix console, run:</p>
<pre style="font-size: 10pt">install file:C:\dev\osgi\bundle-b\target\bundle-b-1.0.0.jar</pre>
<p>You should see a response:</p>
<pre style="font-size: 10pt">  Bundle ID: 26</pre>
<p>Type “start 26”. Among the other log message you should see the message: “2008-02-14 11:02:38,353; INFO  [SpringOsgiExtenderThread-22] BeanClient: This is Bundle B, version 1.  I have a reference to [Bundle A, version 1]”.</p>
<p>Now let’s create Bundle B, version 2 with a reference to Bundle A, version 2.</p>
<p>Edit bundle B’s pom.xml and change the &lt;version&gt; at the top of the file from 1.0.0 to 2.0.0.  Change the dependency reference to bundle-a from version 1.0.0 to version 2.0.0.<br />
Note that “&lt;Export-Package&gt;com.example.osgi.client; version=${project.version}&lt;/Export-Package&gt;” will now export version 2.0.0.  Change the Import-Package line “com.example.osgi;version=&#8221;[1,2)",” to “com.example.osgi;version=2.0.0,".</p>
<p>Edit the BeanClient.java source file and change the log message from “This is Bundle B, version 1…” to “This is Bundle B, version 2…”.</p>
<p>Run “mvn package” at the command prompt.  In the Felix console, run:</p>
<p>install file:C:\dev\osgi\bundle-b\target\bundle-b-2.0.0.jar</p>
<p>You should see the response:</p>
<pre style="font-size: 10pt">Bundle ID: 27</pre>
<p>Type “start 27”.  Among the warnings and other message you should see: “2008-02-14 16:29:33,836; INFO  [SpringOsgiExtenderThread-26] BeanClient: This is Bundle B, version 2.  I have a reference to [Bundle A, version 2]”.</p>
<p>You can stop and start bundles 24 through 27 independently.  Service 27 (Bundle B, version 2) depends on bundle 25 (Bundle A, version 2) and bundle 26 depends on bundle 24.  Note that if you execute “stop 25 27” and then “start 27”, then the log message “This is Bundle B, version 2…” is not printed.  At least not until you also restart bundle 25.</p>
<p>If the dependant service is not started before a timeout expires, the context creation fails.  The default timeout is 300ms.  See the Spring documentation for more details on this.</p>
]]></content:encoded>
			<wfw:commentRss>http://notehive.com/wp/2008/03/23/introduction-to-osgi/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Back from China!</title>
		<link>http://notehive.com/wp/2007/11/26/back-from-china/</link>
		<comments>http://notehive.com/wp/2007/11/26/back-from-china/#comments</comments>
		<pubDate>Mon, 26 Nov 2007 18:35:28 +0000</pubDate>
		<dc:creator>loedolff</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://notehive.com/wp/?p=22</guid>
		<description><![CDATA[Shujie &#38; I returned home yesterday after an 11 day trip to the People&#8217;s Republic of China.  I uploaded my 400+ photos and Shujie&#8217;s 115 photos.
Here are some touristy photos of Shujie and me in the Palace Museum / Forbidden City:
  

I learned so much about China, Shujie, her family, and also a [...]]]></description>
			<content:encoded><![CDATA[<p>Shujie &amp; I returned home yesterday after an 11 day trip to the People&#8217;s Republic of China.  I uploaded <a href="http://notehive.com/gallery/v/China-2007/by-hans/">my 400+ photos</a> and <a href="http://notehive.com/gallery/v/China-2007/by-shujie/" target="_blank">Shujie&#8217;s 115 photos</a>.</p>
<p>Here are some touristy photos of Shujie and me in the Palace Museum / Forbidden City:</p>
<p><a href="http://notehive.com/wp/2007/11/26/back-from-china/attachment/27/" rel="attachment wp-att-27" title="img_4549.jpg"><img src="http://notehive.com/wp/wp-content/uploads/2007/11/img_4549.thumbnail.jpg" alt="img_4549.jpg" /></a> <a href="http://notehive.com/wp/2007/11/26/back-from-china/attachment/28/" rel="attachment wp-att-28" title="img_4548.jpg"><img src="http://notehive.com/wp/wp-content/uploads/2007/11/img_4548.thumbnail.jpg" alt="img_4548.jpg" /></a> <a href="http://notehive.com/wp/2007/11/26/back-from-china/attachment/26/" rel="attachment wp-att-26" title="img_4542.jpg"><img src="http://notehive.com/wp/wp-content/uploads/2007/11/img_4542.thumbnail.jpg" alt="img_4542.jpg" /><br />
</a></p>
<p>I learned so much about China, Shujie, her family, and also a little bit more about myself.  I learned that China&#8217;s incredibly vast and rich 5000 year old history is not only found in great walls, gates, pagodas, tombs, literature or artwork, but also on the table at every meal!</p>
<p>It&#8217;s hard to summarize my impressions of the country or the people, but I can say that I felt completely safe the whole time, everyone seems friendly beyond measure, proud of their heritage, proud of the current pace of development, excited about the future, determined to succeed, well spoken, void of criticism, eager to make visitors feel welcome and unselfishly willing to help out whenever they can.</p>
<p>On the down side, traffic in the cities and towns is a mess - your life hangs in the balance every time you cross the street.  Also, pollution is staggering, but as far as I can tell this will improve very rapidly over the next couple of years.</p>
]]></content:encoded>
			<wfw:commentRss>http://notehive.com/wp/2007/11/26/back-from-china/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Going to China!</title>
		<link>http://notehive.com/wp/2007/11/13/going-to-china/</link>
		<comments>http://notehive.com/wp/2007/11/13/going-to-china/#comments</comments>
		<pubDate>Tue, 13 Nov 2007 17:08:15 +0000</pubDate>
		<dc:creator>loedolff</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://notehive.com/wp/?p=12</guid>
		<description><![CDATA[I&#8217;m finally going to China, for the first time, tomorrow.  I have been dreaming about this for years!  I have seen several documentaries on life in China and have traveled a bit around the globe, so I think I have a pretty good idea what it will be like.  However, I still have [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m finally going to China, for the first time, tomorrow.  I have been dreaming about this for years!  I have seen several documentaries on life in China and have traveled a bit around the globe, so I think I have a pretty good idea what it will be like.  However, I still have a feeling that this experience will still touch and change me in unexpected ways.  The Twin Peaks quote comes to mind: &#8220;When you see me again, it won&#8217;t be me.&#8221;</p>
<p>My fiancée, Shujie, will be my tour guide.  She grew up in China in a small town, Fengqiu, in the Henan province.  We actually found the town on Google Earth!  It&#8217;s ferry cool, and a bit freaky.  Here&#8217;s the GPS coordinates of a circle in the middle of town, which Shujie says she walked past to school every day: 35.04009 N, 114.41635 E.</p>
<p>We&#8217;ll visit Beijing for a couple of days and then go to Fengqiu, where I will meet my future mother-in-law.  Shujie will have to interpret for us, as her mother doesn&#8217;t speak English and I speak neither Manderin nor their local dialect.  I&#8217;m hoping to take down and memorize some polite words though while we are there.</p>
]]></content:encoded>
			<wfw:commentRss>http://notehive.com/wp/2007/11/13/going-to-china/feed/</wfw:commentRss>
		</item>
		<item>
		<title>New Blog!</title>
		<link>http://notehive.com/wp/2007/11/13/new-blog/</link>
		<comments>http://notehive.com/wp/2007/11/13/new-blog/#comments</comments>
		<pubDate>Tue, 13 Nov 2007 05:29:02 +0000</pubDate>
		<dc:creator>loedolff</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://notehive.com/wp/?p=6</guid>
		<description><![CDATA[Today I went through the exercise of setting up a new blog.  I prefer to install the blog software myself at a domain that I own and this begs the question:  Which blog software should I use?
After a couple of web searches and skimming some results, I looked a little closer at b2evolution, [...]]]></description>
			<content:encoded><![CDATA[<p>Today I went through the exercise of setting up a new blog.  I prefer to install the blog software myself at a domain that I own and this begs the question:  Which blog software should I use?</p>
<p>After a couple of web searches and skimming some results, I looked a little closer at <a href="http://b2evolution.net/" target="_blank">b2evolution</a>, <a href="http://www.s9y.org/" target="_blank">Serendipity</a>, <a href="http://nucleuscms.org/" target="_blank">Nucleus</a>, <a href="http://tikiwiki.org" target="_blank">TikiWiki</a> (mostly the blogging module) and <a href="http://wordpress.org" target="_blank">WordPress</a>.</p>
<p>b2evolution looked promising, but there are little animated emoticons on the blog editor which I found rather distracting and silly.   I couldn&#8217;t figure out how to hide them, and promptly gave up.</p>
<p>I&#8217;ve tried Serendipity before, but never tried to do any customization.  The default theme was rather humdrum.  Also, on their feature list they reference WordPress integration, which really made me think I should look more closely at WordPress instead.</p>
<p>I turned away from Nucleus because the screenshots link on their websites took me into the Gallery application.  I like Gallery, but I really want a blog system that will make it easy for me to post photos inside the blogs and I reason that, if the Nucleus website doesn&#8217;t use the Nucleus software to display screenshots, then it&#8217;s probably a little too cumbersome to do.</p>
<p>I&#8217;ve played with TikiWiki in the past and it looks extremely powerful.  However, after installation, it took me several minutes just to figure out how to enable the blog module, so the whole system seemed a little too much like Linux in the 90&#8217;s&#8230;  it&#8217;s very powerful, you have full control, but you really need to put some effort into learning how to tame the beast.</p>
<p>So that left me with WordPress.  It&#8217;s very popular and extremely well supported, but I used it about 2 years ago and remember that inserting images into blogs were really hard.  So I played around for a while on the WordPress site and noticed an example of an image in the Wysiwyg editor.  Very encouraging!</p>
<p>I&#8217;m very impressed with the wide range of plug-ins for WordPress and the plethora of themes!  I settled on this very cool Paalam 1.1 theme by <a href="http://wpthemepark.com/" target="_blank">Sadish Bala</a> (&#8221;made free by <a href="http://www.webhostingbluebook.com/" target="_blank" title="Web Hosting Bluebook">Web Hosting Bluebook</a>&#8220;).  Uploading my own banner image is dead simple, and WordPress even has a built in image editor to help crop the image.  The default Wysiwyg editor I&#8217;m using right now (I believe it&#8217;s called <a href="http://tinymce.moxiecode.com/" target="_blank">TinyMCE</a>) is  also really cool.  I&#8217;ve gotten so used to editing Google docs documents - editing WikiText is really awkward by comparison.</p>
<p>The blog research and implementation took me several hours&#8230;  but I&#8217;m happy with the result.  Now, lets see how easy it is to add an image to this blog&#8230;</p>
<p style="text-align: center" align="center"><img src="http://notehive.com/wp/wp-content/uploads/2007/11/leaf.jpg" alt="Leaf" height="213" width="319" /></p>
<p style="text-align: left"> Wow! That was fantastic! There&#8217;s a file upload form right below the editor. After uploading, a button lets you insert a link to the uploaded image into the document. It couldn&#8217;t be simpler (well, without drag &amp; drop at least). I&#8217;d like to figure out how to make the text flow around the image (without having to edit the HTML and stilysheet), but this is a great start!  BTW, it appears that switching between &#8220;Code&#8221; and &#8220;Visual&#8221; input is also really seamless in the Wysiwyg editor.</p>
<p style="text-align: left">&nbsp;</p>
<p align="center">&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://notehive.com/wp/2007/11/13/new-blog/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>

