Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2008, 2010 VMware Inc. All rights reserved. This program and the accompanying materials are made available under the terms of the Eclipse Public License v1.0 which accompanies this distribution, and is available at http://www.eclipse.org/legal/epl-v10.html Contributors: VMware Inc. - initial contribution /
 
 
 package org.eclipse.virgo.nano.core.internal;
 
 import java.util.Map;
 
 
 import  org.osgi.service.event.Event;
 import  org.osgi.service.event.EventAdmin;
StartupTracker tracks the startup of the Kernel and produces event log entries, and EventAdmin events as the kernel starts.

Concurrent Semantics
Thread-safe.

 
 final class StartupTracker {
 
     private static final String THREAD_NAME_STARTUP_TRACKER = "startup-tracker";
 
 	static final String APPLICATION_CONTEXT_FILTER = "(objectClass=org.springframework.context.ApplicationContext)";
 
     private static final String KERNEL_EVENT_TOPIC = "org/eclipse/virgo/kernel/";
 
     private static final String KERNEL_EVENT_STARTING =  + "STARTING";
 
     private static final String KERNEL_EVENT_STARTED =  + "STARTED";
 
     private static final String KERNEL_EVENT_START_TIMED_OUT =  + "START_TIMED_OUT";
     
     private static final String KERNEL_EVENT_START_ABORTED =  + "START_ABORTED";
     
     private static final String KERNEL_EVENT_START_FAILED =  + "START_FAILED";
     
     private static final String KERNEL_BSN_PREFIX = "org.eclipse.virgo.kernel";
     
     private static final String NANO_CORE_BSN_PREFIX = "org.eclipse.virgo.nano.core";
     
     private static final String NANO_AUTH_BSN_PREFIX = "org.eclipse.virgo.nano.authentication";
     
     private static final Logger LOGGER = LoggerFactory.getLogger(StartupTracker.class);
 
     private final KernelStatus status = new KernelStatus();
     
     private final KernelConfiguration configuration;
 
     private final Thread startupTrackingThread;
     
     private volatile ObjectInstance statusInstance;
 
     StartupTracker(BundleContext contextKernelConfiguration configurationint startupWaitTimeBundleStartTracker asyncBundleStartTrackerShutdown shutdownDumpGenerator dumpGenerator) {
     	Runnable startupTrackingRunnable = new StartupTrackingRunnable(contextstartupWaitTimeasyncBundleStartTrackerthis.shutdowndumpGenerator);
         this. = new Thread(startupTrackingRunnable);
         this. = configuration;
     }
 
     void start() {
         registerKernelStatusMBean();
         this..start();
     }
     
     void stop() {
         unregisterKernelStatusMBean();
     }
 
     private void registerKernelStatusMBean() {
        MBeanServer server = ManagementFactory.getPlatformMBeanServer();
        try {
            ObjectName name = ObjectName.getInstance(StartupTracker.this..getDomain(), "type""KernelStatus");
            this. = server.registerMBean(this.name);
        } catch (JMException e) {
            throw new FatalKernelException("Unable to register KernelStatus MBean"e);
        }
    }
    private void unregisterKernelStatusMBean() {
        MBeanServer server = ManagementFactory.getPlatformMBeanServer();
        try {
            ObjectInstance instance = this.;
            if (instance != null && server.isRegistered(instance.getObjectName())) {
                server.unregisterMBean(this..getObjectName());
            }
        } catch (JMException e) {
            throw new FatalKernelException("Unable to unregister KernelStatus MBean"e);
        }
    }
    private final static class StartupTrackingRunnable implements Runnable {        
        private final BundleContext context;
        
        private final int startupWaitTime;
        
        private final BundleStartTracker asyncBundleStartTracker;
        private final KernelStatus kernelStatus;
        private final Shutdown shutdown;
        private final DumpGenerator dumpGenerator;
        
        private final ServiceReferenceTracker serviceReferenceTracker;
        
        private EventLogger eventLogger = null;
        private EventAdmin eventAdmin = null;
        
        private StartupTrackingRunnable(BundleContext contextint startupWaitTimeBundleStartTracker asyncBundleStartTrackerKernelStatus kernelStatusShutdown shutdownDumpGenerator dumpGenerator) {
            this. = context;
            this. = startupWaitTime;
            this. = asyncBundleStartTracker;
            this. = kernelStatus;
            this. = shutdown;
            this. = dumpGenerator;
            this. = new ServiceReferenceTracker(context);
        }
        public void run() {
            this. = getEventLoggerService();
            this. = getEventAdminService();
            try {
                kernelStarting();
                
                Bundle[] bundles = this..getBundles();
                
                try {
                    long waitTime = ..toMillis(this.);
                    for (Bundle bundle : bundles) {
                        if (!BundleUtils.isFragmentBundle(bundle) && isKernelBundle(bundle)) {
                            BlockingAbortableSignal signal = new BlockingAbortableSignal();
                            this..trackStart(bundlesignal);
                            .debug("Awaiting startup of bundle {} for up to {} milliseconds with signal {}."new Object[]{bundlewaitTimesignal});
                            long startTime = System.currentTimeMillis();
                            boolean bundleStarted = signal.awaitCompletion(waitTime.);
                            waitTime -= System.currentTimeMillis() - startTime;
                            if (!bundleStarted) {
                                if(signal.isAborted()){
                                    .error("Bundle {} aborted before the Kernel timeout of {} seconds with {} seconds remaining."new Object[]{bundlethis...toSeconds(waitTime)});
                                    kernelStartAborted(bundle);
                                } else if (waitTime <= 0) {
                                    .error("Kernel has failed to start before the timeout of {} seconds."this.);
                                    kernelStartTimedOut();
                                } else {
                                    .error("Bundle {} did not start within the Kernel timeout of {} seconds."bundlethis.);
                                    kernelStartTimedOut();
                                }
                                return;
                            }
                        }
                    }
                } catch (FailureSignalledException fse) {
                    kernelStartFailed(fse.getCause());
                    return;
                } catch (Exception e) {
                    kernelStartFailed(e);
                    return;
                }
                
                kernelStarted();
            } finally {
                this..ungetAll();
            }
        }
        @SuppressWarnings("unchecked")
        private EventLogger getEventLoggerService() {
            EventLogger eventLogger = null;
            ServiceReference<EventLoggereventLoggerServiceReference = (ServiceReference<EventLogger>) this..getServiceReference(EventLogger.class.getName());
            if (eventLoggerServiceReference != null) {
                eventLogger = (EventLoggerthis..getService(this..track(eventLoggerServiceReference));
            }
            return eventLogger;
        }
        
        @SuppressWarnings("unchecked")
        private EventAdmin getEventAdminService() {
            EventAdmin eventAdmin = null;
            ServiceReference<EventAdmin> eventAdminServiceReference = (ServiceReference<EventAdmin>) this..getServiceReference(EventAdmin.class.getName());
            if (eventAdminServiceReference != null) {
                eventAdmin = (EventAdmin) this..getService(this..track(eventAdminServiceReference));
            }
            return eventAdmin;
        }
        
        private boolean isKernelBundle(Bundle bundle) {
        	String symbolicName = bundle.getSymbolicName();
			return symbolicName != null && (symbolicName.startsWith() || 
											symbolicName.startsWith() || 
        }
        private void kernelStarting() {
            postEvent();
            logEvent(.);
        }
        private void kernelStarted() {
            this..setStarted();
            postEvent();
            logEvent(.);
        }
        
        private void kernelStartAborted(Bundle bundle) {
            postEvent();
            logEvent(.bundle.getSymbolicName(), bundle.getVersion());
            generateDumpAndShutdown("startupTimedOut"null);
        }
        
        private void kernelStartTimedOut() {
            postEvent();
            generateDumpAndShutdown("startupTimedOut"null);
        }
        
        private void kernelStartFailed(Throwable failure) {
            postEvent();
            logEvent(.failure);
            generateDumpAndShutdown("startupFailed"failure);
        }
        
        private void generateDumpAndShutdown(String causeThrowable failure) {
            if (failure != null) {
                this..generateDump(causefailure);
            } else {
                this..generateDump(cause);
            }
            this..immediateShutdown();
        }
        
        private void logEvent(KernelLogEvents eventThrowable throwableObject...args) {
            if (this. != null) {
                this..log(eventthrowableargs);
            }
        }
        private void logEvent(KernelLogEvents eventObject... args) {
            this.logEvent(eventnullargs);
        }
        private void postEvent(String topic) {
            if (this. != null) {
                this..postEvent(new Event(topic, (Map<String, ?>)null));
            }
        }
    }    
New to GrepCode? Check out our FAQ X