Skip to content

Commit

Permalink
issue SolrNet#70 cores configuration by xml
Browse files Browse the repository at this point in the history
git-svn-id: http://solrnet.googlecode.com/svn/trunk@500 66c3f25c-543c-0410-ae2e-6f2ca0bd8c61
  • Loading branch information
mausch committed Jan 3, 2010
1 parent 29d08f4 commit deda334
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 4 deletions.
40 changes: 37 additions & 3 deletions Castle.Facilities.SolrNetIntegration.Tests/Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@
using System;
using System.Reflection;
using Castle.Core.Configuration;
using Castle.Core.Resource;
using Castle.MicroKernel.Facilities;
using Castle.MicroKernel.Registration;
using Castle.MicroKernel.SubSystems.Configuration;
using Castle.Windsor;
using Castle.Windsor.Configuration.Interpreters;
using MbUnit.Framework;
using Rhino.Mocks;
using SolrNet;
Expand Down Expand Up @@ -171,15 +173,47 @@ public void AddCore() {
var container = new WindsorContainer();
container.AddFacility("solr", solrFacility);

TestCores(container);
}

[Test]
public void AddCoreFromXML() {
var solrFacility = new SolrNetFacility();
var container = new WindsorContainer(new XmlInterpreter(new StaticContentResource(@"<castle>
<facilities>
<facility id='solr'>
<solrURL>http://localhost:8983/solr/defaultCore</solrURL>
<cores>
<core id='core0-id'>
<documentType>Castle.Facilities.SolrNetIntegration.Tests.Tests+Document, Castle.Facilities.SolrNetIntegration.Tests</documentType>
<url>http://localhost:8983/solr/core0</url>
</core>
<core id='core1-id'>
<documentType>Castle.Facilities.SolrNetIntegration.Tests.Tests+Document, Castle.Facilities.SolrNetIntegration.Tests</documentType>
<url>http://localhost:8983/solr/core1</url>
</core>
<core id='core2-id'>
<documentType>Castle.Facilities.SolrNetIntegration.Tests.Tests+Core1Entity, Castle.Facilities.SolrNetIntegration.Tests</documentType>
<url>http://localhost:8983/solr/core1</url>
</core>
</cores>
</facility>
</facilities>
</castle>")));
container.AddFacility("solr", solrFacility);
TestCores(container);
}

public void TestCores(IWindsorContainer container) {
// assert that everything is correctly wired
container.Kernel.DependencyResolving += (client, model, dep) => {
if (model.TargetType == typeof(ISolrConnection)) {
if (client.Name.StartsWith("core0-id"))
Assert.AreEqual(core0url, ((ISolrConnection)dep).ServerURL);
Assert.AreEqual("http://localhost:8983/solr/core0", ((ISolrConnection)dep).ServerURL);
if (client.Name.StartsWith("core1-id"))
Assert.AreEqual(core1url, ((ISolrConnection)dep).ServerURL);
Assert.AreEqual("http://localhost:8983/solr/core1", ((ISolrConnection)dep).ServerURL);
if (client.Name.StartsWith("core2-id"))
Assert.AreEqual(core1url, ((ISolrConnection)dep).ServerURL);
Assert.AreEqual("http://localhost:8983/solr/core1", ((ISolrConnection)dep).ServerURL);
}
};

Expand Down
2 changes: 1 addition & 1 deletion Castle.Facilities.SolrNetIntegration/SolrCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
using System;

namespace Castle.Facilities.SolrNetIntegration {
internal class SolrCore {
public class SolrCore {
public string Id { get; private set; }
public Type DocumentType { get; private set; }
public string Url { get; private set; }
Expand Down
38 changes: 38 additions & 0 deletions Castle.Facilities.SolrNetIntegration/SolrNetFacility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

using System;
using System.Collections.Generic;
using Castle.Core.Configuration;
using Castle.MicroKernel.Facilities;
using Castle.MicroKernel.Registration;
using SolrNet;
Expand Down Expand Up @@ -88,6 +89,7 @@ protected override void Init() {
Kernel.Register(Component.For<ISolrDocumentPropertyVisitor>().ImplementedBy<DefaultDocumentVisitor>());

// set up cores
AddCoresFromConfig();
foreach (var core in cores) {
var coreConnectionId = core.Id + typeof (SolrConnection);
Kernel.Register(Component.For<ISolrConnection>().ImplementedBy<SolrConnection>()
Expand Down Expand Up @@ -128,11 +130,47 @@ private static void ValidateUrl(string url) {
}
}

public void AddCore(Type documentType, string coreUrl) {
AddCore(Guid.NewGuid().ToString(), documentType, coreUrl);
}

public void AddCore(string coreId, Type documentType, string coreUrl) {
ValidateUrl(coreUrl);
cores.Add(new SolrCore(coreId, documentType, coreUrl));
}

private void AddCoresFromConfig() {
if (FacilityConfig == null)
return;
var coresConfig = FacilityConfig.Children["cores"];
if (coresConfig == null)
return;
foreach (var coreConfig in coresConfig.Children) {
var id = coreConfig.Attributes["id"] ?? Guid.NewGuid().ToString();
var documentType = GetCoreDocumentType(coreConfig);
var coreUrl = GetCoreUrl(coreConfig);
AddCore(id, documentType, coreUrl);
}
}

private string GetCoreUrl(IConfiguration coreConfig) {
var node = coreConfig.Children["url"];
if (node == null)
throw new FacilityException("Core url missing in SolrNet core configuration");
return node.Value;
}

private Type GetCoreDocumentType(IConfiguration coreConfig) {
var node = coreConfig.Children["documentType"];
if (node == null)
throw new FacilityException("Document type missing in SolrNet core configuration");
try {
return Type.GetType(node.Value);
} catch (Exception e) {
throw new FacilityException(string.Format("Error getting document type '{0}'", node.Value), e);
}
}

private readonly List<SolrCore> cores = new List<SolrCore>();
}
}

0 comments on commit deda334

Please sign in to comment.