Looking at various sources on the internet, I’m a little confused as to what the current practice is of using Vaadin 7 in an OSGi environment, more specifically, using Vaadin in an embedded Jetty server which to is embedded in an OSGi environment.
A few months back I got the current code to work:
== WEB SERVER ==
[font=Courier New]
SelectChannelConnector connector = new SelectChannelConnector();
connector.setPort(9080);
connector.setMaxIdleTime(30000);
I’ve kinda looked at the link provided and the package at github before. My first impression of the github package looked overly complicated - I’ll try looking at it again in the future if that is the only source of information for using Vaadin 7 in an OSGi environment, unfortunately I don’t have time to look at it for the current project so Vaadin 7 wiil have to sit out for this one.
As a proof of concept I got the following “Hello World” application to work.
I have not done extensive testing but constructive criticisms and suggestions are more than welcome.
[b]
Create a class that extends VaadinServlet
[/b]
public final class VaadinOSGiServlet extends VaadinServlet {
private static final long serialVersionUID = -6803764941423184854L;
@Override
protected VaadinServletService createServletService(final DeploymentConfiguration deploymentConfiguration) {
VaadinOSGiServletService servletService = new VaadinOSGiServletService(this, deploymentConfiguration);
return servletService;
}
}
[b]
Create a class that extends VaadinServletService
[/b]
public final class VaadinOSGiServletService extends VaadinServletService {
private static final long serialVersionUID = -6452889839000802542L;
public VaadinOSGiServletService(final VaadinServlet servlet, final DeploymentConfiguration deploymentConfiguration) {
super(servlet, deploymentConfiguration);
}
@Override
protected VaadinSession createVaadinSession(final VaadinRequest request) throws ServiceException {
VaadinOSGiSession session = new VaadinOSGiSession(this);
return session;
}
}
[b]
Create a class that extends VaadinSession
[/b]
public final class VaadinOSGiSession extends VaadinSession {
private static final long serialVersionUID = -7624953346675752761L;
private final List<UIProvider> uiProviders;
public VaadinOSGiSession(VaadinService service) {
super(service);
uiProviders = new ArrayList<UIProvider>() {
private static final long serialVersionUID = -9193410632996461485L;
{
add(OSGiUIProvider.instance());
}
};
}
@Override
public List<UIProvider> getUIProviders() {
return uiProviders;
}
}
[b]
Create a class that extends UIProvider
[/b]
public final class OSGiUIProvider extends UIProvider {
private static final long serialVersionUID = 1451931523729856181L;
private static final OSGiUIProvider INSTANCE = new OSGiUIProvider();
private OSGiUIProvider() {}
public static OSGiUIProvider instance() {
return INSTANCE;
}
@Override
public Class<? extends UI> getUIClass(final UIClassSelectionEvent event) {
return Greeter.class;
}
@Override
public UI createInstance(final UICreateEvent e) {
UI ui = new Greeter();
return ui;
}
}
[b]
Create a class that extends UI
[/b]
public final class Greeter extends UI {
private static final long serialVersionUID = -7333618595654346783L;
@Override
protected void init(final VaadinRequest request) {
final VerticalLayout view = new VerticalLayout();
final Label label = new Label("Hello World");
view.addComponent(label);
setContent(view);
}
}
[b]
Register the VaadinOSGiServlet (usging jetty web server)
[/b]
final WebAppContext context = new WebAppContext("webapps/greeter", "/");
context.setDefaultsDescriptor(null);
context.addServlet(new ServletHolder(VaadinOSGiServlet.class),"/*");
[b]
Under the directory that was registered in the WebAppContext class create the following structure:
[/b]
webapps
|__greeter
|__VAADIN
| |__themes (extracted from vaadin-themes-7.0.0.jar)
| |__widgetsets (extracted from vaadin-client-compiled-7.0.0.jar)
| |__vaadinBootstrap.js (extracted from vaadin-server-7.0.0.jar)
|__WEB-INF
|__web.xml
I’m not certain if this would work, but it might be possible to simplify this so that you don’t need custom VaadinOSGiServletService and VaadinOSGiSession classes.
Instead, you could register a SessionInitListener in VaadinOSGiServlet.init(…), and do event.getSession().addUIProvider(OSGiUIProvider.instance()) in sessionInit(SessionInitEvent) of the listener.
If you need to eliminate other UIProviders, it gets a little more complicated.