/// <summary>
        /// Writes the file with schema information.
        /// </summary>
        /// <param name="stream">Stream to which to write</param>
        /// <param name="uri">Scope uri to be written</param>
        /// <param name="scopeName">Scope Name</param>
        /// <param name="schema">Schema to be written</param>
        private void WriteSchemaFile(Stream stream,
                                     Uri uri,
                                     string scopeName,
                                     IsolatedStorageSchema schema)
        {
            // Write data as text, so create the stream reader.
            using (StreamWriter writer = new StreamWriter(stream))
            {
                // Write the text version of the Uri.
                writer.WriteLine(uri.AbsoluteUri);
                writer.WriteLine(scopeName);

                // Get the list of types as strings and sort to make comparison
                // faster when reading.
                List <string> types = (from type in schema.Collections
                                       select type.FullName).ToList();
                types.Sort();

                // Write the types.
                foreach (string type in types)
                {
                    writer.WriteLine(type);
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Constructor for the offline context which allows a symmetric encryption algorithm to be specified.
        /// </summary>
        /// <param name="schema">The schema that specifies the set of the collections for the context.</param>
        /// <param name="scopeName">The scope name used to identify the scope on the service.</param>
        /// <param name="cachePath">Path in isolated storage where the data will be stored.</param>
        /// <param name="uri">Uri of the scopeName.  Used to intialize the CacheController.</param>
        /// <param name="encryptionAlgorithm">The symmetric encryption algorithm to use for files on disk</param>
        /// <remarks>
        /// If the Uri specified is different from the one that is stored in the cache path, the
        /// Load method will throw an InvalidOperationException.
        /// </remarks>
        public IsolatedStorageOfflineContext(IsolatedStorageSchema schema, string scopeName, string cachePath,
                                             Uri uri, SymmetricAlgorithm encryptionAlgorithm)
        {
            if (schema == null)
            {
                throw new ArgumentNullException("schema");
            }

            if (string.IsNullOrEmpty(scopeName))
            {
                throw new ArgumentNullException("scopeName");
            }

            if (string.IsNullOrEmpty(cachePath))
            {
                throw new ArgumentNullException("cachePath");
            }

            if (uri == null)
            {
                throw new ArgumentNullException("uri");
            }

            _isDisposed = false;

            _schema         = schema;
            _scopeUri       = uri;
            _scopeName      = scopeName;
            _cachePath      = cachePath;
            _storageHandler = new SQLiteStorageHandler(this, schema, cachePath, encryptionAlgorithm);
            _saveSyncLock   = new AutoResetLock();

            CreateCacheController();
        }
        /// <summary>
        /// Constructor for the offline context which allows a symmetric encryption algorithm to be specified.
        /// </summary>
        /// <param name="schema">The schema that specifies the set of the collections for the context.</param>
        /// <param name="scopeName">The scope name used to identify the scope on the service.</param>
        /// <param name="cachePath">Path in isolated storage where the data will be stored.</param>
        /// <param name="uri">Uri of the scopeName.  Used to intialize the CacheController.</param>
        /// <param name="encryptionAlgorithm">The symmetric encryption algorithm to use for files on disk</param>
        /// <remarks>
        /// If the Uri specified is different from the one that is stored in the cache path, the
        /// Load method will throw an InvalidOperationException.
        /// </remarks>
        public IsolatedStorageOfflineContext(IsolatedStorageSchema schema, string scopeName, string cachePath,
            Uri uri, SymmetricAlgorithm encryptionAlgorithm)
        {
            if (schema == null)
            {
                throw new ArgumentNullException("schema");
            }

            if (string.IsNullOrEmpty(scopeName))
            {
                throw new ArgumentNullException("scopeName");
            }

            if (string.IsNullOrEmpty(cachePath))
            {
                throw new ArgumentNullException("cachePath");
            }

            if (uri == null)
            {
                throw new ArgumentNullException("uri");
            }

            _isDisposed = false;

            _schema = schema;
            _scopeUri = uri;
            _scopeName = scopeName;
            _cachePath = cachePath;
            _storageHandler = new SQLiteStorageHandler(this, schema, cachePath, encryptionAlgorithm);
            _saveSyncLock = new AutoResetLock();
            
            CreateCacheController();
        }
Exemplo n.º 4
0
        public SQLiteCacheData(IsolatedStorageSchema schema, IsolatedStorageOfflineContext context)
        {
//            Collections = new Dictionary<EntityType, IsolatedStorageCollection>();
            SyncConflicts = new List<SyncConflict>();
            SyncErrors = new List<SyncError>();

            CreateCollections(schema, context);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Constructor which initializes the data.
        /// </summary>
        /// <param name="schema">Schema for the data</param>
        public CacheData(IsolatedStorageSchema schema)
        {
            Collections   = new Dictionary <Type, IsolatedStorageCollection>();
            SyncConflicts = new List <SyncConflict>();
            SyncErrors    = new List <SyncError>();

            CreateCollections(schema);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Constructor which initializes the data.
        /// </summary>
        /// <param name="schema">Schema for the data</param>
        public CacheData(IsolatedStorageSchema schema)
        {
            Collections = new Dictionary<Type, IsolatedStorageCollection>();
            SyncConflicts = new List<SyncConflict>();
            SyncErrors = new List<SyncError>();

            CreateCollections(schema);
        }
Exemplo n.º 7
0
        public SQLiteStorageHandler(IOfflineContext ctx, IsolatedStorageSchema schema, string cachePath, SymmetricAlgorithm encryptionAlgorithm)
        {
            _context = ctx;
            _schema = schema;
            _cachePath = cachePath;
            _encryptionAlgorithm = encryptionAlgorithm;
            _anchor = null;

            _knownTypes = new List<IEntityType>();

            AddKnownTypes();
        }
Exemplo n.º 8
0
        /// <summary>
        /// Creates the collections for the types in the schema
        /// </summary>
        /// <param name="schema">Schema for which to create collections</param>
        private void CreateCollections(IsolatedStorageSchema schema)
        {
            Type collectionType = typeof(IsolatedStorageCollection <>);

            foreach (Type t in schema.Collections)
            {
                // Create the generic type for the type in the collection.
                Type generic = collectionType.MakeGenericType(t);
                IsolatedStorageCollection collection = (IsolatedStorageCollection)Activator.CreateInstance(generic);
                Collections[t] = collection;
            }
        }
Exemplo n.º 9
0
        public SQLiteStorageHandler(IsolatedStorageOfflineContext ctx, IsolatedStorageSchema schema, string cachePath, SymmetricAlgorithm encryptionAlgorithm)
        {
            _context = ctx;
            _schema = schema;
            _cachePath = cachePath;
            _encryptionAlgorithm = encryptionAlgorithm;
            _anchor = null;

            _knownTypes = new List<EntityType>
            {
//                new EntityType(typeof (SyncConflict)),
//                new EntityType(typeof (SyncError))
            };

            AddKnownTypes();
        }
Exemplo n.º 10
0
        private void CreateCollections(IsolatedStorageSchema schema, IsolatedStorageOfflineContext context)
        {
//            Type collectionType = typeof(IsolatedStorageCollection<>);
//            foreach (EntityType t in schema.Collections)
//            {
//                // CreateInstance the generic type for the type in the collection.
//                Type generic = collectionType.MakeGenericType(t);
//                IsolatedStorageCollection collection = (IsolatedStorageCollection)Activator.CreateInstance(generic, context);
//                Collections[t] = collection;
//            }
        }
Exemplo n.º 11
0
 /// <summary>
 /// Constructor for the offline context.
 /// </summary>
 /// <param name="schema">The schema that specifies the set of the collections for the context.</param>
 /// <param name="scopeName">The scope name used to identify the scope on the service.</param>
 /// <param name="cachePath">Path in isolated storage where the data will be stored.</param>
 /// <param name="uri">Uri of the scopeName.  Used to intialize the CacheController.</param>
 /// <remarks>
 /// If the Uri specified is different from the one that is stored in the cache path, the
 /// Load method will throw an InvalidOperationException.
 /// </remarks>
 public IsolatedStorageOfflineContext(IsolatedStorageSchema schema, string scopeName, string cachePath,
                                      Uri uri) : this(schema, scopeName, cachePath, uri, null)
 {
 }
        /// <summary>
        /// Method that verifies a previously cached schema and uri (if they exist) with the current schema and uri.
        /// </summary>
        /// <param name="cachePath">Cache path for the context</param>
        /// <param name="schema">Schema to verify</param>
        /// <param name="uri">Uri to verify</param>
        /// <param name="scopeName">The scope name that the client will be accessing on the service</param>
        /// <param name="encryptionAlgorithm">The encryption algorithm which will be used to verify the schema</param>
        private void CheckSchemaAndUri(string cachePath, IsolatedStorageSchema schema, Uri uri, string scopeName, SymmetricAlgorithm encryptionAlgorithm)
        {
            // Get the isolated storage file for the application.
            using (IsolatedStorageFile isoFile = IsolatedStorageFile.GetUserStoreForApplication())
            {
                // Generate the path to the scopeName info file.
                string infoPath = Path.Combine(cachePath, Constants.SCOPE_INFO);

                // If the file exists, read it, otherwise, everything is fine.
                if (isoFile.FileExists(infoPath))
                {
                    // Open the scopeName file.
                    using (IsolatedStorageFileStream stream = isoFile.OpenFile(infoPath, FileMode.Open))
                    {
                        Stream           readStream = stream;
                        ICryptoTransform decryptor  = null;

                        try
                        {
                            if (encryptionAlgorithm != null)
                            {
                                decryptor  = encryptionAlgorithm.CreateDecryptor();
                                readStream = new CryptoStream(stream, decryptor, CryptoStreamMode.Read);
                            }

                            List <string> fileTypes;
                            string        fileUri;
                            string        fileScopeName;

                            // Read the file types and uri from the file.
                            ReadSchemaAndUri(readStream, out fileUri, out fileScopeName, out fileTypes);

                            // Verify the scopeName uri.
                            if (fileUri != uri.AbsoluteUri)
                            {
                                throw new ArgumentException("Specified uri does not match uri previously used for the specified cache path");
                            }

                            if (fileScopeName != scopeName)
                            {
                                throw new ArgumentException("Specified scope name does not match scope name previously used for the specified cache path");
                            }

                            // Verify the types.
                            List <Type> userTypes = schema.Collections.ToList();

                            // Sort by name (the class Type isn't sortable)
                            userTypes.Sort((x, y) =>
                            {
                                return(x.FullName.CompareTo(y.FullName));
                            });

                            if (userTypes.Count != fileTypes.Count)
                            {
                                throw new ArgumentException("Specified schema does not match schema previously used for cache path");
                            }

                            for (int i = 0; i < userTypes.Count; ++i)
                            {
                                if (userTypes[i].FullName != fileTypes[i])
                                {
                                    throw new ArgumentException("Specified schema does not match schema previously used for cache path");
                                }
                            }
                        }
                        finally
                        {
                            readStream.Dispose();

                            if (decryptor != null)
                            {
                                decryptor.Dispose();
                            }
                        }
                    }
                }
                else
                {
                    // If the file doesn't exist, write the new info.
                    using (IsolatedStorageFileStream stream = isoFile.CreateFile(infoPath))
                    {
                        Stream           writeStream = stream;
                        ICryptoTransform encryptor   = null;

                        try
                        {
                            if (encryptionAlgorithm != null)
                            {
                                encryptor   = encryptionAlgorithm.CreateEncryptor();
                                writeStream = new CryptoStream(writeStream, encryptor, CryptoStreamMode.Write);
                            }

                            WriteSchemaFile(writeStream, uri, scopeName, schema);
                        }
                        finally
                        {
                            writeStream.Dispose();

                            if (encryptor != null)
                            {
                                encryptor.Dispose();
                            }
                        }
                    }
                }
            }
        }
 /// <summary>
 /// Constructor for the offline context.
 /// </summary>
 /// <param name="schema">The schema that specifies the set of the collections for the context.</param>
 /// <param name="scopeName">The scope name used to identify the scope on the service.</param>
 /// <param name="cachePath">Path in isolated storage where the data will be stored.</param>
 /// <param name="uri">Uri of the scopeName.  Used to intialize the CacheController.</param>
 /// <remarks>
 /// If the Uri specified is different from the one that is stored in the cache path, the
 /// Load method will throw an InvalidOperationException.
 /// </remarks>
 public IsolatedStorageOfflineContext(IsolatedStorageSchema schema, string scopeName, string cachePath,
     Uri uri) : this(schema, scopeName, cachePath, uri, null)
 {
 }
Exemplo n.º 14
0
 /// <summary>
 /// Creates the collections for the types in the schema
 /// </summary>
 /// <param name="schema">Schema for which to create collections</param>
 private void CreateCollections(IsolatedStorageSchema schema)
 {
     Type collectionType = typeof(IsolatedStorageCollection<>);
     foreach (Type t in schema.Collections)
     {
         // Create the generic type for the type in the collection.
         Type generic = collectionType.MakeGenericType(t);
         IsolatedStorageCollection collection = (IsolatedStorageCollection)Activator.CreateInstance(generic);
         Collections[t] = collection;
     }
 }