Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 4a2d4367733b00fe698cebce0f3c9a4bea93dd5e (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
/*******************************************************************************
 * Copyright (c) 2013, 2017 SAP AG and others.
 * 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:
 * 	   Lazar Kirchev, SAP AG - initial API and implementation   
 *******************************************************************************/
package org.eclipse.equinox.console.commands;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import org.apache.felix.service.command.CommandProcessor;
import org.osgi.framework.BundleContext;
import org.osgi.framework.Filter;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceReference;
import org.osgi.util.tracker.ServiceTracker;
import org.osgi.util.tracker.ServiceTrackerCustomizer;

public class CommandsTracker {
    private Set<String> commandNames;
    private ServiceTracker<Object, Set<String>> commandsTracker = null;
    private static final Object lock = new Object(); 
    
    public CommandsTracker(BundleContext bundleContext) {
    	commandNames = Collections.synchronizedSet(new HashSet<String>());
    	try {
			Filter filter = bundleContext.createFilter(String.format("(&(%s=*)(%s=*))", CommandProcessor.COMMAND_SCOPE, CommandProcessor.COMMAND_FUNCTION));
			commandsTracker = new ServiceTracker<>(bundleContext, filter, new CommandsTrackerCustomizer());
			commandsTracker.open();
		} catch (InvalidSyntaxException e) {
			//do nothing;
		}
    }
    
    public Set<String> getCommands() {
    	synchronized (lock) {
    		return new HashSet<>(commandNames);
    	}
    }
    
    class CommandsTrackerCustomizer implements ServiceTrackerCustomizer<Object, Set<String>> {
    	@Override
		public Set<String> addingService(ServiceReference<Object> reference) {
			Object scope = reference.getProperty(CommandProcessor.COMMAND_SCOPE);
        	Object function = reference.getProperty(CommandProcessor.COMMAND_FUNCTION);

        	if (scope != null && function != null) {
        		synchronized (lock) {
        			if (function.getClass().isArray()) {
        				for (Object func : ((Object[]) function)) {
        					commandNames.add(scope.toString() + ":" + func.toString());
        				}
        			} else {
        				commandNames.add(scope.toString() + ":" + function.toString());
        			}
        			return commandNames;
        		}
        	}
            return null;
		}

		@Override
		public void modifiedService(ServiceReference<Object> reference, Set<String> commandNames) {
			// nothing to do
		}

		@Override
		public void removedService(ServiceReference<Object> reference, Set<String> commandNames) {
			Object scope = reference.getProperty(CommandProcessor.COMMAND_SCOPE);
            Object function = reference.getProperty(CommandProcessor.COMMAND_FUNCTION);

            if (scope != null && function != null) {
                if (!function.getClass().isArray()) {
                    commandNames.remove(scope.toString() + ":" + function.toString());
                } else {
                    for (Object func : (Object[]) function) {
                        commandNames.remove(scope.toString() + ":" + func.toString());
                    }
                }
            }
		}
    	
    }
}

Back to the top