1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle
.Facilities
.Prevalence
18 using System
.Collections
;
19 using System
.Collections
.Specialized
;
21 using Bamboo
.Prevalence
;
22 using Bamboo
.Prevalence
.Util
;
24 using Castle
.MicroKernel
;
25 using Castle
.MicroKernel
.Facilities
;
26 using Castle
.Core
.Configuration
;
29 /// Summary description for PrevalenceFacility.
31 public class PrevalenceFacility
: AbstractFacility
33 private static readonly String IdKey
= "id";
34 private static readonly String IntervalKey
= "snapshotIntervalInHours";
36 public static readonly String SystemTypePropertyKey
= "prevalence.systemtype";
37 public static readonly String StorageDirPropertyKey
= "prevalence.storagedir";
38 public static readonly String AutoMigrationPropertyKey
= "prevalence.autoversionmigration";
39 public static readonly String EngineIdPropertyKey
= "prevalence.engineid";
40 public static readonly String ResetStoragePropertyKey
= "prevalence.resetStorage";
41 public static readonly String SnapshotPeriodPropertyKey
= "prevalence.snapshotPeriod";
43 public static readonly String CleanupPolicyComponentPropertyKey
= "prevalence.cleanupPolicyComponent";
44 public static readonly String SnapShotTakerComponentPropertyKey
= "prevalence.snapshot.taker";
46 public PrevalenceFacility()
50 #region IFacility implementation
52 protected override void Init()
58 public override void Dispose()
60 IConfiguration engines
= GetEnginesConfig();
62 foreach(IConfiguration engine
in engines
.Children
)
64 TakeSnapshotIfRequired(engine
);
65 HandsOffFiles(engine
);
71 private IConfiguration
GetEnginesConfig()
73 return FacilityConfig
.Children
["engines"];
76 protected void RegisterEngines()
78 IConfiguration engines
= GetEnginesConfig();
80 foreach(IConfiguration engine
in engines
.Children
)
82 RegisterEngine(engine
);
86 protected void RegisterExtensions()
88 Kernel
.ComponentModelBuilder
.AddContributor(
89 new PrevalenceActivatorOverriderModelInspector() );
92 protected void RegisterEngine(IConfiguration engineConfig
)
94 String engineKey
= engineConfig
.Attributes
[IdKey
];
95 String systemId
= engineConfig
.Attributes
["systemId"];
96 String resetStorage
= engineConfig
.Attributes
["resetStorage"];
98 Type systemType
= ObtainSystemType(engineConfig
);
99 bool autoVersion
= Convert
.ToBoolean( engineConfig
.Attributes
["autoVersionMigration"] );
101 if (resetStorage
== null) resetStorage
= "false";
103 IDictionary properties
= new HybridDictionary(true);
105 properties
.Add(SystemTypePropertyKey
, systemType
);
106 properties
.Add(AutoMigrationPropertyKey
, autoVersion
);
107 properties
.Add(ResetStoragePropertyKey
, Convert
.ToBoolean(resetStorage
) );
108 properties
.Add(StorageDirPropertyKey
, engineConfig
.Attributes
["storageDir"]);
110 ConfigureSnapshot(engineConfig
, properties
);
112 Kernel
.AddComponentWithExtendedProperties(engineKey
, typeof(PrevalenceEngine
), properties
);
114 RegisterSystem(engineKey
, systemId
, systemType
);
117 private void ConfigureSnapshot(IConfiguration engineConfig
, IDictionary properties
)
119 float period
= GetSnapshotInterval(engineConfig
);
121 if (RequiresSnapshots(period
))
123 if (engineConfig
.Attributes
["cleanupPolicyComponent"] == null)
125 Kernel
.AddComponentInstance(CleanupPolicyComponentPropertyKey
, CleanUpAllFilesPolicy
.Default
);
128 properties
.Add(SnapshotPeriodPropertyKey
, period
);
132 properties
.Add(SnapshotPeriodPropertyKey
, 0f
);
136 private float GetSnapshotInterval(IConfiguration engineConfig
)
140 return Convert
.ToSingle(engineConfig
.Attributes
[IntervalKey
]);
142 catch (FormatException e
)
144 throw new KernelException("Invalid snapshotHoursPeriod.", e
);
148 private bool RequiresSnapshots(float period
)
153 protected Type
ObtainSystemType(IConfiguration config
)
155 String systemTypeName
= config
.Attributes
["systemType"];
157 Type systemType
= null;
159 systemType
= Type
.GetType(systemTypeName
, false, false);
161 if (systemType
== null)
163 String message
= String
.Format(
164 "Could not obtain type for prevalent system '{0}'.", systemTypeName
);
166 throw new KernelException(message
);
172 protected void RegisterSystem(String engineKey
, String systemId
, Type systemType
)
174 IDictionary properties
= new HybridDictionary(true);
176 properties
[EngineIdPropertyKey
] = engineKey
;
178 Kernel
.AddComponentWithExtendedProperties(systemId
, systemType
, properties
);
181 protected void TakeSnapshotIfRequired(IConfiguration engineConfig
)
183 float period
= GetSnapshotInterval(engineConfig
);
185 if (RequiresSnapshots(period
))
187 PrevalenceEngine engine
= (PrevalenceEngine
) Kernel
[engineConfig
.Attributes
[IdKey
]];
189 engine
.TakeSnapshot();
193 protected void HandsOffFiles(IConfiguration engineConfig
)
195 PrevalenceEngine engine
= (PrevalenceEngine
) Kernel
[engineConfig
.Attributes
[IdKey
]];
197 engine
.HandsOffOutputLog();