Skip to content

Commit

Permalink
ACE-473 - add servlet initialization parameters:
Browse files Browse the repository at this point in the history
- propagate `init.*` service properties as servlet initialization
  properties similar as Felix HTTP whiteboard implementation does.



git-svn-id: https://svn.apache.org/repos/asf/ace/trunk@1652847 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
Jan Willem Janssen committed Jan 18, 2015
1 parent 0869556 commit ac0de4f
Showing 1 changed file with 62 additions and 39 deletions.
101 changes: 62 additions & 39 deletions org.apache.ace.http/src/org/apache/ace/http/listener/Activator.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
*/
package org.apache.ace.http.listener;

import java.util.Dictionary;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Map;
import java.util.Set;

Expand All @@ -34,22 +36,26 @@
import org.osgi.service.log.LogService;

/**
* Service responsible for registrating Servlets at HttpServices.<p>
* Service responsible for registrating Servlets at HttpServices.
* <p>
*
* When a Servlet is being added or removed, the callback methods in this class are being
* called via the DependencyManager. A Servlet is being added to all HttpServices currently
* available or removed from all available HttpServices.<p>
* When a Servlet is being added or removed, the callback methods in this class are being called via the
* DependencyManager. A Servlet is being added to all HttpServices currently available or removed from all available
* HttpServices.
* <p>
*
* In case a HttpService is being added or removed, other callback methods are being called
* via the DependencyManager. When a HttpService is added, all previously registered Servlets
* are being registered to this new HttpService. In case of removal, all Servlet endpoints are
* being removed from the HttpService that is going to be removed.<p>
* In case a HttpService is being added or removed, other callback methods are being called via the DependencyManager.
* When a HttpService is added, all previously registered Servlets are being registered to this new HttpService. In case
* of removal, all Servlet endpoints are being removed from the HttpService that is going to be removed.
* <p>
*/
public class Activator extends DependencyActivatorBase {
private static final String INIT_PREFIX = "init.";

private volatile LogService m_log; // injected
private final Set<ServiceReference> m_httpServices = new HashSet<ServiceReference>();
private final Map<ServiceReference, String> m_servlets = new HashMap<ServiceReference, String>();

private volatile LogService m_log; // injected
private BundleContext m_context;

@Override
Expand All @@ -71,21 +77,25 @@ public synchronized void init(BundleContext context, DependencyManager manager)
}

