392985 - Adds startup time event log to Virgo distributions
diff --git a/org.eclipse.virgo.nano.core/src/main/java/org/eclipse/virgo/nano/core/internal/ServerReadinessTracker.java b/org.eclipse.virgo.nano.core/src/main/java/org/eclipse/virgo/nano/core/internal/ServerReadinessTracker.java
new file mode 100644
index 0000000..8504205
--- /dev/null
+++ b/org.eclipse.virgo.nano.core/src/main/java/org/eclipse/virgo/nano/core/internal/ServerReadinessTracker.java
@@ -0,0 +1,64 @@
+package org.eclipse.virgo.nano.core.internal;
+
+import org.eclipse.virgo.nano.diagnostics.KernelLogEvents;
+import org.osgi.service.event.Event;
+import org.osgi.service.event.EventHandler;
+import org.eclipse.virgo.medic.eventlog.EventLogger;
+import org.osgi.framework.BundleContext;
+
+public class ServerReadinessTracker implements EventHandler {
+    
+    private static final String TOPIC_RECOVERY_COMPLETED = "org/eclipse/virgo/kernel/deployer/recovery/COMPLETED";
+    private EventLogger eventLogger;
+    private BundleContext bundleContext;
+    /** 
+     * {@inheritDoc}
+     */
+    public void handleEvent(Event event) {
+        if (TOPIC_RECOVERY_COMPLETED.equals(event.getTopic())) {
+            Thread readinessPrinter = new Thread(new ServerReadinessPrinter(this.bundleContext, this.eventLogger));
+            readinessPrinter.setName("startup-readiness");
+            readinessPrinter.start();
+        }
+    }
+    
+    private class ServerReadinessPrinter implements Runnable {
+
+        private BundleContext bundleContext;
+        private EventLogger eventLogger;
+        
+        ServerReadinessPrinter(BundleContext bundleContext, EventLogger logger) {
+            this.bundleContext = bundleContext;
+            this.eventLogger = logger;
+        }
+
+        @Override
+        public void run() {
+            String frameworkStartTimeString = this.bundleContext.getProperty("eclipse.startTime");
+            if (frameworkStartTimeString != null) {
+                Long frameworkStartTime = Long.valueOf(frameworkStartTimeString);
+                long sinceStart = System.currentTimeMillis() - frameworkStartTime;
+                this.eventLogger.log(KernelLogEvents.VIRGO_STARTED, String.valueOf(sinceStart/1000) + "." + String.valueOf(sinceStart%1000));
+            } else {
+                this.eventLogger.log(KernelLogEvents.VIRGO_STARTED_NOTIME);
+            }
+        }
+    }
+    
+    public void activate(BundleContext context) {
+        this.bundleContext = context;
+    }
+    
+    public void deactivate(BundleContext context) {
+        this.bundleContext = null;
+    }
+    
+    public void bindEventLogger(EventLogger logger) {
+        this.eventLogger = logger;
+    }
+    
+    public void unbindEventLogger(EventLogger logger) {
+        this.eventLogger = null;
+    }
+    
+}
diff --git a/org.eclipse.virgo.nano.core/src/main/java/org/eclipse/virgo/nano/diagnostics/KernelLogEvents.java b/org.eclipse.virgo.nano.core/src/main/java/org/eclipse/virgo/nano/diagnostics/KernelLogEvents.java
index 63b1787..8e9507c 100644
--- a/org.eclipse.virgo.nano.core/src/main/java/org/eclipse/virgo/nano/diagnostics/KernelLogEvents.java
+++ b/org.eclipse.virgo.nano.core/src/main/java/org/eclipse/virgo/nano/diagnostics/KernelLogEvents.java
@@ -30,6 +30,8 @@
     KERNEL_START_TIMED_OUT(4, Level.ERROR), //
     KERNEL_PLAN_ARGUMENTS_INCORRECT(5, Level.WARNING), //
     KERNEL_EVENT_START_ABORTED(6, Level.ERROR), //
+    VIRGO_STARTED(7, Level.INFO), //
+    VIRGO_STARTED_NOTIME(8, Level.INFO), //
     
     SHUTDOWN_INITIATED(10, Level.INFO), //
     IMMEDIATE_SHUTDOWN_INITIATED(11, Level.INFO), //
diff --git a/org.eclipse.virgo.nano.core/src/main/resources/EventLogMessages.properties b/org.eclipse.virgo.nano.core/src/main/resources/EventLogMessages.properties
index 3eec203..1cb7745 100644
--- a/org.eclipse.virgo.nano.core/src/main/resources/EventLogMessages.properties
+++ b/org.eclipse.virgo.nano.core/src/main/resources/EventLogMessages.properties
@@ -4,6 +4,9 @@
 KE0004E = Kernel failed to start within {} seconds.
 KE0005W = Option -plan requires one or two arguments but {} were provided: '{}'.
 KE0006E = Kernel failed to start due to the aborted start of '{}' version '{}'.
+#Server Readiness Events
+KE0007I = Virgo ready. Started for {}s.
+KE0008I = Virgo ready.
 
 KE0010I = Shutdown initiated.
 KE0011I = Immediate shutdown initiated.
