Author: cziegeler
Date: Fri Sep 16 07:03:46 2016
New Revision: 1761005

URL: http://svn.apache.org/viewvc?rev=1761005&view=rev
Log:
FELIX-5349 : add ManagedServiceFactory to HTTP service

Added:
    
felix/trunk/http/jetty/src/main/java/org/apache/felix/http/jetty/internal/JettyManagedServiceFactory.java
   (with props)
    
felix/trunk/http/jetty/src/main/java/org/apache/felix/http/jetty/internal/JettyServiceStarter.java
   (with props)
Modified:
    
felix/trunk/http/base/src/main/java/org/apache/felix/http/base/internal/AbstractActivator.java
    
felix/trunk/http/jetty/src/main/java/org/apache/felix/http/jetty/internal/ConfigMetaTypeProvider.java
    
felix/trunk/http/jetty/src/main/java/org/apache/felix/http/jetty/internal/JettyActivator.java
    
felix/trunk/http/jetty/src/main/java/org/apache/felix/http/jetty/internal/JettyConfig.java
    
felix/trunk/http/jetty/src/main/java/org/apache/felix/http/jetty/internal/JettyService.java

Modified: 
felix/trunk/http/base/src/main/java/org/apache/felix/http/base/internal/AbstractActivator.java
URL: 
http://svn.apache.org/viewvc/felix/trunk/http/base/src/main/java/org/apache/felix/http/base/internal/AbstractActivator.java?rev=1761005&r1=1761004&r2=1761005&view=diff
==============================================================================
--- 
felix/trunk/http/base/src/main/java/org/apache/felix/http/base/internal/AbstractActivator.java
 (original)
+++ 
felix/trunk/http/base/src/main/java/org/apache/felix/http/base/internal/AbstractActivator.java
 Fri Sep 16 07:03:46 2016
@@ -32,6 +32,11 @@ public abstract class AbstractActivator
         return this.context;
     }
 
+    protected final void setBundleContext(BundleContext context)
+    {
+        this.context = context;
+    }
+
     public final void start(BundleContext context)
         throws Exception
     {

Modified: 
felix/trunk/http/jetty/src/main/java/org/apache/felix/http/jetty/internal/ConfigMetaTypeProvider.java
URL: 
http://svn.apache.org/viewvc/felix/trunk/http/jetty/src/main/java/org/apache/felix/http/jetty/internal/ConfigMetaTypeProvider.java?rev=1761005&r1=1761004&r2=1761005&view=diff
==============================================================================
--- 
felix/trunk/http/jetty/src/main/java/org/apache/felix/http/jetty/internal/ConfigMetaTypeProvider.java
 (original)
+++ 
felix/trunk/http/jetty/src/main/java/org/apache/felix/http/jetty/internal/ConfigMetaTypeProvider.java
 Fri Sep 16 07:03:46 2016
@@ -315,6 +315,12 @@ class ConfigMetaTypeProvider implements
                 -1,
                 
bundle.getBundleContext().getProperty(JettyConfig.FELIX_JETTY_SERVLET_SESSION_MAX_AGE)));
 
