FELIX-4311 - log real Jetty version.

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1540634 13f79535-47bb-0310-9956-ffa450edef68
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 4dc0a36..ceac364 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
@@ -68,9 +68,7 @@
 
 import javax.servlet.ServletContext;
 
-public final class JettyService
-        extends AbstractLifeCycle.AbstractLifeCycleListener
-        implements BundleTrackerCustomizer, ServiceTrackerCustomizer
+public final class JettyService extends AbstractLifeCycle.AbstractLifeCycleListener implements BundleTrackerCustomizer, ServiceTrackerCustomizer
 {
     /** PID for configuration of the HTTP service. */
     private static final String PID = "org.apache.felix.http";
@@ -100,8 +98,7 @@
     private EventAdmin eventAdmin;
     private Map<String, Deployment> deployments = new LinkedHashMap<String, Deployment>();
 
-    public JettyService(BundleContext context, DispatcherServlet dispatcher, EventDispatcher eventDispatcher,
-        HttpServiceController controller)
+    public JettyService(BundleContext context, DispatcherServlet dispatcher, EventDispatcher eventDispatcher, HttpServiceController controller)
     {
         this.context = context;
         this.config = new JettyConfig(this.context);
@@ -110,25 +107,26 @@
         this.controller = controller;
     }
 
-    public void start()
-        throws Exception
+    public void start() throws Exception
     {
         Properties props = new Properties();
         props.put(Constants.SERVICE_PID, PID);
-        this.configServiceReg = this.context.registerService("org.osgi.service.cm.ManagedService",
-            new JettyManagedService(this), props);
+        this.configServiceReg = this.context.registerService("org.osgi.service.cm.ManagedService", new JettyManagedService(this), props);
 
-        this.executor = Executors.newSingleThreadExecutor(new ThreadFactory() {
-           public Thread newThread(Runnable runnable)
-           {
+        this.executor = Executors.newSingleThreadExecutor(new ThreadFactory()
+        {
+            public Thread newThread(Runnable runnable)
+            {
                 Thread t = new Thread(runnable);
                 t.setName("Jetty HTTP Service");
                 return t;
             }
         });
-        this.executor.submit(new JettyOperation() {
+        this.executor.submit(new JettyOperation()
+        {
             @Override
-            protected void doExecute() throws Exception {
+            protected void doExecute() throws Exception
+            {
                 startJetty();
             }
         });
@@ -140,25 +138,30 @@
         this.bundleTracker.open();
     }
 
-    public void stop()
-        throws Exception
+    public void stop() throws Exception
     {
-        if (this.executor != null && !this.executor.isShutdown()) {
-            this.executor.submit(new JettyOperation() {
+        if (this.executor != null && !this.executor.isShutdown())
+        {
+            this.executor.submit(new JettyOperation()
+            {
                 @Override
-                protected void doExecute() throws Exception {
+                protected void doExecute() throws Exception
+                {
                     stopJetty();
                 }
             });
             this.executor.shutdown();
         }
-        if (this.configServiceReg != null) {
+        if (this.configServiceReg != null)
+        {
             this.configServiceReg.unregister();
         }
-        if (this.bundleTracker != null) {
+        if (this.bundleTracker != null)
+        {
             this.bundleTracker.close();
         }
-        if (this.serviceTracker != null) {
+        if (this.serviceTracker != null)
+        {
             this.serviceTracker.close();
         }
     }
@@ -175,10 +178,13 @@
     {
         this.config.update(props);
 
-        if (this.executor != null  && !this.executor.isShutdown()) {
-            this.executor.submit(new JettyOperation() {
+        if (this.executor != null && !this.executor.isShutdown())
+        {
+            this.executor.submit(new JettyOperation()
+            {
                 @Override
-                protected void doExecute() throws Exception {
+                protected void doExecute() throws Exception
+                {
                     stopJetty();
                     startJetty();
                 }
@@ -188,9 +194,12 @@
 
     private void startJetty()
     {
-        try {
+        try
+        {
             initializeJetty();
-        } catch (Exception e) {
+        }
+        catch (Exception e)
+        {
             SystemLogger.error("Exception while initializing Jetty.", e);
         }
     }
@@ -217,12 +226,11 @@
         }
     }
 
-    private void initializeJetty()
-        throws Exception
+    private void initializeJetty() throws Exception
     {
         if (this.config.isUseHttp() || this.config.isUseHttps())
         {
-            StringBuffer message = new StringBuffer("Started jetty ").append(Server.getVersion()).append(" at port(s)");
+            StringBuffer message = new StringBuffer("Started jetty ").append(getJettyVersion()).append(" at port(s)");
             HashLoginService realm = new HashLoginService("OSGi HTTP Service Realm");
             this.server = new Server();
             this.server.addLifeCycleListener(this);
@@ -246,8 +254,7 @@
 
             this.parent = new ContextHandlerCollection();
 
-            ServletContextHandler context = new ServletContextHandler(this.parent,
-                    this.config.getContextPath(), ServletContextHandler.SESSIONS);
+            ServletContextHandler context = new ServletContextHandler(this.parent, this.config.getContextPath(), ServletContextHandler.SESSIONS);
 
             message.append(" on context path ").append(this.config.getContextPath());
             configureSessionManager(context);
@@ -274,19 +281,28 @@
         publishServiceProperties();
     }
 
-    private void initializeHttp()
-        throws Exception
+    private String getJettyVersion()
     {
-        Connector connector = this.config.isUseHttpNio()
-                ? new SelectChannelConnector()
-                : new SocketConnector();
+        // FELIX-4311: report the real version of Jetty...
+        Dictionary headers = this.context.getBundle().getHeaders();
+        String version = (String) headers.get("X-Jetty-Version");
+        if (version == null)
+        {
+            version = Server.getVersion();
+        }
+        return version;
+    }
+
+    private void initializeHttp() throws Exception
+    {
+        Connector connector = this.config.isUseHttpNio() ? new SelectChannelConnector() : new SocketConnector();
         connector.setPort(this.config.getHttpPort());
         configureConnector(connector);
         this.server.addConnector(connector);
     }
 
-    private void initializeHttps()
-        throws Exception
+    @SuppressWarnings("deprecation")
+    private void initializeHttps() throws Exception
     {
         // this massive code duplication is caused by the SslSelectChannelConnector
         // and the SslSocketConnector not have a common API to setup security
@@ -417,16 +433,16 @@
         }
 
         String address = ia.getHostAddress().trim().toLowerCase();
-        if ( ia instanceof Inet6Address )
+        if (ia instanceof Inet6Address)
         {
             // skip link-local
-            if ( address.startsWith("fe80:0:0:0:") )
+            if (address.startsWith("fe80:0:0:0:"))
             {
                 return null;
             }
             address = "[" + address + "]";
         }
-        else if ( ! ( ia instanceof Inet4Address ) )
+        else if (!(ia instanceof Inet4Address))
         {
             return null;
         }
@@ -439,14 +455,14 @@
         final StringBuilder sb = new StringBuilder();
         sb.append("http");
         int defaultPort = 80;
-        if ( listener instanceof SslConnector )
+        if (listener instanceof SslConnector)
         {
             sb.append('s');
             defaultPort = 443;
         }
         sb.append("://");
         sb.append(hostname);
-        if ( listener.getPort() != defaultPort )
+        if (listener.getPort() != defaultPort)
         {
             sb.append(':');
             sb.append(String.valueOf(listener.getPort()));
@@ -480,23 +496,23 @@
         final List<String> endpoints = new ArrayList<String>();
 
         final Connector[] connectors = this.server.getConnectors();
-        if ( connectors != null )
+        if (connectors != null)
         {
-            for(int i=0 ; i < connectors.length; i++)
+            for (int i = 0; i < connectors.length; i++)
             {
                 final Connector connector = connectors[i];
 
-                if ( connector.getHost() == null )
+                if (connector.getHost() == null)
                 {
                     try
                     {
                         final List<NetworkInterface> interfaces = new ArrayList<NetworkInterface>();
                         final List<NetworkInterface> loopBackInterfaces = new ArrayList<NetworkInterface>();
                         final Enumeration<NetworkInterface> nis = NetworkInterface.getNetworkInterfaces();
-                        while ( nis.hasMoreElements() )
+                        while (nis.hasMoreElements())
                         {
                             final NetworkInterface ni = nis.nextElement();
-                            if ( ni.isLoopback() )
+                            if (ni.isLoopback())
                             {
                                 loopBackInterfaces.add(ni);
                             }
@@ -524,7 +540,7 @@
                 else
                 {
                     final String endpoint = this.getEndpoint(connector, connector.getHost());
-                    if ( endpoint != null )
+                    if (endpoint != null)
                     {
                         endpoints.add(endpoint);
                     }
@@ -540,31 +556,27 @@
 
         // check existing deployments
         Deployment deployment = this.deployments.get(contextPath);
-        if (deployment != null) {
-            SystemLogger.warning(String.format(
-                    "Web application bundle %s has context path %s which is already registered",
-                    bundle.getSymbolicName(), contextPath), null);
-            postEvent(WebEvent.FAILED(bundle, this.context.getBundle(), null, contextPath,
-                    deployment.getBundle().getBundleId()));
+        if (deployment != null)
+        {
+            SystemLogger.warning(String.format("Web application bundle %s has context path %s which is already registered", bundle.getSymbolicName(), contextPath), null);
+            postEvent(WebEvent.FAILED(bundle, this.context.getBundle(), null, contextPath, deployment.getBundle().getBundleId()));
             return null;
         }
 
         // check context path belonging to Http Service implementation
-        if (contextPath.equals("/")) {
-            SystemLogger.warning(String.format(
-                    "Web application bundle %s has context path %s which is reserved",
-                    bundle.getSymbolicName(), contextPath), null);
-            postEvent(WebEvent.FAILED(bundle, this.context.getBundle(), null, contextPath,
-                    this.context.getBundle().getBundleId()));
+        if (contextPath.equals("/"))
+        {
+            SystemLogger.warning(String.format("Web application bundle %s has context path %s which is reserved", bundle.getSymbolicName(), contextPath), null);
+            postEvent(WebEvent.FAILED(bundle, this.context.getBundle(), null, contextPath, this.context.getBundle().getBundleId()));
             return null;
         }
 
         // check against excluded paths
-        for (String path : this.config.getPathExclusions()) {
-            if (contextPath.startsWith(path)) {
-                SystemLogger.warning(String.format(
-                        "Web application bundle %s has context path %s which clashes with excluded path prefix %s",
-                        bundle.getSymbolicName(), contextPath, path), null);
+        for (String path : this.config.getPathExclusions())
+        {
+            if (contextPath.startsWith(path))
+            {
+                SystemLogger.warning(String.format("Web application bundle %s has context path %s which clashes with excluded path prefix %s", bundle.getSymbolicName(), contextPath, path), null);
                 postEvent(WebEvent.FAILED(bundle, this.context.getBundle(), null, path, null));
                 return null;
             }
@@ -580,14 +592,18 @@
 
     public void deploy(final Deployment deployment, final WebAppBundleContext context)
     {
-        if (this.executor != null  && !this.executor.isShutdown()) {
-            this.executor.submit(new JettyOperation() {
+        if (this.executor != null && !this.executor.isShutdown())
+        {
+            this.executor.submit(new JettyOperation()
+            {
                 @Override
-                protected void doExecute() {
+                protected void doExecute()
+                {
                     final Bundle webAppBundle = deployment.getBundle();
                     final Bundle extenderBundle = JettyService.this.context.getBundle();
 
-                    try {
+                    try
+                    {
                         JettyService.this.parent.addHandler(context);
                         context.start();
 
@@ -595,13 +611,14 @@
                         props.put(WEB_SYMBOLIC_NAME, webAppBundle.getSymbolicName());
                         props.put(WEB_VERSION, webAppBundle.getVersion());
                         props.put(WEB_CONTEXT_PATH, deployment.getContextPath());
-                        deployment.setRegistration(webAppBundle.getBundleContext().registerService(
-                                ServletContext.class.getName(), context.getServletContext(), props));
+                        deployment.setRegistration(webAppBundle.getBundleContext().registerService(ServletContext.class.getName(), context.getServletContext(), props));
 
                         context.getServletContext().setAttribute(OSGI_BUNDLE_CONTEXT, webAppBundle.getBundleContext());
 
                         postEvent(WebEvent.DEPLOYED(webAppBundle, extenderBundle));
-                    } catch (Exception e) {
+                    }
+                    catch (Exception e)
+                    {
                         SystemLogger.error(String.format("Deploying web application bundle %s failed.", webAppBundle.getSymbolicName()), e);
                         postEvent(WebEvent.FAILED(webAppBundle, extenderBundle, e, null, null));
                         deployment.setContext(null);
@@ -614,27 +631,36 @@
 
     public void undeploy(final Deployment deployment, final WebAppBundleContext context)
     {
-        if (this.executor != null  && !this.executor.isShutdown()) {
-            this.executor.submit(new JettyOperation(){
+        if (this.executor != null && !this.executor.isShutdown())
+        {
+            this.executor.submit(new JettyOperation()
+            {
                 @Override
-                protected void doExecute() {
+                protected void doExecute()
+                {
                     final Bundle webAppBundle = deployment.getBundle();
                     final Bundle extenderBundle = JettyService.this.context.getBundle();
 
-                    try {
+                    try
+                    {
                         postEvent(WebEvent.UNDEPLOYING(webAppBundle, extenderBundle));
 
                         context.getServletContext().removeAttribute(OSGI_BUNDLE_CONTEXT);
 
                         ServiceRegistration registration = deployment.getRegistration();
-                        if (registration != null) {
+                        if (registration != null)
+                        {
                             registration.unregister();
                         }
                         deployment.setRegistration(null);
                         context.stop();
-                    } catch (Exception e) {
+                    }
+                    catch (Exception e)
+                    {
                         SystemLogger.error(String.format("Undeploying web application bundle %s failed.", webAppBundle.getSymbolicName()), e);
-                    } finally {
+                    }
+                    finally
+                    {
                         postEvent(WebEvent.UNDEPLOYED(webAppBundle, extenderBundle));
                     }
                 }
@@ -655,11 +681,12 @@
 
     private Object detectWebAppBundle(Bundle bundle)
     {
-        if (bundle.getState() == Bundle.ACTIVE || (bundle.getState() == Bundle.STARTING &&
-                "Lazy".equals(bundle.getHeaders().get(HEADER_ACTIVATION_POLICY)))) {
+        if (bundle.getState() == Bundle.ACTIVE || (bundle.getState() == Bundle.STARTING && "Lazy".equals(bundle.getHeaders().get(HEADER_ACTIVATION_POLICY))))
+        {
 
             String contextPath = (String) bundle.getHeaders().get(HEADER_WEB_CONTEXT_PATH);
-            if (contextPath != null) {
+            if (contextPath != null)
+            {
                 return startWebAppBundle(bundle, contextPath);
             }
         }
@@ -669,12 +696,14 @@
     public void removedBundle(Bundle bundle, BundleEvent event, Object object)
     {
         String contextPath = (String) bundle.getHeaders().get(HEADER_WEB_CONTEXT_PATH);
-        if (contextPath == null) {
+        if (contextPath == null)
+        {
             return;
         }
 
         Deployment deployment = this.deployments.remove(contextPath);
-        if (deployment != null && deployment.getContext() != null) {
+        if (deployment != null && deployment.getContext() != null)
+        {
             // remove registration, since bundle is already stopping
             deployment.setRegistration(null);
             undeploy(deployment, deployment.getContext());
@@ -701,18 +730,20 @@
 
     private void postEvent(Event event)
     {
-        if (this.eventAdmin != null) {
+        if (this.eventAdmin != null)
+        {
             this.eventAdmin.postEvent(event);
         }
     }
 
     public void lifeCycleStarted(LifeCycle event)
     {
-        for (Deployment deployment : this.deployments.values()) {
-            if (deployment.getContext() == null) {
+        for (Deployment deployment : this.deployments.values())
+        {
+            if (deployment.getContext() == null)
+            {
                 postEvent(WebEvent.DEPLOYING(deployment.getBundle(), this.context.getBundle()));
-                WebAppBundleContext context = new WebAppBundleContext(deployment.getContextPath(),
-                        deployment.getBundle(), this.getClass().getClassLoader());
+                WebAppBundleContext context = new WebAppBundleContext(deployment.getContextPath(), deployment.getBundle(), this.getClass().getClassLoader());
                 deploy(deployment, context);
             }
         }
@@ -720,8 +751,10 @@
 
     public void lifeCycleStopping(LifeCycle event)
     {
-        for (Deployment deployment : this.deployments.values()) {
-            if (deployment.getContext() != null) {
+        for (Deployment deployment : this.deployments.values())
+        {
+            if (deployment.getContext() != null)
+            {
                 undeploy(deployment, deployment.getContext());
             }
         }
@@ -784,11 +817,14 @@
         {
             ClassLoader cl = Thread.currentThread().getContextClassLoader();
 
-            try {
+            try
+            {
                 Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
                 doExecute();
                 return null;
-            } finally {
+            }
+            finally
+            {
                 Thread.currentThread().setContextClassLoader(cl);
             }
         }