aboutsummaryrefslogtreecommitdiffstats
blob: dd64b2d0601e69a882a4099acbc3b8e308e8be6d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*******************************************************************************
 * This file is part of the Virgo Web Server.
 *
 * Copyright (c) 2010 Eclipse Foundation.
 * 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:
 *    SpringSource, a division of VMware - initial API and implementation and/or initial documentation
 *******************************************************************************/

package org.eclipse.virgo.kernel.osgicommand;

import org.eclipse.osgi.framework.console.CommandProvider;
import org.eclipse.virgo.kernel.osgi.framework.OsgiFrameworkUtils;
import org.eclipse.virgo.kernel.osgi.framework.OsgiServiceHolder;
import org.eclipse.virgo.kernel.osgicommand.internal.OsgiKernelShellCommand;
import org.eclipse.virgo.kernel.shell.CommandExecutor;
import org.eclipse.virgo.util.osgi.ServiceRegistrationTracker;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;

/**
 * {@link BundleActivator} for the osgi.console command extension bundle
 * <p />
 *
 * <strong>Concurrent Semantics</strong><br />
 * thread-safe
 *
 * @author Steve Powell
 */
public class Activator implements BundleActivator {

    private static final int COMMAND_EXECUTOR_SERVICE_WAIT = 20*1000; // 20 seconds
    
    private final ServiceRegistrationTracker registrationTracker = new ServiceRegistrationTracker();
    
    public void start(BundleContext context) throws Exception {
        Runnable runnable = new PostStartInitialisationRunnable(context, this.registrationTracker);
        Thread thread = new Thread(runnable);
        thread.setDaemon(true);
        thread.start();
    }

    public void stop(BundleContext context) throws Exception {
        this.registrationTracker.unregisterAll();
    }

    private static final int SERVICE_WAIT_PAUSE = 100;
    /**
     * Get a service which might not be immediately available
     * @param <T> type of service to get
     * @param context in which to search for service
     * @param serviceClass of service to locate
     * @param millis maximum time to delay in milliseconds
     * @return null if timeout before getting service, otherwise service
     */
    private static <T> T getPotentiallyDelayedService(BundleContext context, Class<T> serviceClass, long millis) {
        T service = null;
        
        while (service == null) {
            try {
                OsgiServiceHolder<T> serviceHolder = OsgiFrameworkUtils.getService(context, serviceClass);
                if (serviceHolder != null) {
                   service = serviceHolder.getService();
                }
            } catch (IllegalStateException e) {
                try {
                    millis -= SERVICE_WAIT_PAUSE;
                    if (millis>0) {
                        Thread.sleep(SERVICE_WAIT_PAUSE);
                    } else {
                        return null;
                    }
                } catch (InterruptedException ie) {
                }
            }
        }
        
        return service;
    }
    
    private static final class PostStartInitialisationRunnable implements Runnable {
        
        private final BundleContext context;
        
        private final ServiceRegistrationTracker registrationTracker;
        
        public PostStartInitialisationRunnable(BundleContext context, ServiceRegistrationTracker registrationTracker) {
            this.context = context;
            this.registrationTracker = registrationTracker;
        }

        /** 
         * {@inheritDoc}
         */
        public void run() {
            CommandExecutor commandExecutor = getPotentiallyDelayedService(context, CommandExecutor.class, COMMAND_EXECUTOR_SERVICE_WAIT);
            if (commandExecutor == null)
                return; // TODO: report this failure -- but where?

            this.registrationTracker.track(context.registerService(CommandProvider.class.getName(), new OsgiKernelShellCommand(commandExecutor), null));
        }
    }
}