FELIX-4060 : Implement HTTP Service Update (RFC-189)
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1655665 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/http/base/src/main/java/org/apache/felix/http/base/internal/HttpServiceController.java b/http/base/src/main/java/org/apache/felix/http/base/internal/HttpServiceController.java
index 34d5903..535ad38 100644
--- a/http/base/src/main/java/org/apache/felix/http/base/internal/HttpServiceController.java
+++ b/http/base/src/main/java/org/apache/felix/http/base/internal/HttpServiceController.java
@@ -30,9 +30,12 @@
import org.apache.felix.http.base.internal.listener.ServletRequestAttributeListenerManager;
import org.apache.felix.http.base.internal.listener.ServletRequestListenerManager;
import org.apache.felix.http.base.internal.service.HttpServiceFactory;
+import org.apache.felix.http.base.internal.service.HttpServiceRuntimeImpl;
+import org.apache.felix.http.base.internal.whiteboard.ExtenderManager;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.http.HttpService;
+import org.osgi.service.http.runtime.HttpServiceRuntime;
public final class HttpServiceController
{
@@ -67,7 +70,9 @@
private final HttpSessionAttributeListenerManager sessionAttributeListener;
private final boolean sharedContextAttributes;
private final HttpServicePlugin plugin;
- private ServiceRegistration serviceReg;
+ private volatile ExtenderManager manager;
+ private volatile ServiceRegistration serviceReg;
+ private volatile ServiceRegistration<HttpServiceRuntime> runtimeReg;
public HttpServiceController(BundleContext bundleContext)
{
@@ -138,13 +143,23 @@
HttpServiceFactory factory = new HttpServiceFactory(servletContext, this.registry, this.contextAttributeListener, this.sharedContextAttributes);
this.serviceReg = this.bundleContext.registerService(ifaces, factory, this.serviceProps);
+ this.manager = new ExtenderManager((HttpService)factory.getService(this.bundleContext.getBundle(), this.serviceReg), this.bundleContext);
+
+ this.runtimeReg = this.bundleContext.registerService(HttpServiceRuntime.class, new HttpServiceRuntimeImpl(), null);
}
public void unregister()
{
- if (this.serviceReg == null)
+ if ( this.manager != null )
{
- return;
+ this.manager.close();
+ this.manager = null;
+ }
+
+ if ( this.runtimeReg != null )
+ {
+ this.runtimeReg.unregister();
+ this.runtimeReg = null;
}
this.sessionAttributeListener.close();
@@ -152,16 +167,20 @@
this.contextAttributeListener.close();
this.requestListener.close();
this.requestAttributeListener.close();
+
this.plugin.unregister();
- try
+ if ( this.serviceReg != null )
{
- this.serviceReg.unregister();
- this.registry.removeAll();
- }
- finally
- {
- this.serviceReg = null;
+ try
+ {
+ this.serviceReg.unregister();
+ this.registry.removeAll();
+ }
+ finally
+ {
+ this.serviceReg = null;
+ }
}
}
diff --git a/http/base/src/main/java/org/apache/felix/http/base/internal/handler/AbstractHandler.java b/http/base/src/main/java/org/apache/felix/http/base/internal/handler/AbstractHandler.java
index 008732e..ad68e2a 100644
--- a/http/base/src/main/java/org/apache/felix/http/base/internal/handler/AbstractHandler.java
+++ b/http/base/src/main/java/org/apache/felix/http/base/internal/handler/AbstractHandler.java
@@ -16,15 +16,18 @@
*/
package org.apache.felix.http.base.internal.handler;
+import java.util.Dictionary;
+import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.concurrent.atomic.AtomicInteger;
+
import javax.servlet.Filter;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import org.apache.felix.http.base.internal.context.ExtServletContext;
-import java.util.*;
-import java.util.concurrent.atomic.AtomicInteger;
-
public abstract class AbstractHandler
{
private final static AtomicInteger ID = new AtomicInteger();
@@ -61,6 +64,10 @@
public abstract void init() throws ServletException;
+ /**
+ * TODO - We can remove this, once we switched to {@link #setInitParams(Map)}
+ * @param map
+ */
public final void setInitParams(Dictionary map)
{
this.initParams.clear();
@@ -82,6 +89,17 @@
}
}
+ public final void setInitParams(Map<String, String> map)
+ {
+ this.initParams.clear();
+ if (map == null)
+ {
+ return;
+ }
+
+ this.initParams.putAll(map);
+ }
+
protected final ExtServletContext getContext()
{
return this.context;
diff --git a/http/base/src/main/java/org/apache/felix/http/base/internal/service/HttpServiceImpl.java b/http/base/src/main/java/org/apache/felix/http/base/internal/service/HttpServiceImpl.java
index f591b1c..99b9f64 100644
--- a/http/base/src/main/java/org/apache/felix/http/base/internal/service/HttpServiceImpl.java
+++ b/http/base/src/main/java/org/apache/felix/http/base/internal/service/HttpServiceImpl.java
@@ -109,31 +109,10 @@
filterInfo.name = filter.getClass().getName();
}
- ExtServletContext servletContext = getServletContext(filterInfo.context);
-
- try
- {
- FilterHandler handler = new FilterHandler(servletContext, filter, filterInfo);
-
- synchronized (this)
- {
- if (this.localFilters.add(filter))
- {
- this.handlerRegistry.addFilter(handler);
- }
- }
- }
- catch (Exception e)
- {
- if (e instanceof ServletException)
- {
- throw (ServletException) e;
- }
- else
- {
- throw new ServletException("Failed to register filter " + filterInfo.name, e);
- }
- }
+ FilterHandler handler = new FilterHandler(getServletContext(filterInfo.context), filter, filterInfo);
+ handler.setInitParams(filterInfo.initParams);
+ this.handlerRegistry.addFilter(handler);
+ this.localFilters.add(filter);
}
@Override
@@ -188,44 +167,13 @@
servletInfo.name = servlet.getClass().getName();
}
- ExtServletContext servletContext = getServletContext(servletInfo.context);
-
- try
- {
- ServletHandler handler = new ServletHandler(servletContext, servlet, servletInfo);
-
- synchronized (this)
- {
- if (this.localServlets.add(servlet))
- {
- this.handlerRegistry.addServlet(handler);
- if (servletInfo.errorPage != null && servletInfo.errorPage.length != 0)
- {
- for (String errorPage : servletInfo.errorPage)
- {
- this.handlerRegistry.addErrorServlet(errorPage, handler);
- }
- }
- }
- }
- }
- catch (Exception e)
- {
- if (e instanceof ServletException)
- {
- throw (ServletException) e;
- }
- else if (e instanceof NamespaceException)
- {
- throw (NamespaceException) e;
- }
- else
- {
- throw new ServletException("Failed to register servlet " + servletInfo.name, e);
- }
- }
+ ServletHandler handler = new ServletHandler(getServletContext(servletInfo.context), servlet, servletInfo);
+ handler.setInitParams(servletInfo.initParams);
+ this.handlerRegistry.addServlet(handler);
+ this.localServlets.add(servlet);
}
+ @Override
public void registerServlet(String alias, Servlet servlet, Dictionary initParams, HttpContext context) throws ServletException, NamespaceException
{
if (servlet == null)
@@ -243,6 +191,7 @@
this.localServlets.add(servlet);
}
+ @Override
public void unregister(String alias)
{
final Servlet servlet = this.handlerRegistry.getServletByAlias(alias);
@@ -330,7 +279,7 @@
this.localServlets.remove(servlet);
}
}
-
+
private boolean isAliasValid(String alias)
{
if (alias == null)
diff --git a/http/base/src/test/java/org/apache/felix/http/base/internal/handler/AbstractHandlerTest.java b/http/base/src/test/java/org/apache/felix/http/base/internal/handler/AbstractHandlerTest.java
index 5e97af7..30df83e 100644
--- a/http/base/src/test/java/org/apache/felix/http/base/internal/handler/AbstractHandlerTest.java
+++ b/http/base/src/test/java/org/apache/felix/http/base/internal/handler/AbstractHandlerTest.java
@@ -16,6 +16,7 @@
*/
package org.apache.felix.http.base.internal.handler;
+import java.util.Dictionary;
import java.util.Hashtable;
import org.apache.felix.http.base.internal.context.ExtServletContext;
@@ -51,7 +52,7 @@
AbstractHandler handler = createHandler();
Assert.assertEquals(0, handler.getInitParams().size());
- Hashtable<String, String> map = new Hashtable<String, String>();
+ Dictionary<String, String> map = new Hashtable<String, String>();
map.put("key1", "value1");
handler.setInitParams(map);