Ejemplo n.º 1
0
        /// <summary>
        /// Attempts to load an Object of the given type identified by the given Node and returned as the Type that this loader generates.
        /// </summary>
        /// <param name="g">Configuration Graph.</param>
        /// <param name="objNode">Object Node.</param>
        /// <param name="targetType">Target Type.</param>
        /// <param name="obj">Created Object.</param>
        /// <returns>True if the loader succeeded in creating an Object.</returns>
        public bool TryLoadObject(IGraph g, INode objNode, Type targetType, out object obj)
        {
            obj = null;

            String server, port, db, user, pwd;
            int    p = -1, timeout = 0;

            //Create the URI Nodes we're going to use to search for things
            INode propServer = g.CreateUriNode(UriFactory.Create(ConfigurationLoader.PropertyServer)),
                  propDb     = g.CreateUriNode(UriFactory.Create(ConfigurationLoader.PropertyDatabase));

            //Get Server and Database details
            server = ConfigurationLoader.GetConfigurationString(g, objNode, propServer);
            if (server == null)
            {
                server = "localhost";
            }
            db = ConfigurationLoader.GetConfigurationString(g, objNode, propDb);
            if (db == null)
            {
                db = VirtuosoManager.DefaultDB;
            }

            //Get Port
            port = ConfigurationLoader.GetConfigurationString(g, objNode, g.CreateUriNode(UriFactory.Create(ConfigurationLoader.PropertyPort)));
            if (!Int32.TryParse(port, out p))
            {
                p = VirtuosoManager.DefaultPort;
            }

            //Get timeout
            timeout = ConfigurationLoader.GetConfigurationInt32(g, objNode, g.CreateUriNode(UriFactory.Create(ConfigurationLoader.PropertyTimeout)), timeout);

            //Get user credentials
            ConfigurationLoader.GetUsernameAndPassword(g, objNode, true, out user, out pwd);
            if (user == null || pwd == null)
            {
                return(false);
            }

            //Based on this information create a Manager if possible
            switch (targetType.FullName)
            {
            case Virtuoso:
                //Get Server settings
                server = ConfigurationLoader.GetConfigurationString(g, objNode, propServer);
                if (server == null)
                {
                    return(false);
                }

                obj = new VirtuosoManager(server, p, db, user, pwd, timeout);

                break;
            }

            return(obj != null);
        }
        public void ConfigurationLookupInt2()
        {
            String graph = Prefixes + @"
_:a dnr:type ""not an integer"" .";

            Graph g = new Graph();

            g.LoadFromString(graph);

            int value = ConfigurationLoader.GetConfigurationInt32(g, g.GetBlankNode("a"), g.CreateUriNode("dnr:type"), 0);

            Assert.Equal(0, value);
        }
        public void ConfigurationLookupLong3()
        {
            String graph = Prefixes + @"
_:a dnr:type <appsetting:ConfigurationLookupLong3> .";

            _testSettings.SettSetting("ConfigurationLookupLong3", "123");

            Graph g = new Graph();

            g.LoadFromString(graph);

            long value = ConfigurationLoader.GetConfigurationInt32(g, g.GetBlankNode("a"), g.CreateUriNode("dnr:type"), 0);

            Assert.Equal(123, value);
        }
