aboutsummaryrefslogtreecommitdiffstats
path: root/jdisc_core/src/main/java/com/yahoo/jdisc/application/ServerRepository.java
blob: 5098a1a9f76c72cf2f64813410cd060aa9d737d2 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.jdisc.application;

import com.yahoo.jdisc.Container;
import com.yahoo.jdisc.service.ServerProvider;
import org.osgi.framework.Bundle;

import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Logger;

/**
 * This is a repository of {@link ServerProvider}s. An instance of this class is owned by the {@link ContainerBuilder},
 * and is used to configure the set of ServerProviders that eventually become part of the active {@link Container}.
 *
 * @author Simon Thoresen Hult
 */
public class ServerRepository implements Iterable<ServerProvider> {

    private static final Logger log = Logger.getLogger(ServerRepository.class.getName());
    private final List<ServerProvider> servers = new LinkedList<>();
    private final GuiceRepository guice;

    public ServerRepository(GuiceRepository guice) {
        this.guice = guice;
    }

    public Iterable<ServerProvider> activate() { return List.copyOf(servers); }

    public List<ServerProvider> installAll(Bundle bundle, Iterable<String> serverNames) throws ClassNotFoundException {
        List<ServerProvider> lst = new LinkedList<>();
        for (String serverName : serverNames) {
            lst.add(install(bundle, serverName));
        }
        return lst;
    }

    public ServerProvider install(Bundle bundle, String serverName) throws ClassNotFoundException {
        log.finer("Installing server provider '" + serverName + "'.");
        Class<?> namedClass = bundle.loadClass(serverName);
        Class<ServerProvider> serverClass = ContainerBuilder.safeClassCast(ServerProvider.class, namedClass);
        ServerProvider server = guice.getInstance(serverClass);
        install(server);
        return server;
    }

    public void installAll(Iterable<? extends ServerProvider> servers) {
        for (ServerProvider server : servers) {
            install(server);
        }
    }

    public void install(ServerProvider server) {
        servers.add(server);
    }

    public void uninstallAll(Iterable<? extends ServerProvider> handlers) {
        for (ServerProvider handler : handlers) {
            uninstall(handler);
        }
    }

    public void uninstall(ServerProvider handler) {
        servers.remove(handler);
    }

    public Collection<ServerProvider> collection() {
        return List.copyOf(servers);
    }

    @Override
    public Iterator<ServerProvider> iterator() {
        return collection().iterator();
    }
}