![JAR search and dependency download from the Maven repository](/logo.png)
bt.torrent.AdhocTorrentRegistry Maven / Gradle / Ivy
package bt.torrent;
import bt.data.IDataDescriptorFactory;
import bt.data.Storage;
import bt.metainfo.Torrent;
import bt.metainfo.TorrentId;
import bt.service.IRuntimeLifecycleBinder;
import com.google.inject.Inject;
import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
/**
* Simple in-memory torrent registry, that creates new descriptors upon request.
*
*Note that this class implements a service.
* Hence, is not a part of the public API and is a subject to change.
*/
public class AdhocTorrentRegistry implements TorrentRegistry {
private IDataDescriptorFactory dataDescriptorFactory;
private IRuntimeLifecycleBinder lifecycleBinder;
private Set torrentIds;
private ConcurrentMap torrents;
private ConcurrentMap descriptors;
@Inject
public AdhocTorrentRegistry(IDataDescriptorFactory dataDescriptorFactory,
IRuntimeLifecycleBinder lifecycleBinder) {
this.dataDescriptorFactory = dataDescriptorFactory;
this.lifecycleBinder = lifecycleBinder;
this.torrentIds = ConcurrentHashMap.newKeySet();
this.torrents = new ConcurrentHashMap<>();
this.descriptors = new ConcurrentHashMap<>();
}
@Override
public Collection getTorrents() {
return Collections.unmodifiableCollection(torrents.values());
}
@Override
public Collection getTorrentIds() {
return Collections.unmodifiableCollection(torrentIds);
}
@Override
public Optional getTorrent(TorrentId torrentId) {
return Optional.ofNullable(torrents.get(torrentId));
}
@Override
public Optional getDescriptor(Torrent torrent) {
return Optional.ofNullable(descriptors.get(torrent.getTorrentId()));
}
@Override
public Optional getDescriptor(TorrentId torrentId) {
return Optional.ofNullable(descriptors.get(torrentId));
}
@Override
public TorrentDescriptor getOrCreateDescriptor(Torrent torrent, Storage storage) {
return register(torrent, storage);
}
@Override
public TorrentDescriptor register(Torrent torrent, Storage storage) {
TorrentId torrentId = torrent.getTorrentId();
DefaultTorrentDescriptor descriptor = descriptors.get(torrentId);
if (descriptor != null) {
if (descriptor.getDataDescriptor() != null) {
throw new IllegalStateException(
"Torrent already registered and data descriptor created: " + torrent.getTorrentId());
}
descriptor.setDataDescriptor(dataDescriptorFactory.createDescriptor(torrent, storage));
} else {
descriptor = new DefaultTorrentDescriptor();
descriptor.setDataDescriptor(dataDescriptorFactory.createDescriptor(torrent, storage));
DefaultTorrentDescriptor existing = descriptors.putIfAbsent(torrentId, descriptor);
if (existing != null) {
descriptor = existing;
} else {
torrentIds.add(torrentId);
addShutdownHook(torrentId, descriptor);
}
}
torrents.putIfAbsent(torrentId, torrent);
return descriptor;
}
@Override
public TorrentDescriptor register(TorrentId torrentId) {
return getDescriptor(torrentId).orElseGet(() -> {
DefaultTorrentDescriptor descriptor = new DefaultTorrentDescriptor();
DefaultTorrentDescriptor existing = descriptors.putIfAbsent(torrentId, descriptor);
if (existing != null) {
descriptor = existing;
} else {
torrentIds.add(torrentId);
addShutdownHook(torrentId, descriptor);
}
return descriptor;
});
}
private void addShutdownHook(TorrentId torrentId, TorrentDescriptor descriptor) {
lifecycleBinder.onShutdown("Closing data descriptor for torrent ID: " + torrentId, () -> {
if (descriptor.getDataDescriptor() != null) {
try {
descriptor.getDataDescriptor().close();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy