~do-win/do/test-paths

« back to all changes in this revision

Viewing changes to Mono.Addins/Mono.Addins/RuntimeAddin.cs

  • Committer: Chris S.
  • Date: 2009-06-21 03:37:34 UTC
  • Revision ID: chris@szikszoy.com-20090621033734-ud2jdcd5pq9r3ue9
initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// RuntimeAddin.cs
 
3
//
 
4
// Author:
 
5
//   Lluis Sanchez Gual,
 
6
//   Georg Wächter
 
7
//
 
8
// Copyright (C) 2007 Novell, Inc (http://www.novell.com)
 
9
//
 
10
// Permission is hereby granted, free of charge, to any person obtaining
 
11
// a copy of this software and associated documentation files (the
 
12
// "Software"), to deal in the Software without restriction, including
 
13
// without limitation the rights to use, copy, modify, merge, publish,
 
14
// distribute, sublicense, and/or sell copies of the Software, and to
 
15
// permit persons to whom the Software is furnished to do so, subject to
 
16
// the following conditions:
 
17
// 
 
18
// The above copyright notice and this permission notice shall be
 
19
// included in all copies or substantial portions of the Software.
 
20
// 
 
21
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 
22
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 
23
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 
24
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 
25
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 
26
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 
27
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
28
//
 
29
 
 
30
 
 
31
using System;
 
32
using System.Collections;
 
33
using System.IO;
 
34
using System.Reflection;
 
35
using System.Xml;
 
36
using System.Resources;
 
37
using System.Globalization;
 
38
 
 
39
using Mono.Addins.Description;
 
40
using Mono.Addins.Localization;
 
41
 
 
42
namespace Mono.Addins
 
