Пример #1
0
        internal static FolderSyncState GetSyncState(SyncStateStorage syncStateStorage, Folder syncStateParentFolder, ISyncProviderFactory syncProviderFactory, string syncStateName, Func <SyncStateStorage, StoreObject, FolderSyncStateMetadata, SyncStateInfo, ISyncProviderFactory, bool, ISyncLogger, FolderSyncState> creator, ISyncLogger syncLogger = null)
        {
            ArgumentValidator.ThrowIfNullOrEmpty("syncStateName", syncStateName);
            SyncStateInfo syncStateInfo        = new FolderSyncStateInfo(syncStateName);
            StoreObject   syncStateStoreObject = SyncState.GetSyncStateStoreObject(syncStateStorage, syncStateParentFolder, syncStateInfo, syncLogger, new PropertyDefinition[]
            {
                InternalSchema.SyncFolderSourceKey
            });

            if (syncStateStoreObject == null)
            {
                return(null);
            }
            byte[] valueOrDefault = syncStateStoreObject.GetValueOrDefault <byte[]>(InternalSchema.SyncFolderSourceKey);
            if (syncProviderFactory != null)
            {
                try
                {
                    syncProviderFactory.SetCollectionIdFromBytes(valueOrDefault);
                }
                catch (ArgumentException innerException)
                {
                    syncStateStorage.DeleteFolderSyncState(syncStateName);
                    syncStateStorage.DeviceMetadata.TryRemove(syncStateName, null);
                    throw new CorruptSyncStateException(ServerStrings.ExSyncStateCorrupted(syncStateName), innerException);
                }
            }
            FolderSyncStateMetadata folderSyncStateMetadata = FolderSyncState.GetFolderSyncStateMetadata(syncStateStorage, syncStateStoreObject.Session as MailboxSession, syncStateName, syncLogger);

            if (creator == null)
            {
                return(new FolderSyncState(syncStateStorage, syncStateStoreObject, folderSyncStateMetadata, syncStateInfo, syncProviderFactory, false, syncLogger));
            }
            return(creator(syncStateStorage, syncStateStoreObject, folderSyncStateMetadata, syncStateInfo, syncProviderFactory, false, syncLogger));
        }
Пример #2
0
        private static FolderSyncStateMetadata GetFolderSyncStateMetadata(SyncStateStorage syncStateStorage, MailboxSession mailboxSession, string name, ISyncLogger syncLogger = null)
        {
            if (syncLogger == null)
            {
                syncLogger = TracingLogger.Singleton;
            }
            SyncStateMetadata       syncState = syncStateStorage.DeviceMetadata.GetSyncState(mailboxSession, name, syncLogger);
            FolderSyncStateMetadata folderSyncStateMetadata = syncState as FolderSyncStateMetadata;

            if (folderSyncStateMetadata == null)
            {
                syncLogger.TraceDebug <SmtpAddress, string>(ExTraceGlobals.SyncProcessTracer, 0L, "[FolderSyncState.GetFolderSyncStateMetadata] SyncStateMetadata in place of FolderSyncStateMetadata for Mailbox: {0}, State: {1}.  Trying re-read...", mailboxSession.MailboxOwner.MailboxInfo.PrimarySmtpAddress, name);
                syncStateStorage.DeviceMetadata.TryRemove(name, syncLogger);
                syncState = syncStateStorage.DeviceMetadata.GetSyncState(mailboxSession, name, syncLogger);
                folderSyncStateMetadata = (syncState as FolderSyncStateMetadata);
                if (folderSyncStateMetadata == null)
                {
                    syncStateStorage.DeleteFolderSyncState(name);
                    syncStateStorage.DeviceMetadata.TryRemove(name, null);
                    throw new CorruptSyncStateException(ServerStrings.ExSyncStateCorrupted(name), new InvalidOperationException("SyncStateMetadata in place of FolderSyncStateMetadata"));
                }
                syncLogger.TraceDebug <string>(ExTraceGlobals.SyncProcessTracer, 0L, "[FolderSyncState.GetFolderSyncStateMetadata] Re-read of sync state {0} was successful.", name);
            }
            return(folderSyncStateMetadata);
        }
