Mono.Zeroconf – Blame information for rev 2

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 //
2 // ProviderFactory.cs
3 //
4 // Authors:
5 // Aaron Bockover <abockover@novell.com>
6 //
7 // Copyright (C) 2006-2007 Novell, Inc (http://www.novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28  
29 using System;
30 using System.IO;
31 using System.Reflection;
32 using System.Collections.Generic;
33  
34 namespace Mono.Zeroconf.Providers
35 {
36 internal static class ProviderFactory
37 {
38 private static IZeroconfProvider [] providers;
39 private static IZeroconfProvider selected_provider;
40  
41 private static IZeroconfProvider DefaultProvider {
42 get {
43 if(providers == null) {
44 GetProviders();
45 }
46  
47 return providers[0];
48 }
49 }
50  
51 public static IZeroconfProvider SelectedProvider {
52 get { return selected_provider == null ? DefaultProvider : selected_provider; }
53 set { selected_provider = value; }
54 }
55  
56 private static IZeroconfProvider [] GetProviders()
57 {
58 if(providers != null) {
59 return providers;
60 }
61  
62 List<IZeroconfProvider> providers_list = new List<IZeroconfProvider>();
63 List<string> directories = new List<string>();
64 Assembly asm = Assembly.GetExecutingAssembly();
65  
66 string env_path = Environment.GetEnvironmentVariable("MONO_ZEROCONF_PROVIDERS");
67 if(!String.IsNullOrEmpty(env_path)) {
68 foreach(string path in env_path.Split(':')) {
69 if(Directory.Exists(path)) {
70 directories.Add(path);
71 }
72 }
73 }
74  
75 string this_asm_path = asm.Location;
76 directories.Add(Path.GetDirectoryName(this_asm_path));
77  
78 if(Assembly.GetExecutingAssembly().GlobalAssemblyCache) {
79 string [] path_parts = directories[0].Split(Path.DirectorySeparatorChar);
80 string new_path = Path.DirectorySeparatorChar.ToString();
81 string root = Path.GetPathRoot (this_asm_path);
82 if (root.StartsWith (path_parts[0])) {
83 path_parts[0] = root;
84 }
85  
86 for(int i = 0; i < path_parts.Length - 4; i++) {
87 new_path = Path.Combine(new_path, path_parts[i]);
88 }
89  
90 directories.Add(Path.Combine(new_path, "mono-zeroconf"));
91 }
2 office 92  
93 try
94 {
95 foreach (string directory in directories)
96 {
97 foreach (string file in Directory.GetFiles(directory, "Mono.Zeroconf.Providers.*.dll"))
98 {
99 if (Path.GetFileName(file) != Path.GetFileName(this_asm_path))
100 {
101 Assembly provider_asm = Assembly.LoadFile(file);
102 foreach (Attribute attr in provider_asm.GetCustomAttributes(false))
103 {
104 if (attr is ZeroconfProviderAttribute)
105 {
106 Type type = (attr as ZeroconfProviderAttribute).ProviderType;
107 IZeroconfProvider provider = (IZeroconfProvider) Activator.CreateInstance(type);
108 try
109 {
110 provider.Initialize();
111 providers_list.Add(provider);
112 }
113 catch (Exception e)
114 {
115 Console.WriteLine(e);
116 }
1 office 117 }
118 }
119 }
120 }
121 }
122 }
2 office 123 catch
124 {
125 // Could not load a provider.
126 }
127  
1 office 128 if(providers_list.Count == 0) {
129 throw new Exception("No Zeroconf providers could be found or initialized. Necessary daemon may not be running.");
130 }
131  
132 providers = providers_list.ToArray();
133  
134 return providers;
135 }
136 }
137 }