FELIX-5207 : Flexible Customizer for 'Enable Proxy/Load Balancer Connection'. Apply slightly modified patch from Antonio Sanso

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1734777 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/http/jetty/src/main/java/org/apache/felix/http/jetty/internal/JettyActivator.java b/http/jetty/src/main/java/org/apache/felix/http/jetty/internal/JettyActivator.java
index 5e8bb90..a7c9da9 100644
--- a/http/jetty/src/main/java/org/apache/felix/http/jetty/internal/JettyActivator.java
+++ b/http/jetty/src/main/java/org/apache/felix/http/jetty/internal/JettyActivator.java
@@ -20,6 +20,7 @@
 import java.util.Hashtable;
 
 import org.apache.felix.http.base.internal.AbstractHttpActivator;
+import org.apache.felix.http.jetty.LoadBalancerCustomizerFactory;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.Constants;
 import org.osgi.framework.ServiceFactory;
@@ -30,6 +31,7 @@
     private JettyService jetty;
 
     private ServiceRegistration<?> metatypeReg;
+    private ServiceRegistration<LoadBalancerCustomizerFactory> loadBalancerCustomizerFactoryReg;
 
     @Override
     protected void doStart() throws Exception
@@ -58,6 +60,29 @@
                 }, properties);
         this.jetty = new JettyService(getBundleContext(), getDispatcherServlet(), getEventDispatcher(), getHttpServiceController());
         this.jetty.start();
+
+        final Dictionary<String, Object> propertiesCustomizer = new Hashtable<String, Object>();
+        propertiesCustomizer.put(Constants.SERVICE_DESCRIPTION, "Load Balancer Customizer Factory for Jetty Http Service");
+        propertiesCustomizer.put(Constants.SERVICE_VENDOR, "The Apache Software Foundation");
+        loadBalancerCustomizerFactoryReg = this.getBundleContext().registerService(LoadBalancerCustomizerFactory.class,
+                new ServiceFactory<LoadBalancerCustomizerFactory>()
+                {
+
+                    @Override
+                    public LoadBalancerCustomizerFactory getService(final Bundle bundle,
+                            final ServiceRegistration<LoadBalancerCustomizerFactory> registration)
+                    {
+                        return new ForwardedRequestCustomizerFactory();
+                    }
+
+                    @Override
+                    public void ungetService(final Bundle bundle,
+                            final ServiceRegistration<LoadBalancerCustomizerFactory> registration,
+                            final LoadBalancerCustomizerFactory service)
+                    {
+                        // nothing to do
+                    }
+                }, propertiesCustomizer);
     }
 
     @Override
@@ -69,6 +94,12 @@
             metatypeReg.unregister();
             metatypeReg = null;
         }
+        if ( loadBalancerCustomizerFactoryReg != null )
+        {
+            loadBalancerCustomizerFactoryReg.unregister();
+            loadBalancerCustomizerFactoryReg = null;
+        }
+
         super.doStop();
     }
 }
diff --git a/http/jetty/src/main/java/org/apache/felix/http/jetty/internal/JettyService.java b/http/jetty/src/main/java/org/apache/felix/http/jetty/internal/JettyService.java
index cdf56f0..5e93b11 100644
--- a/http/jetty/src/main/java/org/apache/felix/http/jetty/internal/JettyService.java
+++ b/http/jetty/src/main/java/org/apache/felix/http/jetty/internal/JettyService.java
@@ -47,7 +47,6 @@
 import org.eclipse.jetty.security.HashLoginService;
 import org.eclipse.jetty.server.Connector;
 import org.eclipse.jetty.server.ConnectorStatistics;
-import org.eclipse.jetty.server.ForwardedRequestCustomizer;
 import org.eclipse.jetty.server.HttpConfiguration;
 import org.eclipse.jetty.server.HttpConnectionFactory;
 import org.eclipse.jetty.server.SecureRequestCustomizer;
@@ -105,6 +104,8 @@
     private BundleTracker bundleTracker;
     private ServiceTracker eventAdmintTracker;
     private ServiceTracker connectorTracker;
+    private ServiceTracker loadBalancerCustomizerTracker;
+    private CustomizerWrapper customizerWrapper;
     private EventAdmin eventAdmin;
 
     public JettyService(final BundleContext context,
@@ -218,6 +219,13 @@
                 this.connectorTracker.close();
                 this.connectorTracker = null;
             }
+            
+            if (this.loadBalancerCustomizerTracker != null)
+            {
+                this.loadBalancerCustomizerTracker.close();
+                this.loadBalancerCustomizerTracker = null;
+            } 
+            
             try
             {
                 this.server.stop();
@@ -276,8 +284,14 @@
             this.server.setHandler(this.parent);
             this.server.start();
 
+            if (this.config.isProxyLoadBalancerConnection()) 
+            {
+                customizerWrapper = new CustomizerWrapper();
+                this.loadBalancerCustomizerTracker = new LoadBalancerCustomizerFactoryTracker(this.context, customizerWrapper);
+                this.loadBalancerCustomizerTracker.open();
+            }
+            
             final StringBuilder message = new StringBuilder("Started Jetty ").append(version).append(" at port(s)");
-
             if (this.config.isUseHttp() && initializeHttp())
             {
                 message.append(" HTTP:").append(this.config.getHttpPort());
@@ -290,7 +304,7 @@
 
             this.connectorTracker = new ConnectorFactoryTracker(this.context, this.server);
             this.connectorTracker.open();
-
+            
             if (this.server.getConnectors() != null && this.server.getConnectors().length > 0)
             {
                 message.append(" on context path ").append(this.config.getContextPath());
@@ -355,11 +369,10 @@
 
         configureConnector(connector, this.config.getHttpPort());
 
-        if (this.config.isProxyLoadBalancerConnection())
+        if (this.config.isProxyLoadBalancerConnection()) 
         {
-            connFactory.getHttpConfiguration().addCustomizer(new ForwardedRequestCustomizer());
+            connFactory.getHttpConfiguration().addCustomizer(customizerWrapper);
         }
-
         return startConnector(connector);
     }
 
@@ -382,11 +395,11 @@
         HttpConfiguration httpConfiguration = connFactory.getHttpConfiguration();
         httpConfiguration.addCustomizer(new SecureRequestCustomizer());
 
-        if (this.config.isProxyLoadBalancerConnection())
+        if (this.config.isProxyLoadBalancerConnection()) 
         {
-            httpConfiguration.addCustomizer(new ForwardedRequestCustomizer());
+            httpConfiguration.addCustomizer(customizerWrapper);
         }
-
+        
         configureConnector(connector, this.config.getHttpsPort());
         return startConnector(connector);
     }
diff --git a/http/jetty/src/main/java/org/apache/felix/http/jetty/package-info.java b/http/jetty/src/main/java/org/apache/felix/http/jetty/package-info.java
index 148afa2..77d7dfe 100644
--- a/http/jetty/src/main/java/org/apache/felix/http/jetty/package-info.java
+++ b/http/jetty/src/main/java/org/apache/felix/http/jetty/package-info.java
@@ -16,7 +16,7 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-@Version("2.0")
+@Version("2.1")
 package org.apache.felix.http.jetty;
 
 import org.osgi.annotation.versioning.Version;
\ No newline at end of file