+        adList.add(new 
AttributeDefinitionImpl(JettyConfig.FELIX_HTTP_SERVICE_NAME,
+                "HTTP Service Name",
+                "HTTP Service Name used in service filter to target specific 
HTTP instance. Default is null.",
+                null,
+                
bundle.getBundleContext().getProperty(JettyConfig.FELIX_HTTP_SERVICE_NAME)));
+
         return new ObjectClassDefinition()
         {
 

Modified: 
felix/trunk/http/jetty/src/main/java/org/apache/felix/http/jetty/internal/JettyActivator.java
URL: 
http://svn.apache.org/viewvc/felix/trunk/http/jetty/src/main/java/org/apache/felix/http/jetty/internal/JettyActivator.java?rev=1761005&r1=1761004&r2=1761005&view=diff
==============================================================================
--- 
felix/trunk/http/jetty/src/main/java/org/apache/felix/http/jetty/internal/JettyActivator.java
 (original)
+++ 
felix/trunk/http/jetty/src/main/java/org/apache/felix/http/jetty/internal/JettyActivator.java
 Fri Sep 16 07:03:46 2016
@@ -25,6 +25,7 @@ import org.osgi.framework.Bundle;
 import org.osgi.framework.Constants;
 import org.osgi.framework.ServiceFactory;
 import org.osgi.framework.ServiceRegistration;
+import org.osgi.service.cm.ManagedServiceFactory;
 
 public final class JettyActivator extends AbstractHttpActivator
 {
@@ -32,6 +33,7 @@ public final class JettyActivator extend
 
     private ServiceRegistration<?> metatypeReg;
     private ServiceRegistration<LoadBalancerCustomizerFactory> 
loadBalancerCustomizerFactoryReg;
+    private ServiceRegistration<?> configServiceFactoryReg;
 
     @Override
     protected void doStart() throws Exception
@@ -83,6 +85,11 @@ public final class JettyActivator extend
                         // nothing to do
                     }
                 }, propertiesCustomizer);
+        
+        Dictionary<String, Object> factoryProps = new Hashtable<String, 
Object>();
+        factoryProps.put(Constants.SERVICE_PID, JettyService.PID);
+        this.configServiceFactoryReg = this.getBundleContext().registerService(
+                       ManagedServiceFactory.class.getName(), new 
JettyManagedServiceFactory(this.getBundleContext()), factoryProps);
     }
 
     @Override
@@ -99,6 +106,11 @@ public final class JettyActivator extend
             loadBalancerCustomizerFactoryReg.unregister();
             loadBalancerCustomizerFactoryReg = null;
         }
+        if ( configServiceFactoryReg != null )
+        {
+            configServiceFactoryReg.unregister();
+            configServiceFactoryReg = null;
+        }
 
         super.doStop();
     }

Modified: 
felix/trunk/http/jetty/src/main/java/org/apache/felix/http/jetty/internal/JettyConfig.java
URL: 
http://svn.apache.org/viewvc/felix/trunk/http/jetty/src/main/java/org/apache/felix/http/jetty/internal/JettyConfig.java?rev=1761005&r1=1761004&r2=1761005&view=diff
==============================================================================
--- 
felix/trunk/http/jetty/src/main/java/org/apache/felix/http/jetty/internal/JettyConfig.java
 (original)
+++ 
felix/trunk/http/jetty/src/main/java/org/apache/felix/http/jetty/internal/JettyConfig.java
 Fri Sep 16 07:03:46 2016
@@ -162,6 +162,10 @@ public final class JettyConfig
     /**  Felix specific property to configure session max age */
     public static final String FELIX_JETTY_SERVLET_SESSION_MAX_AGE = 
"org.eclipse.jetty.servlet.MaxAge";
 
+    /** Felix specific property to set HTTP instance name. */
+    public static final String FELIX_HTTP_SERVICE_NAME = 
"org.apache.felix.http.name";
+
+
     private static String validateContextPath(String ctxPath)
     {
         // undefined, empty, or root context path
@@ -404,7 +408,7 @@ public final class JettyConfig
         boolean useHttps = getBooleanProperty(FELIX_HTTPS_ENABLE, 
getBooleanProperty(OSCAR_HTTPS_ENABLE, false));
         return useHttps && getHttpsPort() > 0;
     }
-
+    
     public boolean isProxyLoadBalancerConnection()
     {
         return getBooleanProperty(FELIX_PROXY_LOAD_BALANCER_CONNECTION_ENABLE, 
false);
@@ -414,6 +418,11 @@ public final class JettyConfig
         return getBooleanProperty(FELIX_JETTY_RENEGOTIATION_ALLOWED, false);
     }
 
