Didn't find one, so I thought I could write a post.
Download:
Eclipse 3.4 RC3
Spring Dynamic Modules for OSGi(tm) 1.0.2
Now create a new Plug-in-Project, using the settings below:
As a next step you have to import the Spring DM Plug-ins.
File->Import->Plug-in Development->Plug-ins and Fragments.
Point the plug-in location to the extracted "spring-osgi-1.0.2/dist" directory.
On the next page, add the following plug-ins:
- org.springframework.bundle.osgi.core
- org.springframework.bundle.osgi.extender
- org.springframework.bundle.osgi.io
Do the same as above, but instead of the "dist" directory select the "spring-osgi-1.0.2/lib" dir and select those plugins:
- org.springframework.bundle.spring.aop
- org.springframework.bundle.spring.beans
- org.springframework.bundle.spring.context
- org.springframework.bundle.spring.core
- org.springframework.osgi.aopalliance.osgi
Spring DM has some classloading problems with the existing plugin contained in the eclipse platform.
New->Plug-in Development->Plug-in from existing JAR archives.
Add External...
Now if you have the Spring framework including dependencies on your machine select the commons-logging.jar located at "spring-framework-2.5.x/lib/jakarta-commons", otherwise download the jar here.
Name the plugin "org.apache.commons.logging
Ok, so now your workspace is ready.
Let's create a Spring service.
package swissdev.springdm; public interface IMyService { String getSomething(); }
package swissdev.springdm; public class MyService implements IMyService { @Override public String getSomething() { return "something"; } public void start() { System.out.print("start service"); } public void stop() { System.out.print("stop service"); } }META-INF/spring/applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="myService" class="swissdev.springdm.MyService" init-method="start" destroy-method="stop"/> </beans>
META-INF/spring/applicationContext-osgi.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:osgi="http://www.springframework.org/schema/osgi" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi.xsd"> <osgi:service id="myServiceOsgi" ref="myService" interface="swissdev.springdm.IMyService"/> </beans>
Our application is basically ready to launch, so lets do that.
Right click on the swissdev.springdm plug-in: Run-As -> Eclipse Application.
A simple window containing a view is launched, nothing special.
Open the run configuration: Run -> Run Configurations...
Select your "swissdev.springdm.application" launch config and change to the "Arguments" tab.
Add "-console" to the Program arguments.
Switch to the Plug-ins tab and select all the "Workspace" plug-ins, hit the "Add Required Plug-ins" button and run again.
Type ss in the console, you will get the following output:
So our Spring plug-ins are not active.
Open the run configurations again and switch to the "Configurations" tab, change the Configurations File option to "Use existing config.ini file as a template" and enter ${workspace_loc}/swissdev.springdm/config.ini.
We can get the default generated config.ini in our workspace.
It's located at /.metadata/.plugins/org.eclipse.pde.core/swissdev.springdm.application/config.ini, copy it to your plug-in.
Right click on the ini file and select Open With -> Text Editor.
Ctrl+f Find: extender
You should find ....org.springframework.bundle.osgi.extender add @start at the end:
org.springframework.bundle.osgi.extender@start
Do the same with the swissdev.springdm plugin.
Launch again, you will see a lot of output and somewhere between "start service", thats the output we defined in the MyService class.
Spring DM is working!
To use our service we first need to modify our Activator:
private BundleContext context; public BundleContext getContext() { return context; } public void start(BundleContext context) throws Exception { super.start(context); plugin = this; this.context = context; }
Open the generated View class and edit the createPartControl method:
public void createPartControl(Composite parent) { viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL); viewer.setContentProvider(new ViewContentProvider()); viewer.setLabelProvider(new ViewLabelProvider()); viewer.setInput(getViewSite()); viewer.addDoubleClickListener(new IDoubleClickListener() { @Override public void doubleClick(DoubleClickEvent event) { ServiceTracker tracker = new ServiceTracker(Activator.getDefault().getContext(), IMyService.class.getName(),null); tracker.open(); IMyService service = (IMyService) tracker.getService(); System.out.println(service.getSomething()); } }); }
Launch again and double click one of the itms in the window, in the console there should appear an "something" output.
That was it....