import java.net.MalformedURLException;
import java.util.ArrayList;
+import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import net.pterodactylus.sone.data.Sone.ShowCustomAvatars;
import net.pterodactylus.sone.data.Sone.SoneStatus;
import net.pterodactylus.sone.data.TemporaryImage;
+import net.pterodactylus.sone.database.Database;
import net.pterodactylus.sone.fcp.FcpInterface;
import net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired;
import net.pterodactylus.sone.freenet.wot.Identity;
/** Whether we’re currently saving the configuration. */
private boolean storingConfiguration = false;
+ /** The database. */
+ private Database database;
+
/** The identity manager. */
private final IdentityManager identityManager;
/* synchronize access on this on localSones. */
private final Map<Sone, SoneRescuer> soneRescuers = new HashMap<Sone, SoneRescuer>();
- /** All local Sones. */
- /* synchronize access on this on itself. */
- private final Map<String, Sone> localSones = new HashMap<String, Sone>();
-
- /** All remote Sones. */
- /* synchronize access on this on itself. */
- private final Map<String, Sone> remoteSones = new HashMap<String, Sone>();
-
/** All known Sones. */
private final Set<String> knownSones = new HashSet<String>();
*
* @param configuration
* The configuration of the core
+ * @param database
+ * The database to use
* @param freenetInterface
* The freenet interface
* @param identityManager
* The identity manager
*/
- public Core(Configuration configuration, FreenetInterface freenetInterface, IdentityManager identityManager) {
+ public Core(Configuration configuration, Database database, FreenetInterface freenetInterface, IdentityManager identityManager) {
super("Sone Core");
this.configuration = configuration;
+ this.database = database;
this.freenetInterface = freenetInterface;
this.identityManager = identityManager;
this.soneDownloader = new SoneDownloader(this, freenetInterface);
*/
public SoneRescuer getSoneRescuer(Sone sone) {
Validation.begin().isNotNull("Sone", sone).check().is("Local Sone", isLocalSone(sone)).check();
- synchronized (localSones) {
+ synchronized (soneRescuers) {
SoneRescuer soneRescuer = soneRescuers.get(sone);
if (soneRescuer == null) {
soneRescuer = new SoneRescuer(this, soneDownloader, sone);
*
* @return All Sones
*/
- public Set<Sone> getSones() {
- Set<Sone> allSones = new HashSet<Sone>();
- allSones.addAll(getLocalSones());
- allSones.addAll(getRemoteSones());
- return allSones;
+ public Collection<Sone> getSones() {
+ return database.getSones();
}
/**
* Sone
*/
public Sone getSone(String id) {
- return getSone(id, true);
+ return database.getSone(id, true);
}
/**
*/
@Override
public Sone getSone(String id, boolean create) {
- if (isLocalSone(id)) {
- return getLocalSone(id);
- }
- return getRemoteSone(id, create);
+ return database.getSone(id, create);
}
/**
* otherwise
*/
public boolean hasSone(String id) {
- return isLocalSone(id) || isRemoteSone(id);
+ return database.isLocalSone(id) || database.isRemoteSone(id);
}
/**
* @return {@code true} if the given Sone is local, {@code false} otherwise
*/
public boolean isLocalSone(Sone sone) {
- synchronized (localSones) {
- return localSones.containsKey(sone.getId());
- }
+ return database.isLocalSone(sone);
}
/**
* otherwise
*/
public boolean isLocalSone(String id) {
- synchronized (localSones) {
- return localSones.containsKey(id);
- }
+ return database.isLocalSone(id);
}
/**
*
* @return All local Sones
*/
- public Set<Sone> getLocalSones() {
- synchronized (localSones) {
- return new HashSet<Sone>(localSones.values());
- }
+ public Collection<Sone> getLocalSones() {
+ return database.getLocalSones();
}
/**
* @return The Sone with the given ID, or {@code null}
*/
public Sone getLocalSone(String id, boolean create) {
- synchronized (localSones) {
- Sone sone = localSones.get(id);
- if ((sone == null) && create) {
- sone = new Sone(id);
- localSones.put(id, sone);
- }
- return sone;
- }
+ return database.getLocalSone(id, create);
}
/**
*
* @return All remote Sones
*/
- public Set<Sone> getRemoteSones() {
- synchronized (remoteSones) {
- return new HashSet<Sone>(remoteSones.values());
- }
+ public Collection<Sone> getRemoteSones() {
+ return database.getRemoteSones();
}
/**
* @return The Sone with the given ID
*/
public Sone getRemoteSone(String id, boolean create) {
- synchronized (remoteSones) {
- Sone sone = remoteSones.get(id);
- if ((sone == null) && create && (id != null) && (id.length() == 43)) {
- sone = new Sone(id);
- remoteSones.put(id, sone);
- }
- return sone;
- }
+ return database.getRemoteSone(id, create);
}
/**
* otherwise
*/
public boolean isRemoteSone(Sone sone) {
- synchronized (remoteSones) {
- return remoteSones.containsKey(sone.getId());
- }
+ return database.isRemoteSone(sone);
}
/**
* {@code false} otherwise
*/
public boolean isRemoteSone(String id) {
- synchronized (remoteSones) {
- return remoteSones.containsKey(id);
- }
+ return database.isRemoteSone(id);
}
/**
* @return All replies for the given post
*/
public List<PostReply> getReplies(Post post) {
- Set<Sone> sones = getSones();
+ Collection<Sone> sones = getSones();
List<PostReply> replies = new ArrayList<PostReply>();
for (Sone sone : sones) {
for (PostReply reply : sone.getReplies()) {
logger.log(Level.WARNING, "Given OwnIdentity is null!");
return null;
}
- synchronized (localSones) {
- final Sone sone;
- try {
- sone = getLocalSone(ownIdentity.getId()).setIdentity(ownIdentity).setInsertUri(new FreenetURI(ownIdentity.getInsertUri())).setRequestUri(new FreenetURI(ownIdentity.getRequestUri()));
- } catch (MalformedURLException mue1) {
- logger.log(Level.SEVERE, String.format("Could not convert the Identity’s URIs to Freenet URIs: %s, %s", ownIdentity.getInsertUri(), ownIdentity.getRequestUri()), mue1);
- return null;
- }
- sone.setLatestEdition(Numbers.safeParseLong(ownIdentity.getProperty("Sone.LatestEdition"), (long) 0));
- sone.setClient(new Client("Sone", SonePlugin.VERSION.toString()));
- sone.setKnown(true);
- /* TODO - load posts ’n stuff */
- localSones.put(ownIdentity.getId(), sone);
- final SoneInserter soneInserter = new SoneInserter(this, freenetInterface, sone);
- soneInserter.addSoneInsertListener(this);
- soneInserters.put(sone, soneInserter);
- sone.setStatus(SoneStatus.idle);
- loadSone(sone);
- soneInserter.start();
- return sone;
+ Sone sone;
+ try {
+ sone = getLocalSone(ownIdentity.getId()).setIdentity(ownIdentity).setInsertUri(new FreenetURI(ownIdentity.getInsertUri())).setRequestUri(new FreenetURI(ownIdentity.getRequestUri()));
+ } catch (MalformedURLException mue1) {
+ logger.log(Level.SEVERE, String.format("Could not convert the Identity’s URIs to Freenet URIs: %s, %s", ownIdentity.getInsertUri(), ownIdentity.getRequestUri()), mue1);
+ return null;
}
+ sone.setLatestEdition(Numbers.safeParseLong(ownIdentity.getProperty("Sone.LatestEdition"), (long) 0));
+ sone.setClient(new Client("Sone", SonePlugin.VERSION.toString()));
+ sone.setKnown(true);
+ database.saveSone(sone);
+ /* TODO - load posts ’n stuff */
+ final SoneInserter soneInserter = new SoneInserter(this, freenetInterface, sone);
+ soneInserter.addSoneInsertListener(this);
+ soneInserters.put(sone, soneInserter);
+ sone.setStatus(SoneStatus.idle);
+ loadSone(sone);
+ soneInserter.start();
+ return sone;
}
/**
logger.log(Level.WARNING, "Given Identity is null!");
return null;
}
- synchronized (remoteSones) {
- final Sone sone = getRemoteSone(identity.getId(), true).setIdentity(identity);
- boolean newSone = sone.getRequestUri() == null;
- sone.setRequestUri(getSoneUri(identity.getRequestUri()));
- sone.setLatestEdition(Numbers.safeParseLong(identity.getProperty("Sone.LatestEdition"), (long) 0));
+ final Sone sone = getRemoteSone(identity.getId(), true).setIdentity(identity);
+ boolean newSone = sone.getRequestUri() == null;
+ sone.setRequestUri(getSoneUri(identity.getRequestUri()));
+ sone.setLatestEdition(Numbers.safeParseLong(identity.getProperty("Sone.LatestEdition"), (long) 0));
+ if (newSone) {
+ synchronized (knownSones) {
+ newSone = !knownSones.contains(sone.getId());
+ }
+ sone.setKnown(!newSone);
if (newSone) {
- synchronized (knownSones) {
- newSone = !knownSones.contains(sone.getId());
- }
- sone.setKnown(!newSone);
- if (newSone) {
- coreListenerManager.fireNewSoneFound(sone);
- for (Sone localSone : getLocalSones()) {
- if (localSone.getOptions().getBooleanOption("AutoFollow").get()) {
- followSone(localSone, sone);
- }
+ coreListenerManager.fireNewSoneFound(sone);
+ for (Sone localSone : getLocalSones()) {
+ if (localSone.getOptions().getBooleanOption("AutoFollow").get()) {
+ followSone(localSone, sone);
}
}
}
- soneDownloader.addSone(sone);
- soneDownloaders.execute(new Runnable() {
+ }
+ database.saveSone(sone);
+ soneDownloader.addSone(sone);
+ soneDownloaders.execute(new Runnable() {
- @Override
- @SuppressWarnings("synthetic-access")
- public void run() {
- soneDownloader.fetchSone(sone, sone.getRequestUri());
- }
+ @Override
+ @SuppressWarnings("synthetic-access")
+ public void run() {
+ soneDownloader.fetchSone(sone, sone.getRequestUri());
+ }
- });
- return sone;
- }
+ });
+ return sone;
}
/**
logger.log(Level.WARNING, String.format("Tried to delete Sone of non-own identity: %s", sone));
return;
}
- synchronized (localSones) {
- if (!localSones.containsKey(sone.getId())) {
- logger.log(Level.WARNING, String.format("Tried to delete non-local Sone: %s", sone));
- return;
- }
- localSones.remove(sone.getId());
- SoneInserter soneInserter = soneInserters.remove(sone);
- soneInserter.removeSoneInsertListener(this);
- soneInserter.stop();
+ if (!database.isLocalSone(sone)) {
+ logger.log(Level.WARNING, String.format("Tried to delete non-local Sone: %s", sone));
+ return;
}
+ database.removeSone(sone.getId());
+ SoneInserter soneInserter = soneInserters.remove(sone);
+ soneInserter.removeSoneInsertListener(this);
+ soneInserter.stop();
try {
((OwnIdentity) sone.getIdentity()).removeContext("Sone");
((OwnIdentity) sone.getIdentity()).removeProperty("Sone.LatestEdition");
*/
@Override
public void serviceStop() {
- synchronized (localSones) {
- for (SoneInserter soneInserter : soneInserters.values()) {
- soneInserter.removeSoneInsertListener(this);
- soneInserter.stop();
- }
+ for (SoneInserter soneInserter : soneInserters.values()) {
+ soneInserter.removeSoneInsertListener(this);
+ soneInserter.stop();
}
updateChecker.stop();
updateChecker.removeUpdateListener(this);
}
}
}
- synchronized (remoteSones) {
- remoteSones.remove(identity.getId());
- }
+ database.removeSone(identity.getId());
coreListenerManager.fireSoneRemoved(sone);
}