RSS

Search Engine

Saturday, October 23, 2010

Services

11.1. Using services

As explained earlier you can use the Eclipse e4 dependency injection mechanism to inject your own OSGi services into your model elements. This removes the need for you to create your own singletons. In the following we will use OSGi declarative services to define a service which maintains our current todo items. You could use other approaches but declarative services are a simple way to creating services. For details on creating DS Services please see OSGi Tutorial .

A service in OSGi is defined by a standard Java class or interface. It is common practice to define the service via a bundle which only contains the interface definition. This allows to change the implementation of the service via a different bundle.

11.2. Todo model and service interface

Create a plugin project "de.vogella.e4.todo.model" which will contains the model and the definition of the service interface. You do not need an activator. Create also the package "de.vogella.e4.todo.model". Do not use a template. Select "plugin.xml" and the "Runtime" tab. Add "de.vogella.e4.todo.model" to the exported packages.

Create the following data model.

    
package de.vogella.e4.todo.model;

public class Todo {
private String summary="";
private String description="";

public Todo(String summary) {
this.summary = summary;
}

public Todo(String summary, String description){
this.summary = summary;
this.description = description;

}

public String getSummary() {
return summary;
}
public void setSummary(String summary) {
this.summary = summary;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}

@Override
public String toString() {
return "Todo: " + summary;
}
}

Create the following interface "ITodoModel".

    
package de.vogella.e4.todo.model;

import java.util.List;

public interface ITodoModel {
List getTodos();
}

11.3. Todo model service

Create a plugin project "de.vogella.e4.todo.model.servicemock". Do not use a template. You do not need an activator. Create the package "de.vogella.e4.todo.model.servicemock". Add "de.vogella.e4.todo.model" as a dependency. Also set the overview tab "MANIFEST.MF" and set the flag "Activate this plugin when one of its classed id loaded".

Create the Folder "OSGI-INF" in your project. Select the new folder, right-click on it and select New -> Other -> Plug-in Development -> Component Definition. Create the file with the following content.

    








Create the implementation class for this service.

    
package de.vogella.e4.todo.model.servicemock;

import java.util.ArrayList;
import java.util.List;

import de.vogella.e4.todo.model.ITodoModel;
import de.vogella.e4.todo.model.Todo;

public class TodoMockModel implements ITodoModel {

@Override
public List getTodos() {
List todos = new ArrayList();
Todo todo = new Todo("Write more about e4");
todos.add(todo);
todo = new Todo("Android", "Write a widget.");
todos.add(todo);
return todos;
}

}

Also make sure that the entry "Service-Component: OSGI-INF/component.xml" is available in "MANIFEST.MF".

    
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Servicemock
Bundle-SymbolicName: de.vogella.e4.todo.model.servicemock
Bundle-Version: 1.0.0.qualifier
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Require-Bundle: de.vogella.e4.todo.model;bundle-version="1.0.0"
Service-Component: OSGI-INF/component.xml

11.4. Using the model service

Add the two new bundles to your runtime configuration and add a dependency to "de.vogella.e4.todo.model" in the plugin "de.vogella.e4.todo". Change your view so that the new services gets injected into your view. Currently we will just verify that the service was injected by displaying the total number of todos.

    
package de.vogella.e4.todo.parts;

import javax.annotation.PostConstruct;
import javax.inject.Inject;

import org.eclipse.e4.core.contexts.IEclipseContext;
import org.eclipse.e4.core.services.log.Logger;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

import de.vogella.e4.todo.model.ITodoModel;

public class TodoOverview {

@Inject
private Logger logger;
@Inject
private IEclipseContext context;
@Inject
private ITodoModel model;

// PostConstruct ensures that the dependency injection has been finished
// this this method is called

private final Composite parent;

// e4 will inject the composite "parent" into the view
@Inject
public TodoOverview(Composite parent) {
// the following is standard SWT Stuff
this.parent = parent;
}

@PostConstruct
public void buildUI() {
logger.info("Start building UI");
final GridLayout layout = new GridLayout(2, false);
parent.setLayout(layout);
createLable("Number of Todo entries:");
createText(String.valueOf(model.getTodos().size()));

}

// Standard SWT Stuff
private void createLable(String text) {
Label label = new Label(parent, SWT.NONE);
label.setText(text);
GridData gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
gridData.horizontalIndent = 20;
label.setLayoutData(gridData);
}

// Standard SWT Stuff
private void createText(String text) {
GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
Text textField = new Text(parent, SWT.NONE);
textField.setText(text);
textField.setLayoutData(gridData);
}

}


0 comments:

Post a Comment