Accueil > Spring, Spring DM > Eclipse RCP/RAP with Spring DM, Spring Data JPA and Remoting [step1]

Eclipse RCP/RAP with Spring DM, Spring Data JPA and Remoting [step1]


In [step0] we have seen that I will explain step by step how to develop « Eclipse RCP/RAP with Spring DM, Spring Data JPA and Remoting ». In my articles, I will use Spring DM instead of using Eclipse Gemini Blueprint because today CXF DOSGi supports only Spring DM. However I have created the patch DOSGI-115 to support Eclipse Gemini Blueprint with DOSGi.

In this article we will just initialize Spring DM :

But what is Spring DM? Spring DM gives you the capability to use Spring on OSGi context:

  1. any OSGi bundles can declare Spring bean in XML Spring file stored in their META-INF/spring folder. Spring DM provides a Spring Extender bundle which :
    1. load XML Spring file for each OSGi bundles which starts.
    2. unload XML Spring file for each OSGi bundles which stops.
  2. it’s possible to declare in Spring bean, services to publish/consume in the OSGi registry services.

Download

You can download eclipsespring_step1.zip which contains the following explained projects :

  • fr.opensagres.config.log4j : OSGi fragment which configures log4j.
  • TargetPlatform: simple project which hosts the Spring DM JARS, the target definition and launch.

To use this zip, unzip it :

  1. import this 2 projects in a workspace.
  2. open the TargetPlatform/eclipsespring.target file and click on Set as Target Platform.
  3. select TargetPlatform/launch/Simple OSGi client.launch and Run it.

TargetPLatform & Spring DM

To use Spring DM, we need starting Spring DM Bundles in the OSGi container (in your case Equinox). To configure bundles which must be started, Eclipse provides the TargetPlatform concept which is used by PDE to know which OSGI bundles are available in the Eclipse Workspace and when you start the OSGi container.

So the first step to use Spring DM is :

Download Spring DM

You can download Spring DM Bundles JARs on Spring DM WebSite, but I prefer download JARs with maven to assure dependencies version between bundles. To do that :

  • create in your workspace a simple Eclipse Project TargetPlatform (File->New->Other… and General->Project) :
  • Create on this project, maven pom.xml with Spring DM dependencies like this :
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    	<modelVersion>4.0.0</modelVersion>
    	<groupId>fr.opensagres.samples</groupId>
    	<artifactId>fr.opensagres.samples.targetplatform</artifactId>
    	<packaging>pom</packaging>
    	<version>0.0.1-SNAPSHOT</version>
    
    	<build>
    		<plugins>
    			<plugin>
    				<groupId>org.apache.maven.plugins</groupId>
    				<artifactId>maven-dependency-plugin</artifactId>
    				<version>2.1</version>
    				<executions>
    					<execution>
    						<id>copy-dependencies</id>
    						<phase>process-resources</phase>
    						<goals>
    							<goal>copy-dependencies</goal>
    						</goals>
    						<configuration>
    							<outputDirectory>lib</outputDirectory>
    							<overWriteReleases>true</overWriteReleases>
    							<overWriteSnapshots>true</overWriteSnapshots>
    							<overWriteIfNewer>true</overWriteIfNewer>
    						</configuration>
    					</execution>
    				</executions>
    			</plugin>
    		</plugins>
    	</build>
    
    	<properties>
    		<spring.dm.version>1.2.1</spring.dm.version>
    		<slf4j.version>1.6.1</slf4j.version>
    	</properties>
    
    	<repositories>
    
    		<repository>
    			<id>spring-maven-milestone</id>
    			<name>Springframework Maven Repository</name>
    			<url>http://maven.springframework.org/milestone</url>
    		</repository>
    
    		<repository>
    			<id>com.springsource.repository.bundles.external</id>
    			<name>SpringSource Enterprise Bundle Repository - External Bundle Releases</name>
    			<url>http://repository.springsource.com/maven/bundles/external</url>
    		</repository>
    
    	</repositories>
    
    	<dependencies>
    
    		<!-- Spring DM -->
    		<dependency>
    			<groupId>org.springframework.osgi</groupId>
    			<artifactId>spring-osgi-extender</artifactId>
    			<version>${spring.dm.version}</version>
    		</dependency>
    
    		<!-- API logger: Commons Logging required by Spring DM -->
    		<dependency>
    			<groupId>org.slf4j</groupId>
    			<artifactId>com.springsource.slf4j.org.apache.commons.logging</artifactId>
    			<version>${slf4j.version}</version>
    			<scope>provided</scope>
    		</dependency>
    
    		<!-- Implementation logger: log4j -->
    		<dependency>
    			<groupId>org.slf4j</groupId>
    			<artifactId>com.springsource.slf4j.log4j</artifactId>
    			<version>${slf4j.version}</version>
    			<scope>provided</scope>
    		</dependency>
    
    	</dependencies>
    
    </project>
    

    You can notice that commons logging (API Logger) is required to use Spring DM. Here I have used Log4J as Implementation Logger.

  • Open a dos/sh command, go to the folder TargetPlaform and launch mvn like this (or use M2Eclipse) :
    mvn process-resources
  • this command download Spring DM JARs and their dependencies and copy it to the TargetPlatform/lib folder. Once maven has finished, refresh TargetPlatform project, lib folder must appears:

Target Definition

At this step we have Spring DM JARs. Now we must create a target definition to use it as target platform and can use Spring DM with OSGi container.

Once target definition will be created and activated, Spring DM bundles will be available in the workspace and we can reference it in OSGi launch.

In this section we will create TargetPlatform/eclipsespring.target target definition which will reference Spring DM bundles and we will activate this target paltform. To do that select TargetPlatform project and click on right mouse. Select New/Target Definition :

This action opens the New Target Definition wizard :

  1. select the TargetPlatform project
  2. fill file name with eclipsespring.target
  3. select template « Base RCP (Binary Only) » to have a target definition with RCP bundles (we need that when we will create Eclipse RCP client).

Click on Finish button to generate TargetPlatform/eclipsespring.target file :

At this step we have a target definition with RCP bundles.

Add Spring DM to TargetDefintion

Now we can add Spring DM bundles from TargetPlatform/lib folder to the target definition. To do that open eclipsespring.target and click on Add… button :

This action opens the wizard Add content. We will use it to reference the JARS of the TargetPlatform/lib in the target definition. To do that, select Directory :

Click on Next button and fill location field with ${workspace_loc}/TargetPlatform/lib:

Click on Finish button, target definition have now Spring DM bundles:

Now we must set this target definition as target platform to use Spring DM bundles. To do that, click on Set as Target Platform :

At this step, RCP bundles and Spring DM bundles are available in the workspace and OSGi container (in the launch).

Create OSGi Launch

At this step we can create an OSGi launch to start OSGi container with Spring DM. To do that click on Run/Run Configurations…:

This actions opens the Run Configurations dialog. Select OSGi Framework tree node and click on New:

This action creates an OSGi launch by selecting the all bundles defined in the target platform. An important thing is to deselect the RCP bundles (org.eclipse.ui, org.eclipse.ui.workbench, etc…), because in this case we have not an RCP project to launch :

Creating an OSGi launch can be complex, because some OSGi bundle can depend on another OSGi bundles. So before starting an OSGi launch, click on Validate Bundles :

to ckeck you have none errors :

Note: you can use Add required Bundles button to select the missing bundles.

Run OSGi Launch

Once the OSGi launch is validated, we can start the OSGi launch. To do that click on Run button of the OSGi launch :

This action starts the OSGi container with the selected bundles of the launch. Your OSGi console looks like this :

You will notice that there is a warn messages with log4j.

osgi> log4j:WARN No appenders could be found for logger (org.springframework.osgi.extender.internal.activator.ContextLoaderListener).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

We will fix this problem in the Configure Log4j section.

You can type ss in the OSGi console to see the started bundles to check for instance that Spring extender bundle org.springframework.osgi.extender is ACTIVE:

Configure Log4j

To avoid having warn log4j message in the OSGi console, we must configure log4j. In no OSGi context, to configure log4j we need create log4j.properties in the src folder of a project. In OSGi context to configure log4j, you must create an OSGi fragment linked to log4j bundle and add a log4j.properties in the src folder of this OSGi fragment.

To create an OSGi fragment, go at the menu File/New/Other… -> Plug-in Development/Fragment Project:

