Permalink
Browse files

Refactored registration to remove extra conditions placed on containe…

…r implementations.

Introduced IBusContainerBuilder, yes it's one class, but it's easy to follow.
Implemented StructureMap, still needs testing.
  • Loading branch information...
1 parent c70d1c9 commit feeda0357b16628f8ea5973ef95dc7c08c3fb9ec @CoreyKaylor CoreyKaylor committed Jan 27, 2011
Showing with 4,907 additions and 633 deletions.
  1. +350 −0 Rhino.ServiceBus.Castle/CastleBuilder.cs
  2. +0 −58 Rhino.ServiceBus.Castle/Config/BusConfiguration.cs
  3. +0 −74 Rhino.ServiceBus.Castle/Config/LoadBalancerConfiguration.cs
  4. +0 −35 Rhino.ServiceBus.Castle/Config/LoadBalancerEndpointConfiguration.cs
  5. +0 −35 Rhino.ServiceBus.Castle/Config/LoggingConfiguration.cs
  6. +0 −65 Rhino.ServiceBus.Castle/Config/MsmqConfiguration.cs
  7. +0 −68 Rhino.ServiceBus.Castle/Config/OneWayBusConfiguration.cs
  8. +0 −50 Rhino.ServiceBus.Castle/Config/RhinoQueuesConfiguration.cs
  9. +0 −70 Rhino.ServiceBus.Castle/Config/SecurityConfiguration.cs
  10. +1 −76 Rhino.ServiceBus.Castle/Extensions.cs
  11. +1 −8 Rhino.ServiceBus.Castle/Rhino.ServiceBus.Castle.csproj
  12. +36 −0 Rhino.ServiceBus.StructureMap/ConsumerInterceptor.cs
  13. +19 −0 Rhino.ServiceBus.StructureMap/Extensions.cs
  14. +71 −0 Rhino.ServiceBus.StructureMap/Rhino.ServiceBus.StructureMap.csproj
  15. +18 −0 Rhino.ServiceBus.StructureMap/SingletonConvention.cs
  16. +79 −0 Rhino.ServiceBus.StructureMap/StructureMapBootStrapper.cs
  17. +224 −0 Rhino.ServiceBus.StructureMap/StructureMapBuilder.cs
  18. +51 −0 Rhino.ServiceBus.StructureMap/StructureMapServiceLocator.cs
  19. +10 −0 Rhino.ServiceBus.sln
  20. +1 −1 Rhino.ServiceBus/Config/IBusConfigurationAware.cs
  21. +22 −0 Rhino.ServiceBus/Config/IBusContainerBuilder.cs
  22. +28 −0 Rhino.ServiceBus/Config/LoadBalancerConfiguration.cs
  23. +12 −6 Rhino.ServiceBus/Config/{LoadBalancerConfigurationReader.cs → LoadBalancerEndpointConfiguration.cs}
  24. +31 −0 Rhino.ServiceBus/Config/LoggingConfiguration.cs
  25. +0 −27 Rhino.ServiceBus/Config/LoggingConfigurationReader.cs
  26. +5 −25 Rhino.ServiceBus/Config/MsmqTransportConfigurationAware.cs
  27. +34 −0 Rhino.ServiceBus/Config/OneWayBusConfiguration.cs
  28. +3 −13 Rhino.ServiceBus/Config/RhinoQueuesConfigurationAware.cs
  29. +9 −5 Rhino.ServiceBus/Config/{SecurityConfigurationReader.cs → SecurityConfiguration.cs}
  30. +13 −12 Rhino.ServiceBus/Impl/AbstractRhinoServiceBusFacility.cs
  31. +3 −2 Rhino.ServiceBus/Impl/OnewayRhinoServiceBusFacility.cs
  32. +6 −0 Rhino.ServiceBus/Impl/RhinoServiceBusFacility.cs
  33. +6 −3 Rhino.ServiceBus/Rhino.ServiceBus.csproj
  34. BIN SharedLibs/StructureMap.dll
  35. +3,874 −0 SharedLibs/StructureMap.xml