+    public String getHttpServiceName()
+    {
+       return (String) getProperty(FELIX_HTTP_SERVICE_NAME);
+    }
+
     public void reset()
     {
         update(null);
@@ -425,6 +434,10 @@ public final class JettyConfig
         props.put(HTTPS_PORT, Integer.toString(getHttpsPort()));
         props.put(FELIX_HTTP_ENABLE, Boolean.toString(isUseHttp()));
         props.put(FELIX_HTTPS_ENABLE, Boolean.toString(isUseHttps()));
+        if (getHttpServiceName() != null)
+        {
+                       props.put(FELIX_HTTP_SERVICE_NAME, 
getHttpServiceName());
+        }
     }
 
     /**

Added: 
felix/trunk/http/jetty/src/main/java/org/apache/felix/http/jetty/internal/JettyManagedServiceFactory.java
URL: 
http://svn.apache.org/viewvc/felix/trunk/http/jetty/src/main/java/org/apache/felix/http/jetty/internal/JettyManagedServiceFactory.java?rev=1761005&view=auto
==============================================================================
--- 
felix/trunk/http/jetty/src/main/java/org/apache/felix/http/jetty/internal/JettyManagedServiceFactory.java
 (added)
+++ 
felix/trunk/http/jetty/src/main/java/org/apache/felix/http/jetty/internal/JettyManagedServiceFactory.java
 Fri Sep 16 07:03:46 2016
@@ -0,0 +1,88 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.felix.http.jetty.internal;
+
+import java.util.Dictionary;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.osgi.framework.BundleContext;
+import org.osgi.service.cm.ConfigurationException;
+import org.osgi.service.cm.ManagedServiceFactory;
+
+public class JettyManagedServiceFactory implements ManagedServiceFactory
+{
+       private final Map<String, JettyServiceStarter> services = new 
HashMap<>();
+       private final BundleContext context;
+
+       JettyManagedServiceFactory(BundleContext context)
+       {
+               this.context = context;
+       }
+
+       @Override
+       public String getName()
+       {
+               return "Apache Felix Http Jetty";
+       }
+
+       @Override
+       public void updated(String pid, Dictionary<String, ?> properties) 
throws ConfigurationException
+       {
+               JettyServiceStarter jetty = services.get(pid);
+
+               try
+               {
+                       if (jetty == null)
+                       {
+                               jetty = new JettyServiceStarter(context, 
properties);
+                               services.put(pid, jetty);
+                               jetty.start();
+                       }
+                       else
+                       {
+                               jetty.updated(properties);
+                       }
+               }
+               catch (Exception e)
+               {
+                       throw new ConfigurationException(null, "Failed to start 
Http Jetty pid=" + pid, e);
+               }
+       }
+
+       @Override
+       public void deleted(String pid)
+       {
+               JettyServiceStarter jetty = services.remove(pid);
+
+               if (jetty != null)
+               {
+                       try
+                       {
+                               jetty.stop();
+                       }
+                       catch (Exception e)
+                       {
+                               throw new RuntimeException("Faiiled to stop 
Http Jetty pid=" + pid, e);
+                       }
+               }
+
+       }
+
+}

Propchange: 
felix/trunk/http/jetty/src/main/java/org/apache/felix/http/jetty/internal/JettyManagedServiceFactory.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
felix/trunk/http/jetty/src/main/java/org/apache/felix/http/jetty/internal/JettyManagedServiceFactory.java
------------------------------------------------------------------------------
    svn:keywords = author date id revision rev url

Modified: 
felix/trunk/http/jetty/src/main/java/org/apache/felix/http/jetty/internal/JettyService.java
URL: 
http://svn.apache.org/viewvc/felix/trunk/http/jetty/src/main/java/org/apache/felix/http/jetty/internal/JettyService.java?rev=1761005&r1=1761004&r2=1761005&view=diff
==============================================================================
--- 
felix/trunk/http/jetty/src/main/java/org/apache/felix/http/jetty/internal/JettyService.java
 (original)
+++ 
felix/trunk/http/jetty/src/main/java/org/apache/felix/http/jetty/internal/JettyService.java
 Fri Sep 16 07:03:46 2016
@@ -107,6 +107,7 @@ public final class JettyService extends
     private ServiceTracker loadBalancerCustomizerTracker;
     private CustomizerWrapper customizerWrapper;
     private EventAdmin eventAdmin;
+    private boolean registerManagedService = true;
 
     public JettyService(final BundleContext context,
             final DispatcherServlet dispatcher,
@@ -131,14 +132,27 @@ public final class JettyService extends
         });
     }
 
+    public JettyService(final BundleContext context,
+            final DispatcherServlet dispatcher,
+            final EventDispatcher eventDispatcher,
+            final HttpServiceController controller,
+            final Dictionary<String,?> props)
+    {
+       this(context, dispatcher, eventDispatcher, controller);
+       this.config.update(props);
+       this.registerManagedService = false;
+    }
+
     public void start() throws Exception
     {
         // FELIX-4422: start Jetty synchronously...
         startJetty();
 
-        Dictionary<String, Object> props = new Hashtable<String, Object>();
-        props.put(Constants.SERVICE_PID, PID);
-        this.configServiceReg = 
this.context.registerService("org.osgi.service.cm.ManagedService", new 
JettyManagedService(this), props);
+        if (this.registerManagedService) {
+                       Dictionary<String, Object> props = new 
Hashtable<String, Object>();
+                       props.put(Constants.SERVICE_PID, PID);
+                       this.configServiceReg = 
this.context.registerService("org.osgi.service.cm.ManagedService", new 
JettyManagedService(this), props);
+        }
 
         this.eventAdmintTracker = new ServiceTracker(this.context, 
EventAdmin.class.getName(), this);
         this.eventAdmintTracker.open();

Added: 
felix/trunk/http/jetty/src/main/java/org/apache/felix/http/jetty/internal/JettyServiceStarter.java
URL: 
http://svn.apache.org/viewvc/felix/trunk/http/jetty/src/main/java/org/apache/felix/http/jetty/internal/JettyServiceStarter.java?rev=1761005&view=auto
==============================================================================
--- 
felix/trunk/http/jetty/src/main/java/org/apache/felix/http/jetty/internal/JettyServiceStarter.java
 (added)
+++ 
felix/trunk/http/jetty/src/main/java/org/apache/felix/http/jetty/internal/JettyServiceStarter.java
 Fri Sep 16 07:03:46 2016
@@ -0,0 +1,59 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.felix.http.jetty.internal;
+
+import java.util.Dictionary;
+
+import org.apache.felix.http.base.internal.AbstractHttpActivator;
+import org.osgi.framework.BundleContext;
+
+public class JettyServiceStarter extends AbstractHttpActivator
+{
+
+       private final BundleContext context;
+       private final Dictionary<String, ?> props;
+    private JettyService jetty;
+
+    JettyServiceStarter(BundleContext context, Dictionary<String, ?> 
properties)
+    {
+       this.context = context;
+               this.props = properties;
+    }
+    
+    public void start() throws Exception
+    {
+               super.setBundleContext(context);
+               super.doStart();
+               jetty = new JettyService(context, getDispatcherServlet(), 
getEventDispatcher(),
+                               getHttpServiceController(), props);
+               jetty.start();
+    }
+
+    public void stop() throws Exception
+    {
+       jetty.stop();
+       super.doStop();
+    }
+
+    public void updated(Dictionary<String, ?> properties) throws Exception
+    {
+       jetty.updated(properties);
+    }
+
+}

Propchange: 
felix/trunk/http/jetty/src/main/java/org/apache/felix/http/jetty/internal/JettyServiceStarter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
felix/trunk/http/jetty/src/main/java/org/apache/felix/http/jetty/internal/JettyServiceStarter.java
------------------------------------------------------------------------------
    svn:keywords = author date id revision rev url


Reply via email to