Skip to content

Instantly share code, notes, and snippets.

@brindy
Created February 11, 2010 15:09
Show Gist options
  • Save brindy/301602 to your computer and use it in GitHub Desktop.
Save brindy/301602 to your computer and use it in GitHub Desktop.
package com.vaadin.osgi;
import java.util.Dictionary;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Map;
import java.util.Set;
import javax.servlet.http.HttpServlet;
import org.osgi.service.component.ComponentFactory;
import org.osgi.service.http.HttpService;
import aQute.bnd.annotation.component.Activate;
import aQute.bnd.annotation.component.Component;
import aQute.bnd.annotation.component.Deactivate;
import aQute.bnd.annotation.component.Reference;
import com.vaadin.Application;
/**
* This class runs as an OSGi component and looks for OSGi component factories
* with their <code>component.factory</code> property set to
* <code>vaadin.app</code>.
* <p/>
*
* The component factory name (specified as <code>component.name</code>) is used
* as the alias to register an instance of {@link VaadinOSGiServlet} which then
* uses the component factory to create an instance of {@link Application}. For
* example, if <code>component.name</code> was set to myapp, the servlet would
* be registered at <code>http://<em>localhost:8080</em>/myapp</code>.
*
* <p/>
* Properties registered against the component factory are passed in to the
* servlet as init params. Thus to make your application production safe specify
* productionMode=true as a property of your application component.
*
* @author brindy
*/
@Component(enabled = true, properties = { "productionMode=false" })
public class VaadinOSGiApplicationManager {
private HttpService httpService;
private Map<String, HttpServlet> servlets = new HashMap<String, HttpServlet>();
private Set<Runnable> waiting = new HashSet<Runnable>();
private Dictionary<String, String> initParams;
private boolean started;
@Activate
public void start(Map<?, ?> properties) {
System.out.println("VaadinOSGiApplicationManager.start: " + properties);
String productionMode = (String) properties.get("productionMode");
if (null != productionMode) {
initParams = new Hashtable<String, String>();
initParams.put("productionMode", productionMode);
}
synchronized (waiting) {
for (Runnable run : waiting) {
run.run();
}
waiting.clear();
}
started = true;
}
@Deactivate
public void stop() {
started = false;
initParams = null;
}
@Reference
public void bindHttpService(HttpService service) {
this.httpService = service;
}
@Reference(type = '*', target = "(component.factory=vaadin.app)", unbind = "applicationRemoved", dynamic = true)
public void applicationRegistered(ComponentFactory factory,
Map<String, Object> properties) throws Exception {
System.out.println(properties);
final String path = "/" + (String) properties.get("component.name");
System.out.println("New Vaadin context : " + path);
final HttpServlet servlet = new VaadinOSGiServlet(factory);
servlets.put(path, servlet);
System.out
.println("VaadinOSGiApplicationManager.bindComponentFactory: initParams="
+ initParams);
Runnable run = new Runnable() {
@Override
public void run() {
try {
httpService
.registerServlet(path, servlet, initParams, null);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
};
if (started) {
run.run();
} else {
synchronized (waiting) {
waiting.add(run);
}
}
}
public void applicationRemoved(ComponentFactory factory,
Map<String, Object> properties) {
String path = "/" + (String) properties.get("component.name");
System.out.println("Removing Vaadin context : " + path);
httpService.unregister(path);
servlets.remove(path).destroy();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment