blob: b36495454eb55abf7f076ab9c95667d4a91a83e4 [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2012, 2014 - Lunifera GmbH (Gross Enzersdorf, Austria), Loetz GmbH&Co.KG (69115 Heidelberg, Germany)
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Information:
* Based on original sources of
* - org.vaadin.osgi.Activator from Chris Brind
* - com.c4biz.osgiutils.vaadin.equinox.shiro.ApplicationRegister from Cristiano Gaviao
*
* Contributors:
* Florian Pirchner - migrated to vaadin 7 and copied into org.eclipse.osbp namespace
*
*******************************************************************************/
package org.eclipse.osbp.vaaclipse.addons.app.webapp;
import java.util.Dictionary;
import java.util.Hashtable;
import javax.servlet.Filter;
import org.eclipse.equinox.http.servlet.ExtendedHttpService;
import org.eclipse.osbp.vaaclipse.addons.app.Activator;
import org.eclipse.osbp.vaaclipse.addons.app.common.Constants;
import org.eclipse.osbp.vaaclipse.addons.app.servlet.VaadinOSGiServlet;
import org.eclipse.osbp.vaaclipse.addons.app.servlet.WebResourcesHttpContext;
import org.osgi.service.cm.ConfigurationException;
import org.osgi.service.cm.ManagedService;
import org.osgi.service.component.ComponentFactory;
import org.osgi.service.http.HttpContext;
import com.vaadin.server.VaadinServlet;
import com.vaadin.server.VaadinSession;
/**
* This class is responsible for registering the {@link ComponentFactory} as a
* vaadin {@link VaadinSession}. It is a {@link ManagedService} so that it can
* receive properties which are then passed in to the {@link VaadinOSGiServlet}
* as init parameters, e.g. to enable production mode.
*/
@SuppressWarnings("deprecation")
public class VaadinWebApplicationRegister implements ManagedService {
/** The http. */
private final ExtendedHttpService http;
/** The servlet. */
private VaadinServlet servlet;
/** The filter. */
private Filter filter;
/** The resource base. */
private final String RESOURCE_BASE = "/VAADIN";
/** The web application. */
private VaadinWebApplication webApplication;
/** The current alias. */
private String currentAlias;
/**
* Instantiates a new vaadin web application register.
*
* @param http
* the http
* @param webApplication
* the web application
*/
public VaadinWebApplicationRegister(ExtendedHttpService http,
VaadinWebApplication webApplication) {
super();
this.http = http;
this.webApplication = webApplication;
}
/**
* Kill.
*/
public void kill() {
if (filter != null) {
http.unregisterFilter(filter);
filter = null;
}
if (currentAlias != null) {
try {
http.unregister(currentAlias);
http.unregister(RESOURCE_BASE);
} catch (java.lang.IllegalArgumentException e) {
// ignore in case alias was not found exception
}
}
if (servlet != null) {
servlet.destroy();
servlet = null;
}
}
/* (non-Javadoc)
* @see org.osgi.service.cm.ManagedService#updated(java.util.Dictionary)
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public void updated(Dictionary properties) throws ConfigurationException {
kill();
try {
if (properties == null) {
properties = new Hashtable();
}
// update the webapplication
//
webApplication.updated(properties);
// update the properties
//
properties.put(Constants.PROP_WIDGETSET,
webApplication.getWidgetsetName());
properties.put(Constants.PROP_PRODUCTION_MODE,
Boolean.toString(webApplication.isProductionMode()));
for (String propName : webApplication.getInitProperties()) {
properties.put(propName,
webApplication.getInitProperty(propName));
}
servlet = new VaadinOSGiServlet();
HttpContext defaultContext = new WebResourcesHttpContext(Activator
.getDefault().getBundle());
// http.registerResources(RESOURCE_BASE, RESOURCE_BASE,
// defaultContext);
currentAlias = webApplication.getAlias();
http.registerServlet(String.format("/%s", currentAlias), servlet,
properties, defaultContext);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}