43
{
 
44
        public class RuntimeAddin
 
45
        {
 
46
                string id;
 
47
                string baseDirectory;
 
48
                string privatePath;
 
49
                Addin ainfo;
 
50
                
 
51
                Assembly[] assemblies;
 
52
                RuntimeAddin[] depAddins;
 
53
                ResourceManager[] resourceManagers;
 
54
                AddinLocalizer localizer;
 
55
                
 
56
                internal RuntimeAddin()
 
57
                {
 
58
                }
 
59
                
 
60
                internal Assembly[] Assemblies {
 
61
                        get { return assemblies; }
 
62
                }
 
63
                
 
64
                public string Id {
 
65
                        get { return Addin.GetIdName (id); }
 
66
                }
 
67
                
 
68
                public string Version {
 
69
                        get { return Addin.GetIdVersion (id); }
 
70
                }
 
71
                
 
72
                internal Addin Addin {
 
73
                        get { return ainfo; }
 
74
                }
 
75
                
 
76
                public override string ToString ()
 
77
                {
 
78
                        return ainfo.ToString ();
 
79
                }
 
80
 
 
81
                void CreateResourceManagers ()
 
82
                {
 
83
                        ArrayList managersList = new ArrayList ();
 
84
 
 
85
                        // Search for embedded resource files
 
86
                        foreach (Assembly asm in assemblies)
 
87
                        {
 
88
                                foreach (string res in asm.GetManifestResourceNames ()) {
 
89
                                        if (res.EndsWith (".resources"))
 
90
                                                managersList.Add (new ResourceManager (res.Substring (0, res.Length - ".resources".Length), asm));
 
91
                                }
 
92
                        }
 
93
 
 
94
                        resourceManagers = (ResourceManager[]) managersList.ToArray (typeof(ResourceManager));
 
95
                }
 
96
 
 
97
                public string GetResourceString (string name)
 
98
                {
 
99
                        return (string) GetResourceObject (name, true, null);
 
100
                }
 
101
 
 
102
                public string GetResourceString (string name, bool throwIfNotFound)
 
103
                {
 
104
                        return (string) GetResourceObject (name, throwIfNotFound, null);
 
105
                }
 
106
 
 
107
                public string GetResourceString (string name, bool throwIfNotFound, CultureInfo culture)
 
108
                {
 
109
                        return (string) GetResourceObject (name, throwIfNotFound, culture);
 
110
                }
 
111
 
 
112
                public object GetResourceObject (string name)
 
113
                {
 
114
                        return GetResourceObject (name, true, null);
 
115
                }
 
116
 
 
117
                public object GetResourceObject (string name, bool throwIfNotFound)
 
118
                {
 
119
                        return GetResourceObject (name, throwIfNotFound, null);
 
120
                }
 
121
 
 
122
                public object GetResourceObject (string name, bool throwIfNotFound, CultureInfo culture)
 
123
                {
 
124
                        if (resourceManagers == null)
 
125
                                CreateResourceManagers ();
 
126
                        
 
127
                        // Look in resources of this add-in
 
128
                        foreach (ResourceManager manager in resourceManagers)
 
129
                        {
 
130
                                object t = manager.GetObject (name, culture);
 
131
                                if (t != null)
 
132
                                        return t;
 
133
                        }
 
134
 
 
135
                        // Look in resources of dependent add-ins
 
136
                        foreach (RuntimeAddin addin in depAddins)
 
137
                        {
 
138
                                object t = addin.GetResourceObject (name, false, culture);
 
139
                                if (t != null)
 
140
                                        return t;
 
141
                        }
 
142
 
 
143
                        if (throwIfNotFound)
 
144
                                throw new InvalidOperationException ("Resource object '" + name + "' not found in add-in '" + id + "'");
 
145
 
 
146
                        return null;
 
147
                }
 
148
 
 
149
 
 
150
                public Type GetType (string typeName)
 
151
                {
 
152
                        return GetType (typeName, true);
 
153
                }
 
154
                
 
155
                public Type GetType (string typeName, bool throwIfNotFound)
 
156
                {
 
157
                        // Look in the addin assemblies
 
158
                        
 
159
                        Type at = Type.GetType (typeName, false);
 
160
                        if (at != null)
 
161
                                return at;
 
162
                        
 
163
                        foreach (Assembly asm in assemblies) {
 
164
                                Type t = asm.GetType (typeName, false);
 
165
                                if (t != null)
 
166
                                        return t;
 
167
                        }
 
168
                        
 
169
                        // Look in the dependent add-ins
 
170
                        foreach (RuntimeAddin addin in depAddins) {
 
171
                                Type t = addin.GetType (typeName, false);
 
172
                                if (t != null)
 
173
                                        return t;
 
174
                        }
 
175
                        
 
176
                        if (throwIfNotFound)
 
177
                                throw new InvalidOperationException ("Type '" + typeName + "' not found in add-in '" + id + "'");
 
178
                        return null;
 
179
                }
 
180
                
 
181
                public object CreateInstance (string typeName)
 
182
                {
 
183
                        return CreateInstance (typeName, true);
 
184
                }
 
185
                
 
186
                public object CreateInstance (string typeName, bool throwIfNotFound)
 
187
                {
 
188
                        Type type = GetType (typeName, throwIfNotFound);
 
189
                        if (type == null)
 
190
                                return null;
 
191
                        else
 
192
                                return Activator.CreateInstance (type, true);
 
193
                }
 
194
                
 
195
                public string GetFilePath (string fileName)
 
196
                {
 
197
                        return Path.Combine (baseDirectory, fileName);
 
198
                }
 
199
                
 
200
                public string PrivateDataPath {
 
201
                        get {
 
202
                                if (privatePath == null) {
 
203
                                        privatePath = ainfo.PrivateDataPath;
 
204
                                        if (!Directory.Exists (privatePath))
 
205
                                                Directory.CreateDirectory (privatePath);
 
206
                                }
 
207
                                return privatePath;
 
208
                        }
 
209
                }
 
210
                
 
211
                public Stream GetResource (string resourceName)
 
212
                {
 
213
                        return GetResource (resourceName, false);
 
214
                }
 
215
                
 
216
                public Stream GetResource (string resourceName, bool throwIfNotFound)
 
217
                {
 
218
                        // Look in the addin assemblies
 
219
                        
 
220
                        foreach (Assembly asm in assemblies) {
 
221
                                Stream res = asm.GetManifestResourceStream (resourceName);
 
222
                                if (res != null)
 
223
                                        return res;
 
224
                        }
 
225
                        
 
226
                        // Look in the dependent add-ins
 
227
                        foreach (RuntimeAddin addin in depAddins) {
 
228
                                Stream res = addin.GetResource (resourceName);
 
229
                                if (res != null)
 
230
                                        return res;
 
231
                        }
 
232
                        
 
233
                        if (throwIfNotFound)
 
234
                                throw new InvalidOperationException ("Resource '" + resourceName + "' not found in add-in '" + id + "'");
 
235
                                
 
236
                        return null;
 
237
                }
 
238
                
 
239
                public AddinLocalizer Localizer {
 
240
                        get {
 
241
                                if (localizer != null)
 
242
                                        return localizer;
 
243
                                else
 
244
                                        return AddinManager.DefaultLocalizer;
 
245
                        }
 
246
                }
 
247
                
 
248
                internal AddinDescription Load (Addin iad)
 
249
                {
 
250
                        ainfo = iad;
 
251
                        
 
252
                        ArrayList plugList = new ArrayList ();
 
253
                        ArrayList asmList = new ArrayList ();
 
254
                        
 
255
                        AddinDescription description = iad.Description;
 
256
                        id = description.AddinId;
 
257
                        baseDirectory = description.BasePath;
 
258
                        
 
259
                        // Load the main modules
 
260
                        LoadModule (description.MainModule, description.Namespace, plugList, asmList);
 
261
                        
 
262
                        // Load the optional modules, if the dependencies are present
 
263
                        foreach (ModuleDescription module in description.OptionalModules) {
 
264
                                if (CheckAddinDependencies (module))
 
265
                                        LoadModule (module, description.Namespace, plugList, asmList);
 
266
                        }
 
267
                        
 
268
                        depAddins = (RuntimeAddin[]) plugList.ToArray (typeof(RuntimeAddin));
 
269
                        assemblies = (Assembly[]) asmList.ToArray (typeof(Assembly));
 
270
                        
 
271
                        if (description.Localizer != null) {
 
272
                                string cls = description.Localizer.GetAttribute ("type");
 
273
                                
 
274
                                // First try getting one of the stock localizers. If none of found try getting the type.
 
275
                                object fob = CreateInstance ("Mono.Addins.Localization." + cls + "Localizer", false);
 
276
                                if (fob == null)
 
277
                                        fob = CreateInstance (cls, true);
 
278
                                
 
279
                                IAddinLocalizerFactory factory = fob as IAddinLocalizerFactory;
 
280
                                if (factory == null)
 
281
                                        throw new InvalidOperationException ("Localizer factory type '" + cls + "' must implement IAddinLocalizerFactory");
 
282
                                localizer = new AddinLocalizer (factory.CreateLocalizer (this, description.Localizer));
 
283
                        }
 
284
                        
 
285
                        return description;
 
286
                }
 
287
                
 
288
                void LoadModule (ModuleDescription module, string ns, ArrayList plugList, ArrayList asmList)
 
289
                {
 
290
                        // Load the assemblies
 
291
                        foreach (string s in module.Assemblies) {
 
292
                                Assembly asm = null;
 
293
 
 
294
                                // don't load the assembly if it's already loaded
 
295
                                string asmPath = Path.Combine (baseDirectory, s);
 
296
                                foreach (Assembly a in AppDomain.CurrentDomain.GetAssemblies ()) {
 
297
                                        // Sorry, you can't load addins from
 
298
                                        // dynamic assemblies as get_Location
 
299
                                        // throws a NotSupportedException
 
300
                                        if (a is System.Reflection.Emit.AssemblyBuilder) {
 
301
                                                continue;
 
302
                                        }
 
303
 
 
304
                                        if (a.Location == asmPath) {
 
305
                                                asm = a;
 
306
                                                break;
 
307
                                        }
 
308
                                }
 
309
 
 
310
                                if (asm == null) {
 
311
                                        asm = Assembly.LoadFrom (asmPath);
 
312
                                }
 
313
 
 
314
                                asmList.Add (asm);
 
315
                        }
 
316
                                
 
317
                        // Collect dependent ids
 
318
                        foreach (Dependency dep in module.Dependencies) {
 
319
                                AddinDependency pdep = dep as AddinDependency;
 
320
                                if (pdep != null) {
 
321
                                        RuntimeAddin adn = AddinManager.SessionService.GetAddin (Addin.GetFullId (ns, pdep.AddinId, pdep.Version));
 
322
                                        if (adn != null)
 
323
                                                plugList.Add (adn);
 
324
                                        else
 
325
                                                AddinManager.ReportError ("Add-in dependency not loaded: " + pdep.FullAddinId, module.ParentAddinDescription.AddinId, null, false);
 
326
                                }
 
327
                        }
 
328
                }
 
329
                
 
330
                internal void UnloadExtensions ()
 
331
                {
 
332
                        // Create the extension points (but do not load them)
 
333
                        AddinDescription emap = Addin.Description;
 
334
                        if (emap == null) return;
 
335
                                
 
336
                        foreach (ExtensionNodeSet rel in emap.ExtensionNodeSets)
 
337
                                AddinManager.SessionService.UnregisterNodeSet (rel);
 
338
                }
 
339
                
 
340
                bool CheckAddinDependencies (ModuleDescription module)
 
341
                {
 
342
                        foreach (Dependency dep in module.Dependencies) {
 
343
                                AddinDependency pdep = dep as AddinDependency;
 
344
                                if (pdep != null && !AddinManager.SessionService.IsAddinLoaded (pdep.FullAddinId))
 
345
                                        return false;
 
346
                        }
 
347
                        return true;
 
348
                }
 
349
        }
 
350
}