FELIX-769 Service property now set with actual HTTP and HTTPS ports used. Default service property names are same as OSGi config properties for these values:
"org.osgi.service.http.port"
"org.osgi.service.http.port.secure"
These can be overridden with alternative service property names using the following properties:
"org.apache.felix.http.svcprop.port"
"org.apache.felix.http.svcprop.port.secure"
e.g.
org.apache.felix.http.svcprop.port=http.port
org.apache.felix.http.svcprop.port.secure=https.port
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@706552 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/http.jetty/src/main/java/org/apache/felix/http/jetty/Activator.java b/http.jetty/src/main/java/org/apache/felix/http/jetty/Activator.java
index c1a91f6..a0d5bd9 100644
--- a/http.jetty/src/main/java/org/apache/felix/http/jetty/Activator.java
+++ b/http.jetty/src/main/java/org/apache/felix/http/jetty/Activator.java
@@ -18,6 +18,7 @@
*/
package org.apache.felix.http.jetty;
+import java.util.Properties;
import org.mortbay.jetty.Connector;
import org.mortbay.jetty.Server;
@@ -28,6 +29,8 @@
import org.mortbay.jetty.servlet.Context;
import org.mortbay.jetty.servlet.OsgiServletHandler;
import org.mortbay.jetty.servlet.SessionHandler;
+import org.mortbay.component.LifeCycle;
+
import org.mortbay.log.Log;
import org.mortbay.log.Logger;
import org.mortbay.log.StdErrLog;
@@ -66,6 +69,28 @@
*/
public class Activator implements BundleActivator
{
+ /** Standard OSGi port property for HTTP service */
+ public static final String HTTP_PORT = "org.osgi.service.http.port";
+
+ /** Standard OSGi https port property for HTTP service */
+ public static final String HTTPS_PORT = "org.osgi.service.http.port.secure";
+
+ /** Felix and Jetty specific property. Enables Jetty debug messages */
+ public static final String HTTP_DEBUG = "org.apache.felix.http.jetty.debug";
+
+ /** Felix specific property. This property will be looked up to determine the
+ name of the service property to set with the http port used. If not supplied
+ then the HTTP_PORT property name will be used for the service property */
+ public static final String HTTP_SVCPROP_PORT = "org.apache.felix.http.svcprop.port";
+
+ /** Felix specific property. This property will be looked up to determine the
+ name of the service property to set with the https port used. If not supplied
+ then the HTTPS_PORT property name will be used for the service property */
+ public static final String HTTPS_SVCPROP_PORT = "org.apache.felix.http.svcprop.port.secure";
+
+ /** Legacy Oscar property support. Controls whether to enable HTTPS */
+ public static final String OSCAR_HTTPS_ENABLE = "org.ungoverned.osgi.bundle.https.enable";
+
protected static boolean debug = false;
private static ServiceTracker m_logTracker = null;
@@ -78,6 +103,11 @@
private int m_httpPort;
private int m_httpsPort;
+ private Properties m_svcProperties = new Properties();
+
+ //
+ // Main class instance code
+ //
public void start( BundleContext bundleContext ) throws BundleException
{
@@ -86,33 +116,11 @@
// org.mortbay.util.Loader needs this (used for JDK 1.4 log classes)
Thread.currentThread().setContextClassLoader( this.getClass().getClassLoader() );
- String optDebug = m_bundleContext.getProperty( "org.apache.felix.http.jetty.debug" );
- if ( optDebug != null && optDebug.toLowerCase().equals( "true" ) )
- {
- debug = true;
- }
+ debug = getBooleanProperty(HTTP_DEBUG, false);
// get default HTTP and HTTPS ports as per the OSGi spec
- try
- {
- m_httpPort = Integer.parseInt( m_bundleContext.getProperty( "org.osgi.service.http.port" ) );
- }
- catch ( Exception e )
- {
- // maybe log a message saying using default?
- m_httpPort = 80;
- }
-
- try
- {
- // TODO: work out how/when we should use the HTTPS port
- m_httpsPort = Integer.parseInt( m_bundleContext.getProperty( "org.osgi.service.http.port.secure" ) );
- }
- catch ( Exception e )
- {
- // maybe log a message saying using default?
- m_httpsPort = 443;
- }
+ m_httpPort = getIntProperty(HTTP_PORT, 80);
+ m_httpsPort = getIntProperty(HTTPS_PORT, 443);
m_logTracker = new ServiceTracker( bundleContext, LogService.class.getName(), null );
m_logTracker.open();
@@ -133,7 +141,10 @@
}
m_httpServ = new HttpServiceFactory();
- m_svcReg = m_bundleContext.registerService( HttpService.class.getName(), m_httpServ, null );
+ m_svcReg = m_bundleContext.registerService( HttpService.class.getName(), m_httpServ, m_svcProperties );
+ // OSGi spec states the properties should not be changed after registration,
+ // so create new copy for later clone for updates
+ m_svcProperties = new Properties(m_svcProperties);
}
@@ -161,6 +172,60 @@
m_logTracker.close();
}
+
+ public int getIntProperty(String name, int dflt_val)
+ {
+ int retval = dflt_val;
+
+ try
+ {
+ retval = Integer.parseInt( m_bundleContext.getProperty( name ) );
+ }
+ catch ( Exception e )
+ {
+ // maybe log a message saying using default?
+ retval = dflt_val;
+ }
+
+ return retval;
+ }
+
+
+ public boolean getBooleanProperty(String name, boolean dflt_val)
+ {
+ boolean retval = dflt_val;
+
+ String strval = m_bundleContext.getProperty( name );
+ if ( strval != null)
+ {
+ if (strval.toLowerCase().equals( "true" ) ||
+ strval.toLowerCase().equals( "yes" ))
+ {
+ retval = true;
+ }
+ else
+ {
+ // poss should raise error/warn here
+ retval = false;
+ }
+ }
+
+ return retval;
+ }
+
+
+ public String getStringProperty(String name, String dflt_val)
+ {
+ String retval = dflt_val;
+
+ String strval = m_bundleContext.getProperty( name );
+ if ( strval != null)
+ {
+ retval = strval;
+ }
+
+ return retval;
+ }
protected void initializeJettyLogger() {
String oldProperty = System.getProperty( "org.mortbay.log.class" );
@@ -189,13 +254,16 @@
// Add a regular HTTP listener
Connector connector = new SelectChannelConnector();
+ connector.addLifeCycleListener(
+ new ConnectorListener(getStringProperty(HTTP_SVCPROP_PORT, HTTP_PORT))
+ );
+
connector.setPort( m_httpPort );
connector.setMaxIdleTime( 60000 );
m_server.addConnector( connector );
// See if we need to add an HTTPS listener
- String enableHTTPS = m_bundleContext.getProperty( "org.ungoverned.osgi.bundle.https.enable" );
- if ( enableHTTPS != null && enableHTTPS.toLowerCase().equals( "true" ) )
+ if ( getBooleanProperty( OSCAR_HTTPS_ENABLE, false ))
{
initializeHTTPS();
}
@@ -233,8 +301,11 @@
sslProvider = "org.mortbay.http.SunJsseListener";
}
-
SslSocketConnector s_listener = new SslSocketConnector();
+ s_listener.addLifeCycleListener(
+ new ConnectorListener(getStringProperty(HTTPS_SVCPROP_PORT, HTTPS_PORT))
+ );
+
s_listener.setPort( m_httpsPort );
s_listener.setMaxIdleTime( 60000 );
@@ -315,5 +386,44 @@
( ( HttpServiceImpl ) service ).unregisterAll();
}
}
+
+ // Innner class to listen for connector startup and register service
+ // properties for actual ports used. Possible connections may have deferred
+ // startup, so this should ensure "port" is retrieved once available
+
+ public class ConnectorListener implements LifeCycle.Listener
+ {
+ String m_svcPropName;
+
+ public ConnectorListener(String svcPropName)
+ {
+ m_svcPropName = svcPropName;
+ }
+
+ public void lifeCycleFailure(LifeCycle event, Throwable cause) {}
+
+ public void lifeCycleStarted(LifeCycle event)
+ {
+ Connector conn = (Connector) event;
+ int actualPort = conn.getLocalPort();
+
+ debug( "** http set service prop:" + m_svcPropName + ", value: " + actualPort );
+
+ m_svcProperties.setProperty(m_svcPropName, String.valueOf(actualPort));
+
+ if (m_svcReg != null)
+ {
+ m_svcReg.setProperties(m_svcProperties);
+ m_svcProperties = new Properties(m_svcProperties);
+ }
+ }
+
+ public void lifeCycleStarting(LifeCycle event) {}
+
+ public void lifeCycleStopped(LifeCycle event) {}
+
+ public void lifeCycleStopping(LifeCycle event) {}
+
+ }
}
\ No newline at end of file