Пример #3
0
 public void ChangeIPMFolderId(FolderSyncStateMetadata folderSyncStateMetadata, StoreObjectId oldId, ISyncLogger syncLogger = null)
 {
     if (syncLogger == null)
     {
         syncLogger = TracingLogger.Singleton;
     }
     if (oldId != null)
     {
         FolderSyncStateMetadata folderSyncStateMetadata2;
         this.ipmToFolderSyncStateMap.TryRemove(oldId, out folderSyncStateMetadata2);
     }
     syncLogger.TraceDebug <string, string, string>(ExTraceGlobals.SyncTracer, (long)this.GetHashCode(), "[DeviceSyncStateMetadata.ChangeIPMFolderId] IPM Folder Id for collection '{0}' changed from '{1}' to '{2}'", folderSyncStateMetadata.Name, (oldId == null) ? "<NULL>" : oldId.ToBase64String(), (folderSyncStateMetadata.IPMFolderId == null) ? "<NULL>" : folderSyncStateMetadata.IPMFolderId.ToBase64String());
     if (folderSyncStateMetadata.IPMFolderId != null)
     {
         this.ipmToFolderSyncStateMap.TryAdd(folderSyncStateMetadata.IPMFolderId, folderSyncStateMetadata);
     }
 }
Пример #4
0
        public SyncStateMetadata TryRemove(string name, ISyncLogger syncLogger = null)
        {
            if (syncLogger == null)
            {
                syncLogger = TracingLogger.Singleton;
            }
            SyncStateMetadata syncStateMetadata;
            bool arg = this.syncStateMap.TryRemove(name, out syncStateMetadata);

            syncLogger.TraceDebug <string, bool>(ExTraceGlobals.SyncProcessTracer, (long)this.GetHashCode(), "[DeviceSyncStateMetadata.TryRemove] Removing '{0}'.  Success? {1}", name, arg);
            FolderSyncStateMetadata folderSyncStateMetadata = syncStateMetadata as FolderSyncStateMetadata;

            if (folderSyncStateMetadata != null && folderSyncStateMetadata.IPMFolderId != null)
            {
                this.ipmToFolderSyncStateMap.TryRemove(folderSyncStateMetadata.IPMFolderId, out folderSyncStateMetadata);
            }
            return(syncStateMetadata);
        }
Пример #5
0
        public SyncStateMetadata TryAdd(SyncStateMetadata syncStateMetadata, ISyncLogger syncLogger = null)
        {
            if (syncLogger == null)
            {
                syncLogger = TracingLogger.Singleton;
            }
            SyncStateMetadata syncStateMetadata2 = this.syncStateMap.GetOrAdd(syncStateMetadata.Name, syncStateMetadata);

            if (syncStateMetadata.GetType().IsSubclassOf(syncStateMetadata2.GetType()))
            {
                syncLogger.TraceDebug <string, string>(ExTraceGlobals.SyncProcessTracer, (long)this.GetHashCode(), "[DeviceSyncStateMetadata.TryAdd] New sync state metadata instance ({0}) is subclass of cached one ({1}).  Replacing.", syncStateMetadata.GetType().Name, syncStateMetadata2.GetType().Name);
                this.syncStateMap[syncStateMetadata.Name] = syncStateMetadata;
                syncStateMetadata2 = syncStateMetadata;
            }
            FolderSyncStateMetadata folderSyncStateMetadata = syncStateMetadata2 as FolderSyncStateMetadata;

            if (folderSyncStateMetadata != null && folderSyncStateMetadata.IPMFolderId != null)
            {
                syncLogger.TraceDebug <string>(ExTraceGlobals.SyncProcessTracer, (long)this.GetHashCode(), "[DeviceSyncStateMetadata.TryAdd] Encountered FolderSyncStateMetadata instance for '{0}'.  Adding source key to reverse mapping.", syncStateMetadata.Name);
                this.ipmToFolderSyncStateMap[folderSyncStateMetadata.IPMFolderId] = folderSyncStateMetadata;
            }
            return(syncStateMetadata2);
        }