/**
* Callback method used in case a Servlet is being added. This Servlet is being added
* to all available HttpServices under the endpoint configured via the Configurator.
* Callback method used in case a Servlet is being added. This Servlet is being added to all available HttpServices
* under the endpoint configured via the Configurator.
*
* @param ref reference to the Servlet
* @param ref
* reference to the Servlet
*/
public synchronized void addServlet(ServiceReference ref) {
// register servlet to all HttpServices
String endpoint = (String)ref.getProperty(HttpConstants.ENDPOINT);
String endpoint = (String) ref.getProperty(HttpConstants.ENDPOINT);
m_servlets.put(ref, endpoint);
Servlet servlet = (Servlet)m_context.getService(ref);

Servlet servlet = (Servlet) m_context.getService(ref);
Dictionary<String, Object> initParams = getInitParams(ref);

for (ServiceReference reference : m_httpServices) {
HttpService httpService = (HttpService) m_context.getService(reference);
try {
if ((httpService != null) && (endpoint != null) && (servlet != null)) {
httpService.registerServlet(endpoint, servlet, null, null);
httpService.registerServlet(endpoint, servlet, initParams, null);
}
else {
m_log.log(LogService.LOG_WARNING, "Unable to register servlet with endpoint '" + endpoint + "'");
Expand All @@ -97,26 +107,37 @@ public synchronized void addServlet(ServiceReference ref) {
}
}

private Dictionary<String, Object> getInitParams(ServiceReference ref) {
Dictionary<String, Object> initParams = new Hashtable<String, Object>();
for (String key : ref.getPropertyKeys()) {
if (key.startsWith(INIT_PREFIX)) {
initParams.put(key.substring(INIT_PREFIX.length()), ref.getProperty(key));
}
}
return initParams;
}

/**
* Callback method used in case a Servlet is being changed. This Servlet is removed
* and then added again.
* Callback method used in case a Servlet is being changed. This Servlet is removed and then added again.
*
* @param ref reference to the Servlet
* @param ref
* reference to the Servlet
*/
public synchronized void changeServlet(ServiceReference ref) {
removeServlet(ref, m_servlets.get(ref));
addServlet(ref);
}

/**
* Callback method used in case a Servlet is being removed. This Servlet is being removed
* from all available HttpServices using the endpoint configured via the Configurator.
* Callback method used in case a Servlet is being removed. This Servlet is being removed from all available
* HttpServices using the endpoint configured via the Configurator.
*
* @param ref reference to the Servlet
* @param ref
* reference to the Servlet
*/
public synchronized void removeServlet(ServiceReference ref) {
// remove servlet from all HttpServices
String endpoint = (String)ref.getProperty(HttpConstants.ENDPOINT);
String endpoint = (String) ref.getProperty(HttpConstants.ENDPOINT);
removeServlet(ref, endpoint);
}

Expand All @@ -136,21 +157,23 @@ private void removeServlet(ServiceReference ref, String endpoint) {
}

/**
* Callback method used in case a HttpService is being added. To this Service all previously
* registered Servlet are added under the endpoints of the Servlets (which are configured
* via the Configurator).
* Callback method used in case a HttpService is being added. To this Service all previously registered Servlet are
* added under the endpoints of the Servlets (which are configured via the Configurator).
*
* @param ref reference to the Service
* @param ref
* reference to the Service
*/
public synchronized void addHttpService(ServiceReference ref, HttpService httpService) {
m_httpServices.add(ref);
// register all servlets to this new HttpService
for (ServiceReference reference : m_servlets.keySet()) {
Servlet servlet = (Servlet)m_context.getService(reference);
String endpoint = (String)reference.getProperty(HttpConstants.ENDPOINT);
Servlet servlet = (Servlet) m_context.getService(reference);
String endpoint = (String) reference.getProperty(HttpConstants.ENDPOINT);
if ((servlet != null) && (endpoint != null)) {
Dictionary<String, Object> initParams = getInitParams(reference);

try {
httpService.registerServlet(endpoint, servlet, null, null);
httpService.registerServlet(endpoint, servlet, initParams, null);
}
catch (Exception e) {
m_log.log(LogService.LOG_WARNING, "Already registered under existing endpoint", e);
Expand All @@ -160,11 +183,11 @@ public synchronized void addHttpService(ServiceReference ref, HttpService httpSe
}

/**
* Callback method used in case a HttpService is being removed. From this Service all previously
* registered Servlet are removed using the endpoints of the Servlets (which are configured
* via the Configurator).
* Callback method used in case a HttpService is being removed. From this Service all previously registered Servlet
* are removed using the endpoints of the Servlets (which are configured via the Configurator).
*
* @param ref reference to the Service
* @param ref
* reference to the Service
*/
public synchronized void removeHttpService(ServiceReference ref, HttpService httpService) {
m_httpServices.remove(ref);
Expand All @@ -175,7 +198,7 @@ public synchronized void removeHttpService(ServiceReference ref, HttpService htt
@Override
public synchronized void destroy(BundleContext context, DependencyManager arg1) throws Exception {
for (ServiceReference httpRef : m_httpServices) {
HttpService httpService = (HttpService)m_context.getService(httpRef);
HttpService httpService = (HttpService) m_context.getService(httpRef);
if (httpService != null) {
unregisterEndpoints(httpService);
}
Expand All @@ -186,14 +209,14 @@ public synchronized void destroy(BundleContext context, DependencyManager arg1)
}

/**
* Unregisters all Servlets (via their endpoints) from the HttpService being passed to
* this method.
* Unregisters all Servlets (via their endpoints) from the HttpService being passed to this method.
*
* @param httpService the HttpService that is being unregistered
* @param httpService
* the HttpService that is being unregistered
*/
private void unregisterEndpoints(HttpService httpService) {
for (ServiceReference reference : m_servlets.keySet()) {
String endpoint = (String)reference.getProperty(HttpConstants.ENDPOINT);
String endpoint = (String) reference.getProperty(HttpConstants.ENDPOINT);
if (endpoint != null) {
try {
httpService.unregister(endpoint);
Expand All @@ -204,4 +227,4 @@ private void unregisterEndpoints(HttpService httpService) {
}
}
}
}
}

0 comments on commit ac0de4f

Please sign in to comment.