Added the ability to change the invocation target in the DefaultProxyFactory.
[castle.git] / Facilities / Prevalence / Castle.Facilities.Prevalence / PrevalenceEngineComponentActivator.cs
blobf2525df5e80709d84c21830c900be5b621687306
1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
2 //
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
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
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
17 using System;
18 using System.IO;
20 using Bamboo.Prevalence;
21 using Bamboo.Prevalence.Util;
23 using Castle.Core;
25 using Castle.MicroKernel;
26 using Castle.MicroKernel.ComponentActivator;
28 /// <summary>
29 /// Summary description for PrevalenceEngineComponentActivator.
30 /// </summary>
31 public class PrevalenceEngineComponentActivator : DefaultComponentActivator
33 public PrevalenceEngineComponentActivator(ComponentModel model, IKernel kernel,
34 ComponentInstanceDelegate onCreation,
35 ComponentInstanceDelegate onDestruction) : base(model, kernel, onCreation, onDestruction)
39 protected override object Instantiate(Castle.MicroKernel.CreationContext context)
41 Type systemType = (Type)
42 Model.ExtendedProperties[PrevalenceFacility.SystemTypePropertyKey];
43 String dir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, (String)
44 Model.ExtendedProperties[PrevalenceFacility.StorageDirPropertyKey]);
45 bool autoVersionMigration = (bool)
46 Model.ExtendedProperties[PrevalenceFacility.AutoMigrationPropertyKey];
47 bool resetStorage = (bool)
48 Model.ExtendedProperties[PrevalenceFacility.ResetStoragePropertyKey];
49 float snapshotPeriod = (float)Model.ExtendedProperties[PrevalenceFacility.SnapshotPeriodPropertyKey];
51 if (resetStorage)
53 DeleteStorageDir(dir);
56 PrevalenceEngine engine = PrevalenceActivator.CreateEngine(
57 systemType,
58 dir,
59 autoVersionMigration );
61 if (snapshotPeriod > 0)
63 CreateSnapshotTaker(engine, snapshotPeriod);
66 return engine;
69 private void CreateSnapshotTaker(PrevalenceEngine engine, float snapshotPeriod)
71 TimeSpan period = TimeSpan.FromHours(snapshotPeriod);
72 ICleanUpPolicy policy = (ICleanUpPolicy) Kernel[PrevalenceFacility.CleanupPolicyComponentPropertyKey];
74 SnapshotTaker taker = new SnapshotTaker(engine, period, policy);
76 Kernel.AddComponentInstance(PrevalenceFacility.SnapShotTakerComponentPropertyKey, taker);
79 private void DeleteStorageDir(String dir)
81 DirectoryInfo di = new DirectoryInfo(dir);
83 if (di.Exists)
85 di.Delete(true);
89 public static void Kernel_ComponentDestroyed(ComponentModel model, object instance)
91 if (instance is PrevalenceEngine)
93 PrevalenceEngine engine = (PrevalenceEngine) instance;
95 engine.TakeSnapshot();