Пример #6
0
        internal static FolderSyncState CreateSyncState(SyncStateStorage syncStateStorage, Folder syncStateParentFolder, ISyncProviderFactory syncProviderFactory, string syncFolderId, ISyncLogger syncLogger = null)
        {
            if (syncLogger == null)
            {
                syncLogger = TracingLogger.Singleton;
            }
            SyncStateInfo syncStateInfo = new FolderSyncStateInfo(syncFolderId);
            StoreObject   storeObject   = SyncState.CreateSyncStateStoreObject(syncStateStorage, syncStateInfo, syncStateParentFolder, new PropertyDefinition[]
            {
                InternalSchema.SyncFolderSourceKey
            }, new object[]
            {
                syncProviderFactory.GetCollectionIdBytes()
            }, syncLogger);

            if (syncStateStorage.DeviceMetadata.TryRemove(syncStateInfo.UniqueName, syncLogger) != null)
            {
                syncLogger.TraceDebug <DeviceIdentity, string>(ExTraceGlobals.SyncTracer, 0L, "[FolderSyncState.CreateSyncState] Removed stale cached sync state metadata for device {0}, sync state {1}", syncStateStorage.DeviceMetadata.Id, syncStateInfo.UniqueName);
            }
            FolderSyncStateMetadata syncStateMetadata = (storeObject is Item) ? new FolderSyncStateMetadata(syncStateStorage.DeviceMetadata, syncStateInfo.UniqueName, syncStateStorage.SaveOnDirectItems ? null : storeObject.ParentId, storeObject.Id.ObjectId, StoreObjectId.Deserialize(syncProviderFactory.GetCollectionIdBytes())) : new FolderSyncStateMetadata(syncStateStorage.DeviceMetadata, syncStateInfo.UniqueName, storeObject.Id.ObjectId, null, StoreObjectId.Deserialize(syncProviderFactory.GetCollectionIdBytes()));

            return(new FolderSyncState(syncStateStorage, storeObject, syncStateMetadata, syncStateInfo, syncProviderFactory, true, syncLogger));
        }
Пример #7
0
        protected static StoreObject GetSyncStateStoreObject(SyncStateStorage syncStateStorage, Folder syncStateParentFolder, SyncStateInfo syncStateInfo, byte[] identBytes, ISyncLogger syncLogger, params PropertyDefinition[] propsToReturn)
        {
            FolderSyncStateMetadata folderSyncStateMetadata = FolderSyncState.GetFolderSyncStateMetadata(syncStateStorage, syncStateParentFolder.Session as MailboxSession, syncStateInfo.UniqueName, syncLogger);

            if (folderSyncStateMetadata == null)
            {
                return(null);
            }
            StoreObject storeObject = null;

            if (folderSyncStateMetadata.StorageType != StorageType.Folder)
            {
                if (folderSyncStateMetadata.StorageType != StorageType.Item)
                {
                    if (folderSyncStateMetadata.StorageType != StorageType.DirectItem)
                    {
                        goto IL_85;
                    }
                }
                try
                {
                    storeObject = Microsoft.Exchange.Data.Storage.Item.Bind(syncStateParentFolder.Session, folderSyncStateMetadata.ItemSyncStateId, SyncState.AppendAdditionalProperties(propsToReturn));
                    ((Item)storeObject).OpenAsReadWrite();
                    return(storeObject);
                }
                catch
                {
                    storeObject.Dispose();
                    throw;
                }
IL_85:
                throw new InvalidOperationException("Unsupported storage type for sync state");
            }
            storeObject = Folder.Bind(syncStateParentFolder.Session, folderSyncStateMetadata.FolderSyncStateId, SyncState.AppendAdditionalProperties(propsToReturn));
            return(storeObject);
        }
