// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.jdisc.application;
import com.google.common.collect.ImmutableList;
import com.yahoo.jdisc.Container;
import com.yahoo.jdisc.service.ServerProvider;
import org.osgi.framework.Bundle;
import java.util.*;
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
*/
public class ServerRepository implements Iterable {
private static final Logger log = Logger.getLogger(ServerRepository.class.getName());
private final List servers = new LinkedList<>();
private final GuiceRepository guice;
public ServerRepository(GuiceRepository guice) {
this.guice = guice;
}
public Iterable activate() { return ImmutableList.copyOf(servers); }
public List installAll(Bundle bundle, Iterable serverNames) throws ClassNotFoundException {
List 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 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 collection() {
return Collections.unmodifiableCollection(servers);
}
@Override
public Iterator iterator() {
return collection().iterator();
}
}