@@ -0,0 +1,350 @@
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using System.Messaging;
+using Castle.Core;
+using Castle.Core.Configuration;
+using Castle.MicroKernel.Registration;
+using Castle.MicroKernel.Resolvers.SpecializedResolvers;
+using Castle.Windsor;
+using Rhino.Queues;
+using Rhino.ServiceBus.Actions;
+using Rhino.ServiceBus.Config;
+using Rhino.ServiceBus.Convertors;
+using Rhino.ServiceBus.DataStructures;
+using Rhino.ServiceBus.Exceptions;
+using Rhino.ServiceBus.Impl;
+using Rhino.ServiceBus.Internal;
+using Rhino.ServiceBus.LoadBalancer;
+using Rhino.ServiceBus.MessageModules;
+using Rhino.ServiceBus.Msmq;
+using Rhino.ServiceBus.Msmq.TransportActions;
+using Rhino.ServiceBus.RhinoQueues;
+using Rhino.ServiceBus.Sagas;
+using ErrorAction = Rhino.ServiceBus.Msmq.TransportActions.ErrorAction;
+
+namespace Rhino.ServiceBus.Castle
+{
+ public class CastleBuilder : IBusContainerBuilder
+ {
+ private readonly IWindsorContainer container;
+ private readonly AbstractRhinoServiceBusFacility config;
+
+ public CastleBuilder(IWindsorContainer container, AbstractRhinoServiceBusFacility config)
+ {
+ this.container = container;
+ this.config = config;
+ this.config.BuildWith(this);
+ }
+
+ public void RegisterDefaultServices()
+ {
+ if (!container.Kernel.HasComponent(typeof(IWindsorContainer)))
+ container.Register(Component.For<IWindsorContainer>().Instance(container));
+
+ container.Register(Component.For<IServiceLocator>().ImplementedBy<CastleServiceLocator>());
+
+ container.Register(
+ AllTypes.FromAssembly(typeof(IServiceBus).Assembly)
+ .BasedOn<IBusConfigurationAware>()
+ );
+
+ foreach (var configurationAware in container.ResolveAll<IBusConfigurationAware>())
+ {
+ configurationAware.Configure(config, this);
+ }
+
+ container.Kernel.ComponentModelCreated += Kernel_OnComponentModelCreated;
+ container.Kernel.Resolver.AddSubResolver(new ArrayResolver(container.Kernel));
+
+ foreach (var type in config.MessageModules)
+ {
+ if (container.Kernel.HasComponent(type) == false)
+ container.Register(Component.For(type).Named(type.FullName));
+ }
+
+ container.Register(
+ Component.For<IReflection>()
+ .LifeStyle.Is(LifestyleType.Singleton)
+ .ImplementedBy<DefaultReflection>(),
+
+ Component.For<IMessageSerializer>()
+ .LifeStyle.Is(LifestyleType.Singleton)
+ .ImplementedBy(config.SerializerType),
+ Component.For<IEndpointRouter>()
+ .ImplementedBy<EndpointRouter>()
+ );
+ }
+
+ private static void Kernel_OnComponentModelCreated(ComponentModel model)
+ {
+ if (typeof(IMessageConsumer).IsAssignableFrom(model.Implementation) == false)
+ return;
+
+ var interfaces = model.Implementation.GetInterfaces()
+ .Where(x => x.IsGenericType && x.IsGenericTypeDefinition == false)
+ .Select(x => x.GetGenericTypeDefinition())
+ .ToList();
+
+ if (interfaces.Contains(typeof(InitiatedBy<>)) &&
+ interfaces.Contains(typeof(ISaga<>)) == false)
+ {
+ throw new InvalidUsageException("Message consumer: " + model.Implementation + " implements InitiatedBy<TMsg> but doesn't implment ISaga<TState>. " + Environment.NewLine +
+ "Did you forget to inherit from ISaga<TState> ?");
+ }
+
+ if (interfaces.Contains(typeof(InitiatedBy<>)) == false &&
+ interfaces.Contains(typeof(Orchestrates<>)))
+ {
+ throw new InvalidUsageException("Message consumer: " + model.Implementation + " implements Orchestrates<TMsg> but doesn't implment InitiatedBy<TState>. " + Environment.NewLine +
+ "Did you forget to inherit from InitiatedBy<TState> ?");
+ }
+
+ model.LifestyleType = LifestyleType.Transient;
+ }
+
+ public void RegisterBus()
+ {
+ var busConfig = (RhinoServiceBusFacility) config;
+
+ container.Register(
+ Component.For<IDeploymentAction>()
+ .ImplementedBy<CreateLogQueueAction>(),
+ Component.For<IDeploymentAction>()
+ .ImplementedBy<CreateQueuesAction>(),
+ Component.For<IServiceBus, IStartableServiceBus>()
+ .ImplementedBy<DefaultServiceBus>()
+ .LifeStyle.Is(LifestyleType.Singleton)
+ .DependsOn(new
+ {
+ messageOwners = busConfig.MessageOwners.ToArray()
+ })
+ .Parameters(
+ Parameter.ForKey("modules").Eq(CreateModuleConfigurationNode(busConfig.MessageModules))
+ )
+ );
+ }
+
+ private static IConfiguration CreateModuleConfigurationNode(IEnumerable<Type> messageModules)
+ {
+ var config = new MutableConfiguration("array");
+ foreach (Type type in messageModules)
+ {
+ config.CreateChild("item", "${" + type.FullName + "}");
+ }
+ return config;
+ }
+
+ public void RegisterPrimaryLoadBalancer()
+ {
+ var loadBalancerConfig = (LoadBalancerFacility) config;
+ container.Register(Component.For<MsmqLoadBalancer>()
+ .ImplementedBy(loadBalancerConfig.LoadBalancerType)
+ .LifeStyle.Is(LifestyleType.Singleton)
+ .DependsOn(new
+ {
+ endpoint = loadBalancerConfig.Endpoint,
+ threadCount = loadBalancerConfig.ThreadCount,
+ primaryLoadBalancer = loadBalancerConfig.PrimaryLoadBalancer,
+ transactional = loadBalancerConfig.Transactional
+ }));
+
+ container.Register(
+ Component.For<IDeploymentAction>()
+ .ImplementedBy<CreateLoadBalancerQueuesAction>()
+ );
+ }
+
+ public void RegisterSecondaryLoadBalancer()
+ {
+ var loadBalancerConfig = (LoadBalancerFacility) config;
+ container.Register(Component.For<MsmqLoadBalancer>()
+ .ImplementedBy(loadBalancerConfig.LoadBalancerType)
+ .LifeStyle.Is(LifestyleType.Singleton)
+ .DependsOn(new
+ {
+ endpoint = loadBalancerConfig.Endpoint,
+ threadCount = loadBalancerConfig.ThreadCount,
+ primaryLoadBalancer = loadBalancerConfig.PrimaryLoadBalancer,
+ transactional = loadBalancerConfig.Transactional,
+ secondaryLoadBalancer = loadBalancerConfig.SecondaryLoadBalancer,
+ }));
+
+ container.Register(
+ Component.For<IDeploymentAction>()
+ .ImplementedBy<CreateLoadBalancerQueuesAction>()
+ );
+ }
+
+ public void RegisterReadyForWork()
+ {
+ var loadBalancerConfig = (LoadBalancerFacility) config;
+ container.Register(Component.For<MsmqReadyForWorkListener>()
+ .LifeStyle.Is(LifestyleType.Singleton)
+ .DependsOn(new
+ {
+ endpoint = loadBalancerConfig.ReadyForWork,
+ threadCount = loadBalancerConfig.ThreadCount,
+ transactional = loadBalancerConfig.Transactional
+ }));
+ container.Register(
+ Component.For<IDeploymentAction>()
+ .ImplementedBy<CreateReadyForWorkQueuesAction>()
+ );
+ }
+
+ public void RegisterLoadBalancerEndpoint(Uri loadBalancerEndpoint)
+ {
+ container.Register(
+ Component.For<LoadBalancerMessageModule>()
+ .DependsOn(new {loadBalancerEndpoint})
+ );
+ }
+
+ public void RegisterLoggingEndpoint(Uri logEndpoint)
+ {
+ container.Register(
+ Component.For<MessageLoggingModule>()
+ .DependsOn(new {logQueue = logEndpoint})
+ );
+ }
+
+ public void RegisterMsmqTransport(Type queueStrategyType)
+ {
+ container.Kernel.Register(
+ Component.For<IQueueStrategy>()
+ .LifeStyle.Is(LifestyleType.Singleton)
+ .ImplementedBy(queueStrategyType)
+ .DependsOn(new { endpoint = config.Endpoint }),
+ Component.For<IMsmqTransportAction>()
+ .ImplementedBy<ErrorAction>()
+ .DependsOn(new { numberOfRetries = config.NumberOfRetries}),
+ Component.For<ISubscriptionStorage>()
+ .LifeStyle.Is(LifestyleType.Singleton)
+ .ImplementedBy(typeof(MsmqSubscriptionStorage))
+ .DependsOn(new
+ {
+ queueBusListensTo = config.Endpoint
+ }),
+ Component.For<ITransport>()
+ .LifeStyle.Is(LifestyleType.Singleton)
+ .ImplementedBy(typeof(MsmqTransport))
+ .DependsOn(new
+ {
+ threadCount = config.ThreadCount,
+ endpoint = config.Endpoint,
+ queueIsolationLevel = config.IsolationLevel,
+ numberOfRetries = config.NumberOfRetries,
+ transactional = config.Transactional,
+ consumeInTransaction = config.ConsumeInTransaction,
+ }),
+ AllTypes.FromAssembly(typeof(IMsmqTransportAction).Assembly)
+ .BasedOn<IMsmqTransportAction>()
+ .Unless(x => x == typeof(ErrorAction))
+ .WithService.FirstInterface()
+ .Configure(registration =>
+ registration.LifeStyle.Is(LifestyleType.Singleton))
+ );
+ }
+
+ public void RegisterQueueCreation()
+ {
+ container.Kernel.Register(Component.For<QueueCreationModule>());
+ }
+
+ public void RegisterMsmqOneWay()
+ {
+ var oneWayConfig = (OnewayRhinoServiceBusFacility) config;
+ container.Register(
+ Component.For<IMessageBuilder<Message>>()
+ .LifeStyle.Is(LifestyleType.Singleton)
+ .ImplementedBy<MsmqMessageBuilder>(),
+ Component.For<IOnewayBus>()
+ .LifeStyle.Is(LifestyleType.Singleton)
+ .ImplementedBy<MsmqOnewayBus>()
+ .DependsOn(new { messageOwners = oneWayConfig.MessageOwners }));
+ }
+
+ public void RegisterRhinoQueuesTransport(string path, string name)
+ {
+ container.Register(
+ Component.For<ISubscriptionStorage>()
+ .LifeStyle.Is(LifestyleType.Singleton)
+ .ImplementedBy(typeof(PhtSubscriptionStorage))
+ .DependsOn(new
+ {
+ subscriptionPath = Path.Combine(path, name + "_subscriptions.esent")
+ }),
+ Component.For<ITransport>()
+ .LifeStyle.Is(LifestyleType.Singleton)
+ .ImplementedBy(typeof(RhinoQueuesTransport))
+ .DependsOn(new
+ {
+ threadCount = config.ThreadCount,
+ endpoint = config.Endpoint,
+ queueIsolationLevel = config.IsolationLevel,
+ numberOfRetries = config.NumberOfRetries,
+ path = Path.Combine(path, name + ".esent")
+ }),
+ Component.For<IMessageBuilder<MessagePayload>>()
+ .ImplementedBy<RhinoQueuesMessageBuilder>()
+ .LifeStyle.Is(LifestyleType.Singleton)
+ );
+ }
+
+ public void RegisterRhinoQueuesOneWay()
+ {
+ var oneWayConfig = (OnewayRhinoServiceBusFacility) config;
+ container.Register(
+ Component.For<IMessageBuilder<MessagePayload>>()
+ .ImplementedBy<RhinoQueuesMessageBuilder>()
+ .LifeStyle.Is(LifestyleType.Singleton),
+ Component.For<IOnewayBus>()
+ .LifeStyle.Is(LifestyleType.Singleton)
+ .ImplementedBy<RhinoQueuesOneWayBus>()
+ .DependsOn(new
+ {
+ messageOwners = oneWayConfig.MessageOwners.ToArray(),
+ })
+ );
+ }
+
+ public void RegisterSecurity(byte[] key)
+ {
+ container.Register(
+ Component.For<IEncryptionService>()
+ .ImplementedBy<RijndaelEncryptionService>()
+ .DependsOn(new
+ {
+ key,
+ })
+ .Named("esb.security")
+ );
+
+ container.Register(
+ Component.For<IValueConvertor<WireEcryptedString>>()
+ .ImplementedBy<WireEcryptedStringConvertor>()
+ .ServiceOverrides(ServiceOverride.ForKey("encryptionService").Eq("esb.security"))
+ );
+
+ container.Register(
+ Component.For<IElementSerializationBehavior>()
+ .ImplementedBy<WireEncryptedMessageConvertor>()
+ .ServiceOverrides(ServiceOverride.ForKey("encryptionService").Eq("esb.security"))
+ );
+ }
+
+ public void RegisterNoSecurity()
+ {
+ container.Register(
+ Component.For<IValueConvertor<WireEcryptedString>>()
+ .ImplementedBy<ThrowingWireEcryptedStringConvertor>()
+ );
+ container.Register(
+ Component.For<IElementSerializationBehavior>()
+ .ImplementedBy<ThrowingWireEncryptedMessageConvertor>()
+ );
+ }
+ }
+}
Oops, something went wrong.

0 comments on commit feeda03

Please sign in to comment.