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