felix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Felix > Apache Felix Dependency Manager - Getting Started
Date Tue, 22 Dec 2009 15:33:00 GMT
<html>
<head>
    <base href="http://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/1519/1/1/_/styles/combined.css?spaceKey=FELIX&amp;forWysiwyg=true"
type="text/css">
    </head>
<body style="background-color: white" bgcolor="white">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
     <h2><a href="http://cwiki.apache.org/confluence/display/FELIX/Apache+Felix+Dependency+Manager+-+Getting+Started">Apache
Felix Dependency Manager - Getting Started</a></h2>
     <h4>Page <b>edited</b> by             <a href="http://cwiki.apache.org/confluence/display/~marrs">Marcel
Offermans</a>
    </h4>
     
          <br/>
     <div class="notificationGreySide">
         <h2><a name="ApacheFelixDependencyManager-GettingStarted-GettingStarted"></a>Getting
Started</h2>

<p>When developing an OSGi bundle that has dependencies and possibly registers services,
there are two classes in particular we need to implement:</p>
<ol>
	<li>The bundle activator which controls the life-cycle of the bundle.</li>
	<li>The actual service implementation, which can be a POJO.</li>
</ol>


<p>When using the dependency manager, your bundle activator is a subclass of DependencyActivatorBase.
It needs to implement two life cycle methods: init and destroy. Both methods take two arguments:
BundleContext and DependencyManager. The latter is your interface to the declarative API you
can use to define your services and dependencies.</p>

<p>The following paragraphs will show various examples that explain how to do this.
Subsequently, some more advanced scenarios will be covered that involve listening to dependency
and service state changes and interacting with the OSGi framework from within your service
implementation.</p>

<h3><a name="ApacheFelixDependencyManager-GettingStarted-Registeringaservice"></a>Registering
a service</h3>

<p>The first example is about registering a service. We extend DependencyActivatorBase
and in the init method we use the reference to the DependencyManager to create and add a service.
For this service we subsequently set its interface and implementation. In this case the interface
is the Store interface, the second parameter, null, allows you to provide properties along
with the service registration. For the implementation, we only mention the Class of the implementation,
which means the dependency manager will lazily instantiate it. In this case, there is not
much point in doing that because the service has no dependencies, but if it had, the instantiation
would only happen when those dependencies were resolved.The first example is about registering
a service. We extend DependencyActivatorBase and in the init method we use the reference to
the DependencyManager to create and add a service. For this service we subsequently set its
interface and implementation. In this case the interface is the Store interface, the second
parameter, null, allows you to provide properties along with the service registration. For
the implementation, we only mention the Class of the implementation, which means the dependency
manager will lazily instantiate it. In this case, there is not much point in doing that because
the service has no dependencies, but if it had, the instantiation would only happen when those
dependencies were resolved.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java"><span class="code-keyword">public</span> class Activator
<span class="code-keyword">extends</span> DependencyActivatorBase {
    <span class="code-keyword">public</span> void init(BundleContext context,
DependencyManager manager) <span class="code-keyword">throws</span> Exception
{
        manager.add(createService()
            .setInterface(Store.class.getName(), <span class="code-keyword">null</span>)
            .setImplementation(MemoryStore.class)
        );
    }
    
    <span class="code-keyword">public</span> void destroy(BundleContext context,
DependencyManager manager) <span class="code-keyword">throws</span> Exception
{}
}
</pre>
</div></div><div class="code panel" style="border-width: 1px;"><div class="codeContent
panelContent">
<pre class="code-java">
This is the service <span class="code-keyword">interface</span>. Nothing special
here.

</pre>
</div></div>

<p>This is the service interface. Nothing special here.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java"><span class="code-keyword">public</span> <span
class="code-keyword">interface</span> Store {
    <span class="code-keyword">public</span> void put(<span class="code-object">String</span>
key, <span class="code-object">Object</span> value);
    <span class="code-keyword">public</span> <span class="code-object">Object</span>
get(<span class="code-object">String</span> key);
}
</pre>
</div></div><div class="code panel" style="border-width: 1px;"><div class="codeContent
panelContent">
<pre class="code-java">
And <span class="code-keyword">finally</span> the implementation. Again, <span
class="code-keyword">this</span> is just a POJO, there is no reference here to any
OSGi or dependency manager specific class or annotation.

</pre>
</div></div>

<p>And finally the implementation. Again, this is just a POJO, there is no reference
here to any OSGi or dependency manager specific class or annotation.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java"><span class="code-keyword">public</span> class MemoryStore
<span class="code-keyword">implements</span> Store {
    <span class="code-keyword">private</span> Map m_map = <span class="code-keyword">new</span>
HashMap();

    <span class="code-keyword">public</span> <span class="code-object">Object</span>
get(<span class="code-object">String</span> key) {
        <span class="code-keyword">return</span> m_map.get(key);
    }

    <span class="code-keyword">public</span> void put(<span class="code-object">String</span>
key, <span class="code-object">Object</span> value) {
        m_map.put(key, value);
    }
}
</pre>
</div></div>

<h3><a name="ApacheFelixDependencyManager-GettingStarted-Dependingonaservice"></a>Depending
on a service</h3>

<h3><a name="ApacheFelixDependencyManager-GettingStarted-Trackingserviceswithcallbacks"></a>Tracking
services with callbacks</h3>

<h3><a name="ApacheFelixDependencyManager-GettingStarted-Dependingonaconfiguration"></a>Depending
on a configuration</h3>

<h3><a name="ApacheFelixDependencyManager-GettingStarted-..."></a>...</h3>
     </div>
     <div id="commentsSection" class="wiki-content pageSection">
       <div style="float: right;">
            <a href="http://cwiki.apache.org/confluence/users/viewnotifications.action"
class="grey">Change Notification Preferences</a>
       </div>

       <a href="http://cwiki.apache.org/confluence/display/FELIX/Apache+Felix+Dependency+Manager+-+Getting+Started">View
Online</a>
       |
       <a href="http://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=9374243&revisedVersion=5&originalVersion=4">View
Change</a>
              |
       <a href="http://cwiki.apache.org/confluence/display/FELIX/Apache+Felix+Dependency+Manager+-+Getting+Started?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message