public void Open()
        {
            // Connection string
            if (ConnectionString == null)
            {
                throw new ConnectorException(ConnectorException.MSG_NULL_CONNECTION_STRING, ConnectorException.ReasonType.NullConnectionString);
            }
            else if (Formatter == null)
            {
                throw new ConnectorException(ConnectorException.MSG_NULL_FORMATTER, ConnectorException.ReasonType.NullFormatter);
            }

            // Split to key value pairs
            string[] kvps = ConnectionString.Split(';');
            foreach (var kv in ConnectionStringParser.ParseConnectionString(this.ConnectionString))
            {
                switch (kv.Key)
                {
                case "directory":
                    this.targetDir = kv.Value[0];
                    break;

                case "file":
                    this.targetFile = kv.Value[0];
                    break;

                case "naming":
                    switch (kv.Value[0].ToLower())
                    {
                    case "guid":         // guid naming
                        this.targetFunctiod = delegate(object data) { return(System.Guid.NewGuid()); };
                        break;

                    case "id":         // id naming
                        this.targetFunctiod = delegate(object data)
                        {
                            if (data.GetType().GetInterface("MARC.Everest.Interfaces.IIdentifiable") != null)
                            {
                                return(string.Format("{0}.{1}", ((IIdentifiable)data).Id.Root, ((IIdentifiable)data).Id.Extension));
                            }
                            else
                            {
                                throw new InvalidOperationException("Can't determine name for object as it does not implemnt IIDentifiable");
                            }
                        };
                        break;
                    }
                    break;

                case "overwrite":
                    this.overwriteFile = Convert.ToBoolean(kv.Value[0]);
                    break;
                }
            }

            // Ensure the directory exists
            if (!Directory.Exists(this.targetDir))
            {
                throw new DirectoryNotFoundException(string.Format("Could not find directory '{0}'", this.targetDir));
            }

            // Ensure we have write permission
            try
            {
                string fn = Path.Combine(this.targetDir, Guid.NewGuid().ToString());
                System.IO.File.Create(fn).Close();
                System.IO.File.Delete(fn);
            }
            catch (Exception) //TODO: This should throw a more specific exception.
            {
                throw new Exception(string.Format("No write permission to directory '{0}'", this.targetDir));
            }

            this.isOpen = true; // Set connection to open
        }
Example #2
0
        public void Open()
        {
            // Connection string
            if (ConnectionString == null)
                throw new ConnectorException(ConnectorException.MSG_NULL_CONNECTION_STRING, ConnectorException.ReasonType.NullConnectionString);
            else if (Formatter == null)
                throw new ConnectorException(ConnectorException.MSG_NULL_FORMATTER, ConnectorException.ReasonType.NullFormatter);

            // Split to key value pairs
            string[] kvps = ConnectionString.Split(';');
            foreach (var kv in ConnectionStringParser.ParseConnectionString(this.ConnectionString))
            {
                switch (kv.Key)
                {
                    case "directory":
                        this.targetDir = kv.Value[0];
                        break;
                    case "file":
                        this.targetFile = kv.Value[0];
                        break;
                    case "naming":
                        switch (kv.Value[0].ToLower())
                        {
                            case "guid": // guid naming
                                this.targetFunctiod = delegate(object data) { return System.Guid.NewGuid(); };
                                break; 
                            case "id": // id naming
                                this.targetFunctiod = delegate(object data) 
                                {
                                    if (data.GetType().GetInterface("MARC.Everest.Interfaces.IIdentifiable") != null)
                                        return string.Format("{0}.{1}", ((IIdentifiable)data).Id.Root, ((IIdentifiable)data).Id.Extension);
                                    else
                                        throw new InvalidOperationException("Can't determine name for object as it does not implemnt IIDentifiable");
                                };
                                break;
                        }
                        break;
                    case "overwrite":
                        this.overwriteFile = Convert.ToBoolean(kv.Value[0]);
                        break;
                }
            }

            // Ensure the directory exists
            if (!Directory.Exists(this.targetDir))
                throw new DirectoryNotFoundException(string.Format("Could not find directory '{0}'", this.targetDir));

            // Ensure we have write permission
            try
            {
                string fn = Path.Combine(this.targetDir, Guid.NewGuid().ToString());
                System.IO.File.Create(fn).Close();
                System.IO.File.Delete(fn);
            }
            catch (Exception) //TODO: This should throw a more specific exception.
            {
                throw new Exception(string.Format("No write permission to directory '{0}'", this.targetDir));
            }

            this.isOpen = true; // Set connection to open
        }