Пример #8
0
        internal static FolderSyncState GetSyncState(SyncStateStorage syncStateStorage, Folder deviceFolder, ISyncProviderFactory syncProviderFactory, Func <SyncStateStorage, StoreObject, FolderSyncStateMetadata, SyncStateInfo, ISyncProviderFactory, bool, ISyncLogger, FolderSyncState> creator, ISyncLogger syncLogger = null)
        {
            if (syncLogger == null)
            {
                syncLogger = TracingLogger.Singleton;
            }
            ArgumentValidator.ThrowIfNull("syncStateStorage", syncStateStorage);
            ArgumentValidator.ThrowIfNull("deviceFolder", deviceFolder);
            ArgumentValidator.ThrowIfNull("syncProviderFactory", syncProviderFactory);
            byte[] collectionIdBytes = syncProviderFactory.GetCollectionIdBytes();
            if (collectionIdBytes == null || collectionIdBytes.Length == 0)
            {
                throw new ArgumentException("SyncProviderFactory CollectionId bytes cannot be null or empty.");
            }
            StoreObjectId storeObjectId = null;

            try
            {
                storeObjectId = StoreObjectId.Deserialize(collectionIdBytes);
            }
            catch (ArgumentException innerException)
            {
                syncLogger.TraceError <string>(ExTraceGlobals.SyncTracer, 0L, "[FolderSyncState.GetSyncState(syncProviderFactory)] The IPMFolderBytes that the provider gave us are invalid for folder {0}", deviceFolder.DisplayName);
                throw new CorruptSyncStateException(ServerStrings.ExSyncStateCorrupted(deviceFolder.DisplayName), innerException);
            }
            FolderSyncStateMetadata folderSyncStateMetadata = null;

            if (!syncStateStorage.DeviceMetadata.SyncStatesByIPMFolderId.TryGetValue(storeObjectId, out folderSyncStateMetadata))
            {
                syncLogger.TraceDebug <DeviceIdentity, string>(ExTraceGlobals.SyncTracer, 0L, "[FolderSyncState.GetSyncState(syncProviderFactory)] Cache miss for device {0}, IPM folder Id {1}", syncStateStorage.DeviceMetadata.Id, storeObjectId.ToBase64String());
                return(null);
            }
            SyncStateMetadata syncStateMetadata = folderSyncStateMetadata;
            StoreObject       storeObject       = SyncState.GetSyncStateStoreObject(deviceFolder, ref syncStateMetadata, syncLogger, new PropertyDefinition[]
            {
                InternalSchema.SyncFolderSourceKey
            });

            if (!object.ReferenceEquals(folderSyncStateMetadata, syncStateMetadata))
            {
                FolderSyncStateMetadata folderSyncStateMetadata2 = syncStateMetadata as FolderSyncStateMetadata;
                if (folderSyncStateMetadata2 == null)
                {
                    syncLogger.TraceDebug <string, string>(ExTraceGlobals.SyncProcessTracer, 0L, "[FolderSyncState.GetSyncState] Device {0} has non-folder sync state for {1}.  Returning null.", deviceFolder.DisplayName, folderSyncStateMetadata.Name);
                    if (storeObject != null)
                    {
                        storeObject.Dispose();
                        storeObject = null;
                    }
                }
                else
                {
                    syncLogger.TraceDebug <string, string>(ExTraceGlobals.SyncProcessTracer, 0L, "[FolderSyncState.GetSyncState] Device {0} had  state folder sync state metadata for {1}.  Replacing.", deviceFolder.DisplayName, folderSyncStateMetadata.Name);
                    folderSyncStateMetadata = folderSyncStateMetadata2;
                }
            }
            if (storeObject == null)
            {
                return(null);
            }
            SyncStateInfo syncStateInfo = new FolderSyncStateInfo(folderSyncStateMetadata.Name);

            if (creator == null)
            {
                return(new FolderSyncState(syncStateStorage, storeObject, folderSyncStateMetadata, syncStateInfo, syncProviderFactory, false, syncLogger));
            }
            return(creator(syncStateStorage, storeObject, folderSyncStateMetadata, syncStateInfo, syncProviderFactory, false, syncLogger));
        }
Пример #9
0
 protected FolderSyncState(SyncStateStorage syncStateStorage, StoreObject storeObject, FolderSyncStateMetadata syncStateMetadata, SyncStateInfo syncStateInfo, ISyncProviderFactory syncProviderFactory, bool newSyncState, ISyncLogger syncLogger = null) : base(syncStateStorage, storeObject, syncStateMetadata, syncStateInfo, newSyncState, syncLogger)
 {
     this.syncProviderFactory = syncProviderFactory;
 }