Click on Next button, and fill project name with fr.opensagres.config.log4j :

Click on Next button and select com.springsource.org.apache.log4j (Log4J bundle) :

Click on Finish button to generate Fragment project and create src/log4j.properties like this :

log4j.rootLogger=info, con
log4j.appender.con=org.apache.log4j.ConsoleAppender
log4j.appender.con.layout=org.apache.log4j.PatternLayout
log4j.appender.con.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n

Your workspace looks like this :

If you Run the OSGi launch, you will notice that log4j warn messages disappears and you will have some logs of the Spring DM :

Conclusion

In this article we have seen how to initialize Spring DM and launch it in OSG container. In the next article [step2] we will create a new bundle which will define a Spring file which declare as Spring bean a Thread class. We will see how to Spring DM load/unload this XMl Spring file.

Catégories :Spring, Spring DM Étiquettes :
  1. Jay Jonas
    septembre 23, 2012 à 3:56

    For Eclipse 4.2 Juno I have to include the org.apache.felix.gogo.* and org.apache.felix.service.command to the Simple OSGi Client run configurations required bundles to ran it successfully. So that’s the tip for your readers. Thank you so much for this tutorial. Go to the next part! ^ ^

  2. Jay Jonas
    octobre 19, 2012 à 10:36

    Angelo, is there a chance to use the SpringExtensionFactory factory under Eclipse 4.x framework as you use it under 3.x? Are there any news in this directions?

    • octobre 20, 2012 à 6:13

      Hi Jonas,

      With (pur) E4 framework, VewPart and EditorPart doesn’t exist. You must just create a simple Pojo with well annotation (@Focus, etc). E4 provides Dependency Injection which works too with OSGi services.

      So you need not to use SpringExtensionFactory to inject your OSGi services in your part. You must use @Inject like this :

      ————————————-
      public class MyViewPart {
      @Inject
      private IMyService service;
      }
      ————————————-

      It’s E4 framework which instantiates the MyViewPart and manage the injection of the OSGi service (with SpringExtensionFactory, it’s Spring framework which manages that).

      But the big problem I find with E4, it doesn’t provide abstract class for editor (likemulti page, form toolkit etc). I’m following this bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=299990 to see a good sample for implement E4 editor.

      E4 provides a compatibilty layer with 3.x and it provides the capability to use DI with 3.x EditorPart, ViewPart, but I think it’s not a good ideatouse 3.x framework.

      I suggest you to read 2 interesting post in E4 forum:

      1) Injecting 3.x Views/Editors => http://dev.eclipse.org/mhonarc/lists/e4-dev/msg06763.html

      2) How to use E4 DI with Wizard,Dialog and FormEditor? => http://dev.eclipse.org/mhonarc/lists/e4-dev/msg06767.html

      Regards Angelo

  3. Jay Jonas
    octobre 21, 2012 à 2:04

    Thanks for you reply, Angelo!

    So you consider that the techniques demonstrated in this series are not able to be used effectively in Eclipse 4.x. Is it? I think that your technique is the best way I knew to write an RCP application that uses a database. Are there a chance for one more step to demonstrate how to use Eclipse Gemini instead Spring/DM?

    BTW, for the UI layer I really like RCPForms. Take a look at http://sourceforge.net/projects/rcpforms/.

    Thanks for the series of articles including that one in French. I believe this is the way to tie the various layers of a good design using RCP.

    Regards, JJ.

    • octobre 21, 2012 à 3:28

      Hi Jay,

      > So you consider that the techniques demonstrated in this series are not able to be used >effectively in Eclipse 4.x. Is it?

      I’m sorry with my bad english. No it’s possible to use Spring DM with E4 and it’s more cleaner because you need not SpringExtensionFactory. But IMHO, I think E4 should provide some base class for editor and viewpart. In my article UI is very basic and I think you could replace the 3.x ViewPart by your Pojo and use @Inject to consume the userService.

      > I think that your technique is the best way I knew to write an RCP application that uses a > >database.
      Wow, many thanks! Spring DM (Eclipse Gemini Blueprint) and Spring Data JPA is very fantastic. For remoting CXF DOSGi is really cool too.

      > Are there a chance for one more step to demonstrate how to use Eclipse Gemini instead >Spring/DM?
      It’s really easy to do that (if you don’t want use CXF DOSGi). You must just replace your Spring DM bundles with Eclipse Gemini Blueprint. We use that in our RCP/RAP demo http://xdocreport-rap.opensagres.cloudbees.net/xdocreport?startup=fr.opensagres.xdocreport.eclipse.ui.application

      >BTW, for the UI layer I really like RCPForms. Take a look at >http://sourceforge.net/projects/rcpforms/.

      Yes I have seen this project. But its is based on 3.x framework and I don’t know if Google SWT Designer can manage that?

      >Thanks for the series of articles including that one in French. I believe this is the way to tie >the various layers of a good design using RCP.

      My goal of my articles was to describe the architecture that I love and meet people (like you) who critic (Pro/Cons)this architecture.

      Regards Angelo

  4. Jay Jonas
    octobre 22, 2012 à 1:06

    Thank you again, Angelo! I’ll take a look at your links!
    And I always come here for the good news!
    Regards, JJ.

  5. Jay Jonas
    mai 30, 2013 à 3:01

    Angelo, did you see these news?

    https://github.com/muuki88/e4GeminiJPA
    https://github.com/gkvas/gemini.dbaccess
    http://www.eclipse.org/forums/index.php/t/290891/

    Maybe it’s time to take a step forward with this its amazing architecture.
    Perhaps making it a standard for the access layer databases for RCP / RAP.

    I’m looking forward to the review focused on the Eclipse e4.

    Regards,
    JJ

    • juin 2, 2013 à 2:33

      Many thank’s Jay for your information.

      As I said you, my big problem with e4, is it doesn’t provide pure e4 Editor/View component
      (I would like avoid using 3x View/EditorPart).

      More I’m not sure that e4 RAP works great. So I’m waiting that those 2 topics are finished to write some articles.

      Regards Angelo

  6. Jay Jonas
    mai 30, 2013 à 5:52

    Also take a look to the
    https://bugs.eclipse.org/bugs/show_bug.cgi?id=372278

    Regards,
    JJ

  7. août 6, 2013 à 2:36

    hi Angelo,
    « deselect the RCP bundles (org.eclipse.ui, org.eclipse.ui.workbench, etc…), » Please can you help out with a list of these bundles, I see like 433 bundles and dont know which one to deselect except the 2 you mentioned.

    « Click on Next button and select com.springsource.org.apache.log4j (Log4J bundle) : » – Please I cant find where to select this after clicking next. When I imported your sample project, my worksapce says: « hOST BUNDLE ‘com.springsource.org.apache.log4j’ cannot be resolved » .
    Please help out urgently. My company just changed to ECLIPSE RAP and I found very few resources online.
    Kind regards

  8. août 7, 2013 à 7:07

    Hi Femi,

    The screenshots shows you RCP bundles (red line), but perhaps if you use E4, bundles are not the same.

    I think my provided launch doesn’t work with E4, it explains perhaps your problem with « hOST BUNDLE ‘com.springsource.org.apache.log4j’ cannot be resolved » .

    > Please help out urgently.

    Please stop saying that, everything is urgent.

    Using RAP is easy (see https://angelozerr.wordpress.com/2012/04/16/eclipse_spring_step10/) but not magic if you want support RCP and RAP both (RAP doesn’t support the all features of RCP).

    I suggets you to post your question about RAP to the RAP forum.

    Regards Angelo

  1. avril 6, 2012 à 2:08
  2. avril 6, 2012 à 3:58
  3. avril 16, 2012 à 1:24
  4. avril 16, 2012 à 10:11
  5. juin 18, 2012 à 7:31

Laisser un commentaire

Entrez vos coordonnées ci-dessous ou cliquez sur une icône pour vous connecter:

Logo WordPress.com

Vous commentez à l'aide de votre compte WordPress.com. Déconnexion / Changer )

Image Twitter

Vous commentez à l'aide de votre compte Twitter. Déconnexion / Changer )

Photo Facebook

Vous commentez à l'aide de votre compte Facebook. Déconnexion / Changer )

Photo Google+

Vous commentez à l'aide de votre compte Google+. Déconnexion / Changer )

Connexion à %s

%d blogueurs aiment cette page :