Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: f7248eadfba60bc258bed5c44f10b4df29fff823 (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
/*******************************************************************************
 * Copyright (c) 2008, 2011 Anyware Technologies 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:
 *     Anyware Technologies  - initial API and implementation
 *******************************************************************************/
package org.eclipse.tcf.internal.core;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;

import org.eclipse.tcf.internal.services.local.DiagnosticsService;
import org.eclipse.tcf.internal.services.remote.GenericProxy;
import org.eclipse.tcf.internal.services.remote.LocatorProxy;
import org.eclipse.tcf.protocol.IChannel;
import org.eclipse.tcf.protocol.IService;
import org.eclipse.tcf.protocol.IServiceProvider;
import org.eclipse.tcf.protocol.Protocol;

public class ServiceManager {

    private static final Collection<IServiceProvider> providers = new ArrayList<IServiceProvider>();

    static {
        addServiceProvider(new IServiceProvider() {

            private final String package_name = LocatorProxy.class.getPackage().getName();

            public IService[] getLocalService(IChannel channel) {
                return new IService[]{ new DiagnosticsService(channel) };
            }

            public IService getServiceProxy(IChannel channel, String service_name) {
                IService service = null;
                try {
                    Class<?> cls = Class.forName(package_name + "." + service_name + "Proxy");
                    service = (IService)cls.getConstructor(IChannel.class).newInstance(channel);
                    assert service_name.equals(service.getName());
                }
                catch (Exception x) {
                }
                return service;
            }
        });
    }

    public static String getID() {
        // In current implementation ServiceManager is a singleton,
        // so its ID is same as agent ID.
        return Protocol.getAgentID();
    }

    public static synchronized void addServiceProvider(IServiceProvider provider) {
        providers.add(provider);
    }

    public static synchronized void removeServiceProvider(IServiceProvider provider) {
        providers.remove(provider);
    }

    public static synchronized void onChannelCreated(IChannel channel, Map<String,IService> services) {
        IService zero_copy = new IService() {
            public String getName() {
                return "ZeroCopy";
            }
        };
        services.put(zero_copy.getName(), zero_copy);
        for (IServiceProvider provider : providers) {
            try {
                IService[] arr = provider.getLocalService(channel);
                if (arr == null) continue;
                for (IService service : arr) {
                    if (services.containsKey(service.getName())) continue;
                    services.put(service.getName(), service);
                }
            }
            catch (Throwable x) {
                Protocol.log("Error calling TCF service provider", x);
            }
        }
    }

    public static synchronized void onChannelOpened(IChannel channel, Collection<String> service_names, Map<String,IService> services) {
        for (String name : service_names) {
            for (IServiceProvider provider : providers) {
                try {
                    IService service = provider.getServiceProxy(channel, name);
                    if (service == null) continue;
                    services.put(name, service);
                    break;
                }
                catch (Throwable x) {
                    Protocol.log("Error calling TCF service provider", x);
                }
            }
            if (services.containsKey(name)) continue;
            services.put(name, new GenericProxy(channel, name));
        }
    }
}

Back to the top