Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Eclipse RCP Meets Spring: A Perfect Thick-Client Match : Page 3

Eclipse Rich Client Platform (RCP) is fast becoming the framework of choice for building thick client applications. Get a step-by-step guide to leveraging Eclipse RCP with Spring.


advertisement

Adding Spring Remoting to Your Application

Add Spring to your Eclipse rich client so that it will make requests of the StockTradeServer project from my previous article.

First of all, when developing Eclipse plug-ins/RCP applications, the recommended way of adding third-party libraries is through another plug-in. By doing this, you don't need to keep adding these third-party jars to each project you create. Instead, you can just make a dependency between your plug-in/RCP project and the third-party library project. However, this is easier said then done. If you are not familiar with Eclipse's classloader, read up on it. The gist is that you have to do a couple of extra steps to ensure that your classes can find each other between plug-in dependencies:

  1. Create a new Plug-in project in Eclipse. Call this new project SpringClient, and set the following values:

    ID = SpringClient Class = springClient.SpringClientPlugin



    After filling the Plug-in Properties, click Finish. You don't need any of the templates for the SpringClient plug-in project, as its main purpose is to hold spring libraries and any Spring-related service classes.

  2. In your unzipped spring-framework-1.2.8 distribution, you'll find the following jars:
    • spring-aop.jar – in the folder dist
    • spring-beans.jar – in the folder dist
    • spring-context.jar – in the folder dist
    • spring-core.jar – in the folder dist
    • spring-remoting.jar – in the folder dist
    • commons-logging.jar – in the folder lib\jakarta-commons
    • log4j-1.2.13.jar – in the folder lib\log4j

    Copy them all to the root of your SpringClient directory.

  3. In Eclipse's package explorer, right click on SpringClient to open the project properties. Select Java Build Path, click the Libraries tab, and add all those jars you just added via the Add JARs button. Make sure you export these libraries as well! Click the Order and Export tab, and check all the libraries (see Figure 9). By doing this, you ensure that when you make a project dependency on SpringClient the spring and log jars will be available as well. After you are done, click OK.

    Click to enlarge

    Figure 9. Export Third-Party Libraries
  4. Now you have to make changes to the SpringClient's manifest. In Package Explorer, expand SpringClient -> META-INF and open MANIFEST.MF. Click on the Runtime tab and click Add in the Classpath area. Select all the Spring jars and logging jars and click OK. Now, in the Exported Packages section, click Add. Select all the packages for exporting, and click OK (see Figure 10).

    Click to enlarge

    Figure 10. Adding Third-Party Libraries to Plug-In Classpath and Exporting Packages

    I mentioned previously that Eclipse's classloader can often cause problems. To help remedy this, click the MANIFEST.MF tab and add the following line:

    Eclipse-BuddyPolicy: registered

  5. Now, let's add the spring configuration files. In Package Explorer, go to the src directory, create a new file called applicationContext.xml, and add the following:

    Under the src directory, also create a new file called beanRefFactory.xml and add the following:

    In case you were wondering, yes these are the same spring config files that you used in the unit tests for the stocktradeserver project, except you have renamed applicationContext.xml.

  6. For the sake of brevity, copy classes from the stocktradeserver project to the src directory of SpringClient. Under the src directory of SpringClient, create packages stephenlum.services.stock and stephenlum.services.stock.dto.

    If you haven't already, download the source or refer to my previous article and copy class StockService.java under stephenlum.services.stock. Next, copy class StockDTO.java under stephenlum.services.stock.dto (see Figure 11).

    Click to enlarge

    Figure 11. Package Explorer as It Should Look After This Is Done
  7. Add code to class SpringClientPlugin to load Spring's application context. You will add this code to the constructor. The following is the new SpringClientPlugin class:

    package springClient; import org.eclipse.jface.resource.ImageDescriptor; import org.eclipse.ui.plugin.AbstractUIPlugin; import org.osgi.framework.BundleContext; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.access.BeanFactoryLocator; import org.springframework.beans.factory.access.BeanFactoryReference; import org.springframework.beans.factory.access.SingletonBeanFactoryLocator; /** * The main plugin class to be used in the desktop. */ public class SpringClientPlugin extends AbstractUIPlugin { private BeanFactory beanFactory; //The shared instance. private static SpringClientPlugin plugin; /** * The constructor. */ public SpringClientPlugin() { plugin = this; BeanFactoryLocator beanFactoryLocator = SingletonBeanFactoryLocator.getInstance(); BeanFactoryReference beanFactoryReference = beanFactoryLocator.useBeanFactory("ctx"); beanFactory = beanFactoryReference.getFactory(); } /** * This method is called upon plug-in activation */ public void start(BundleContext context) throws Exception { super.start(context); } /** * This method is called when the plug-in is stopped */ public void stop(BundleContext context) throws Exception { super.stop(context); plugin = null; } /** * Returns the shared instance. */ public static SpringClientPlugin getDefault() { return plugin; } /** * Returns an image descriptor for the image file at the given * plug-in relative path. * * @param path the path * @return the image descriptor */ public static ImageDescriptor getImageDescriptor(String path) { return AbstractUIPlugin.imageDescriptorFromPlugin("SpringClient", path); } public BeanFactory getBeanFactory() { return beanFactory; } }

  8. Finally, add the dependency so that project EclipseTradeClient depends on your new plug-in project SpringClient. In project EclipseTradeClient, open plugin.xml and click the Dependencies tab. In the Required Plug-ins section, click Add, select SpringClient (1.0.0), and click OK (see Figure 12).

    Click to enlarge

    Figure 12. Add SpringClient as a Required Plug-In

    Now you have to make an addition to the EclipseTradeClient manifest file with regards to Eclipse's buddy policy. In file plugin.xml, click the MANIFEST.MF tab and add the following entry:

    Eclipse-RegisterBuddy: SpringClient



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap