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
20 using Bamboo
.Prevalence
;
21 using Bamboo
.Prevalence
.Util
;
25 using Castle
.MicroKernel
;
26 using Castle
.MicroKernel
.ComponentActivator
;
29 /// Summary description for PrevalenceEngineComponentActivator.
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
];
53 DeleteStorageDir(dir
);
56 PrevalenceEngine engine
= PrevalenceActivator
.CreateEngine(
59 autoVersionMigration
);
61 if (snapshotPeriod
> 0)
63 CreateSnapshotTaker(engine
, snapshotPeriod
);
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
);
89 public static void Kernel_ComponentDestroyed(ComponentModel model
, object instance
)
91 if (instance
is PrevalenceEngine
)
93 PrevalenceEngine engine
= (PrevalenceEngine
) instance
;
95 engine
.TakeSnapshot();