Improved adding and moving contacts.

cusax-fix
Damian Minkov 19 years ago
parent 8ac3b03cc1
commit 4106921ada

@ -356,9 +356,8 @@ ContactYahooImpl findContact(String id)
while (contacts.hasNext())
{
ContactYahooImpl item = (ContactYahooImpl) contacts.next();
if(YahooSession.getYahooUserID(item.getAddress())
.equals(YahooSession.getYahooUserID(id)))
if(item.getID().equals(YahooSession.getYahooUserID(id)))
return item;
}
return null;

@ -18,17 +18,19 @@ public class ContactYahooImpl
implements Contact
{
private YahooUser contact = null;
private boolean isLocal = false;
private byte[] image = null;
private PresenceStatus status = YahooStatusEnum.OFFLINE;
private ServerStoredContactListYahooImpl ssclCallback = null;
private boolean isPersistent = false;
private boolean isResolved = false;
private boolean isVolatile = false;
private String tempId = null;
private String yahooID = null;
private String id = null;
/**
* Creates an YahooContactImpl
* Creates an YahooContactImpl with custom yahooID
* @param contactID sets the contact Id if its different from the YahooUser id
* @param contact the contact object that we will be encapsulating.
* @param ssclCallback a reference to the ServerStoredContactListImpl
* instance that created us.
@ -37,28 +39,61 @@ public class ContactYahooImpl
* the server contact list
*/
ContactYahooImpl(
String yahooID,
YahooUser contact,
ServerStoredContactListYahooImpl ssclCallback,
boolean isPersistent,
boolean isResolved)
{
this.yahooID = yahooID;
this.contact = contact;
this.isLocal = isLocal;
this.ssclCallback = ssclCallback;
this.isPersistent = isPersistent;
this.isResolved = isResolved;
if(contact != null)
id = contact.getId();
else if(yahooID != null)
id = YahooSession.getYahooUserID(yahooID);
}
/**
* Creates an YahooContactImpl
* @param contact the contact object that we will be encapsulating.
* @param ssclCallback a reference to the ServerStoredContactListImpl
* instance that created us.
* @param isPersistent determines whether this contact is persistent or not.
* @param isResolved specifies whether the contact has been resolved against
* the server contact list
*/
ContactYahooImpl(
YahooUser contact,
ServerStoredContactListYahooImpl ssclCallback,
boolean isPersistent,
boolean isResolved)
{
this(null, contact, ssclCallback, isPersistent, isResolved);
}
/**
* Creates volatile or unresolved contact
*/
ContactYahooImpl(
String id,
ServerStoredContactListYahooImpl ssclCallback,
boolean isPersistent)
boolean isResolved,
boolean isPersistent,
boolean isVolatile)
{
this.tempId = id;
this.isLocal = isLocal;
this.yahooID = id;
this.ssclCallback = ssclCallback;
this.isPersistent = isPersistent;
this.isResolved = false;
this.isResolved = isResolved;
this.isVolatile = isVolatile;
if(id != null)
this.id = YahooSession.getYahooUserID(yahooID);
}
/**
@ -67,22 +102,37 @@ public class ContactYahooImpl
*/
public String getAddress()
{
if(isResolved)
// if the contact is volatile or with custom id return it
if(yahooID != null)
return yahooID;
// otherwise return the supplied contact id
else
return contact.getId();
else
return tempId;
}
/**
* Determines whether or not this Contact instance represents the user used
* by this protocol provider to connect to the service.
*
* @return true if this Contact represents us (the local user) and false
* otherwise.
* Returns the custom yahooID if set
*/
public boolean isLocal()
String getYahooID()
{
return isLocal;
return yahooID;
}
/**
* Returns the contact Id.
* If contact missing the yahooID without @yahoo.com part is returned
*/
String getID()
{
return id;
}
/**
* Returns whether the contact is volatile.
*/
boolean isVolatile()
{
return isVolatile;
}
public byte[] getImage()
@ -112,10 +162,9 @@ public boolean equals(Object obj)
{
if (obj == null
|| !(obj instanceof ContactYahooImpl)
|| !(YahooSession.getYahooUserID(((ContactYahooImpl)obj).getAddress())
.equals(YahooSession.getYahooUserID(getAddress()))
|| !( ((ContactYahooImpl)obj).getID().equals(getID()) )
&& ((ContactYahooImpl)obj).getProtocolProvider()
== getProtocolProvider()))
== getProtocolProvider())
return false;
else
return true;
@ -170,10 +219,7 @@ public PresenceStatus getPresenceStatus()
*/
public String getDisplayName()
{
if(isResolved)
return getAddress();
else
return tempId;
return getAddress();
}
/**
@ -239,6 +285,7 @@ void setResolved(YahooUser entry)
this.isResolved = true;
contact = entry;
isVolatile = false;
}
/**

@ -65,16 +65,13 @@ public class ServerStoredContactListYahooImpl
private ContactListModListenerImpl contactListModListenerImpl
= new ContactListModListenerImpl();
/**
* indicates whether or not the contactlist is initialized and ready.
*/
private boolean isInitialized = false;
/**
* Handler for incoming authorization requests.
*/
private AuthorizationHandler handler = null;
private Hashtable addedCustomYahooIds = new Hashtable();
/**
* Creates a ServerStoredContactList wrapper for the specified BuddyList.
@ -93,24 +90,6 @@ public class ServerStoredContactListYahooImpl
this.yahooProvider = provider;
rootGroup.setOwnerProvider(provider);
// listens for provider registered events to set the isInitialized state
// of the contact list
provider.addRegistrationStateChangeListener(
new RegistrationStateChangeListener()
{
public void registrationStateChanged(
RegistrationStateChangeEvent evt)
{
if (evt.getNewState() == RegistrationState.UNREGISTERED
|| evt.getNewState() == RegistrationState.AUTHENTICATION_FAILED
|| evt.getNewState() == RegistrationState.CONNECTION_FAILED)
{
isInitialized = false;
}
}
}
);
}
/**
@ -381,7 +360,8 @@ public void addContact(final ContactGroupYahooImpl parent, String id)
OperationFailedException.SUBSCRIPTION_ALREADY_EXISTS);
}
createUnresolvedContact(parent, id);
if(id.indexOf("@") > -1 )
addedCustomYahooIds.put(YahooSession.getYahooUserID(id), id);
try
{
@ -405,14 +385,13 @@ public void addContact(final ContactGroupYahooImpl parent, String id)
*/
ContactYahooImpl createVolatileContact(String id)
{
ContactYahooImpl newVolatileContact
= new ContactYahooImpl(id, this, false);
ContactYahooImpl newVolatileContact =
new ContactYahooImpl(id, this, false, false, true);
//Check whether a volatile group already exists and if not create
//one
//Check whether a volatile group already exists and if not create one
ContactGroupYahooImpl theVolatileGroup = getNonPersistentGroup();
//if the parent group is null then add necessary create the group
//if the parent group is null then create it
if (theVolatileGroup == null)
{
theVolatileGroup = new VolatileContactGroupYahooImpl(
@ -452,7 +431,7 @@ ContactYahooImpl createVolatileContact(String id)
ContactYahooImpl createUnresolvedContact(ContactGroup parentGroup, String id)
{
ContactYahooImpl newUnresolvedContact
= new ContactYahooImpl(id, this, true);
= new ContactYahooImpl(id, this, false, false, false);
if(parentGroup instanceof ContactGroupYahooImpl)
((ContactGroupYahooImpl)parentGroup).
@ -522,14 +501,14 @@ public void removeGroup(ContactGroupYahooImpl groupToRemove)
logger.trace("removing group " + groupToRemove);
// if its not persistent group just remove it
if(!groupToRemove.isPersistent())
if(!groupToRemove.isPersistent() || !groupToRemove.isResolved())
{
rootGroup.removeSubGroup(groupToRemove);
fireGroupEvent(groupToRemove,
ServerStoredGroupEvent.GROUP_REMOVED_EVENT);
return;
}
Vector contacts = groupToRemove.getSourceGroup().getMembers();
if(contacts.size() == 0)
@ -565,6 +544,17 @@ public void removeGroup(ContactGroupYahooImpl groupToRemove)
void removeContact(ContactYahooImpl contactToRemove)
{
logger.trace("Removing yahoo contact " + contactToRemove.getSourceContact());
if(contactToRemove.isVolatile())
{
ContactGroupYahooImpl parent =
(ContactGroupYahooImpl)contactToRemove.getParentContactGroup();
parent.removeContact(contactToRemove);
fireContactRemoved(parent, contactToRemove);
return;
}
try
{
yahooSession.removeFriend(
@ -608,10 +598,9 @@ public void renameGroup(ContactGroupYahooImpl groupToRename, String newName)
public void moveContact(ContactYahooImpl contact,
ContactGroupYahooImpl newParent)
{
String userID = contact.getID();
try
{
String userID = contact.getAddress();
contactListModListenerImpl.
waitForMove(userID,
contact.getParentContactGroup().getGroupName());
@ -622,8 +611,7 @@ public void moveContact(ContactYahooImpl contact,
}
catch(IOException ex)
{
contactListModListenerImpl.
removeWaitForMove(contact.getSourceContact().getId());
contactListModListenerImpl.removeWaitForMove(userID);
logger.error("Contact cannot be added " + ex.getMessage());
}
}
@ -727,18 +715,6 @@ void fireContactResolved( ContactGroup parentGroup,
SubscriptionEvent.SUBSCRIPTION_RESOLVED, contact, parentGroup);
}
/**
* Returns true if the contact list is initialized and
* ready for use, and false otherwise.
*
* @return true if the contact list is initialized and ready for use and false
* otherwise
*/
boolean isInitialized()
{
return isInitialized;
}
/**
* When the protocol is online this method is used to fill or resolve
* the current contact list
@ -832,7 +808,7 @@ public void friendAddedReceived(SessionFriendEvent ev)
findContactGroup(ev.getGroup());
if(group == null){
logger.trace("Group not found!" + group);
logger.trace("Group not found!" + ev.getGroup());
return;
}
@ -849,44 +825,36 @@ public void friendAddedReceived(SessionFriendEvent ev)
return;
}
String contactID = ev.getFriend().getId();
ContactYahooImpl contactToAdd = findContactById(contactID);
boolean isVolatile = false;
if(contactToAdd == null)
{
contactToAdd =
new ContactYahooImpl(ev.getFriend(),
ServerStoredContactListYahooImpl.this, true, true);
}
else
if(!contactToAdd.isPersistent())
if(addedCustomYahooIds.containsKey(contactID))
{
// we must remove the volatile buddy as we will add
// the persistent one.
// Volatile buddy is moving from the volatile group
// to the new one
ContactGroupYahooImpl parent =
(ContactGroupYahooImpl)contactToAdd.getParentContactGroup();
parent.removeContact(contactToAdd);
contactToAdd.setPersistent(true);
contactToAdd.setResolved(ev.getFriend());
group.addContact(contactToAdd);
fireContactMoved(parent, group, contactToAdd);
waitMove.remove(contactID);
return;
String expectedContactID =
(String)addedCustomYahooIds.remove(contactID);
contactToAdd =
new ContactYahooImpl(expectedContactID, ev.getFriend(),
ServerStoredContactListYahooImpl.this, true, true);
}
else if(!contactToAdd.isResolved())
else
{
// the contact is already created just resole it
contactToAdd.setResolved(ev.getFriend());
return;
contactToAdd =
new ContactYahooImpl(ev.getFriend(),
ServerStoredContactListYahooImpl.this, true, true);
}
}
else
{
isVolatile = contactToAdd.isVolatile();
}
//first check is contact is moving from a group
Object isWaitingForMove = waitMove.get(contactID);
if(isWaitingForMove != null && isWaitingForMove instanceof String)
@ -907,12 +875,34 @@ else if(!contactToAdd.isResolved())
logger.info("Cannot Remove(till moving) contact :" +
contactToAdd + " from group " + oldParent);
}
return;
}
else
if(isVolatile)
{
// we must remove the volatile buddy as we will add
// the persistent one.
// Volatile buddy is moving from the volatile group
// to the new one
ContactGroupYahooImpl parent =
(ContactGroupYahooImpl)contactToAdd.getParentContactGroup();
parent.removeContact(contactToAdd);
fireContactRemoved(parent, contactToAdd);
contactToAdd.setPersistent(true);
contactToAdd.setResolved(ev.getFriend());
group.addContact(contactToAdd);
fireContactAdded(group, contactToAdd);
waitMove.remove(contactID);
return;
}
group.addContact(contactToAdd);
fireContactAdded(group, contactToAdd);
}
/**
@ -980,7 +970,7 @@ public void contactRequestReceived(SessionEvent ev)
ContactYahooImpl contact = findContactById(ev.getFrom());
if(contact == null)
contact = parentOperationSet.createVolatileContact(ev.getFrom());
contact = createVolatileContact(ev.getFrom());
AuthorizationRequest request = new AuthorizationRequest();
request.setReason(ev.getMessage());

@ -284,32 +284,38 @@ public void subtestStateTransition( YahooStatusEnum newStatus)
//First register a listener to make sure that all corresponding
//events have been generated.
PresenceStatusEventCollector statusEventCollector
PresenceStatusEventCollector statusEventCollector1
= new PresenceStatusEventCollector();
ContactPresenceEventCollector statusEventCollector2
= new ContactPresenceEventCollector(fixture.userID2, newStatus);
operationSetPresence1.addProviderPresenceStatusListener(
statusEventCollector);
statusEventCollector1);
operationSetPresence2.addContactPresenceStatusListener(
statusEventCollector2);
//change the status
operationSetPresence1.publishPresenceStatus(newStatus, null);
pauseAfterStateChanges();
//test event notification.
statusEventCollector.waitForPresEvent(10000);
statusEventCollector1.waitForPresEvent(10000);
statusEventCollector2.waitForEvent(10000);
operationSetPresence1.removeProviderPresenceStatusListener(
statusEventCollector);
statusEventCollector1);
operationSetPresence2.removeContactPresenceStatusListener(
statusEventCollector2);
assertEquals("Events dispatched during an event transition.",
1, statusEventCollector.collectedPresEvents.size());
1, statusEventCollector1.collectedPresEvents.size());
assertEquals("A status changed event contained wrong old status.",
oldStatus,
((ProviderPresenceStatusChangeEvent)
statusEventCollector.collectedPresEvents.get(0))
statusEventCollector1.collectedPresEvents.get(0))
.getOldStatus());
assertEquals("A status changed event contained wrong new status.",
newStatus,
((ProviderPresenceStatusChangeEvent)
statusEventCollector.collectedPresEvents.get(0))
statusEventCollector1.collectedPresEvents.get(0))
.getNewStatus());
// verify that the operation set itself is aware of the status change
@ -342,67 +348,6 @@ private void pauseAfterStateChanges()
logger.debug("Pausing between state changes was interrupted", ex);
}
}
/**
* Verifies that querying status works fine. The tester agent would
* change status and the operation set would have to return the right status
* after every change.
*
* @throws java.lang.Exception if one of the transitions fails
*/
public void testQueryContactStatus()
throws Exception
{
// --- NA ---
logger.debug("Will Query an BRB contact.");
subtestQueryContactStatus(YahooStatusEnum.BE_RIGHT_BACK,
YahooStatusEnum.BE_RIGHT_BACK);
// --- DND ---
logger.debug("Will Query a Busy contact.");
subtestQueryContactStatus(YahooStatusEnum.BUSY,
YahooStatusEnum.BUSY);
// --- FFC ---
logger.debug("Will Query a Idle contact.");
subtestQueryContactStatus(YahooStatusEnum.IDLE,
YahooStatusEnum.IDLE);
// --- INVISIBLE ---
logger.debug("Will Query an Invisible contact.");
subtestQueryContactStatus(YahooStatusEnum.INVISIBLE,
YahooStatusEnum.OFFLINE);
// --- Online ---
logger.debug("Will Query an Online contact.");
subtestQueryContactStatus(YahooStatusEnum.AVAILABLE,
YahooStatusEnum.AVAILABLE);
}
/**
* Used by functions testing the queryContactStatus method of the
* presence operation set.
* @param status the status as specified, that
* the tester agent should switch to.
* @param expectedReturn the PresenceStatus that the presence operation
* set should see the tester agent in once it has switched to taStatusLong.
*
* @throws java.lang.Exception if querying the status causes some exception.
*/
public void subtestQueryContactStatus(PresenceStatus status,
PresenceStatus expectedReturn)
throws Exception
{
operationSetPresence2.publishPresenceStatus(status, "status message");
pauseAfterStateChanges();
PresenceStatus actualReturn
= operationSetPresence1.queryContactStatus(fixture.userID2);
assertEquals("Querying a "
+ expectedReturn.getStatusName()
+ " state did not return as expected"
, expectedReturn, actualReturn);
}
/**
* The method would add a subscription for a contact, wait for a
@ -879,6 +824,10 @@ public void waitForEvent(long waitFor)
try{
wait(waitFor);
if(collectedEvents.size() > 0)
logger.trace("Received a change in contact status.");
else
logger.trace("No change received for "+waitFor+"ms.");
}
catch (InterruptedException ex)
{

Loading…
Cancel
Save