Merge branch 'master' of https://code.grnet.gr/git/pithos-ms-client
[pithos-ms-client] / trunk / Pithos.Client.WPF / AppBootstrapper.cs
1 using System.Windows;
2 using System.Windows.Controls;
3 using System.Windows.Navigation;
4 using Caliburn.Micro;
5 using Caliburn.Micro.Logging;
6 using Pithos.Client.WPF.Caliburn.Micro.Logging;
7 using Pithos.Core;
8 using Pithos.Network;
9 using log4net.Appender;
10 using log4net.Config;
11 using log4net.Filter;
12 using log4net.Layout;
13
14 namespace Pithos.Client.WPF
15 {
16         using System;
17         using System.Collections.Generic;
18         using System.ComponentModel.Composition;
19         using System.ComponentModel.Composition.Hosting;
20         using System.ComponentModel.Composition.Primitives;
21         using System.Linq;
22         using Caliburn.Micro;
23
24         public class AppBootstrapper : Bootstrapper<IShell>
25         {
26                 CompositionContainer container;
27
28             public AppBootstrapper()
29             {
30             log4net.Config.XmlConfigurator.Configure();
31
32             LogManager.GetLog = type => new log4netLogger(type);
33             }
34
35                 /// <summary>
36                 /// By default, we are configured to use MEF
37                 /// </summary>
38                 protected override void Configure() {
39                     var catalog = new AggregateCatalog(
40                         AssemblySource.Instance.Select(x => new AssemblyCatalog(x)).OfType<ComposablePartCatalog>()
41                         );
42
43             Type[] types = { typeof(PithosMonitor), typeof(CloudFilesClient) };
44             foreach (var type in types)
45             {
46                 catalog.Catalogs.Add(new AssemblyCatalog(type.Assembly));
47             }
48
49                         container = new CompositionContainer(catalog);
50
51                         var batch = new CompositionBatch();
52
53                         batch.AddExportedValue<IWindowManager>(new WindowManager());
54                         batch.AddExportedValue<IEventAggregator>(new EventAggregator());
55                         batch.AddExportedValue(container);
56                     batch.AddExportedValue(catalog);
57
58
59                         container.Compose(batch);
60
61             ConventionManager.AddElementConvention<MenuItem>(ItemsControl.ItemsSourceProperty, "DataContext", "Click");
62                 }
63
64                 protected override object GetInstance(Type serviceType, string key)
65                 {
66                         string contract = string.IsNullOrEmpty(key) ? AttributedModelServices.GetContractName(serviceType) : key;
67                         var exports = container.GetExportedValues<object>(contract);
68
69                         if (exports.Count() > 0)
70                                 return exports.First();
71
72                         throw new Exception(string.Format("Could not locate any instances of contract {0}.", contract));
73                 }
74
75                 protected override IEnumerable<object> GetAllInstances(Type serviceType)
76                 {
77                         return container.GetExportedValues<object>(AttributedModelServices.GetContractName(serviceType));
78                 }
79
80                 protected override void BuildUp(object instance)
81                 {
82                         container.SatisfyImportsOnce(instance);
83                 }
84         }
85 }