// the avatars on the server.
try {
List<ContentNode> avatarList = serverCollection.getChildren();
for (ContentNode node : avatarList) {
if (node instanceof ContentResource) {
ContentResource resource = (ContentResource)node;
ImiServerAvatar serverAvatar = new ImiServerAvatar(resource);
String avatarName = serverAvatar.avatarName;
logger.info("Looking at server avatar named " +
avatarName + " with version " + serverAvatar.version);
// Check to see if the server avatar already exists in
// the known list of server avatars.
ImiServerAvatar previous = serverAvatars.put(avatarName, serverAvatar);
if (previous != null && previous.version > serverAvatar.version) {
logger.info("Found a previous version named " +
avatarName + " with version " + previous.version);
// If we somehow find a more recent avatar in our
// list, then we remove the one we just found on
// the server. (The most recent one will be added
// back to the server later).
serverAvatars.put(previous.avatarName, previous);
String fileName = serverAvatar.getFilename();
serverCollection.removeChild(fileName);
}
}
}
// Make a copy of the map of server avatars. This will serve
// as a list of all avatars that need to be downloaded from
// the server.
Map<String, ImiServerAvatar> tmpServerAvatars = new HashMap(serverAvatars);
// Loop through all of the avatars we know about locally. See
// if one by the same name exists on the server. If so, and
// the server version needs to be updated, then do so. If the
// local copy needs to be updated then do so.
synchronized (localAvatars) {
logger.info("Taking a look at all of our local avatars");
for (ImiAvatar avatar : localAvatars.values()) {
String avatarName = avatar.getName();
ImiServerAvatar serverVersion = tmpServerAvatars.get(avatarName);
logger.info("Looking at local avatar named " +
avatarName + " server version " + serverVersion);
// If the local avatar is not on the server, or if the
// version of the server is older than locally, then
// mark this avatar for upload.
if (serverVersion == null ||
serverVersion.version < avatar.getVersion()) {
logger.info("Server version for avatar named " +
avatarName + " does not exist or is older" +
" than version " + avatar.getVersion());
uploadList.add(avatar);
tmpServerAvatars.remove(avatarName);
}
else if (serverVersion.version > avatar.getVersion()) {
logger.info("Server version for avatar named " +
avatarName + " is more recent than local " +
"with server version " + serverVersion.version +
" and local version " + avatar.getVersion());
// Otherwise, if the server has a more recent copy
// of the avatar, then mark this avatar for
// download.
downloadList.add(serverVersion);
tmpServerAvatars.remove(avatarName);
}
else if (serverVersion.version == avatar.getVersion()) {
logger.info("Server version for avatar named " +
avatarName + " is same as local version " +
avatar.getVersion());
// If the two versions are the same, we just want
// to do nothing with it.
// XXX Why do we need to re-add it to the server
// avatar list? It should already be there.
tmpServerAvatars.remove(avatarName);
serverAvatars.put(avatarName, serverVersion);
}
}
}
// Avatars left in the serverAvatars map are only on the server,
// and not on the client (so the previous code block did not
// see them), so add them to the download list.
for (ImiServerAvatar serverAvatar : tmpServerAvatars.values()) {
logger.info("Adding Server avatar to download list " +
serverAvatar.avatarName + " version " +
serverAvatar.version);
downloadList.add(serverAvatar);
}
// For all of the avatar configuration files that we wish to
// upload, do so synchronously.
logger.info("Doing upload of local avatars, number to upload " +
uploadList.size());
for (ImiAvatar avatar : uploadList) {
logger.info("Uploading Local avatar to server " +
avatar.getName() + " version " + avatar.getVersion());
uploadFileImpl(avatar);
}
// Keep a list around of all of the avatars we have just
// downloaded, to be added to the set of local avatars later.
List<ImiAvatar> newAvatarList = new ArrayList();
// For all of the avatar configuration files that we wish to
// download, do so synchronously.
logger.info("Doing download of local avatars to the server," +
" number to download " + downloadList.size());
for (ImiServerAvatar serverAvatar : downloadList) {
try {
logger.info("Downloading server avatar named " +
serverAvatar.avatarName + " to file name " +
serverAvatar.resource.getName());
// Create an entry for the configuration file locally.
// Write the contents of the server version to this file.
String fileName = serverAvatar.resource.getName();
ContentResource localFile = (ContentResource) imiCollection.createChild(fileName, Type.RESOURCE);
InputStream is = serverAvatar.resource.getURL().openStream();
localFile.put(new BufferedInputStream(is));
// Create a new entry to put on the local list. These
// will be added below.
ImiAvatar newAvatar = new ImiAvatar(localFile);
newAvatarList.add(newAvatar);
logger.info("Local avatar created in resource " +
localFile.getPath());
} catch (IOException excp) {
logger.log(Level.WARNING, "Error downloading server " +
"avater named " + serverAvatar.avatarName, excp);
}