<?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"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Developers, Developers, Developers! &#187; maven</title>
	<atom:link href="http://maksim.sorokin.dk/it/tag/maven/feed/" rel="self" type="application/rss+xml" />
	<link>http://maksim.sorokin.dk/it</link>
	<description>Maksim Sorokin IT Blog</description>
	<lastBuildDate>Sun, 05 Feb 2012 19:37:38 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.3</generator>
		<item>
		<title>Maven + Apache Felix + CXF + DOSGi: An Example of DOSGi Service</title>
		<link>http://maksim.sorokin.dk/it/2011/09/18/maven-apache-felix-cxf-dosgi-an-example-of-dosgi-service/</link>
		<comments>http://maksim.sorokin.dk/it/2011/09/18/maven-apache-felix-cxf-dosgi-an-example-of-dosgi-service/#comments</comments>
		<pubDate>Sun, 18 Sep 2011 22:04:29 +0000</pubDate>
		<dc:creator>Maksim Sorokin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Apache Felix]]></category>
		<category><![CDATA[DOSGi]]></category>
		<category><![CDATA[example]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[maven]]></category>
		<category><![CDATA[OSGi]]></category>
		<category><![CDATA[web services]]></category>

		<guid isPermaLink="false">http://maksim.sorokin.dk/it/?p=827</guid>
		<description><![CDATA[Link in series Link to two Felix instances Here is yet another post in Maven + Apache Felix + CXF + DOSGi series. Here I will show how to use Apache CXF DOSGi for cosuming remote services. You may see the sources on my GitHub account. We will have three projects dosgiRemote.service, dosgiRemote.service.api and dosgiRemote.consumer. [...]]]></description>
			<content:encoded><![CDATA[<p>Link in series</p>
<p>Link to two Felix instances</p>
<p>Here is yet another post in <a href="http://maksim.sorokin.dk/it/2011/07/19/maven-apache-felix-cxf-dosgi-series/">Maven + Apache Felix + CXF + DOSGi series</a>.</p>
<p>Here I will show how to use <a href="http://cxf.apache.org/dosgi-releases.html">Apache CXF DOSGi</a> for cosuming remote services. You may see the sources on <a href="https://github.com/mah01/examples/tree/master/dosgiRemote">my GitHub account</a>.</p>
<p>We will have <span id="more-827"></span> three projects <code>dosgiRemote.service</code>, <code>dosgiRemote.service.api</code> and <code>dosgiRemote.consumer</code>. <code>dosgiRemote.service.api</code> provides an interface for a service. <code>dosgiRemote.service</code> provides implementation for the service. <code>dosgiRemote.consume</code> will consume the service.<br />
<code>dosgiRemote.service.api</code> has to be shared between <code>dosgiRemote.service</code> and <code>dosgiRemote.consumer</code> as both has to know which service they are publishing and consuming. <code>dosgiRemote.service.api</code> and <code>dosgiRemote.service</code> will run in one OSGi container. <code>dosgiRemote.service.api</code> and <code>dosgiRemote.consumer</code> will run in another OSGi container. You may see how to do that in Eclipse in the <a href="http://maksim.sorokin.dk/it/2011/08/11/apache-felix-running-two-instances-of-felix-launcher-in-the-eclipse/">previous post</a>.</p>
<p><code>dosgiRemote.service.api</code> contains the following interface to a service:</p>
<pre class="brush: java; title: ;">
package dk.sorokin.maksim.dosgiRemote.service.api;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path(&quot;greeter&quot;)
public interface Greeter {

  @GET
  @Path(&quot;sayHello/{name}&quot;)
  @Produces(MediaType.TEXT_PLAIN)
  String sayHello(@PathParam(&quot;name&quot;) String name);
}
</pre>
<p><code>dosgiRemote.service</code> provides the following implementation:</p>
<pre class="brush: java; title: ;">
package dk.sorokin.maksim.dosgiRemote.internal.service;

import dk.sorokin.maksim.dosgiRemote.service.api.Greeter;

public class GreeterImpl implements Greeter {

  public String sayHello(String name) {
    return &quot;Hello, &quot; + name;
  }
}
</pre>
<p>Also, <code>dosgiRemote.service</code> registers a service in the following way:</p>
<pre class="brush: java; title: ;">
package dk.sorokin.maksim.dosgiRemote.internal;

import java.util.Dictionary;
import java.util.Hashtable;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;

import dk.sorokin.maksim.dosgiRemote.internal.service.GreeterImpl;
import dk.sorokin.maksim.dosgiRemote.service.api.Greeter;

public class Activator implements BundleActivator {

  public void start(BundleContext context) throws Exception {
    Dictionary&lt;String, String&gt; restProps = new Hashtable&lt;String, String&gt;();

    restProps.put(&quot;service.exported.interfaces&quot;, &quot;*&quot;);
    restProps.put(&quot;service.exported.configs&quot;, &quot;org.apache.cxf.rs&quot;);
    restProps.put(&quot;service.exported.intents&quot;, &quot;HTTP&quot;);
    restProps.put(&quot;org.apache.cxf.rs.address&quot;, &quot;http://localhost:8080/&quot;);
    context.registerService(Greeter.class.getName(), new GreeterImpl(), restProps);
  }

  public void stop(BundleContext context) throws Exception {
    //
  }
}
</pre>
<p><code>dosgiRemote.consumer</code> will have <code>OSGI-INF\remote-service\remote-services.xml</code>, according to <a href="http://cxf.apache.org/distributed-osgi-reference.html#DistributedOSGiReference-%7B%7Bremoteservices.xml%7D%7Dfiles">DOSGi documentation</a>:</p>
<pre class="brush: xml; title: ;">
&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;service-descriptions xmlns=&quot;http://www.osgi.org/xmlns/sd/v1.0.0&quot;&gt;
  &lt;service-description&gt;
    &lt;provide interface=&quot;dk.sorokin.maksim.dosgiRemote.service.api.Greeter&quot; /&gt;
    &lt;property name=&quot;service.exported.interfaces&quot;&gt;*&lt;/property&gt;
    &lt;property name=&quot;service.exported.configs&quot;&gt;org.apache.cxf.rs&lt;/property&gt;
    &lt;property name=&quot;service.exported.intents&quot;&gt;HTTP&lt;/property&gt;
    &lt;property name=&quot;org.apache.cxf.rs.address&quot;&gt;http://localhost:8080&lt;/property&gt;
  &lt;/service-description&gt;
&lt;/service-descriptions&gt;
</pre>
<p>And the Activator class for <code>dosgiRemote.consumer</code> will be:</p>
<pre class="brush: java; title: ;">
package dk.sorokin.maksim.dosgiRemote.consumer.internal;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.util.tracker.ServiceTracker;

import dk.sorokin.maksim.dosgiRemote.service.api.Greeter;

public class Activator implements BundleActivator {

  @Override
  public void start(BundleContext context) throws Exception {
    ServiceTracker serviceTracker = new ServiceTracker(context, Greeter.class.getName(), null) {
      @Override
      public Object addingService(ServiceReference reference) {
        Object result = super.addingService(reference);

        Object svc = context.getService(reference);
        if (svc instanceof Greeter) {
          final Greeter greeter = (Greeter) svc;
          System.out.println(greeter.sayHello(&quot;Max&quot;));
        }

        return result;
      }
    };
    serviceTracker.open();
  }

  public void stop(BundleContext context) throws Exception {
    //
  }
}
</pre>
<p>We will have two startup configuration for Apache Felix, as described above. The first one:</p>
<pre class="brush: plain; title: ;">
felix.auto.deploy.action=install,start
felix.log.level=1

org.osgi.framework.storage.clean=onFirstInit

felix.auto.start.1 = \
 assembly:/C:/projects/dosgiRemote/dosgiRemote.service.api/target/classes \
 assembly:/C:/projects/dosgiRemote/dosgiRemote.service/target/classes
</pre>
<p>And the second one:</p>
<pre class="brush: plain; title: ;">
felix.auto.deploy.action=install,start
felix.log.level=1

org.osgi.framework.storage.clean=onFirstInit

felix.auto.start.1 = \
 assembly:/C:/projects/dosgiRemote/dosgiRemote.service.api/target/classes \
 assembly:/C:/projects/dosgiRemote/dosgiRemote.service/target/classes
</pre>
<p>Launch the first configuration and then the second. You should see the following message in the console:</p>
<pre class="brush: plain; title: ;">
Hello, Max
</pre>
<p>You could also see the same message in the browser on <a href="http://localhost:8080/greeter/sayHello/Max">http://localhost:8080/greeter/sayHello/Max</a>.</p>
<p>You may pass objects as well. Refer to <a href="http://maksim.sorokin.dk/it/2011/07/24/maven-apache-felix-cxf-creating-a-restful-webservice-with-cxf-returning-and-object/">previous</a> <a href="http://maksim.sorokin.dk/it/2011/07/26/maven-apache-felix-cxf-creating-a-restful-webservice-with-cxf-consuming-an-object/">posts</a> to do that.</p>
<p>You can take the sources from may <a href="https://github.com/mah01/examples/tree/master/dosgiRemote">GitHub account</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://maksim.sorokin.dk/it/2011/09/18/maven-apache-felix-cxf-dosgi-an-example-of-dosgi-service/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Maven + Apache Felix: Strategy to Handle non-OSGi Dependencies</title>
		<link>http://maksim.sorokin.dk/it/2011/08/09/maven-apache-felix-strategy-to-handle-non-osgi-dependencies/</link>
		<comments>http://maksim.sorokin.dk/it/2011/08/09/maven-apache-felix-strategy-to-handle-non-osgi-dependencies/#comments</comments>
		<pubDate>Tue, 09 Aug 2011 16:06:47 +0000</pubDate>
		<dc:creator>Maksim Sorokin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Apache Felix]]></category>
		<category><![CDATA[dependency]]></category>
		<category><![CDATA[maven]]></category>
		<category><![CDATA[OSGi]]></category>

		<guid isPermaLink="false">http://maksim.sorokin.dk/it/?p=821</guid>
		<description><![CDATA[This is another post in Maven + Apache Felix + CXF + DOSGi series. Sometimes in the project plain Maven dependencies has to be used. A simple strategy to use those in OSGi project handled by Maven is to have a separate module, containing all non-OSGi dependencies and converting those to OSGi bundles using Apache [...]]]></description>
			<content:encoded><![CDATA[<p>This is another post in <a href="http://maksim.sorokin.dk/it/2011/07/19/maven-apache-felix-cxf-dosgi-series/">Maven + Apache Felix + CXF + DOSGi series</a>.</p>
<p>Sometimes in the project plain Maven dependencies has to be used. A simple strategy to use those in OSGi project handled by Maven is to have a separate module, containing all non-OSGi dependencies and converting those to OSGi bundles using <a href="http://felix.apache.org/site/apache-felix-maven-bundle-plugin-bnd.html">Apache Felix Maven Bundle Plugin</a>.<br />
So one may have the following structure of the project:</p>
<pre class="brush: plain; title: ;">
project
    ...
    project.nonOsgiDependencies
        project.nonOsgiDependencies.base64
        project.nonOsgiDependencies.mysqlConnector
        project.nonOsgiDependencies.xercesImpl
</pre>
<p>This <code>nonOsgiDependencies</code> projects would contain <span id="more-821"></span> just a <code>pom.xml</code> file, which would have a dependency on required maven dependency and would use <a href="http://felix.apache.org/site/apache-felix-maven-bundle-plugin-bnd.html">maven-bundle-plugin</a> to wrap it. Here is an example of such <code>pom.xml</code> for <code>net.iharder:base64</code></p>
<pre class="brush: xml; title: ;">
&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
  xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
  &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;

  &lt;parent&gt;
    &lt;artifactId&gt;project.nonOsgiDependencies&lt;/artifactId&gt;
    &lt;groupId&gt;dk.sorokin.maksim&lt;/groupId&gt;
    &lt;version&gt;1.0.0-SNAPSHOT&lt;/version&gt;
  &lt;/parent&gt;

  &lt;artifactId&gt;ecosystem.nonOsgiDependencies.base64&lt;/artifactId&gt;

  &lt;name&gt;net.iharder:base64 Maven Dependency&lt;/name&gt;

  &lt;build&gt;
    &lt;plugins&gt;
      &lt;plugin&gt;
        &lt;groupId&gt;org.apache.felix&lt;/groupId&gt;
        &lt;artifactId&gt;maven-bundle-plugin&lt;/artifactId&gt;
        &lt;executions&gt;
          &lt;execution&gt;
            &lt;id&gt;wrap-dependency&lt;/id&gt;
            &lt;goals&gt;
              &lt;goal&gt;bundle&lt;/goal&gt;
            &lt;/goals&gt;
            &lt;configuration&gt;
              &lt;instructions&gt;
                &lt;Embed-Dependency&gt;*&lt;/Embed-Dependency&gt;
                &lt;Export-Package&gt;net.iharder&lt;/Export-Package&gt;
              &lt;/instructions&gt;
            &lt;/configuration&gt;
          &lt;/execution&gt;
        &lt;/executions&gt;
      &lt;/plugin&gt;
    &lt;/plugins&gt;
  &lt;/build&gt;

  &lt;dependencies&gt;
    &lt;dependency&gt;
      &lt;groupId&gt;net.iharder&lt;/groupId&gt;
      &lt;artifactId&gt;base64&lt;/artifactId&gt;
      &lt;version&gt;2.3.8&lt;/version&gt;
    &lt;/dependency&gt;
  &lt;/dependencies&gt;
&lt;/project&gt;
</pre>
]]></content:encoded>
			<wfw:commentRss>http://maksim.sorokin.dk/it/2011/08/09/maven-apache-felix-strategy-to-handle-non-osgi-dependencies/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Maven + Apache Felix + CXF: Securing a Service with HTTP Basic Authentication</title>
		<link>http://maksim.sorokin.dk/it/2011/08/06/maven-apache-felix-cxf-securing-a-service-with-http-basic-authentication/</link>
		<comments>http://maksim.sorokin.dk/it/2011/08/06/maven-apache-felix-cxf-securing-a-service-with-http-basic-authentication/#comments</comments>
		<pubDate>Sat, 06 Aug 2011 08:35:32 +0000</pubDate>
		<dc:creator>Maksim Sorokin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[basic authentication]]></category>
		<category><![CDATA[CXF]]></category>
		<category><![CDATA[DOSGi]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[maven]]></category>
		<category><![CDATA[OSGi]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[web services]]></category>

		<guid isPermaLink="false">http://maksim.sorokin.dk/it/?p=808</guid>
		<description><![CDATA[This is another post in series Maven + Apache Felix + CXF + DOSGi Series. Here I will describe how to secure CXF published web services with HTTP basic authentication. You can find the sources on my GitHub account. We will have three projects here. The first one defines an interface for a service. Another [...]]]></description>
			<content:encoded><![CDATA[<p>This is another post in series <a href="http://maksim.sorokin.dk/it/2011/07/19/maven-apache-felix-cxf-dosgi-series/">Maven + Apache Felix + CXF + DOSGi Series</a>. Here I will describe how to secure CXF published web services with HTTP basic authentication. You can find the sources on <a href="https://github.com/mah01/examples/tree/master/dosgiSecurity">my GitHub account</a>.</p>
<p>We will have three projects here. The first one defines an interface for a service. Another one provides implementation for it. And the third one will provide security.</p>
<pre class="brush: plain; title: ;">
dosgiSecurity
    dosgiSecurity-api
    dosgiSecurity-impl
    dosgiSecurity-security
</pre>
<p><code>dosgiSecurity</code> will be just a holder project.</p>
<p>Our interface <code>HelloService</code> in bundle <code>dosgiSecurity-api</code> will be similar to the one we defined in<span id="more-808"></span> previous posts:</p>
<pre class="brush: java; title: ;">
package dk.sorokin.maksim.dosgiSecurity.api;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path(&quot;helloService&quot;)
public interface HelloService {

  @GET
  @Path(&quot;sayHello/{name}&quot;)
  @Produces(MediaType.TEXT_PLAIN)
  String sayHello(@PathParam(&quot;name&quot;) String name);
}
</pre>
<p>And implementation class <code>HelloServiceImpl</code> in bundle <code>dosgiSecurity-impl</code>:</p>
<pre class="brush: java; title: ;">
package dk.sorokin.maksim.dosgiSecurity.impl.internal.service;

import dk.sorokin.maksim.dosgiSecurity.api.HelloService;

public class HelloServiceImpl implements HelloService {

  public String sayHello(String name) {
    return &quot;Hello &quot; + name;
  }
}
</pre>
<p>And <code>SecurityFilter</code> in bundle <code>dosgiSecurity-security</code>, is just a servlet filter. It checks for <code>Authorization</code> header and verifies it:</p>
<pre class="brush: java; title: ;">
package dk.sorokin.maksim.dosgiSecurity.impl.internal.service;

import dk.sorokin.maksim.dosgiSecurity.api.HelloService;

public class HelloServiceImpl implements HelloService {

  public String sayHello(String name) {
    return &quot;Hello &quot; + name;
  }
}
</pre>
<p>The service is published in an <code>Activator</code> in <code>dosgiSecurity.impl</code>:</p>
<pre class="brush: java; title: ;">
package dk.sorokin.maksim.dosgiSecurity.impl.internal;

import java.util.Dictionary;
import java.util.Hashtable;

import javax.servlet.Filter;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;

import dk.sorokin.maksim.dosgiSecurity.api.HelloService;
import dk.sorokin.maksim.dosgiSecurity.impl.internal.service.HelloServiceImpl;
import dk.sorokin.maksim.dosgiSecurity.security.SecurityFilter;

public class Activator implements BundleActivator {

  public void start(BundleContext context) throws Exception {
    Dictionary&lt;String, String&gt; restProps = new Hashtable&lt;String, String&gt;();

    restProps.put(&quot;service.exported.interfaces&quot;, &quot;*&quot;);
    restProps.put(&quot;service.exported.configs&quot;, &quot;org.apache.cxf.rs&quot;);
    restProps.put(&quot;service.exported.intents&quot;, &quot;HTTP&quot;);
    restProps.put(&quot;org.apache.cxf.rs.httpservice.context&quot;, &quot;/secured&quot;);
    restProps.put(&quot;org.apache.cxf.rs.address&quot;, &quot;http://localhost:8080/&quot;);
    context.registerService(HelloService.class.getName(), new HelloServiceImpl(), restProps);

    Dictionary&lt;String, String&gt; filterProps = new Hashtable&lt;String, String&gt;();
    filterProps.put(&quot;org.apache.cxf.httpservice.filter&quot;, Boolean.TRUE.toString());
    filterProps.put(&quot;servletNames&quot;, &quot;none&quot;);
    context.registerService(Filter.class.getName(), new SecurityFilter(), filterProps);
  }

  public void stop(BundleContext context) throws Exception {
    //
  }
}
</pre>
<p>And a Felix Launcher (refer to the <a href="http://maksim.sorokin.dk/it/2011/07/19/maven-apache-felix-easy-development-and-debugging-with-eclipse/">first post in series</a>) is:</p>
<pre class="brush: plain; title: ;">
felix.auto.deploy.action=install,start
felix.log.level=1

org.osgi.framework.storage.clean=onFirstInit

felix.auto.start.1 = \
 assembly:/C:/projects/dosgiSecurity/dosgiSecurity.security/target/classes \
 assembly:/C:/projects/dosgiSecurity/dosgiSecurity.api/target/classes \
 assembly:/C:/projects/dosgiSecurity/dosgiSecurity.impl/target/classes
</pre>
<p>Now build all that with Maven using <code>mvn clean install</code> and run it (assuming that you have DOSGi Single Bundle Distribution in Felix, as we discussed in previous posts). When Felix is launched, go to <a href="http://localhost:8080/secured/helloService/sayHello/Max">http://localhost:8080/secured/helloService/sayHello/Max</a>. You should see:</p>
<pre class="brush: plain; title: ;">
HTTP ERROR: 403

FORBIDDEN
RequestURI=/secured/helloService/sayHello/Max

Powered by Jetty://
</pre>
<p>Now launch Fiddler2 (or similar program) and build custom request with Request Builder with the same url as shown above. And add the following line in "Request Headers":</p>
<pre class="brush: plain; title: ;">
Authorization: Basic U3V6eTpyb2NrZXQ=
</pre>
<p>You should get:</p>
<pre class="brush: plain; title: ;">
HTTP/1.1 200 OK
Content-Type: text/plain
Date: Tue, 02 Aug 2011 15:15:43 GMT
Content-Length: 9
Server: Jetty(6.1.x)

Hello max
</pre>
<p>Source files can be found <a href="https://github.com/mah01/examples/tree/master/dosgiSecurity">here</a></p>
]]></content:encoded>
			<wfw:commentRss>http://maksim.sorokin.dk/it/2011/08/06/maven-apache-felix-cxf-securing-a-service-with-http-basic-authentication/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Maven + Apache Felix + CXF: RESTful Webservice with CXF. Using POST.</title>
		<link>http://maksim.sorokin.dk/it/2011/08/02/maven-apache-felix-cxf-restful-webservice-with-cxf-using-post/</link>
		<comments>http://maksim.sorokin.dk/it/2011/08/02/maven-apache-felix-cxf-restful-webservice-with-cxf-using-post/#comments</comments>
		<pubDate>Tue, 02 Aug 2011 15:32:35 +0000</pubDate>
		<dc:creator>Maksim Sorokin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Apache Felix]]></category>
		<category><![CDATA[CXF]]></category>
		<category><![CDATA[DOSGi]]></category>
		<category><![CDATA[maven]]></category>
		<category><![CDATA[OSGi]]></category>
		<category><![CDATA[rest]]></category>
		<category><![CDATA[web services]]></category>

		<guid isPermaLink="false">http://maksim.sorokin.dk/it/?p=786</guid>
		<description><![CDATA[It is another post in Maven + Apache Felix + CXF + DOSGi Series. In this post I showed you how to create a RESTful webservice consuming and creating a String. However, the example was using @GET method. Here I will show, what changes need to be done in order to consume input from @POST. [...]]]></description>
			<content:encoded><![CDATA[<p>It is another post in <a href="http://maksim.sorokin.dk/it/2011/07/19/maven-apache-felix-cxf-dosgi-series/">Maven + Apache Felix + CXF + DOSGi Series</a>.</p>
<p>In <a href="http://maksim.sorokin.dk/it/2011/07/21/maven-apache-felix-cxf-creating-a-restful-webservice-with-cxf-a-simple-string-example/">this post</a> I showed you how to create a RESTful webservice consuming and creating a String. However, the example was using <code>@GET</code> method. Here I will show, what changes need to be done in order to consume input from <code>@POST</code>. As usual, sources are available in the end of the post.</p>
<p>Simply change <code>@GET</code> to <code>@POST</code> and remove <span id="more-786"></span> <code>@PathParam("name")</code> from <code>MyService</code> webservice interface:</p>
<pre class="brush: java; title: ;">
package test.bundle;

import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path(&quot;myService&quot;)
public interface MyService {

  @POST
  @Path(&quot;sayHello&quot;)
  @Produces(MediaType.TEXT_PLAIN)
  String sayHello(String name);
}
</pre>
<p>Then you can make POST requests to http://localhost:8080/myService/sayHello with request body. I suggest using <a href="http://www.fiddler2.com/fiddler2/">Fiddler2</a> tool for debugging.</p>
<p>If you want to consume object (well, XML and then convert it into object), you will need to create <a href="http://cxf.apache.org/docs/jax-rs-basics.html#JAX-RSBasics-CustomMessageBodyProviders">message body reader</a> and reference it in the Activator. An example of message body reader could be:</p>
<pre class="brush: java; title: ;">
package dk.codeunited.ecosystem.integration.myRecord.pub.api;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;

import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.MessageBodyReader;
import javax.ws.rs.ext.Provider;

import dk.codeunited.ecosystem.integration.myRecord.pub.api.pojos.Logbook;

@Provider
public class MyMessageReaderProvider implements MessageBodyReader&lt;MyMessage&gt; {

  @Override
  public boolean isReadable(Class&lt;?&gt; type, Type genericType, Annotation[] annotations, MediaType mt) {
    return true;
  }

  @Override
  public MyMessage readFrom(Class&lt;MyMessage&gt; clazz, Type t, Annotation[] a, MediaType mt,
      MultivaluedMap&lt;String, String&gt; headers, InputStream is) {
    // reader an object from InputStream
  }
}
</pre>
<p>And then registration in Activator:</p>
<pre class="brush: java; title: ;">
    restProps.put(&quot;org.apache.cxf.rs.provider&quot;, MyMessageReaderProvider.class.getName());
</pre>
<p><a href='http://maksim.sorokin.dk/it/wp-content/uploads/2011/07/felix_post.zip'>Here are source to POST example</a></p>
]]></content:encoded>
			<wfw:commentRss>http://maksim.sorokin.dk/it/2011/08/02/maven-apache-felix-cxf-restful-webservice-with-cxf-using-post/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Mule ESB: Building a Self-Contained Standalone Web App with Maven</title>
		<link>http://maksim.sorokin.dk/it/2011/07/29/mule-esb-building-a-self-contained-standalone-web-app-with-maven/</link>
		<comments>http://maksim.sorokin.dk/it/2011/07/29/mule-esb-building-a-self-contained-standalone-web-app-with-maven/#comments</comments>
		<pubDate>Fri, 29 Jul 2011 10:44:19 +0000</pubDate>
		<dc:creator>Maksim Sorokin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[esb]]></category>
		<category><![CDATA[example]]></category>
		<category><![CDATA[maven]]></category>
		<category><![CDATA[mule]]></category>
		<category><![CDATA[mule esb]]></category>

		<guid isPermaLink="false">http://maksim.sorokin.dk/it/?p=802</guid>
		<description><![CDATA[This example demonstrates how to build standalone self-contained Mule ESB web application, which can be deployed to a Java web container, e.g. Tomcat. The sources used are taken from Mule ESB "hello" example, which is distributed together with Mule. You can obtain the sources from this post on my github: https://github.com/mah01/examples/tree/master/muleHelloStandalone I will just show [...]]]></description>
			<content:encoded><![CDATA[<p>This example demonstrates how to build standalone self-contained Mule ESB web application, which can be deployed to a Java web container, e.g. Tomcat. The sources used are taken from Mule ESB "hello" example, which is distributed together with Mule.</p>
<p>You can obtain the sources from this post on my github:<br />
<a href="https://github.com/mah01/examples/tree/master/muleHelloStandalone">https://github.com/mah01/examples/tree/master/muleHelloStandalone</a></p>
<p>I will just show the keypoints of the application and changes, that were different from "hello" application in Mule distributable.</p>
<p>First of all, the <span id="more-802"></span> <code>pom.xml</code> file. We download all required Mule dependencies in order to run the example. Note, that we use <a href="http://dist.codehaus.org/mule/dependencies/maven2">Mule Maven repository</a>, because for some reason Mule artifacts has transitive dependencies on OSGified versions of certain libraries, which are not contained in <a href="http://repo1.maven.org/maven2/">Maven Central</a>:</p>
<pre class="brush: xml; title: ;">
&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
  xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
  &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;

  &lt;groupId&gt;dk.sorokin.maksim&lt;/groupId&gt;
  &lt;artifactId&gt;muleHelloStandalone&lt;/artifactId&gt;
  &lt;version&gt;1.0.0-SNAPSHOT&lt;/version&gt;
  &lt;packaging&gt;war&lt;/packaging&gt;

  &lt;name&gt;Self-Contained Standalone Mule Hello World&lt;/name&gt;
  &lt;description&gt;The sources for this application were copied from Mule distributable&lt;/description&gt;

  &lt;properties&gt;
    &lt;mule.version&gt;3.1.2&lt;/mule.version&gt;
  &lt;/properties&gt;

  &lt;build&gt;
    &lt;resources&gt;
      &lt;resource&gt;
        &lt;directory&gt;src/main/app&lt;/directory&gt;
      &lt;/resource&gt;
      &lt;resource&gt;
        &lt;directory&gt;src/main/resources&lt;/directory&gt;
      &lt;/resource&gt;
    &lt;/resources&gt;
  &lt;/build&gt;

  &lt;dependencies&gt;
    &lt;dependency&gt;
      &lt;groupId&gt;org.codehaus.groovy&lt;/groupId&gt;
      &lt;artifactId&gt;groovy-all&lt;/artifactId&gt;
      &lt;version&gt;1.8.1&lt;/version&gt;
    &lt;/dependency&gt;

    &lt;dependency&gt;
      &lt;groupId&gt;org.mule&lt;/groupId&gt;
      &lt;artifactId&gt;mule-core&lt;/artifactId&gt;
      &lt;version&gt;${mule.version}&lt;/version&gt;
    &lt;/dependency&gt;

    &lt;dependency&gt;
      &lt;groupId&gt;org.mule&lt;/groupId&gt;
      &lt;artifactId&gt;mule-core&lt;/artifactId&gt;
      &lt;version&gt;${mule.version}&lt;/version&gt;
      &lt;scope&gt;test&lt;/scope&gt;
      &lt;type&gt;test-jar&lt;/type&gt;
    &lt;/dependency&gt;

    &lt;dependency&gt;
      &lt;groupId&gt;org.mule.modules&lt;/groupId&gt;
      &lt;artifactId&gt;mule-module-builders&lt;/artifactId&gt;
      &lt;version&gt;${mule.version}&lt;/version&gt;
    &lt;/dependency&gt;

    &lt;dependency&gt;
      &lt;groupId&gt;org.mule.modules&lt;/groupId&gt;
      &lt;artifactId&gt;mule-module-scripting&lt;/artifactId&gt;
      &lt;version&gt;${mule.version}&lt;/version&gt;
    &lt;/dependency&gt;

    &lt;dependency&gt;
      &lt;groupId&gt;org.mule.transports&lt;/groupId&gt;
      &lt;artifactId&gt;mule-transport-vm&lt;/artifactId&gt;
      &lt;version&gt;${mule.version}&lt;/version&gt;
    &lt;/dependency&gt;

    &lt;dependency&gt;
      &lt;groupId&gt;org.mule.transports&lt;/groupId&gt;
      &lt;artifactId&gt;mule-transport-servlet&lt;/artifactId&gt;
      &lt;version&gt;${mule.version}&lt;/version&gt;
    &lt;/dependency&gt;

    &lt;dependency&gt;
      &lt;groupId&gt;org.mule.transports&lt;/groupId&gt;
      &lt;artifactId&gt;mule-transport-stdio&lt;/artifactId&gt;
      &lt;version&gt;${mule.version}&lt;/version&gt;
    &lt;/dependency&gt;
  &lt;/dependencies&gt;

  &lt;repositories&gt;
    &lt;repository&gt;
      &lt;id&gt;mule-deps&lt;/id&gt;
      &lt;name&gt;Mule Dependencies&lt;/name&gt;
      &lt;url&gt;http://dist.codehaus.org/mule/dependencies/maven2&lt;/url&gt;
    &lt;/repository&gt;
  &lt;/repositories&gt;
&lt;/project&gt;
</pre>
<p>And then we also add <code>log4j.properties</code> to <code>src/main/java</code>:</p>
<pre class="brush: plain; title: ;">
log4j.rootLogger=INFO, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %-5p %x - %m\n
</pre>
<p>And our <code>web.xml</code> looks like:</p>
<pre class="brush: xml; title: ;">
&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;web-app xmlns=&quot;http://java.sun.com/xml/ns/javaee&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
  xsi:schemaLocation=&quot;http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd&quot;
  version=&quot;3.0&quot;&gt;
  &lt;display-name&gt;mule.esb&lt;/display-name&gt;

  &lt;context-param&gt;
    &lt;param-name&gt;org.mule.config&lt;/param-name&gt;
    &lt;param-value&gt;mule-config.xml&lt;/param-value&gt;
  &lt;/context-param&gt;

  &lt;listener&gt;
    &lt;listener-class&gt;org.mule.config.builders.MuleXmlBuilderContextListener&lt;/listener-class&gt;
  &lt;/listener&gt;
&lt;/web-app&gt;
</pre>
<p>All other sources are copied directly from Mule "hello" example (the whole <code>src</code> folder).</p>
<p>Build the application with maven with <code>mvn clean install</code> and deploy the war to Tomcat (or other container).<br />
Then go to <a href="http://localhost:8888/?name=Max">http://localhost:8888/?name=Max</a>. You should see the following message:</p>
<pre class="brush: plain; title: ;">
Hello Max, how are you?
</pre>
]]></content:encoded>
			<wfw:commentRss>http://maksim.sorokin.dk/it/2011/07/29/mule-esb-building-a-self-contained-standalone-web-app-with-maven/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Maven + Apache Felix + CXF: Creating a RESTful Webservice with CXF. Consuming an Object</title>
		<link>http://maksim.sorokin.dk/it/2011/07/26/maven-apache-felix-cxf-creating-a-restful-webservice-with-cxf-consuming-an-object/</link>
		<comments>http://maksim.sorokin.dk/it/2011/07/26/maven-apache-felix-cxf-creating-a-restful-webservice-with-cxf-consuming-an-object/#comments</comments>
		<pubDate>Tue, 26 Jul 2011 21:11:20 +0000</pubDate>
		<dc:creator>Maksim Sorokin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Apache Felix]]></category>
		<category><![CDATA[CXF]]></category>
		<category><![CDATA[DOSGi]]></category>
		<category><![CDATA[maven]]></category>
		<category><![CDATA[OSGi]]></category>
		<category><![CDATA[web services]]></category>

		<guid isPermaLink="false">http://maksim.sorokin.dk/it/?p=775</guid>
		<description><![CDATA[This is the following post in Maven + Apache Felix + CXF + DOSGi series. Here we continue working with RESTful webservices. In the last post I showed how to returne an Object in XML with JAXB, and this post will based on it. In this post I will show how to consume an object. [...]]]></description>
			<content:encoded><![CDATA[<p>This is the following post in <a href="http://maksim.sorokin.dk/it/2011/07/19/maven-apache-felix-cxf-dosgi-series/">Maven + Apache Felix + CXF + DOSGi series</a>.</p>
<p>Here we continue working with RESTful webservices. In the <a href="http://maksim.sorokin.dk/it/2011/07/24/maven-apache-felix-cxf-creating-a-restful-webservice-with-cxf-returning-and-object/">last post I showed how to returne an Object in XML with JAXB</a>, and this post will based on it. In this post I will show how to consume an object. The title is rather misleading, but what I meant is "consuming XML and converting it to an object". Again, it is assumed that you followed the setup from the <a href="http://maksim.sorokin.dk/it/2011/07/19/maven-apache-felix-easy-development-and-debugging-with-eclipse/">first post in the series</a>. You will find attached sources in the end of the post.</p>
<p>We will have the same Felix Launcher project as in the previous post. To remind you, we will use <span id="more-775"></span> <a href="http://cxf.apache.org/dosgi-releases.html">CXF DOSGi single bundle distribution</a>, because it contains all the necessary dependencies inside its stomach. So here is <code>pom.xml</code> for the "felixLauncher" project (refer to the first post in series):</p>
<pre class="brush: xml; title: ;">
&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
  xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
  &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;

  &lt;groupId&gt;dk.sorokin.maksim&lt;/groupId&gt;
  &lt;artifactId&gt;felixLauncher&lt;/artifactId&gt;
  &lt;version&gt;1.0.0-SNAPSHOT&lt;/version&gt;

  &lt;name&gt;Felix Launcher&lt;/name&gt;

  &lt;properties&gt;
    &lt;felix.bundlerepository.version&gt;1.6.4&lt;/felix.bundlerepository.version&gt;
    &lt;felix.gogo.version&gt;0.8.0&lt;/felix.gogo.version&gt;
    &lt;felix.framework.version&gt;3.2.2&lt;/felix.framework.version&gt;
  &lt;/properties&gt;

  &lt;build&gt;
    &lt;plugins&gt;
      &lt;plugin&gt;
        &lt;artifactId&gt;maven-clean-plugin&lt;/artifactId&gt;
        &lt;version&gt;2.4.1&lt;/version&gt;
        &lt;configuration&gt;
          &lt;filesets&gt;
            &lt;fileset&gt;
              &lt;directory&gt;bundle&lt;/directory&gt;
            &lt;/fileset&gt;
          &lt;/filesets&gt;
        &lt;/configuration&gt;
      &lt;/plugin&gt;

      &lt;plugin&gt;
        &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
        &lt;artifactId&gt;maven-dependency-plugin&lt;/artifactId&gt;
        &lt;version&gt;2.2&lt;/version&gt;
        &lt;executions&gt;
          &lt;execution&gt;
            &lt;id&gt;copy&lt;/id&gt;
            &lt;phase&gt;generate-resources&lt;/phase&gt;
            &lt;goals&gt;
              &lt;goal&gt;copy&lt;/goal&gt;
            &lt;/goals&gt;
            &lt;configuration&gt;
              &lt;artifactItems&gt;
                &lt;artifactItem&gt;
                  &lt;groupId&gt;org.apache.cxf.dosgi&lt;/groupId&gt;
                  &lt;artifactId&gt;cxf-dosgi-ri-singlebundle-distribution&lt;/artifactId&gt;
                  &lt;version&gt;1.2&lt;/version&gt;
                &lt;/artifactItem&gt;
                &lt;artifactItem&gt;
                  &lt;groupId&gt;org.apache.felix&lt;/groupId&gt;
                  &lt;artifactId&gt;org.apache.felix.gogo.command&lt;/artifactId&gt;
                  &lt;version&gt;${felix.gogo.version}&lt;/version&gt;
                &lt;/artifactItem&gt;
                &lt;artifactItem&gt;
                  &lt;groupId&gt;org.apache.felix&lt;/groupId&gt;
                  &lt;artifactId&gt;org.apache.felix.gogo.runtime&lt;/artifactId&gt;
                  &lt;version&gt;${felix.gogo.version}&lt;/version&gt;
                &lt;/artifactItem&gt;
                &lt;artifactItem&gt;
                  &lt;groupId&gt;org.apache.felix&lt;/groupId&gt;
                  &lt;artifactId&gt;org.apache.felix.gogo.shell&lt;/artifactId&gt;
                  &lt;version&gt;${felix.gogo.version}&lt;/version&gt;
                &lt;/artifactItem&gt;
                &lt;artifactItem&gt;
                  &lt;groupId&gt;org.osgi&lt;/groupId&gt;
                  &lt;artifactId&gt;org.osgi.compendium&lt;/artifactId&gt;
                  &lt;version&gt;4.2.0&lt;/version&gt;
                &lt;/artifactItem&gt;
              &lt;/artifactItems&gt;
              &lt;outputDirectory&gt;bundle&lt;/outputDirectory&gt;
            &lt;/configuration&gt;
          &lt;/execution&gt;
        &lt;/executions&gt;
      &lt;/plugin&gt;
    &lt;/plugins&gt;
  &lt;/build&gt;

  &lt;dependencies&gt;
    &lt;dependency&gt;
      &lt;groupId&gt;org.apache.felix&lt;/groupId&gt;
      &lt;artifactId&gt;org.apache.felix.main&lt;/artifactId&gt;
      &lt;version&gt;${felix.framework.version}&lt;/version&gt;
    &lt;/dependency&gt;

    &lt;dependency&gt;
      &lt;groupId&gt;org.ops4j.pax.url&lt;/groupId&gt;
      &lt;artifactId&gt;pax-url-assembly&lt;/artifactId&gt;
      &lt;version&gt;1.3.3&lt;/version&gt;
    &lt;/dependency&gt;
  &lt;/dependencies&gt;
&lt;/project&gt;
</pre>
<p>Note, that we do the same workaround to downloaded CXF DOSGi single bundle as in previous post by deleting <code>lib\org.apache.servicemix.specs.jaxb-api-2.1-1.3.0.jar</code> jar and adding <code>javax.xml.bind.annotation,javax.xml.bind.annotation.adapters</code> to <code>Import-Package</code> in <code>MANIFEST.MF</code>. It is because there is a JAXB api included in single bundle distribution, which clashes with Java JAXB api (which is embedded in JRE). Otherwise you will see something like:</p>
<pre class="brush: plain; title: ;">
javax.xml.bind.JAXBException: ClassCastException: attempting to cast bundle://1.0:36/javax/xml/bind/JAXBContext.class to jar:file:/C:/Program%20Files/Java/jdk1.6.0_21/jre/lib/rt.jar!/javax/xml/bind/JAXBContext.class.  Please make sure that you are specifying the proper ClassLoader.
</pre>
<p>Our test bundle will be called <code>test.bundle</code>, so <code>config.properties</code> file will be something like:</p>
<pre class="brush: plain; title: ;">
felix.auto.deploy.action=install,start
felix.log.level=1

org.osgi.framework.storage.clean=onFirstInit

felix.auto.start.1 = \
 assembly:/C:/projects/test.bundle/target/classes
</pre>
<p>Now, let's develop our <code>test.bundle</code> bundle, which would be able to consume XML and automatically transform it into an object. <code>pom.xml</code> file will look the same as in previous post:</p>
<pre class="brush: xml; title: ;">
&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
  xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
  &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;

  &lt;groupId&gt;dk.sorokin.maksim&lt;/groupId&gt;
  &lt;artifactId&gt;test.bundle&lt;/artifactId&gt;
  &lt;version&gt;1.0.0-SNAPSHOT&lt;/version&gt;

  &lt;name&gt;Test Bundle&lt;/name&gt;

  &lt;build&gt;
    &lt;plugins&gt;
      &lt;plugin&gt;
        &lt;groupId&gt;org.apache.felix&lt;/groupId&gt;
        &lt;artifactId&gt;maven-bundle-plugin&lt;/artifactId&gt;
        &lt;executions&gt;
          &lt;execution&gt;
            &lt;id&gt;generate-resources&lt;/id&gt;
            &lt;goals&gt;
              &lt;goal&gt;manifest&lt;/goal&gt;
            &lt;/goals&gt;
            &lt;configuration&gt;
              &lt;instructions&gt;
                &lt;Bundle-Name&gt;${project.name}&lt;/Bundle-Name&gt;
                &lt;Bundle-SymbolicName&gt;${project.artifactId}&lt;/Bundle-SymbolicName&gt;
                &lt;Bundle-Activator&gt;test.bundle.internal.Activator&lt;/Bundle-Activator&gt;
                &lt;Export-Package&gt;test.bundle&lt;/Export-Package&gt;
              &lt;/instructions&gt;
            &lt;/configuration&gt;
          &lt;/execution&gt;
        &lt;/executions&gt;
      &lt;/plugin&gt;

      &lt;plugin&gt;
        &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
        &lt;artifactId&gt;maven-jar-plugin&lt;/artifactId&gt;
        &lt;version&gt;2.3.1&lt;/version&gt;
        &lt;configuration&gt;
          &lt;archive&gt;
            &lt;manifestFile&gt;${project.build.outputDirectory}/META-INF/MANIFEST.MF&lt;/manifestFile&gt;
          &lt;/archive&gt;
        &lt;/configuration&gt;
      &lt;/plugin&gt;
    &lt;/plugins&gt;
  &lt;/build&gt;

  &lt;dependencies&gt;
    &lt;dependency&gt;
      &lt;groupId&gt;org.apache.felix&lt;/groupId&gt;
      &lt;artifactId&gt;org.osgi.core&lt;/artifactId&gt;
      &lt;version&gt;1.4.0&lt;/version&gt;
    &lt;/dependency&gt;

    &lt;dependency&gt;
      &lt;groupId&gt;org.apache.servicemix.specs&lt;/groupId&gt;
      &lt;artifactId&gt;org.apache.servicemix.specs.jsr311-api-1.0&lt;/artifactId&gt;
      &lt;version&gt;1.8.0&lt;/version&gt;
    &lt;/dependency&gt;
  &lt;/dependencies&gt;
&lt;/project&gt;
</pre>
<p>Now we create a class, an object of which we want to get. It will be <code>test.bundle.MyMessage</code>:</p>
<pre class="brush: java; title: ;">
package test.bundle;

import java.io.StringReader;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class MyMessage {

  private int field;

  private String message;

  public MyMessage() {
    // needed for JAXB
  }

  // used when webservice is called with GET method
  public MyMessage(String query) {
    System.out.println(query);
    try {
      JAXBContext context = JAXBContext.newInstance(getClass());
      Unmarshaller unmarshaller = context.createUnmarshaller();
      StringReader sr = new StringReader(query);
      MyMessage request = (MyMessage) unmarshaller.unmarshal(sr);
      field = request.getField();
      message = request.getMessage();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  public MyMessage(int field, String message) {
    this.field = field;
    this.message = message;
  }

  @XmlElement
  public int getField() {
    return field;
  }

  // needed for JAXB
  public void setField(int field) {
    this.field = field;
  }

  @XmlElement
  public String getMessage() {
    return message;
  }

  // needed for JAXB
  public void setMessage(String message) {
    this.message = message;
  }
}
</pre>
<p>Notice <code>MyMessage(String query)</code> constructor. It is being called, when someone contacts a <code>@GET</code> webservice. If you will not have this constructor, you will see the following message, when you will call a webservice expecting to get this class:</p>
<pre class="brush: plain; title: ;">
Parameter Class test.bundle.MyMessage has no constructor with single String parameter, static valueOf(String) or fromString(String) methods
</pre>
<p>The interface for the webservice:</p>
<pre class="brush: java; title: ;">
package test.bundle;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path(&quot;myService&quot;)
public interface MyService {

  @GET
  @Path(&quot;saySomething/{name}&quot;)
  @Produces(MediaType.TEXT_PLAIN)
  String saySomething(@PathParam(&quot;name&quot;) MyMessage message);
}
</pre>
<p>And implementation for it:</p>
<pre class="brush: java; title: ;">
package test.bundle.internal;

import test.bundle.MyMessage;
import test.bundle.MyService;

public class MyServiceImpl implements MyService {

  public String saySomething(MyMessage message) {
    System.out.println(message);
    return &quot;I got this message: &quot; + message.getMessage() + &quot;  and an integer: &quot; + message.getField();
  }
}
</pre>
<p>Activator for <code>test.bundle</code>:</p>
<pre class="brush: java; title: ;">
package test.bundle.internal;

import java.util.Dictionary;
import java.util.Hashtable;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;

import test.bundle.MyService;

public class Activator implements BundleActivator {

  public void start(BundleContext context) throws Exception {
    Dictionary&lt;String, String&gt; restProps = new Hashtable&lt;String, String&gt;();

    restProps.put(&quot;service.exported.interfaces&quot;, &quot;*&quot;);
    restProps.put(&quot;service.exported.configs&quot;, &quot;org.apache.cxf.rs&quot;);
    restProps.put(&quot;service.exported.intents&quot;, &quot;HTTP&quot;);
    restProps.put(&quot;org.apache.cxf.rs.address&quot;, &quot;http://localhost:8080/&quot;);
    context.registerService(MyService.class.getName(), new MyServiceImpl(), restProps);
  }

  public void stop(BundleContext context) throws Exception {
    //
  }
}
</pre>
<p>Now run <code>mvn install</code> on <code>test.bundle</code> project and launch "felixLauncher". Then go to <a href="http://localhost:8080/myService/saySomething/%3C%3Fxml%20version%3D%221.0%22%20encoding%3D%22UTF-8%22%20standalone%3D%22yes%22%3F%3E%3CmyMessage%3E%3Cfield%3E1%3C%2Ffield%3E%3Cmessage%3EHello%2C%20Max%3C%2Fmessage%3E%3C%2FmyMessage%3E">http://localhost:8080/myService/saySomething/%3C%3Fxml%20version%3D%221.0%22%20encoding%3D%22UTF-8%22%20standalone%3D%22yes%22%3F%3E%3CmyMessage%3E%3Cfield%3E1%3C%2Ffield%3E%3Cmessage%3EHello%2C%20Max%3C%2Fmessage%3E%3C%2FmyMessage%3E</a> and you should see the following output:</p>
<pre class="brush: plain; title: ;">
I got this message: Hello, Max  and an integer: 1
</pre>
<p><a href='http://maksim.sorokin.dk/it/wp-content/uploads/2011/07/felix_consuming_object.zip'>Here are the sources</a></p>
]]></content:encoded>
			<wfw:commentRss>http://maksim.sorokin.dk/it/2011/07/26/maven-apache-felix-cxf-creating-a-restful-webservice-with-cxf-consuming-an-object/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Maven + Apache Felix + CXF: Creating a RESTful Webservice with CXF. Returning and Object.</title>
		<link>http://maksim.sorokin.dk/it/2011/07/24/maven-apache-felix-cxf-creating-a-restful-webservice-with-cxf-returning-and-object/</link>
		<comments>http://maksim.sorokin.dk/it/2011/07/24/maven-apache-felix-cxf-creating-a-restful-webservice-with-cxf-returning-and-object/#comments</comments>
		<pubDate>Sun, 24 Jul 2011 16:19:06 +0000</pubDate>
		<dc:creator>Maksim Sorokin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Apache Felix]]></category>
		<category><![CDATA[CXF]]></category>
		<category><![CDATA[DOSGi]]></category>
		<category><![CDATA[maven]]></category>
		<category><![CDATA[OSGi]]></category>
		<category><![CDATA[rest]]></category>
		<category><![CDATA[web services]]></category>

		<guid isPermaLink="false">http://maksim.sorokin.dk/it/?p=767</guid>
		<description><![CDATA[It is another post in series Maven + Apache Felix + CXF + DOSGi. This example is based on a post about simple JAX-RS webservice with CXF. But this post describes a way how to return a more complex object, instead of a String. First, we create a class in our test.bundle bundle MyMessage in [...]]]></description>
			<content:encoded><![CDATA[<p>It is another post in series <a href="http://maksim.sorokin.dk/it/2011/07/19/maven-apache-felix-cxf-dosgi-series/">Maven + Apache Felix + CXF + DOSGi</a>.</p>
<p>This example is based on a <a href="http://maksim.sorokin.dk/it/2011/07/21/maven-apache-felix-cxf-creating-a-restful-webservice-with-cxf-a-simple-string-example/">post about simple JAX-RS webservice with CXF</a>. But this post describes a way how to return a more complex object, instead of a String.</p>
<p>First, we create a class in our <code>test.bundle</code> bundle <code>MyMessage</code> in package <code>test.bundle</code>:</p>
<pre class="brush: java; title: ;">
package test.bundle;

public class MyMessage {

  private String message;

  public MyMessage(String message) {
    this.message = message;
  }

  public String getMessage() {
    return message;
  }
}
</pre>
<p>We will return this object in XML format to REST webservice requester. We will use <span id="more-767"></span>JAXB, which is by default supported in CXF. We annotate the <code>MyMessage</code>:</p>
<pre class="brush: java; title: ;">
package test.bundle;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class MyMessage {

  private String message;

  public MyMessage() {
    // needed for JAXB
  }

  public MyMessage(String message) {
    this.message = message;
  }

  @XmlElement
  public String getMessage() {
    return message;
  }

  // needed for JAXB
  public void setMessage(String message) {
    this.message = message;
  }
}
</pre>
<p>Then we define a web service interface class <code>MyService</code> in <code>test.bundle</code> package:</p>
<pre class="brush: java; title: ;">
package test.bundle;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path(&quot;myService&quot;)
public interface MyService {

  @GET
  @Path(&quot;sayHello/{name}&quot;)
  @Produces(MediaType.TEXT_PLAIN)
  MyMessage sayHello(@PathParam(&quot;name&quot;) String name);
}
</pre>
<p>And we define an impelmentation for it in <code>MyServiceImpl</code> class in <code>test.bundle.internal</code> package:</p>
<pre class="brush: java; title: ;">
package test.bundle.internal;

import test.bundle.MyMessage;
import test.bundle.MyService;

public class MyServiceImpl implements MyService {

  public MyMessage sayHello(String name) {
    return new MyMessage(&quot;Hello &quot; + name);
  }
}
</pre>
<p>Then we have to do is to register custom message body providers. You can read more on those on <a href="http://cxf.apache.org/docs/jax-rs-basics.html#JAX-RSBasics-CustomMessageBodyProviders">CXF website</a>. Create a class <code>MyMessageWriterProvider</code> in package <code>test.bundle.internal</code>:</p>
<pre class="brush: java; title: ;">
package test.bundle.internal;

import java.io.IOException;
import java.io.OutputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;

import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.MessageBodyWriter;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;

import test.bundle.MyMessage;

public class MyMessageWriterProvider implements MessageBodyWriter&lt;MyMessage&gt; {

  @Override
  public long getSize(MyMessage message, Class&lt;?&gt; type, Type genericType, Annotation[] annotations, MediaType mediaType) {
    return 0;
  }

  @Override
  public boolean isWriteable(Class&lt;?&gt; type, Type genericType, Annotation[] annotations, MediaType mediaType) {
    return true;
  }

  @Override
  public void writeTo(MyMessage message, Class&lt;?&gt; type, Type genericType, Annotation[] annotations, MediaType mediaType,
      MultivaluedMap&lt;String, Object&gt; headers, OutputStream os) throws IOException, WebApplicationException {
    try {
      JAXBContext context = JAXBContext.newInstance(type);
      Marshaller marshaller = context.createMarshaller();
      marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
      marshaller.marshal(message, os);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}
</pre>
<p>And the last thing is to register it in the Activator</p>
<pre class="brush: java; title: ;">
restProps.put(&quot;service.exported.interfaces&quot;, &quot;*&quot;);
restProps.put(&quot;service.exported.configs&quot;, &quot;org.apache.cxf.rs&quot;);
restProps.put(&quot;service.exported.intents&quot;, &quot;HTTP&quot;);
restProps.put(&quot;org.apache.cxf.rs.provider&quot;, MyMessageWriterProvider.class.getName());
restProps.put(&quot;org.apache.cxf.rs.address&quot;, &quot;http://localhost:8080/&quot;);
context.registerService(MyService.class.getName(), new MyServiceImpl(), restProps);
</pre>
<p>Now run <code>mvn clean install</code> on "felixLauncher" and "test.bundle" projects.</p>
<p>The last problem is that DOSGi single bundle distribution contains JAXB api inside. And that clashes with JRE JAXB api (since it is included in JRE). So you may see the following message:</p>
<pre class="brush: plain; title: ;">
javax.xml.bind.JAXBException: ClassCastException: attempting to cast bundle://1.0:36/javax/xml/bind/JAXBContext.class to jar:file:/C:/Program%20Files/Java/jdk1.6.0_21/jre/lib/rt.jar!/javax/xml/bind/JAXBContext.class.  Please make sure that you are specifying the proper ClassLoader.
	at javax.xml.bind.ContextFinder.handleClassCastException(ContextFinder.java:96)
</pre>
<p>There are some ways around this problem. But in this case will just modify cxf distributable. Go to <code>bundle</code> folder in "felixLauncher" project. Open <code>cxf-dosgi-ri-singlebundle-distribution-1.2</code> jar and remove <code>lib/org.apache.servicemix.specs.jaxb-api-2.1-1.3.0.jar</code> jar. Then modify META-INF/MANIFEST.MF inside cxf single bundle distribution and add "javax.xml.bind.annotation,javax.xml.bind.annotation.adapters" to <code>Import-Package</code> section.</p>
<p>Run "Felix Launcher" (refer to the first post).</p>
<p>Go to http://localhost:8080/myService/sayHello/Max. You should see the following output:</p>
<pre class="brush: xml; title: ;">
&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;yes&quot;?&gt;
&lt;myMessage&gt;
    &lt;message&gt;Hello Max&lt;/message&gt;
&lt;/myMessage&gt;
</pre>
<p><a href='http://maksim.sorokin.dk/it/wp-content/uploads/2011/07/felix-rest2.zip'>Here are the zipped sources.</a></p>
]]></content:encoded>
			<wfw:commentRss>http://maksim.sorokin.dk/it/2011/07/24/maven-apache-felix-cxf-creating-a-restful-webservice-with-cxf-returning-and-object/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Maven + Apache Felix + CXF: Creating a RESTful Webservice with CXF. A Simple String Example.</title>
		<link>http://maksim.sorokin.dk/it/2011/07/21/maven-apache-felix-cxf-creating-a-restful-webservice-with-cxf-a-simple-string-example/</link>
		<comments>http://maksim.sorokin.dk/it/2011/07/21/maven-apache-felix-cxf-creating-a-restful-webservice-with-cxf-a-simple-string-example/#comments</comments>
		<pubDate>Thu, 21 Jul 2011 12:26:27 +0000</pubDate>
		<dc:creator>Maksim Sorokin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Apache Felix]]></category>
		<category><![CDATA[CXF]]></category>
		<category><![CDATA[DOSGi]]></category>
		<category><![CDATA[maven]]></category>
		<category><![CDATA[OSGi]]></category>
		<category><![CDATA[rest]]></category>
		<category><![CDATA[web services]]></category>

		<guid isPermaLink="false">http://maksim.sorokin.dk/it/?p=761</guid>
		<description><![CDATA[This is next post in Maven + Apache Felix + CXF + DOSGi series. This is example is based on the sample application developed in previous post. Again, it is assumed here that you are using M2Eclipse plugin, if you want to run examples from Eclipse. As usual, source is included in the end of [...]]]></description>
			<content:encoded><![CDATA[<p>This is next post in <a href="http://maksim.sorokin.dk/it/2011/07/19/maven-apache-felix-cxf-dosgi-series/">Maven + Apache Felix + CXF + DOSGi</a> series.</p>
<p>This is example is based on the sample application developed in <a href="http://maksim.sorokin.dk/it/2011/07/19/maven-apache-felix-easy-development-and-debugging-with-eclipse/">previous post</a>. Again, it is assumed here that you are using <a href="http://m2eclipse.sonatype.org/">M2Eclipse</a> plugin, if you want to run examples from Eclipse.</p>
<p>As usual, source is included in the end of the post.</p>
<p>First of, we need to<span id="more-761"></span> download CXF DOSGi dependency in "felixLauncher" (refer to previous post) and put it to the "bundle" folder, so Apache Felix would know about CXF. Why CXF DOSGi? Because it contains all necessary dependencies inside. We will use single-bundle distribution in this case. So in <code>artifactItems</code> in maven-dependency-plugin (refer to previous post), define:</p>
<pre class="brush: xml; title: ;">
&lt;artifactItem&gt;
  &lt;groupId&gt;org.apache.cxf.dosgi&lt;/groupId&gt;
  &lt;artifactId&gt;cxf-dosgi-ri-singlebundle-distribution&lt;/artifactId&gt;
  &lt;version&gt;1.2&lt;/version&gt;
&lt;/artifactItem&gt;
</pre>
<p>Now we need to create a webservice code in our bundle. But first, we need to tell maven where to find JAX-RS annotations, so add this to the dependencies of <code>test.bundle</code> project:</p>
<pre class="brush: xml; title: ;">
&lt;dependency&gt;
  &lt;groupId&gt;org.apache.servicemix.specs&lt;/groupId&gt;
  &lt;artifactId&gt;org.apache.servicemix.specs.jsr311-api-1.0&lt;/artifactId&gt;
  &lt;version&gt;1.8.0&lt;/version&gt;
&lt;/dependency&gt;
</pre>
<p>Now create an interface <code>MyService</code> in <code>test.bundle</code> package in the <code>test.bundle</code> project:</p>
<pre class="brush: java; title: ;">
package test.bundle;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path(&quot;myService&quot;)
public interface MyService {

  @GET
  @Path(&quot;sayHello/{name}&quot;)
  @Produces(MediaType.TEXT_PLAIN)
  String sayHello(@PathParam(&quot;name&quot;) String name);
}
</pre>
<p>And implementation class <code>MyServiceImpl</code> in package <code>test.bundle.internal</code>:</p>
<pre class="brush: java; title: ;">
package test.bundle.internal;

import test.bundle.MyService;

public class MyServiceImpl implements MyService {

  public String sayHello(String name) {
    return &quot;Hello &quot; + name;
  }
}
</pre>
<p>Actually, interface for a webservice may be even separated to a different bundle to enforce decoupling. But in this case we will just keep them together. But you may want to explicitly state, that <code>test.bundle.internal</code> is internal package and cannot be visible to outside. This can be done by explicitly exporting only visible packages through <code>Export-Package</code> in the manifest file. Since we are using <a href="http://felix.apache.org/site/apache-felix-maven-bundle-plugin-bnd.html">Maven bundle plugin</a>, we can also define <code>Export-Package</code> in manifest file generation part in <code>pom.xml</code>:</p>
<pre class="brush: xml; title: ;">
  &lt;plugin&gt;
	&lt;groupId&gt;org.apache.felix&lt;/groupId&gt;
	&lt;artifactId&gt;maven-bundle-plugin&lt;/artifactId&gt;
	&lt;executions&gt;
	  &lt;execution&gt;
		&lt;id&gt;generate-resources&lt;/id&gt;
		&lt;goals&gt;
		  &lt;goal&gt;manifest&lt;/goal&gt;
		&lt;/goals&gt;
		&lt;configuration&gt;
		  &lt;instructions&gt;
			&lt;Bundle-Name&gt;${project.name}&lt;/Bundle-Name&gt;
			&lt;Bundle-SymbolicName&gt;${project.artifactId}&lt;/Bundle-SymbolicName&gt;
			&lt;Bundle-Activator&gt;test.bundle.internal.Activator&lt;/Bundle-Activator&gt;
			&lt;Export-Package&gt;test.bundle&lt;/Export-Package&gt;
		  &lt;/instructions&gt;
		&lt;/configuration&gt;
	  &lt;/execution&gt;
	&lt;/executions&gt;
  &lt;/plugin&gt;
</pre>
<p>And also, do not worry about JAX-RS package imports in the manifest file -- they will be automatically generated by maven-bundle-plugin.</p>
<p>And the last changes we have to do -- is to register a webservice. This is done through the Activator. So modify Activator of <code>test.bundle</code>, so it would look like:</p>
<pre class="brush: java; title: ;">
package test.bundle.internal;

import java.util.Dictionary;
import java.util.Hashtable;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;

import test.bundle.MyService;

public class Activator implements BundleActivator {

  public void start(BundleContext context) throws Exception {
    Dictionary&lt;String, String&gt; restProps = new Hashtable&lt;String, String&gt;();

    restProps.put(&quot;service.exported.interfaces&quot;, &quot;*&quot;);
    restProps.put(&quot;service.exported.configs&quot;, &quot;org.apache.cxf.rs&quot;);
    restProps.put(&quot;service.exported.intents&quot;, &quot;HTTP&quot;);
    restProps.put(&quot;org.apache.cxf.rs.address&quot;, &quot;http://localhost:8080/&quot;);
    context.registerService(MyService.class.getName(), new MyServiceImpl(), restProps);
  }

  public void stop(BundleContext context) throws Exception {
    //
  }
}
</pre>
<p>Then run <code>mvn clean install</code> on "felixLauncher" and "test.bundle" projects. Afterwards, launch "felixLauncher" (refer to the <a href="http://maksim.sorokin.dk/it/2011/07/19/maven-apache-felix-easy-development-and-debugging-with-eclipse/">first post in series</a>).</p>
<p>Then you can query the REST webservice throught a browser:</p>
<pre class="brush: plain; title: ;">

http://localhost:8080/myService/sayHello/Max
</pre>
<p>And should see the following response:</p>
<pre class="brush: plain; title: ;">
Hello Max
</pre>
<p><a href='http://maksim.sorokin.dk/it/wp-content/uploads/2011/07/felix-rest.zip'>Here are the zipped sources.</a></p>
]]></content:encoded>
			<wfw:commentRss>http://maksim.sorokin.dk/it/2011/07/21/maven-apache-felix-cxf-creating-a-restful-webservice-with-cxf-a-simple-string-example/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Maven + Apache Felix: Best Practices</title>
		<link>http://maksim.sorokin.dk/it/2011/07/20/maven-apache-felix-best-practices/</link>
		<comments>http://maksim.sorokin.dk/it/2011/07/20/maven-apache-felix-best-practices/#comments</comments>
		<pubDate>Wed, 20 Jul 2011 07:52:46 +0000</pubDate>
		<dc:creator>Maksim Sorokin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Apache Felix]]></category>
		<category><![CDATA[best practices]]></category>
		<category><![CDATA[maven]]></category>
		<category><![CDATA[OSGi]]></category>

		<guid isPermaLink="false">http://maksim.sorokin.dk/it/?p=752</guid>
		<description><![CDATA[This is another post in Maven + Apache Felix + CXF + DOSGi series. As was shown in Maven + Apache Felix: Easy Development and Debugging With Eclipse post, it is easy to develop projects with Maven and Apache Felix. But how about really big projects. How to maintain configuration and easily manage the bundles? [...]]]></description>
			<content:encoded><![CDATA[<p>This is another post in <a href="http://maksim.sorokin.dk/it/2011/07/19/maven-apache-felix-cxf-dosgi-series/">Maven + Apache Felix + CXF + DOSGi</a> series. As was shown in <a href="http://maksim.sorokin.dk/it/2011/07/19/maven-apache-felix-easy-development-and-debugging-with-eclipse">Maven + Apache Felix: Easy Development and Debugging With Eclipse</a> post, it is easy to develop projects with Maven and Apache Felix. But how about really big projects. How to maintain configuration and easily manage the bundles?</p>
<p>The answer is simple -- use the <span id="more-752"></span>power of Maven. If you have really big projects, try to split them into different parents and modules. It is import during this splitting part to preserve some sort of naming convention. For example, each child preserves parent name as a prefix. In this case it is easier to identify and operate the projects. The structure may look like this, assuming that there is a <code>generator</code> project:</p>
<pre class="brush: plain; title: ;">
-- generator
 |-- generator.parser
   |-- generator.parser.full
     |-- generator.parser.full.noboms
     |-- generator.parser.full.universal
   |-- generator.parser.simple
   |-- generator.parser.utils
 |-- generator.security
   |-- generator.security.basic
   |-- generator.security.ldap
 |-- generator.utility
</pre>
<p>Also, do not forget that you can exploit <code>pluginManagement</code> and <code>dependencyManagement</code> in Maven. Using it you can predefine build process routine in your parent (typically the top one) and not care about it in your parents. Example would be something like:</p>
<pre class="brush: xml; title: ;">
&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
  xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
  &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;

  &lt;groupId&gt;dk.sorokin.maksim&lt;/groupId&gt;
  &lt;artifactId&gt;generator&lt;/artifactId&gt;
  &lt;version&gt;1.0.0-SNAPSHOT&lt;/version&gt;
  &lt;packaging&gt;pom&lt;/packaging&gt;

  &lt;name&gt;Generator Parent&lt;/name&gt;

  &lt;modules&gt;
    &lt;module&gt;generator.parser&lt;/module&gt;
    &lt;module&gt;generator.security&lt;/module&gt;
    &lt;module&gt;generator.utility&lt;/module&gt;
  &lt;/modules&gt;

  &lt;properties&gt;
    &lt;org.apache.felix.framework.version&gt;3.2.2&lt;/org.apache.felix.framework.version&gt;
    &lt;org.apache.servicemix.specs.jsr311-api-1.0&gt;1.8.0&lt;/org.apache.servicemix.specs.jsr311-api-1.0&gt;
    &lt;org.osgi.core.version&gt;1.4.0&lt;/org.osgi.core.version&gt;
  &lt;/properties&gt;

  &lt;build&gt;
    &lt;plugins&gt;
      &lt;plugin&gt;
        &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
        &lt;version&gt;2.3.2&lt;/version&gt;
        &lt;configuration&gt;
          &lt;source&gt;1.6&lt;/source&gt;
          &lt;target&gt;1.6&lt;/target&gt;
        &lt;/configuration&gt;
      &lt;/plugin&gt;

      &lt;plugin&gt;
        &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
        &lt;artifactId&gt;maven-jar-plugin&lt;/artifactId&gt;
        &lt;version&gt;2.3.1&lt;/version&gt;
        &lt;configuration&gt;
          &lt;archive&gt;
            &lt;manifestFile&gt;${project.build.outputDirectory}/META-INF/MANIFEST.MF&lt;/manifestFile&gt;
          &lt;/archive&gt;
        &lt;/configuration&gt;
      &lt;/plugin&gt;
    &lt;/plugins&gt;

    &lt;pluginManagement&gt;
      &lt;plugins&gt;
        &lt;plugin&gt;
          &lt;groupId&gt;org.apache.felix&lt;/groupId&gt;
          &lt;artifactId&gt;maven-bundle-plugin&lt;/artifactId&gt;
          &lt;version&gt;2.3.5&lt;/version&gt;
          &lt;executions&gt;
            &lt;execution&gt;
              &lt;id&gt;generate-resources&lt;/id&gt;
              &lt;goals&gt;
                &lt;goal&gt;manifest&lt;/goal&gt;
              &lt;/goals&gt;
            &lt;/execution&gt;
          &lt;/executions&gt;
        &lt;/plugin&gt;
      &lt;/plugins&gt;
    &lt;/pluginManagement&gt;
  &lt;/build&gt;

  &lt;dependencyManagement&gt;
    &lt;dependencies&gt;
      &lt;dependency&gt;
        &lt;groupId&gt;junit&lt;/groupId&gt;
        &lt;artifactId&gt;junit&lt;/artifactId&gt;
        &lt;version&gt;4.8.2&lt;/version&gt;
        &lt;scope&gt;test&lt;/scope&gt;
      &lt;/dependency&gt;

      &lt;dependency&gt;
        &lt;groupId&gt;org.apache.felix&lt;/groupId&gt;
        &lt;artifactId&gt;org.apache.felix.framework&lt;/artifactId&gt;
        &lt;version&gt;${org.apache.felix.framework.version}&lt;/version&gt;
      &lt;/dependency&gt;

      &lt;dependency&gt;
        &lt;groupId&gt;org.apache.servicemix.specs&lt;/groupId&gt;
        &lt;artifactId&gt;org.apache.servicemix.specs.jsr311-api-1.0&lt;/artifactId&gt;
        &lt;version&gt;${org.apache.servicemix.specs.jsr311-api-1.0}&lt;/version&gt;
      &lt;/dependency&gt;

      &lt;dependency&gt;
        &lt;groupId&gt;org.apache.felix&lt;/groupId&gt;
        &lt;artifactId&gt;org.osgi.core&lt;/artifactId&gt;
        &lt;version&gt;${org.osgi.core.version}&lt;/version&gt;
      &lt;/dependency&gt;
    &lt;/dependencies&gt;
  &lt;/dependencyManagement&gt;
&lt;/project&gt;
</pre>
<p>Also, carefully maintain Felix <code>config.properties</code> file. Especially, if you share it with several developers. In order not to have hardcoded path in it, you may use <a href="http://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html">Maven Filtering</a> to filtering it. Instead of paths, you have variables, which are stored in <a href="http://maven.apache.org/settings.html">Maven Settings</a>.</p>
<p>Finally, always make sure, that the whole project and Felix launcher (if you use it) can be checked, built and run immediately on the fresh machine.</p>
]]></content:encoded>
			<wfw:commentRss>http://maksim.sorokin.dk/it/2011/07/20/maven-apache-felix-best-practices/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Maven + Apache Felix: Easy Development and Debugging With Eclipse</title>
		<link>http://maksim.sorokin.dk/it/2011/07/19/maven-apache-felix-easy-development-and-debugging-with-eclipse/</link>
		<comments>http://maksim.sorokin.dk/it/2011/07/19/maven-apache-felix-easy-development-and-debugging-with-eclipse/#comments</comments>
		<pubDate>Tue, 19 Jul 2011 15:38:49 +0000</pubDate>
		<dc:creator>Maksim Sorokin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Apache Felix]]></category>
		<category><![CDATA[Eclipse]]></category>
		<category><![CDATA[maven]]></category>
		<category><![CDATA[OSGi]]></category>

		<guid isPermaLink="false">http://maksim.sorokin.dk/it/?p=744</guid>
		<description><![CDATA[This is a first post on in series about Maven + Apache Felix + CXF + DOSGi. Here we will combine Maven, Apache Felix and Eclipse. You have to use M2Eclipse plugin for this (or similar Maven-Eclipse integration plugin). The problem: Integrating Felix with Eclipse tutorial requires manual download and configuration of Felix distributable into [...]]]></description>
			<content:encoded><![CDATA[<p>This is a first post on in <a href="http://maksim.sorokin.dk/it/2011/07/19/maven-apache-felix-cxf-dosgi-series/">series about Maven + Apache Felix + CXF + DOSGi</a>. Here we will combine Maven, Apache Felix and Eclipse. You have to use <a href="http://m2eclipse.sonatype.org/">M2Eclipse plugin</a> for this (or similar Maven-Eclipse integration plugin).</p>
<p>The problem:</p>
<ul>
<li><a href="http://felix.apache.org/site/integrating-felix-with-eclipse.html">Integrating Felix with Eclipse</a> tutorial requires manual download and configuration of Felix distributable into workspace</li>
<li>Felix allows deploying only jar bundles, which is inappropriate for easy development and debugging</li>
</ul>
<p>We can use the power of Maven and <a href="http://adreghiciu.wordpress.com/2009/09/06/install-your-directory-as-a-bundle-in-apache-felix/">these</a> <a href="http://stackoverflow.com/questions/5127969/pax-url-protocol-not-supported-at-felxs-startup">two</a> resources to solve both problems.</p>
<p>The idea about the first one is to<span id="more-744"></span> use Maven dependency management mechanism to download Felix dependencies and put them to required locations.The idea about the second one is to use <a href="http://wiki.ops4j.org/display/paxurl/Pax+URL">OPS4J Pax URL</a> to load a bundle from a folder (not a jar).</p>
<p>We will have two projects -- one solely for launching Felix environment and another one for a test bundle.</p>
<p>We start with Felix launcher <code>felixLauncher</code> project. We create a Maven project (if you have <a href="http://m2eclipse.sonatype.org/">M2Eclipse</a> installed, that is easier) in Eclipse. First warning is that packaging type should be "jar", not "pom" (the reason will be explained later).</p>
<p>To understand what is happening and what Felix framework requires, you may take a look into Felix Framework Distribution. you will see that there is a "bundle" folder which contains several jars. Also there is a "conf" folder which has "config.properties" inside it. So we will do the same for our <code>felixLauncher</code> project. We will dump those jars (gogo) into "bundle" folder. Also, we dump "osgi compendium" jar into "bundle" folder, because it will be needed for our projects. Since "bundle" folder is populated automatically, we also would like to clean it on the "clean" goal. Therefore, we tell <a href="http://maven.apache.org/plugins/maven-clean-plugin/">Maven Clean Plugin</a> to remove "bundle" folder.<br />
Lastly, we specify Felix Distribution Framework as our dependency, because we want to launch Felix Framework directly from Eclipse. Since we want to use <a href="http://wiki.ops4j.org/display/paxurl/Pax+URL">OPS4J Pax URL</a> to launch our projects from folders, not from jars, we have to depend on that project as well to make it available for Eclipse.<br />
So here is <code>pom.xml</code>:</p>
<pre class="brush: xml; title: ;">
&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
  xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
  &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;

  &lt;groupId&gt;dk.sorokin.maksim&lt;/groupId&gt;
  &lt;artifactId&gt;felixLauncher&lt;/artifactId&gt;
  &lt;version&gt;1.0.0-SNAPSHOT&lt;/version&gt;

  &lt;name&gt;Felix Launcher&lt;/name&gt;

  &lt;properties&gt;
    &lt;felix.bundlerepository.version&gt;1.6.4&lt;/felix.bundlerepository.version&gt;
    &lt;felix.gogo.version&gt;0.8.0&lt;/felix.gogo.version&gt;
    &lt;felix.framework.version&gt;3.2.2&lt;/felix.framework.version&gt;
  &lt;/properties&gt;

  &lt;build&gt;
    &lt;plugins&gt;
      &lt;plugin&gt;
        &lt;artifactId&gt;maven-clean-plugin&lt;/artifactId&gt;
        &lt;version&gt;2.4.1&lt;/version&gt;
        &lt;configuration&gt;
          &lt;filesets&gt;
            &lt;fileset&gt;
              &lt;directory&gt;bundle&lt;/directory&gt;
            &lt;/fileset&gt;
          &lt;/filesets&gt;
        &lt;/configuration&gt;
      &lt;/plugin&gt;

      &lt;plugin&gt;
        &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
        &lt;artifactId&gt;maven-dependency-plugin&lt;/artifactId&gt;
        &lt;version&gt;2.2&lt;/version&gt;
        &lt;executions&gt;
          &lt;execution&gt;
            &lt;id&gt;copy&lt;/id&gt;
            &lt;phase&gt;generate-resources&lt;/phase&gt;
            &lt;goals&gt;
              &lt;goal&gt;copy&lt;/goal&gt;
            &lt;/goals&gt;
            &lt;configuration&gt;
              &lt;artifactItems&gt;
                &lt;artifactItem&gt;
                  &lt;groupId&gt;org.apache.felix&lt;/groupId&gt;
                  &lt;artifactId&gt;org.apache.felix.gogo.command&lt;/artifactId&gt;
                  &lt;version&gt;${felix.gogo.version}&lt;/version&gt;
                &lt;/artifactItem&gt;
                &lt;artifactItem&gt;
                  &lt;groupId&gt;org.apache.felix&lt;/groupId&gt;
                  &lt;artifactId&gt;org.apache.felix.gogo.runtime&lt;/artifactId&gt;
                  &lt;version&gt;${felix.gogo.version}&lt;/version&gt;
                &lt;/artifactItem&gt;
                &lt;artifactItem&gt;
                  &lt;groupId&gt;org.apache.felix&lt;/groupId&gt;
                  &lt;artifactId&gt;org.apache.felix.gogo.shell&lt;/artifactId&gt;
                  &lt;version&gt;${felix.gogo.version}&lt;/version&gt;
                &lt;/artifactItem&gt;
                &lt;artifactItem&gt;
                  &lt;groupId&gt;org.osgi&lt;/groupId&gt;
                  &lt;artifactId&gt;org.osgi.compendium&lt;/artifactId&gt;
                  &lt;version&gt;4.2.0&lt;/version&gt;
                &lt;/artifactItem&gt;
              &lt;/artifactItems&gt;
              &lt;outputDirectory&gt;bundle&lt;/outputDirectory&gt;
            &lt;/configuration&gt;
          &lt;/execution&gt;
        &lt;/executions&gt;
      &lt;/plugin&gt;
    &lt;/plugins&gt;
  &lt;/build&gt;

  &lt;dependencies&gt;
    &lt;dependency&gt;
      &lt;groupId&gt;org.apache.felix&lt;/groupId&gt;
      &lt;artifactId&gt;org.apache.felix.main&lt;/artifactId&gt;
      &lt;version&gt;${felix.framework.version}&lt;/version&gt;
    &lt;/dependency&gt;

    &lt;dependency&gt;
      &lt;groupId&gt;org.ops4j.pax.url&lt;/groupId&gt;
      &lt;artifactId&gt;pax-url-assembly&lt;/artifactId&gt;
      &lt;version&gt;1.3.3&lt;/version&gt;
    &lt;/dependency&gt;
  &lt;/dependencies&gt;
&lt;/project&gt;
</pre>
<p>Next, we need to create a <code>config.properties</code> file, because it is mandatory for Felix. There is a way to have own location for it, but to make things simpler, we will create it in the location, where Felix expects it by default. In the "felixLauncher" project create <code>conf</code> folder. Then create a file <code>config.properties</code> with the following content:</p>
<pre class="brush: plain; title: ;">
felix.auto.deploy.action=install,start
felix.log.level=1

org.osgi.framework.storage.clean=onFirstInit
</pre>
<p>You can read about options available on <a href="http://felix.apache.org/site/apache-felix-framework-configuration-properties.html">Apache Felix</a> website.</p>
<p>Now we have to create an Eclipse launcher. In Eclipse go to "Run - Run Configurations...". In the left menu right-click on "Java Application" and click "New". In "Name" specify "Felix Launcher". In "Project" specify "felixLauncher" (if you do not see this project, that means the project is not understood by Eclipse as "Java" project. So it is important that it has "jar" packaging type). Then click on "Search..." in the "Main class" field. Search for "org.apache.felix.main.Main". Click "OK". Lastly, switch to "Arguments" tab and in "VM arguments" specify "-Djava.protocol.handler.pkgs=org.ops4j.pax.url". Click "Apply" and "Run". You will see something like:</p>
<pre class="brush: plain; title: ;">
____________________________
Welcome to Apache Felix Gogo

g!
</pre>
<p>Now Apache Felix can be launched correctly.</p>
<p>Next, let's create some test project. Create a Maven project "test.bundle". Create a package <code>test.bundle.internal</code> in <code>src/main/java</code>. And create "Activator.java" class there with the following content (do not worry about compilation problems for now):</p>
<pre class="brush: java; title: ;">
package test.bundle.internal;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;

public class Activator implements BundleActivator {

  public void start(BundleContext arg0) throws Exception {
    System.out.println(&quot;Hello&quot;);
  }

  public void stop(BundleContext arg0) throws Exception {
    System.out.println(&quot;Bye&quot;);
  }
}
</pre>
<p>Now, the <code>pom.xml</code> file for the project. As you know, OSGi bundles need to have manifest files. But instead of writing them by hand, we can generate them. There is an excellent <a href="http://felix.apache.org/site/apache-felix-maven-bundle-plugin-bnd.html">Apache Felix Maven Bundle Plugin</a>, which we will use. We will tell <a href="http://maven.apache.org/plugins/maven-jar-plugin/">Maven Jar Plugin</a> to take that generated <code>MANIFEST.MF</code> file. So <code>pom.xml</code> will look like:</p>
<pre class="brush: xml; title: ;">
&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
  xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
  &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;

  &lt;groupId&gt;dk.sorokin.maksim&lt;/groupId&gt;
  &lt;artifactId&gt;test.bundle&lt;/artifactId&gt;
  &lt;version&gt;1.0.0-SNAPSHOT&lt;/version&gt;

  &lt;name&gt;Test Bundle&lt;/name&gt;

  &lt;build&gt;
    &lt;plugins&gt;
      &lt;plugin&gt;
        &lt;groupId&gt;org.apache.felix&lt;/groupId&gt;
        &lt;artifactId&gt;maven-bundle-plugin&lt;/artifactId&gt;
        &lt;executions&gt;
          &lt;execution&gt;
            &lt;id&gt;generate-resources&lt;/id&gt;
            &lt;goals&gt;
              &lt;goal&gt;manifest&lt;/goal&gt;
            &lt;/goals&gt;

            &lt;configuration&gt;
              &lt;instructions&gt;
                &lt;Bundle-Name&gt;${project.name}&lt;/Bundle-Name&gt;
                &lt;Bundle-SymbolicName&gt;${project.artifactId}&lt;/Bundle-SymbolicName&gt;
                &lt;Bundle-Activator&gt;test.bundle.internal.Activator&lt;/Bundle-Activator&gt;
              &lt;/instructions&gt;
            &lt;/configuration&gt;
          &lt;/execution&gt;
        &lt;/executions&gt;
      &lt;/plugin&gt;

      &lt;plugin&gt;
        &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
        &lt;artifactId&gt;maven-jar-plugin&lt;/artifactId&gt;
        &lt;version&gt;2.3.1&lt;/version&gt;
        &lt;configuration&gt;
          &lt;archive&gt;
            &lt;manifestFile&gt;${project.build.outputDirectory}/META-INF/MANIFEST.MF&lt;/manifestFile&gt;
          &lt;/archive&gt;
        &lt;/configuration&gt;
      &lt;/plugin&gt;
    &lt;/plugins&gt;
  &lt;/build&gt;

  &lt;dependencies&gt;
    &lt;dependency&gt;
      &lt;groupId&gt;org.apache.felix&lt;/groupId&gt;
      &lt;artifactId&gt;org.osgi.core&lt;/artifactId&gt;
      &lt;version&gt;1.4.0&lt;/version&gt;
    &lt;/dependency&gt;
  &lt;/dependencies&gt;
&lt;/project&gt;
</pre>
<p>Then you can simply run <code>mvn clean install</code> on the project. Actually, Eclipse will do that for you. But you need to run <code>install</code> goal in order to have correct <code>MANIFEST.MF</code> file generated.</p>
<p>Last thing which is left to do is to inform Apache Felix about the project. Modify <code>config.properties</code> and add the following:</p>
<pre class="brush: plain; title: ;">
felix.auto.start.1 = \
 assembly:/C:/projects/test.bundle/target/classes
</pre>
<p>Path to the project may be different on your machine. You should see "Hello" message in the output.</p>
<p>Here is the zipped version of these two projects: <a href='http://maksim.sorokin.dk/it/wp-content/uploads/2011/07/felix.zip'>zip file</a></p>
]]></content:encoded>
			<wfw:commentRss>http://maksim.sorokin.dk/it/2011/07/19/maven-apache-felix-easy-development-and-debugging-with-eclipse/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

