<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

  <title><![CDATA[Tag: osgi | AlBlue&rsquo;s Blog]]></title>
  <link href="http://alblue.bandlem.com/Tag/osgi/atom.xml" rel="self"/>
  <link href="http://alblue.bandlem.com/"/>
  <updated>2012-02-17T22:24:12+00:00</updated>
  <id>http://alblue.bandlem.com/</id>
  <author>
    <name><![CDATA[Alex Blewitt]]></name>
    
  </author>
  <generator uri="http://octopress.org/">Octopress</generator>

  
  <entry>
    <title type="html"><![CDATA[Declarative Services vs Objects]]></title>
    <link href="http://alblue.bandlem.com/2011/11/declarative-services-vs-objects.html"/>
    <updated>2011-11-16T20:00:00+00:00</updated>
    <id>http://alblue.bandlem.com/2011/11/declarative-services-vs-objects</id>
    <content type="html"><![CDATA[<p>I'm coming to the conclusion there is an opportunity for improvement of the Declarative Services specification. Right now, DS needs to be able to instantiate a class and then mutate it as services come and go, instead of creating and disposing of classes on demand.</p>


<p>This poses somewhat of a problem if you want to publish objects in the OSGi service registry of types which you don't control. According to the Declarative Services specification, you need to have an object which has a default constructor. The object is then fully configured with a number of <code>bind</code> and <code>unbind</code> methods, which means the object goes through a series of states where it is not fully valid.</p>


<p>There's also the category of classes which aren't under your control (e.g. an existing API) or are acquired elsewhere (e.g. a third party library). These often can't be extended or otherwise mutated.</p>


<p><b>Example</b></p>


<p>Let's say we want to have a list of URLs stored as services in the registry. We'll be publishing <code>URL</code> objects as the service (and using <code>java.net.URL</code> as the interface type as well; it is a contrived example, after all). Now, the URL doesn't have a default constructor; every URL must have a url. So we can use it for registering a component which needs a service (e.g. <code>BookmarkService</code>) quite happily. But we can't <em>register</em> a URL with DS.</p>


<p>This presents us with a challenge. We might have a component which can register URL services, but given a 1..n <code>BookmarkService</code> and no URLs published, Declarative Services can't help us. In essence, there's no way to have a delayed URL service in DS.</p>


<p>What you can do is provide a <code>URLFactoryService</code> with DS. This can be activated on demand, and a <code>registerURLs()</code> method called which in turn registers a bunch of <code>URL</code> objects in the registry; and once that's done, DS can kick off the <code>BookmarkService</code>.</p>


<p>Unfortunately, there's no way of being able to tell DS that the <code>URLFactoryService</code> is something which is capable of generating <code>URL</code> objects, so DS never knows it needs to start the <code>URLFactoryService</code> to acquire the <code>URL</code>s.</p>


<p><b>Solution</b></p>


<p>The solutions are either to not use DS to populate the initial set of URLs (and require an <code>earlyStart</code> or <code>startlevel</code> hack to bring the initial bundle on-line) or to modify DS in a way which allows the implementation class to be a factory for the interface type <em>but without being a subtype of it</em>. The existing factory specification currently requires that the factory class is still an instance of the interface class; all that changes is the cardinality.</p>


<p>Here's what the solution might look like:</p>


<blockquote><pre><code>
&lt;scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="URLdemo"&gt;
   &lt;implementation class="URLService"/&gt;
   &lt;service&gt;
      &lt;provide interface="java.net.URL"/&gt;
   &lt;/service&gt;
&lt;/scr:component&gt;
</code></pre></blockquote>


<p>In this case, the <code>URLFactoryService</code> doesn't implement/extend <code>java.net.URL</code>. But this tells DS that when this component is activated, it gets a <code>URL</code> out of it, which is what we want DS to know.</p>


<p>We could use a new API, like <code>ServiceOnDemand</code>, which is a generified type that can provide us with a service when we need it:</p>


<blockquote><pre><code>
public interface ServiceOnDemand&lt;T&gt; {
  public T getService();
}
</code></pre></blockquote>


<p>If the <code>URLFactoryService</code> implemented the <code>ServiceOnDemand&lt;URL&gt;</code> interface, then a call to <code>getService</code> would return us with the URL object that could subsequently be registered on our behalf into the service registry. We can still use the <code>bind</code> and <code>unbind</code> calls as before; they just affect the <code>ServiceOnDemand</code> dataset. After each change of the properties (and providing the component was valid) the <code>getService()</code> could be treated as a factory for these object types, being called only when it is in a fully configured state.</p>


<p>For tear down, it becomes just as easy. Instead of removing the <code>URLFactoryService</code> from the registry, we can just remove its previously generated <code>URL</code> objects. The new DS would have to co-ordinate to know which services were registered and associated with which factory; but this shouldn't be significantly different to the way the factory works at the moment.</p>


<p>We could also use this in conjunction with the DS factory class. The key difference here is that the factory class <em>must</em> implement the interface provided, which isn't necessarily always possible.</p>


<p><b>Summary</b></p>


<p>DS, and in particular its lazy activation of components, is a great way of providing systems which evolve into being. Unfortunately, the current system is constrained to allowing only objects to be registered to which the user has full control. Although the above used <code>URL</code> as an example, the same could also be said for network-discovered services such as over LDAP or DNS service records, or even database connections (which typically can't be subclassed since they are binary only drivers). Finally, this could be the missing link between config admin and declarative services, which have never really played well together. Being able to use DS to fire up a factory, which consumes its configuration and then is able to generate multiple services would allow us all to avoid the pain and effort that is otherwise trying to control start ordering in order to provide services dynamically.</p>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[OSGi Community Event 2011 - OSGi, Past Present and Future Keynote]]></title>
    <link href="http://alblue.bandlem.com/2011/09/osgi-community-event-2011-osgi-past.html"/>
    <updated>2011-09-26T09:00:00+01:00</updated>
    <id>http://alblue.bandlem.com/2011/09/community-event-2011-osgi-past</id>
    <content type="html"><![CDATA[<p>As I mentioned
<a rel="nofollow" href="http://alblue.bandlem.com/2011/09/speaking-at-osgi-community-event.html">a couple of weeks ago</a>,
last week I gave a keynote at the OSGi Community
Event. During the event I unveiled
<a rel="nofollow" href="http://alblue.bandlem.com/2011/09/omega-problem.html">the Omega problem</a>,
which is the condition whereby projects are given inexplicable and unmemorable
Greek letters, which unnecessarily interferes with getting started with OSGi.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[The Omega Problem]]></title>
    <link href="http://alblue.bandlem.com/2011/09/omega-problem.html"/>
    <updated>2011-09-21T09:00:00+01:00</updated>
    <id>http://alblue.bandlem.com/2011/09/omega-problem</id>
    <content type="html"><![CDATA[<p>What is the <span alt="Omega">&Omega;</span> problem? It's a problem caused by the use of greek letters and astrological signs to refer to OSGi projects (or <em>&Omega;SGi projects</em>), thereby hiding the purpose of the project itself behind a name that is likely to be both unfamiliar and eminently forgettable.</p>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Speaking at OSGi Community Event]]></title>
    <link href="http://alblue.bandlem.com/2011/09/speaking-at-osgi-community-event.html"/>
    <updated>2011-09-12T09:43:00+01:00</updated>
    <id>http://alblue.bandlem.com/2011/09/speaking-at-osgi-community-event</id>
    <content type="html"><![CDATA[<p>I'll be giving a keynote at the <a href="http://www.osgi.org/CommunityEvent2011/HomePage">OSGi Community Event</a> in Darmstadt, Germany next week, entitled <a href="http://www.osgi.org/CommunityEvent2011/Speakers#anchor38">OSGi: Past, Present and Future</a>. In the talk, I'll do a retrospective on how we got where we are today, a view of what is happening in today's modular environment, and a peer into the future of the kind of challenges that OSGi will face in the future.</p>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[What's new in OSGi 4.3]]></title>
    <link href="http://alblue.bandlem.com/2011/03/what-new-in-osgi-43.html"/>
    <updated>2011-03-29T10:00:00+01:00</updated>
    <id>http://alblue.bandlem.com/2011/03/what-new-in-osgi-43</id>
    <content type="html"><![CDATA[<p>I've written up a piece on InfoQ about <a href="http://www.infoq.com/news/2011/03/osgi-43">what's new in OSGi 4.3</a>. Although the final specification is not yet available, the proposed <a href="http://www.osgi.org/download/osgi.core-4.3.0-pfd.pdf">public final draft</a> is available prior to the official release.</p>

]]></content>
  </entry>
  
</feed>