Ejemplo n.º 4
0
        public void ConfigurationLookupLong3()
        {
            String graph = Prefixes + @"
_:a dnr:type <appsetting:ConfigurationLookupLong3> .";

            System.Configuration.ConfigurationManager.AppSettings["ConfigurationLookupLong3"] = "123";

            Graph g = new Graph();

            g.LoadFromString(graph);

            long value = ConfigurationLoader.GetConfigurationInt32(g, g.GetBlankNode("a"), g.CreateUriNode("dnr:type"), 0);

            Assert.AreEqual(123, value);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Tries to load an object based on information from the Configuration Graph
        /// </summary>
        /// <param name="g">Configuration Graph</param>
        /// <param name="objNode">Object Node</param>
        /// <param name="targetType">Target Type</param>
        /// <param name="obj">Output Object</param>
        /// <returns></returns>
        public bool TryLoadObject(IGraph g, INode objNode, Type targetType, out object obj)
        {
            obj = null;

            INode index = g.CreateUriNode(new Uri(FullTextHelper.FullTextConfigurationNamespace + "index"));
            //INode indexer = g.CreateUriNode(new Uri(FullTextHelper.FullTextConfigurationNamespace + "indexer"));
            INode searcher = g.CreateUriNode(new Uri(FullTextHelper.FullTextConfigurationNamespace + "searcher"));
            INode analyzer = g.CreateUriNode(new Uri(FullTextHelper.FullTextConfigurationNamespace + "analyzer"));
            INode schema   = g.CreateUriNode(new Uri(FullTextHelper.FullTextConfigurationNamespace + "schema"));
            INode version  = g.CreateUriNode(new Uri(FullTextHelper.FullTextConfigurationNamespace + "version"));

            Object tempIndex, tempAnalyzer, tempSchema;
            int    ver = 2900;

            //Always check for the version
            ver = ConfigurationLoader.GetConfigurationInt32(g, objNode, version, 2900);

            switch (targetType.FullName)
            {
            case DefaultIndexSchema:
                obj = new DefaultIndexSchema();
                break;

            case FullTextOptimiser:
                //Need to get the Search Provider
                INode providerNode = ConfigurationLoader.GetConfigurationNode(g, objNode, searcher);
                if (providerNode == null)
                {
                    throw new DotNetRdfConfigurationException("Unable to load the Full Text Optimiser specified by the Node '" + objNode.ToString() + "' as there was no value specified for the required dnr-ft:searcher property");
                }
                Object tempSearcher = ConfigurationLoader.LoadObject(g, providerNode);
                if (tempSearcher is IFullTextSearchProvider)
                {
                    obj = new FullTextOptimiser((IFullTextSearchProvider)tempSearcher);
                }
                else
                {
                    throw new DotNetRdfConfigurationException("Unable to load the Full Text Optimiser specified by the Node '" + objNode.ToString() + "' as the value specified for the dnr-ft:searcher property pointed to an object which could not be loaded as a type that implements the required IFullTextSearchProvider interface");
                }
                break;

            case LuceneObjectsIndexer:
            case LucenePredicatesIndexer:
            case LuceneSubjectsIndexer:
            case LuceneSearchProvider:
                //For any Lucene Indexer/Search Provider need to know the Index, Analyzer and Schema to be used

                //Then get the Index
                tempIndex = ConfigurationLoader.GetConfigurationNode(g, objNode, index);
                if (tempIndex == null)
                {
                    throw new DotNetRdfConfigurationException("Unable to load the Lucene Indexer specified by the Node '" + objNode.ToString() + "' as there was no value specified for the required dnr-ft:index property");
                }
                tempIndex = ConfigurationLoader.LoadObject(g, (INode)tempIndex);
                if (tempIndex is Directory)
                {
                    //Next get the Analyzer (assume Standard if none specified)
                    tempAnalyzer = ConfigurationLoader.GetConfigurationNode(g, objNode, analyzer);
                    if (tempAnalyzer == null)
                    {
                        tempAnalyzer = new StandardAnalyzer(this.GetLuceneVersion(ver));
                    }
                    else
                    {
                        tempAnalyzer = ConfigurationLoader.LoadObject(g, (INode)tempAnalyzer);
                    }

                    if (tempAnalyzer is Analyzer)
                    {
                        //Finally get the Schema (assume Default if none specified)
                        tempSchema = ConfigurationLoader.GetConfigurationNode(g, objNode, schema);
                        if (tempSchema == null)
                        {
                            tempSchema = new DefaultIndexSchema();
                        }
                        else
                        {
                            tempSchema = ConfigurationLoader.LoadObject(g, (INode)tempSchema);
                        }

                        if (tempSchema is IFullTextIndexSchema)
                        {
                            //Now we can create the Object
                            switch (targetType.FullName)
                            {
                            case LuceneObjectsIndexer:
                                obj = new LuceneObjectsIndexer((Directory)tempIndex, (Analyzer)tempAnalyzer, (IFullTextIndexSchema)tempSchema);
                                break;

                            case LucenePredicatesIndexer:
                                obj = new LucenePredicatesIndexer((Directory)tempIndex, (Analyzer)tempAnalyzer, (IFullTextIndexSchema)tempSchema);
                                break;

                            case LuceneSubjectsIndexer:
                                obj = new LuceneSubjectsIndexer((Directory)tempIndex, (Analyzer)tempAnalyzer, (IFullTextIndexSchema)tempSchema);
                                break;

                            case LuceneSearchProvider:
                                //Before the Search Provider has been loaded determine whether we need to carry out auto-indexing
                                List <INode> sources = ConfigurationLoader.GetConfigurationData(g, objNode, g.CreateUriNode(new Uri(FullTextHelper.FullTextConfigurationNamespace + "buildIndexFor"))).ToList();
                                if (sources.Count > 0)
                                {
                                    //If there are sources to index ensure we have an indexer to index with
                                    INode indexerNode = ConfigurationLoader.GetConfigurationNode(g, objNode, g.CreateUriNode(new Uri(FullTextHelper.FullTextConfigurationNamespace + "buildIndexWith")));
                                    if (indexerNode == null)
                                    {
                                        throw new DotNetRdfConfigurationException("Unable to load the Lucene Search Provider specified by the Node '" + objNode.ToString() + "' as there were values specified for the dnr-ft:buildIndexFor property but no dnr-ft:buildIndexWith property was found");
                                    }
                                    IFullTextIndexer indexer = ConfigurationLoader.LoadObject(g, indexerNode) as IFullTextIndexer;
                                    if (indexer == null)
                                    {
                                        throw new DotNetRdfConfigurationException("Unable to load the Lucene Search Provider specified by the Node '" + objNode.ToString() + "' as the value given for the dnr-ft:buildIndexWith property pointed to an Object which could not be loaded as a type that implements the required IFullTextIndexer interface");
                                    }

                                    try
                                    {
                                        //For Each Source load it and Index it
                                        foreach (INode sourceNode in sources)
                                        {
                                            Object source = ConfigurationLoader.LoadObject(g, sourceNode);
                                            if (source is ISparqlDataset)
                                            {
                                                indexer.Index((ISparqlDataset)source);
                                            }
                                            else if (source is ITripleStore)
                                            {
                                                foreach (IGraph graph in ((ITripleStore)source).Graphs)
                                                {
                                                    indexer.Index(graph);
                                                }
                                            }
                                            else if (source is IGraph)
                                            {
                                                indexer.Index((IGraph)source);
                                            }
                                            else
                                            {
                                                throw new DotNetRdfConfigurationException("Unable to load the Lucene Search Provider specified by the Node '" + objNode.ToString() + "' as a value given for the dnr-ft:buildIndexFor property ('" + sourceNode.ToString() + "') pointed to an Object which could not be loaded as a type that implements one of the required interfaces: IGraph, ITripleStore or ISparqlDataset");
                                            }
                                        }
                                    }
                                    finally
                                    {
                                        indexer.Dispose();
                                    }
                                }

                                //Then we actually load the Search Provider
                                obj = new LuceneSearchProvider(this.GetLuceneVersion(ver), (Directory)tempIndex, (Analyzer)tempAnalyzer, (IFullTextIndexSchema)tempSchema);
                                break;
                            }
                        }
                        else
                        {
                            throw new DotNetRdfConfigurationException("Unable to load the Lucene Indexer specified by the Node '" + objNode.ToString() + "' as the value given for the dnr-ft:schema property pointed to an Object which could not be loaded as a type that implements the required IFullTextIndexSchema interface");
                        }
                    }
                    else
                    {
                        throw new DotNetRdfConfigurationException("Unable to load the Lucene Indexer specified by the Node '" + objNode.ToString() + "' as the value given for the dnr-ft:analyzer property pointed to an Object which could not be loaded as a type that derives from the required Lucene.Net.Analysis.Analyzer type");
                    }
                }
                else
                {
                    throw new DotNetRdfConfigurationException("Unable to load the Lucene Indexer specified by the Node '" + objNode.ToString() + "' as the value given for the dnr-ft:index property pointed to an Object which could not be loaded as a type that derives from the required Lucene.Net.Store.Directory type");
                }
                break;

            default:
                try
                {
                    if (this._luceneAnalyzerType.IsAssignableFrom(targetType))
                    {
                        if (targetType.GetConstructor(new Type[] { typeof(LucVersion) }) != null)
                        {
                            obj = Activator.CreateInstance(targetType, new Object[] { this.GetLuceneVersion(ver) });
                        }
                        else
                        {
                            obj = Activator.CreateInstance(targetType);
                        }
                    }
                    else if (this._luceneDirectoryType.IsAssignableFrom(targetType))
                    {
                        String dir = ConfigurationLoader.GetConfigurationString(g, objNode, ConfigurationLoader.CreateConfigurationNode(g, ConfigurationLoader.PropertyFromFile));
                        if (dir != null)
                        {
                            try
                            {
                                obj = Activator.CreateInstance(targetType, new Object[] { dir });
                            }
                            catch
                            {
                                MethodInfo method = targetType.GetMethod("Open", new Type[] { typeof(DirInfo) });
                                if (method != null)
                                {
                                    obj = method.Invoke(null, new Object[] { new DirInfo(ConfigurationLoader.ResolvePath(dir)) });
                                }
                            }
                        }
                        else
                        {
                            obj = Activator.CreateInstance(targetType);
                        }
                        //Ensure the Index if necessary
                        if (obj != null)
                        {
                            if (ConfigurationLoader.GetConfigurationBoolean(g, objNode, g.CreateUriNode(new Uri(FullTextHelper.FullTextConfigurationNamespace + "ensureIndex")), false))
                            {
                                IndexWriter writer = new IndexWriter((Directory)obj, new StandardAnalyzer(this.GetLuceneVersion(ver)));
                                writer.Close();
                            }
                        }
                    }
                }
                catch
                {
                    //Since we know we don't allow loading of all analyzers and directories we allow for users to inject other object factories
                    //which may know how to load those specific instances
                    obj = null;
                }
                break;
            }

            return(obj != null);
        }
        /// <summary>
        /// Tries to load a Generic IO Manager based on information from the Configuration Graph.
        /// </summary>
        /// <param name="g">Configuration Graph.</param>
        /// <param name="objNode">Object Node.</param>
        /// <param name="targetType">Target Type.</param>
        /// <param name="obj">Output Object.</param>
        /// <returns></returns>
        public bool TryLoadObject(IGraph g, INode objNode, Type targetType, out object obj)
        {
            IStorageProvider          storageProvider = null;
            IStorageServer            storageServer   = null;
            SparqlConnectorLoadMethod loadMode;

            obj = null;

            String server, user, pwd, store, catalog, loadModeRaw;

            Object temp;
            INode  storeObj;

            // Create the URI Nodes we're going to use to search for things
            INode propServer          = g.CreateUriNode(UriFactory.Create(ConfigurationLoader.PropertyServer)),
                  propDb              = g.CreateUriNode(UriFactory.Create(ConfigurationLoader.PropertyDatabase)),
                  propStore           = g.CreateUriNode(UriFactory.Create(ConfigurationLoader.PropertyStore)),
                  propAsync           = g.CreateUriNode(UriFactory.Create(ConfigurationLoader.PropertyAsync)),
                  propStorageProvider = g.CreateUriNode(UriFactory.Create(ConfigurationLoader.PropertyStorageProvider));

            switch (targetType.FullName)
            {
            case AllegroGraph:
                // Get the Server, Catalog and Store
                server = ConfigurationLoader.GetConfigurationString(g, objNode, propServer);
                if (server == null)
                {
                    return(false);
                }
                catalog = ConfigurationLoader.GetConfigurationString(g, objNode, g.CreateUriNode(UriFactory.Create(ConfigurationLoader.PropertyCatalog)));
                store   = ConfigurationLoader.GetConfigurationString(g, objNode, propStore);
                if (store == null)
                {
                    return(false);
                }

                // Get User Credentials
                ConfigurationLoader.GetUsernameAndPassword(g, objNode, true, out user, out pwd);

                if (user != null && pwd != null)
                {
                    storageProvider = new AllegroGraphConnector(server, catalog, store, user, pwd);
                }
                else
                {
                    storageProvider = new AllegroGraphConnector(server, catalog, store);
                }
                break;

            case AllegroGraphServer:
                // Get the Server, Catalog and User Credentials
                server = ConfigurationLoader.GetConfigurationString(g, objNode, propServer);
                if (server == null)
                {
                    return(false);
                }
                catalog = ConfigurationLoader.GetConfigurationString(g, objNode, g.CreateUriNode(UriFactory.Create(ConfigurationLoader.PropertyCatalog)));
                ConfigurationLoader.GetUsernameAndPassword(g, objNode, true, out user, out pwd);

                if (user != null && pwd != null)
                {
                    storageServer = new AllegroGraphServer(server, catalog, user, pwd);
                }
                else
                {
                    storageServer = new AllegroGraphServer(server, catalog);
                }
                break;

            case DatasetFile:
                // Get the Filename and whether the loading should be done asynchronously
                String file = ConfigurationLoader.GetConfigurationString(g, objNode, g.CreateUriNode(UriFactory.Create(ConfigurationLoader.PropertyFromFile)));
                if (file == null)
                {
                    return(false);
                }
                file = ConfigurationLoader.ResolvePath(file);
                bool isAsync = ConfigurationLoader.GetConfigurationBoolean(g, objNode, propAsync, false);
                storageProvider = new DatasetFileManager(file, isAsync);
                break;

            case Dydra:
                throw new DotNetRdfConfigurationException("DydraConnector is no longer supported by dotNetRDF and is considered obsolete");

            case FourStore:
                // Get the Server and whether Updates are enabled
                server = ConfigurationLoader.GetConfigurationString(g, objNode, propServer);
                if (server == null)
                {
                    return(false);
                }
                bool enableUpdates = ConfigurationLoader.GetConfigurationBoolean(g, objNode, g.CreateUriNode(UriFactory.Create(ConfigurationLoader.PropertyEnableUpdates)), true);
                storageProvider = new FourStoreConnector(server, enableUpdates);
                break;

            case Fuseki:
                // Get the Server URI
                server = ConfigurationLoader.GetConfigurationString(g, objNode, propServer);
                if (server == null)
                {
                    return(false);
                }
                storageProvider = new FusekiConnector(server);
                break;

            case InMemory:
                // Get the Dataset/Store
                INode datasetObj = ConfigurationLoader.GetConfigurationNode(g, objNode, g.CreateUriNode(UriFactory.Create(ConfigurationLoader.PropertyUsingDataset)));
                if (datasetObj != null)
                {
                    temp = ConfigurationLoader.LoadObject(g, datasetObj);
                    if (temp is ISparqlDataset)
                    {
                        storageProvider = new InMemoryManager((ISparqlDataset)temp);
                    }
                    else
                    {
                        throw new DotNetRdfConfigurationException("Unable to load the In-Memory Manager identified by the Node '" + objNode.ToString() + "' as the value given for the dnr:usingDataset property points to an Object that cannot be loaded as an object which implements the ISparqlDataset interface");
                    }
                }
                else
                {
                    // If no dnr:usingDataset try dnr:usingStore instead
                    storeObj = ConfigurationLoader.GetConfigurationNode(g, objNode, g.CreateUriNode(UriFactory.Create(ConfigurationLoader.PropertyUsingStore)));
                    if (storeObj != null)
                    {
                        temp = ConfigurationLoader.LoadObject(g, storeObj);
                        if (temp is IInMemoryQueryableStore)
                        {
                            storageProvider = new InMemoryManager((IInMemoryQueryableStore)temp);
                        }
                        else
                        {
                            throw new DotNetRdfConfigurationException("Unable to load the In-Memory Manager identified by the Node '" + objNode.ToString() + "' as the value given for the dnr:usingStore property points to an Object that cannot be loaded as an object which implements the IInMemoryQueryableStore interface");
                        }
                    }
                    else
                    {
                        // If no dnr:usingStore either then create a new empty store
                        storageProvider = new InMemoryManager();
                    }
                }
                break;

            case ReadOnly:
                // Get the actual Manager we are wrapping
                storeObj = ConfigurationLoader.GetConfigurationNode(g, objNode, propStorageProvider);
                temp     = ConfigurationLoader.LoadObject(g, storeObj);
                if (temp is IStorageProvider)
                {
                    storageProvider = new ReadOnlyConnector((IStorageProvider)temp);
                }
                else
                {
                    throw new DotNetRdfConfigurationException("Unable to load the Read-Only Connector identified by the Node '" + objNode.ToString() + "' as the value given for the dnr:genericManager property points to an Object which cannot be loaded as an object which implements the required IStorageProvider interface");
                }
                break;

            case ReadOnlyQueryable:
                // Get the actual Manager we are wrapping
                storeObj = ConfigurationLoader.GetConfigurationNode(g, objNode, propStorageProvider);
                temp     = ConfigurationLoader.LoadObject(g, storeObj);
                if (temp is IQueryableStorage)
                {
                    storageProvider = new QueryableReadOnlyConnector((IQueryableStorage)temp);
                }
                else
                {
                    throw new DotNetRdfConfigurationException("Unable to load the Queryable Read-Only Connector identified by the Node '" + objNode.ToString() + "' as the value given for the dnr:genericManager property points to an Object which cannot be loaded as an object which implements the required IQueryableStorage interface");
                }
                break;

            case Sesame:
            case SesameV5:
            case SesameV6:
                // Get the Server and Store ID
                server = ConfigurationLoader.GetConfigurationString(g, objNode, propServer);
                if (server == null)
                {
                    return(false);
                }
                store = ConfigurationLoader.GetConfigurationString(g, objNode, propStore);
                if (store == null)
                {
                    return(false);
                }
                ConfigurationLoader.GetUsernameAndPassword(g, objNode, true, out user, out pwd);
                if (user != null && pwd != null)
                {
                    storageProvider = (IStorageProvider)Activator.CreateInstance(targetType, new Object[] { server, store, user, pwd });
                }
                else
                {
                    storageProvider = (IStorageProvider)Activator.CreateInstance(targetType, new Object[] { server, store });
                }
                break;

            case SesameServer:
                // Get the Server and User Credentials
                server = ConfigurationLoader.GetConfigurationString(g, objNode, propServer);
                if (server == null)
                {
                    return(false);
                }
                ConfigurationLoader.GetUsernameAndPassword(g, objNode, true, out user, out pwd);

                if (user != null && pwd != null)
                {
                    storageServer = new SesameServer(server, user, pwd);
                }
                else
                {
                    storageServer = new SesameServer(server);
                }
                break;

            case Sparql:
                // Get the Endpoint URI or the Endpoint
                server = ConfigurationLoader.GetConfigurationString(g, objNode, new INode[] { g.CreateUriNode(UriFactory.Create(ConfigurationLoader.PropertyQueryEndpointUri)), g.CreateUriNode(UriFactory.Create(ConfigurationLoader.PropertyEndpointUri)) });

                // What's the load mode?
                loadModeRaw = ConfigurationLoader.GetConfigurationString(g, objNode, g.CreateUriNode(UriFactory.Create(ConfigurationLoader.PropertyLoadMode)));
                loadMode    = SparqlConnectorLoadMethod.Construct;
                if (loadModeRaw != null)
                {
                    try
                    {
                        loadMode = (SparqlConnectorLoadMethod)Enum.Parse(typeof(SparqlConnectorLoadMethod), loadModeRaw);
                    }
                    catch
                    {
                        throw new DotNetRdfConfigurationException("Unable to load the SparqlConnector identified by the Node '" + objNode.ToString() + "' as the value given for the property dnr:loadMode is not valid");
                    }
                }

                if (server == null)
                {
                    INode endpointObj = ConfigurationLoader.GetConfigurationNode(g, objNode, new INode[] { g.CreateUriNode(UriFactory.Create(ConfigurationLoader.PropertyQueryEndpoint)), g.CreateUriNode(UriFactory.Create(ConfigurationLoader.PropertyEndpoint)) });
                    if (endpointObj == null)
                    {
                        return(false);
                    }
                    temp = ConfigurationLoader.LoadObject(g, endpointObj);
                    if (temp is SparqlRemoteEndpoint)
                    {
                        storageProvider = new SparqlConnector((SparqlRemoteEndpoint)temp, loadMode);
                    }
                    else
                    {
                        throw new DotNetRdfConfigurationException("Unable to load the SparqlConnector identified by the Node '" + objNode.ToString() + "' as the value given for the property dnr:endpoint points to an Object which cannot be loaded as an object which is of the type SparqlRemoteEndpoint");
                    }
                }
                else
                {
                    // Are there any Named/Default Graph URIs
                    IEnumerable <Uri> defGraphs = from def in ConfigurationLoader.GetConfigurationData(g, objNode, g.CreateUriNode(UriFactory.Create(ConfigurationLoader.PropertyDefaultGraphUri)))
                                                  where def.NodeType == NodeType.Uri
                                                  select((IUriNode)def).Uri;

                    IEnumerable <Uri> namedGraphs = from named in ConfigurationLoader.GetConfigurationData(g, objNode, g.CreateUriNode(UriFactory.Create(ConfigurationLoader.PropertyNamedGraphUri)))
                                                    where named.NodeType == NodeType.Uri
                                                    select((IUriNode)named).Uri;

                    if (defGraphs.Any() || namedGraphs.Any())
                    {
                        storageProvider = new SparqlConnector(new SparqlRemoteEndpoint(UriFactory.Create(server), defGraphs, namedGraphs), loadMode);
                    }
                    else
                    {
                        storageProvider = new SparqlConnector(UriFactory.Create(server), loadMode);
                    }
                }
                break;

            case ReadWriteSparql:
                SparqlRemoteEndpoint       queryEndpoint;
                SparqlRemoteUpdateEndpoint updateEndpoint;

                // Get the Query Endpoint URI or the Endpoint
                server = ConfigurationLoader.GetConfigurationString(g, objNode, new INode[] { g.CreateUriNode(UriFactory.Create(ConfigurationLoader.PropertyUpdateEndpointUri)), g.CreateUriNode(UriFactory.Create(ConfigurationLoader.PropertyEndpointUri)) });

                // What's the load mode?
                loadModeRaw = ConfigurationLoader.GetConfigurationString(g, objNode, g.CreateUriNode(UriFactory.Create(ConfigurationLoader.PropertyLoadMode)));
                loadMode    = SparqlConnectorLoadMethod.Construct;
                if (loadModeRaw != null)
                {
                    try
                    {
                        loadMode = (SparqlConnectorLoadMethod)Enum.Parse(typeof(SparqlConnectorLoadMethod), loadModeRaw);
                    }
                    catch
                    {
                        throw new DotNetRdfConfigurationException("Unable to load the ReadWriteSparqlConnector identified by the Node '" + objNode.ToString() + "' as the value given for the property dnr:loadMode is not valid");
                    }
                }

                if (server == null)
                {
                    INode endpointObj = ConfigurationLoader.GetConfigurationNode(g, objNode, new INode[] { g.CreateUriNode(UriFactory.Create(ConfigurationLoader.PropertyQueryEndpoint)), g.CreateUriNode(UriFactory.Create(ConfigurationLoader.PropertyEndpoint)) });
                    if (endpointObj == null)
                    {
                        return(false);
                    }
                    temp = ConfigurationLoader.LoadObject(g, endpointObj);
                    if (temp is SparqlRemoteEndpoint)
                    {
                        queryEndpoint = (SparqlRemoteEndpoint)temp;
                    }
                    else
                    {
                        throw new DotNetRdfConfigurationException("Unable to load the ReadWriteSparqlConnector identified by the Node '" + objNode.ToString() + "' as the value given for the property dnr:queryEndpoint/dnr:endpoint points to an Object which cannot be loaded as an object which is of the type SparqlRemoteEndpoint");
                    }
                }
                else
                {
                    // Are there any Named/Default Graph URIs
                    IEnumerable <Uri> defGraphs = from def in ConfigurationLoader.GetConfigurationData(g, objNode, g.CreateUriNode(UriFactory.Create(ConfigurationLoader.PropertyDefaultGraphUri)))
                                                  where def.NodeType == NodeType.Uri
                                                  select((IUriNode)def).Uri;

                    IEnumerable <Uri> namedGraphs = from named in ConfigurationLoader.GetConfigurationData(g, objNode, g.CreateUriNode(UriFactory.Create(ConfigurationLoader.PropertyNamedGraphUri)))
                                                    where named.NodeType == NodeType.Uri
                                                    select((IUriNode)named).Uri;

                    if (defGraphs.Any() || namedGraphs.Any())
                    {
                        queryEndpoint = new SparqlRemoteEndpoint(UriFactory.Create(server), defGraphs, namedGraphs);
                        ;
                    }
                    else
                    {
                        queryEndpoint = new SparqlRemoteEndpoint(UriFactory.Create(server));
                    }
                }

                // Find the Update Endpoint or Endpoint URI
                server = ConfigurationLoader.GetConfigurationString(g, objNode, new INode[] { g.CreateUriNode(UriFactory.Create(ConfigurationLoader.PropertyUpdateEndpointUri)), g.CreateUriNode(UriFactory.Create(ConfigurationLoader.PropertyEndpointUri)) });

                if (server == null)
                {
                    INode endpointObj = ConfigurationLoader.GetConfigurationNode(g, objNode, new INode[] { g.CreateUriNode(UriFactory.Create(ConfigurationLoader.PropertyUpdateEndpoint)), g.CreateUriNode(UriFactory.Create(ConfigurationLoader.PropertyEndpoint)) });
                    if (endpointObj == null)
                    {
                        return(false);
                    }
                    temp = ConfigurationLoader.LoadObject(g, endpointObj);
                    if (temp is SparqlRemoteUpdateEndpoint)
                    {
                        updateEndpoint = (SparqlRemoteUpdateEndpoint)temp;
                    }
                    else
                    {
                        throw new DotNetRdfConfigurationException("Unable to load the ReadWriteSparqlConnector identified by the Node '" + objNode.ToString() + "' as the value given for the property dnr:updateEndpoint/dnr:endpoint points to an Object which cannot be loaded as an object which is of the type SparqlRemoteUpdateEndpoint");
                    }
                }
                else
                {
                    updateEndpoint = new SparqlRemoteUpdateEndpoint(UriFactory.Create(server));
                }
                storageProvider = new ReadWriteSparqlConnector(queryEndpoint, updateEndpoint, loadMode);
                break;

            case SparqlHttpProtocol:
                // Get the Service URI
                server = ConfigurationLoader.GetConfigurationString(g, objNode, propServer);
                if (server == null)
                {
                    return(false);
                }
                storageProvider = new SparqlHttpProtocolConnector(UriFactory.Create(server));
                break;

            case Stardog:
            case StardogV1:
            case StardogV2:
            case StardogV3:
                // Get the Server and Store
                server = ConfigurationLoader.GetConfigurationString(g, objNode, propServer);
                if (server == null)
                {
                    return(false);
                }
                store = ConfigurationLoader.GetConfigurationString(g, objNode, propStore);
                if (store == null)
                {
                    return(false);
                }

                // Get User Credentials
                ConfigurationLoader.GetUsernameAndPassword(g, objNode, true, out user, out pwd);

                // Get Reasoning Mode
                StardogReasoningMode reasoning = StardogReasoningMode.None;
                String mode = ConfigurationLoader.GetConfigurationString(g, objNode, g.CreateUriNode(UriFactory.Create(ConfigurationLoader.PropertyLoadMode)));
                if (mode != null)
                {
                    try
                    {
                        reasoning = (StardogReasoningMode)Enum.Parse(typeof(StardogReasoningMode), mode, true);
                    }
                    catch
                    {
                        reasoning = StardogReasoningMode.None;
                    }
                }

                if (user != null && pwd != null)
                {
                    switch (targetType.FullName)
                    {
                    case StardogV1:
                        storageProvider = new StardogV1Connector(server, store, reasoning, user, pwd);
                        break;

                    case StardogV2:
                        storageProvider = new StardogV2Connector(server, store, reasoning, user, pwd);
                        break;

                    case StardogV3:
                        storageProvider = new StardogV3Connector(server, store, user, pwd);
                        break;

                    case Stardog:
                    default:
                        storageProvider = new StardogConnector(server, store, user, pwd);
                        break;
                    }
                }
                else
                {
                    switch (targetType.FullName)
                    {
                    case StardogV1:
                        storageProvider = new StardogV1Connector(server, store, reasoning);
                        break;

                    case StardogV2:
                        storageProvider = new StardogV2Connector(server, store, reasoning);
                        break;

                    case StardogV3:
                        storageProvider = new StardogV3Connector(server, store);
                        break;

                    case Stardog:
                    default:
                        storageProvider = new StardogConnector(server, store);
                        break;
                    }
                }
                break;

            case StardogServer:
            case StardogServerV1:
            case StardogServerV2:
            case StardogServerV3:
                // Get the Server and User Credentials
                server = ConfigurationLoader.GetConfigurationString(g, objNode, propServer);
                if (server == null)
                {
                    return(false);
                }
                ConfigurationLoader.GetUsernameAndPassword(g, objNode, true, out user, out pwd);

                if (user != null && pwd != null)
                {
                    switch (targetType.FullName)
                    {
                    case StardogServerV1:
                        storageServer = new StardogV1Server(server, user, pwd);
                        break;

                    case StardogServerV2:
                        storageServer = new StardogV2Server(server, user, pwd);
                        break;

                    case StardogServerV3:
                        storageServer = new StardogV3Server(server, user, pwd);
                        break;

                    case StardogServer:
                    default:
                        storageServer = new StardogServer(server, user, pwd);
                        break;
                    }
                }
                else
                {
                    switch (targetType.FullName)
                    {
                    case StardogServerV1:
                        storageServer = new StardogV1Server(server);
                        break;

                    case StardogServerV2:
                        storageServer = new StardogV2Server(server);
                        break;

                    case StardogServerV3:
                        storageServer = new StardogV3Server(server);
                        break;

                    case StardogServer:
                    default:
                        storageServer = new StardogServer(server);
                        break;
                    }
                }
                break;
            }

            // Set the return object if one has been loaded
            if (storageProvider != null)
            {
                obj = storageProvider;
            }
            else if (storageServer != null)
            {
                obj = storageServer;
            }

            // Check whether this is a standard HTTP manager and if so load standard configuration
            if (obj is BaseHttpConnector)
            {
                BaseHttpConnector connector = (BaseHttpConnector)obj;

                int timeout = ConfigurationLoader.GetConfigurationInt32(g, objNode, g.CreateUriNode(UriFactory.Create(ConfigurationLoader.PropertyTimeout)), 0);
                if (timeout > 0)
                {
                    connector.Timeout = timeout;
                }
                INode proxyNode = ConfigurationLoader.GetConfigurationNode(g, objNode, g.CreateUriNode(UriFactory.Create(ConfigurationLoader.PropertyProxy)));
                if (proxyNode != null)
                {
                    temp = ConfigurationLoader.LoadObject(g, proxyNode);
                    if (temp is IWebProxy)
                    {
                        connector.Proxy = (IWebProxy)temp;
                    }
                    else
                    {
                        throw new DotNetRdfConfigurationException("Unable to load storage provider/server identified by the Node '" + objNode.ToString() + "' as the value given for the dnr:proxy property pointed to an Object which could not be loaded as an object of the required type WebProxy");
                    }
                }
            }

            return(obj != null);
        }
        /// <summary>
        /// Populates the settings from an existing serialized configuration
        /// </summary>
        /// <param name="g">Graph</param>
        /// <param name="objNode">Object Node</param>
        public virtual void PopulateFrom(IGraph g, INode objNode)
        {
            foreach (PropertyInfo property in this._properties.Keys)
            {
                ConnectionAttribute attr = this._properties[property];

                if (!String.IsNullOrEmpty(attr.PopulateFrom))
                {
                    INode n = objNode;

                    if (!String.IsNullOrEmpty(attr.PopulateVia))
                    {
                        n = ConfigurationLoader.GetConfigurationNode(g, n, g.CreateUriNode(UriFactory.Create(attr.PopulateVia)));
                        if (n == null)
                        {
                            continue;
                        }
                    }

                    switch (attr.Type)
                    {
                    case ConnectionSettingType.Boolean:
                        bool b = ConfigurationLoader.GetConfigurationBoolean(g, n, g.CreateUriNode(UriFactory.Create(attr.PopulateFrom)), (bool)property.GetValue(this, null));
                        property.SetValue(this, b, null);
                        break;

                    case ConnectionSettingType.File:
                    case ConnectionSettingType.Password:
                    case ConnectionSettingType.String:
                        String s = ConfigurationLoader.GetConfigurationString(g, n, g.CreateUriNode(UriFactory.Create(attr.PopulateFrom)));
                        if (!String.IsNullOrEmpty(s))
                        {
                            property.SetValue(this, s, null);
                        }
                        else
                        {
                            //May be a URI as the object
                            IUriNode u = ConfigurationLoader.GetConfigurationNode(g, n, g.CreateUriNode(UriFactory.Create(attr.PopulateFrom))) as IUriNode;
                            if (u != null)
                            {
                                property.SetValue(this, u.Uri.AbsoluteUri, null);
                            }
                        }
                        break;

                    case ConnectionSettingType.Integer:
                        int i = ConfigurationLoader.GetConfigurationInt32(g, n, g.CreateUriNode(UriFactory.Create(attr.PopulateFrom)), (int)property.GetValue(this, null));
                        property.SetValue(this, i, null);
                        break;

                    case ConnectionSettingType.Enum:
                        String enumStr = ConfigurationLoader.GetConfigurationString(g, n, g.CreateUriNode(UriFactory.Create(attr.PopulateFrom)));
                        if (!String.IsNullOrEmpty(enumStr))
                        {
                            try
                            {
                                Object val = Enum.Parse(property.GetValue(this, null).GetType(), enumStr, false);
                                property.SetValue(this, val, null);
                            }
                            catch
                            {
                                //Ignore errors
                            }
                        }
                        break;
                    }
                }
            }
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Creates a new Base Handler Configuration which loads common Handler settings from a Configuration Graph
        /// </summary>
        /// <param name="g">Configuration Graph</param>
        /// <param name="objNode">Object Node</param>
        public BaseHandlerConfiguration(IGraph g, INode objNode)
        {
            //Are there any User Groups associated with this Handler?
            IEnumerable <INode> groups = ConfigurationLoader.GetConfigurationData(g, objNode, ConfigurationLoader.CreateConfigurationNode(g, "dnr:userGroup"));

            foreach (INode group in groups)
            {
                Object temp = ConfigurationLoader.LoadObject(g, group);
                if (temp is UserGroup)
                {
                    this._userGroups.Add((UserGroup)temp);
                }
                else
                {
                    throw new DotNetRdfConfigurationException("Unable to load Handler Configuration as the RDF Configuration file specifies a value for the Handlers dnr:userGroup property which cannot be loaded as an object which is a UserGroup");
                }
            }

            //General Handler Settings
            this._showErrors = ConfigurationLoader.GetConfigurationBoolean(g, objNode, ConfigurationLoader.CreateConfigurationNode(g, ConfigurationLoader.PropertyShowErrors), this._showErrors);
            String introFile = ConfigurationLoader.GetConfigurationString(g, objNode, ConfigurationLoader.CreateConfigurationNode(g, ConfigurationLoader.PropertyIntroFile));

            if (introFile != null)
            {
                introFile = ConfigurationLoader.ResolvePath(introFile);
                if (File.Exists(introFile))
                {
                    using (StreamReader reader = new StreamReader(introFile))
                    {
                        this._introText = reader.ReadToEnd();
                        reader.Close();
                    }
                }
            }
            this._stylesheet  = ConfigurationLoader.GetConfigurationString(g, objNode, ConfigurationLoader.CreateConfigurationNode(g, ConfigurationLoader.PropertyStylesheet)).ToSafeString();
            this._corsEnabled = ConfigurationLoader.GetConfigurationBoolean(g, objNode, ConfigurationLoader.CreateConfigurationNode(g, ConfigurationLoader.PropertyEnableCors), true);

            //Cache Settings
            this._cacheDuration = ConfigurationLoader.GetConfigurationInt32(g, objNode, ConfigurationLoader.CreateConfigurationNode(g, ConfigurationLoader.PropertyCacheDuration), this._cacheDuration);
            if (this._cacheDuration < MinimumCacheDuration)
            {
                this._cacheDuration = MinimumCacheDuration;
            }
            if (this._cacheDuration > MaximumCacheDuration)
            {
                this._cacheDuration = MaximumCacheDuration;
            }
            this._cacheSliding = ConfigurationLoader.GetConfigurationBoolean(g, objNode, ConfigurationLoader.CreateConfigurationNode(g, ConfigurationLoader.PropertyCacheSliding), this._cacheSliding);

            //SPARQL Expression Factories
            IEnumerable <INode> factories = ConfigurationLoader.GetConfigurationData(g, objNode, ConfigurationLoader.CreateConfigurationNode(g, ConfigurationLoader.PropertyExpressionFactory));

            foreach (INode factory in factories)
            {
                Object temp = ConfigurationLoader.LoadObject(g, factory);
                if (temp is ISparqlCustomExpressionFactory)
                {
                    this._expressionFactories.Add((ISparqlCustomExpressionFactory)temp);
                }
                else
                {
                    throw new DotNetRdfConfigurationException("Unable to load Handler Configuration as the RDF Configuration file specifies a value for the Handlers dnr:expressionFactory property which cannot be loaded as an object which is a SPARQL Expression Factory");
                }
            }

            //Writer Properties
            this._writerCompressionLevel = ConfigurationLoader.GetConfigurationInt32(g, objNode, ConfigurationLoader.CreateConfigurationNode(g, ConfigurationLoader.PropertyCompressionLevel), this._writerCompressionLevel);
            this._writerDtds             = ConfigurationLoader.GetConfigurationBoolean(g, objNode, ConfigurationLoader.CreateConfigurationNode(g, ConfigurationLoader.PropertyDtdWriting), this._writerDtds);
            this._writerHighSpeed        = ConfigurationLoader.GetConfigurationBoolean(g, objNode, ConfigurationLoader.CreateConfigurationNode(g, ConfigurationLoader.PropertyHighSpeedWriting), this._writerHighSpeed);
            this._writerMultiThreading   = ConfigurationLoader.GetConfigurationBoolean(g, objNode, ConfigurationLoader.CreateConfigurationNode(g, ConfigurationLoader.PropertyMultiThreadedWriting), this._writerMultiThreading);
            this._writerPrettyPrinting   = ConfigurationLoader.GetConfigurationBoolean(g, objNode, ConfigurationLoader.CreateConfigurationNode(g, ConfigurationLoader.PropertyPrettyPrinting), this._writerPrettyPrinting);
            this._writerAttributes       = ConfigurationLoader.GetConfigurationBoolean(g, objNode, ConfigurationLoader.CreateConfigurationNode(g, ConfigurationLoader.PropertyAttributeWriting), this._writerAttributes);

            //Load in the Default Namespaces if specified
            INode nsNode = ConfigurationLoader.GetConfigurationNode(g, objNode, ConfigurationLoader.CreateConfigurationNode(g, ConfigurationLoader.PropertyImportNamespacesFrom));

            if (nsNode != null)
            {
                Object nsTemp = ConfigurationLoader.LoadObject(g, nsNode);
                if (nsTemp is IGraph)
                {
                    this._defaultNamespaces.Import(((IGraph)nsTemp).NamespaceMap);
                }
            }
        }