diff --git a/org.eclipse.virgo.nano.core/src/main/resources/OSGI-INF/org.eclipse.virgo.nano.server.readiness.tracker.xml b/org.eclipse.virgo.nano.core/src/main/resources/OSGI-INF/org.eclipse.virgo.nano.server.readiness.tracker.xml
new file mode 100644
index 0000000..a37ca51
--- /dev/null
+++ b/org.eclipse.virgo.nano.core/src/main/resources/OSGI-INF/org.eclipse.virgo.nano.server.readiness.tracker.xml
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="Server Readiness Tracker" immediate="true">
+   <implementation class="org.eclipse.virgo.nano.core.internal.ServerReadinessTracker"/>
+ 
+   	<reference name="EventLogger" 
+   	interface="org.eclipse.virgo.medic.eventlog.EventLogger" 
+   	policy="static"
+   	bind="bindEventLogger" 
+   	unbind="unbindEventLogger"/>
+    
+    <property name="event.topics">org/eclipse/virgo/kernel/*</property>
+    
+    <service>
+       <provide interface="org.osgi.service.event.EventHandler"/>
+    </service>
+    
+</scr:component>
diff --git a/org.eclipse.virgo.nano.core/template.mf b/org.eclipse.virgo.nano.core/template.mf
index 127e5b4..967d197 100644
--- a/org.eclipse.virgo.nano.core/template.mf
+++ b/org.eclipse.virgo.nano.core/template.mf
@@ -3,7 +3,7 @@
 Bundle-Name: Virgo Nano Core
 Bundle-SymbolicName: org.eclipse.virgo.nano.core
 Bundle-Version: ${version}
-Service-Component: OSGI-INF/org.eclipse.virgo.nano.config.xml,OSGI-INF/org.eclipse.virgo.nano.core.xml
+Service-Component: OSGI-INF/org.eclipse.virgo.nano.config.xml,OSGI-INF/org.eclipse.virgo.nano.core.xml,OSGI-INF/org.eclipse.virgo.nano.server.readiness.tracker.xml
 Import-Template: 
  org.eclipse.virgo.util.*;version="${org.eclipse.virgo.util:[=.=.=, =.+1)}",
  org.eclipse.virgo.medic.*;version="${org.eclipse.virgo.medic:[=.=.=, =.+1)}",
diff --git a/org.eclipse.virgo.nano.deployer/src/main/java/org/eclipse/virgo/nano/deployer/NanoLogEvents.java b/org.eclipse.virgo.nano.deployer/src/main/java/org/eclipse/virgo/nano/deployer/NanoLogEvents.java
deleted file mode 100644
index d5ef111..0000000
--- a/org.eclipse.virgo.nano.deployer/src/main/java/org/eclipse/virgo/nano/deployer/NanoLogEvents.java
+++ /dev/null
@@ -1,35 +0,0 @@
-package org.eclipse.virgo.nano.deployer;
-
-import org.eclipse.virgo.nano.serviceability.LogEventDelegate;
-import org.eclipse.virgo.medic.eventlog.Level;
-import org.eclipse.virgo.medic.eventlog.LogEvent;
-
-public enum NanoLogEvents implements LogEvent {
-
-    NANO_STARTED(1, Level.INFO), //
-    NANO_STARTED_NOTIME(1, Level.INFO); //
-
-    private static final String PREFIX = "VN";
-
-    private final LogEventDelegate delegate;
-
-    private NanoLogEvents(int code, Level level) {
-        this.delegate = new LogEventDelegate(PREFIX, code, level);
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    @Override
-    public String getEventCode() {
-        return this.delegate.getEventCode();
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    @Override
-    public Level getLevel() {
-        return this.delegate.getLevel();
-    }
-}
diff --git a/org.eclipse.virgo.nano.deployer/src/main/java/org/eclipse/virgo/nano/deployer/internal/StandardApplicationDeployer.java b/org.eclipse.virgo.nano.deployer/src/main/java/org/eclipse/virgo/nano/deployer/internal/StandardApplicationDeployer.java
index a76a3c0..482959b 100644
--- a/org.eclipse.virgo.nano.deployer/src/main/java/org/eclipse/virgo/nano/deployer/internal/StandardApplicationDeployer.java
+++ b/org.eclipse.virgo.nano.deployer/src/main/java/org/eclipse/virgo/nano/deployer/internal/StandardApplicationDeployer.java
@@ -5,11 +5,11 @@
 import java.net.URI;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Map;
 
 import org.eclipse.virgo.medic.eventlog.EventLogger;
 import org.eclipse.virgo.nano.core.KernelConfig;
 import org.eclipse.virgo.nano.deployer.NanoDeployerLogEvents;
-import org.eclipse.virgo.nano.deployer.NanoLogEvents;
 import org.eclipse.virgo.nano.deployer.SimpleDeployer;
 import org.eclipse.virgo.nano.deployer.api.core.ApplicationDeployer;
 import org.eclipse.virgo.nano.deployer.api.core.DeployerConfiguration;
@@ -21,12 +21,16 @@
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.Version;
 import org.osgi.service.component.ComponentContext;
+import org.osgi.service.event.Event;
+import org.osgi.service.event.EventAdmin;
 import org.osgi.service.packageadmin.PackageAdmin;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 public class StandardApplicationDeployer implements ApplicationDeployer {
 
+    private static final String TOPIC_RECOVERY_COMPLETED = "org/eclipse/virgo/kernel/deployer/recovery/COMPLETED";
+    
     private EventLogger eventLogger;
 
     private PackageAdmin packageAdmin;
@@ -43,20 +47,14 @@
 
     private BundleDeployer defaultDeployer;
 
+    private EventAdmin eventAdmin;
+
     public void activate(ComponentContext context) throws Exception {
         this.bundleContext = context.getBundleContext();
         this.defaultDeployer = new BundleDeployer(context.getBundleContext(), this.packageAdmin, this.eventLogger);
         this.simpleDeployers.add(this.defaultDeployer);
         
-        String frameworkStartTimeString = this.bundleContext.getProperty("eclipse.startTime");
-        if (frameworkStartTimeString != null) {
-            Long frameworkStartTime = Long.valueOf(frameworkStartTimeString);
-            long sinceStart = System.currentTimeMillis() - frameworkStartTime;
-            this.eventLogger.log(NanoLogEvents.NANO_STARTED, String.valueOf(sinceStart/1000) + "." + String.valueOf(sinceStart%1000));
-        } else {
-            this.eventLogger.log(NanoLogEvents.NANO_STARTED_NOTIME);
-        }
-        
+        recoveryComplete();
         initialiseHotDeployer();
 
         // TODO register the deployer MBean when the management classes are factored out in a new bundle.
@@ -240,6 +238,14 @@
     public void unbindEventLogger(EventLogger logger) {
         this.eventLogger = null;
     }
+    
+    public void bindEventAdmin(EventAdmin admin) {
+        this.eventAdmin = admin;
+    }
+
+    public void unbindEventAdmin(EventAdmin admin) {
+        this.eventAdmin = null;
+    }
 
     public void bindKernelConfig(KernelConfig config) {
         this.kernelConfig = config;
@@ -315,4 +321,8 @@
         }
     }
 
+    private void recoveryComplete() {
+        eventAdmin.postEvent(new Event(TOPIC_RECOVERY_COMPLETED, (Map<String, ?>)null));
+    }
+    
 }
diff --git a/org.eclipse.virgo.nano.deployer/src/main/resources/EventLogMessages.properties b/org.eclipse.virgo.nano.deployer/src/main/resources/EventLogMessages.properties
index 9bf7d9d..34f92f6 100644
--- a/org.eclipse.virgo.nano.deployer/src/main/resources/EventLogMessages.properties
+++ b/org.eclipse.virgo.nano.deployer/src/main/resources/EventLogMessages.properties
@@ -28,6 +28,3 @@
 HD0003E = Hot re-deploy failed for file '{}'.
 HD0004E = Hot un-deploy failed for file '{}'.
 HD0005I = Hot deploy not performed because file '{}' is already deployed.
-#Virgo Nano
-VN0001I = Server ready. Started for {}s.
-VN0002I = Server ready.
\ No newline at end of file
diff --git a/org.eclipse.virgo.nano.deployer/src/main/resources/OSGI-INF/org.eclipse.virgo.nano.deployer.xml b/org.eclipse.virgo.nano.deployer/src/main/resources/OSGI-INF/org.eclipse.virgo.nano.deployer.xml
index d05cd57..fbbbf50 100644
--- a/org.eclipse.virgo.nano.deployer/src/main/resources/OSGI-INF/org.eclipse.virgo.nano.deployer.xml
+++ b/org.eclipse.virgo.nano.deployer/src/main/resources/OSGI-INF/org.eclipse.virgo.nano.deployer.xml
@@ -9,6 +9,13 @@
 		bind="bindEventLogger" 
 		unbind="unbindEventLogger"/>
 		
+	<reference name="EventAdmin"
+		interface="org.osgi.service.event.EventAdmin"
+		policy="static"
+		cardinality="1..1" 
+		bind="bindEventAdmin" 
+		unbind="unbindEventAdmin"/>
+		
 	<reference name="PackageAdmin"
 		interface="org.osgi.service.packageadmin.PackageAdmin"
 		policy="static" 
diff --git a/org.eclipse.virgo.nano.deployer/template.mf b/org.eclipse.virgo.nano.deployer/template.mf
index 57d1a6a..e94b120 100644
--- a/org.eclipse.virgo.nano.deployer/template.mf
+++ b/org.eclipse.virgo.nano.deployer/template.mf
@@ -9,6 +9,7 @@
  org.eclipse.virgo.util.*;version="${org.eclipse.virgo.util:[=.=.=, =.+1)}",
  org.eclipse.virgo.medic.*;version="${org.eclipse.virgo.medic:[=.=.=, =.+1)}",
  org.osgi.framework.*;version="0",
+ org.osgi.service.*;version="0",
  javax.management.*;version="0",
  javax.xml.*;version="0",
  org.xml.*;version="0",