Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 2f4155a09d35a3a625fd18bf7e8f75fffe557494 (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) 2008 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.tm.internal.tcf.core;

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

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

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 void addServiceProvider(IServiceProvider provider) {
        providers.add(provider);
    }

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

    public static 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) {
            IService[] arr = provider.getLocalService(channel);
            if (arr == null) continue;
            for (IService service : arr) {
                if (services.containsKey(service.getName())) continue;
                services.put(service.getName(), service);
            }
        }
    }

    public static void onChannelOpened(IChannel channel, Collection<String> service_names, Map<String,IService> services) {
        for (String name : service_names) {
            for (IServiceProvider provider : providers) {
                IService service = provider.getServiceProxy(channel, name);
                if (service == null) continue;
                services.put(name, service);
                break;
            }
            if (!services.containsKey(name)) {
                services.put(name, new GenericProxy(channel, name));
            }
        }
    }
}

Back to the top