forked from hibernating-rhinos/rhino-esb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpringBootStrapper.cs
99 lines (86 loc) · 3.43 KB
/
SpringBootStrapper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
using System;
using System.Linq;
using System.Reflection;
using Rhino.ServiceBus.Actions;
using Rhino.ServiceBus.Hosting;
using Rhino.ServiceBus.Impl;
using Rhino.ServiceBus.Internal;
using Spring.Context;
using Spring.Context.Support;
using Spring.Objects.Factory.Config;
using Spring.Objects.Factory.Support;
namespace Rhino.ServiceBus.Spring
{
[CLSCompliant(false)]
public abstract class SpringBootStrapper : AbstractBootStrapper
{
private IConfigurableApplicationContext applicationContext;
public SpringBootStrapper()
{
}
public SpringBootStrapper(IConfigurableApplicationContext applicationContext)
{
this.applicationContext = applicationContext;
}
protected IConfigurableApplicationContext ApplicationContext
{
get { return applicationContext; }
}
protected override void ConfigureBusFacility(AbstractRhinoServiceBusConfiguration configuration)
{
configuration.UseSpring(applicationContext);
}
public override void ExecuteDeploymentActions(string user)
{
foreach (IDeploymentAction action in applicationContext.GetAll<IDeploymentAction>())
action.Execute(user);
}
public override void ExecuteEnvironmentValidationActions()
{
foreach (IEnvironmentValidationAction action in applicationContext.GetAll<IEnvironmentValidationAction>())
action.Execute();
}
public override T GetInstance<T>()
{
return applicationContext.Get<T>();
}
public override void Dispose()
{
applicationContext.Dispose();
}
public override void CreateContainer()
{
if (applicationContext == null)
applicationContext = new StaticApplicationContext();
ConfigureContainer();
}
protected virtual void ConfigureContainer()
{
foreach (var assembly in Assemblies)
{
applicationContext.RegisterSingletons<IDeploymentAction>(assembly);
applicationContext.RegisterSingletons<IEnvironmentValidationAction>(assembly);
RegisterConsumersFrom(assembly);
}
}
protected virtual void RegisterConsumersFrom(Assembly assembly)
{
assembly.GetTypes()
.Where(t => typeof(IMessageConsumer).IsAssignableFrom(t)
&& !typeof(IOccasionalMessageConsumer).IsAssignableFrom(t)
&& IsTypeAcceptableForThisBootStrapper(t)
&& !t.IsInterface
&& !t.IsAbstract)
.ToList()
.ForEach(type =>
{
ObjectDefinitionBuilder definitionBuilder = ObjectDefinitionBuilder
.RootObjectDefinition(new DefaultObjectDefinitionFactory(), type)
.SetAutowireMode(AutoWiringMode.Constructor)
.SetSingleton(false);
applicationContext.ObjectFactory.RegisterObjectDefinition(type.FullName, definitionBuilder.ObjectDefinition);
}
);
}
}
}