~ubuntu-branches/ubuntu/karmic/mono-addins/karmic

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Mirco Bauer
  • Date: 2007-07-14 12:07:48 UTC
  • Revision ID: james.westby@ubuntu.com-20070714120748-2elczfsjlrdsrpms
Tags: upstream-0.2
ImportĀ upstreamĀ versionĀ 0.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// AddinService.cs
 
3
//
 
4
// Author:
 
5
//   Lluis Sanchez Gual
 
6
//
 
7
// Copyright (C) 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
 
 
30
using System;
 
31
using System.Xml;
 
32
using System.Collections;
 
33
using System.Reflection;
 
34
 
 
35
using Mono.Addins.Description;
 
36
using Mono.Addins.Database;
 
37
 
 
38
namespace Mono.Addins
 
39
{
 
40
        internal class AddinSessionService
 
41
        {
 
42
                bool checkAssemblyLoadConflicts;
 
43
                Hashtable loadedAddins = new Hashtable ();
 
44
                ExtensionContext defaultContext;
 
45
                Hashtable nodeSets = new Hashtable ();
 
46
                Hashtable autoExtensionTypes = new Hashtable ();
 
47
                
 
48
                internal void Initialize ()
 
49
                {
 
50
                        defaultContext = new ExtensionContext ();
 
51
                        ActivateRoots ();
 
52
                        AppDomain.CurrentDomain.AssemblyLoad += new AssemblyLoadEventHandler (OnAssemblyLoaded);
 
53
                }
 
54
                
 
55
                internal void Shutdown ()
 
56
                {
 
57
                        AppDomain.CurrentDomain.AssemblyLoad -= new AssemblyLoadEventHandler (OnAssemblyLoaded);
 
58
                        defaultContext = null;
 
59
                        loadedAddins.Clear ();
 
60
                        defaultContext = null;
 
61
                }
 
62
                
 
63
                internal ExtensionContext DefaultContext {
 
64
                        get { return defaultContext; }
 
65
                }
 
66
                
 
67
                // Enables or disables conflict checking while loading assemblies.
 
68
                // Disabling makes loading faster, but less safe.
 
69
                public bool CheckAssemblyLoadConflicts {
 
70
                        get { return checkAssemblyLoadConflicts; }
 
71
                        set { checkAssemblyLoadConflicts = value; }
 
72
                }
 
73
 
 
74
                public bool IsAddinLoaded (string id)
 
75
                {
 
76
                        return loadedAddins.Contains (Addin.GetIdName (id));
 
77
                }
 
78
                
 
79
                internal RuntimeAddin GetAddin (string id)
 
80
                {
 
81
                        return (RuntimeAddin) loadedAddins [Addin.GetIdName (id)];
 
82
                }
 
83
                
 
84
                internal void ActivateAddin (string id)
 
85
                {
 
86
                        defaultContext.ActivateAddinExtensions (id);
 
87
                }
 
88
                
 
89
                internal void UnloadAddin (string id)
 
90
                {
 
91
                        defaultContext.RemoveAddinExtensions (id);
 
92
                        
 
93
                        RuntimeAddin addin = GetAddin (id);
 
94
                        if (addin != null) {
 
95
                                addin.UnloadExtensions ();
 
96
                                loadedAddins.Remove (Addin.GetIdName (id));
 
97
                                AddinManager.ReportAddinUnload (id);
 
98
                        }
 
99
                }
 
100
                
 
101
                internal bool LoadAddin (IProgressStatus statusMonitor, string id, bool throwExceptions)
 
102
                {
 
103
                        try {
 
104
                                if (IsAddinLoaded (id))
 
105
                                        return true;
 
106
 
 
107
                                if (!AddinManager.Registry.IsAddinEnabled (id)) {
 
108
                                        string msg = GettextCatalog.GetString ("Disabled add-ins can't be loaded.");
 
109
                                        AddinManager.ReportError (msg, id, null, false);
 
110
                                        if (throwExceptions)
 
111
                                                throw new InvalidOperationException (msg);
 
112
                                        return false;
 
113
                                }
 
114
 
 
115
                                ArrayList addins = new ArrayList ();
 
116
                                Stack depCheck = new Stack ();
 
117
                                ResolveLoadDependencies (addins, depCheck, id, false);
 
118
                                addins.Reverse ();
 
119
                                
 
120
                                if (statusMonitor != null)
 
121
                                        statusMonitor.SetMessage ("Loading Addins");
 
122
                                
 
123
                                for (int n=0; n<addins.Count; n++) {
 
124
                                        
 
125
                                        if (statusMonitor != null)
 
126
                                                statusMonitor.SetProgress ((double) n / (double)addins.Count);
 
127
                                        
 
128
                                        Addin iad = (Addin) addins [n];
 
129
                                        if (IsAddinLoaded (iad.Id))
 
130
                                                continue;
 
131
 
 
132
                                        if (statusMonitor != null)
 
133
                                                statusMonitor.SetMessage (string.Format(GettextCatalog.GetString("Loading {0} add-in"), iad.Id));
 
134
                                        
 
135
                                        if (!InsertAddin (statusMonitor, iad))
 
136
                                                return false;
 
137
                                }
 
138
                                return true;
 
139
                        }
 
140
                        catch (Exception ex) {
 
141
                                AddinManager.ReportError ("Add-in could not be loaded: " + ex.Message, id, ex, false);
 
142
                                if (statusMonitor != null)
 
143
                                        statusMonitor.ReportError ("Add-in '" + id + "' could not be loaded.", ex);
 
144
                                if (throwExceptions)
 
145
                                        throw;
 
146
                                return false;
 
147
                        }
 
148
                }
 
149
                        
 
150
                bool InsertAddin (IProgressStatus statusMonitor, Addin iad)
 
151
                {
 
152
                        try {
 
153
                                RuntimeAddin p = new RuntimeAddin ();
 
154
                                
 
155
                                // Read the config file and load the add-in assemblies
 
156
                                AddinDescription description = p.Load (iad);
 
157
                                
 
158
                                // Register the add-in
 
159
                                loadedAddins [Addin.GetIdName (p.Id)] = p;
 
160
                                
 
161
                                if (!AddinDatabase.RunningSetupProcess) {
 
162
                                        // Load the extension points and other addin data
 
163
                                        
 
164
                                        foreach (ExtensionNodeSet rel in description.ExtensionNodeSets) {
 
165
                                                RegisterNodeSet (rel);
 
166
                                        }
 
167
                                        
 
168
                                        foreach (ConditionTypeDescription cond in description.ConditionTypes) {
 
169
                                                Type ctype = p.GetType (cond.TypeName, true);
 
170
                                                defaultContext.RegisterCondition (cond.Id, ctype);
 
171
                                        }
 
172
                                }
 
173
                                        
 
174
                                foreach (ExtensionPoint ep in description.ExtensionPoints)
 
175
                                        InsertExtensionPoint (p, ep);
 
176
                                
 
177
                                // Fire loaded event
 
178
                                defaultContext.NotifyAddinLoaded (p);
 
179
                                AddinManager.ReportAddinLoad (p.Id);
 
180
                                return true;
 
181
                        }
 
182
                        catch (Exception ex) {
 
183
                                AddinManager.ReportError ("Add-in could not be loaded", iad.Id, ex, false);
 
184
                                if (statusMonitor != null)
 
185
                                        statusMonitor.ReportError ("Add-in '" + iad.Id + "' could not be loaded.", ex);
 
186
                                return false;
 
187
                        }
 
188
                }
 
189
                
 
190
                internal void InsertExtensionPoint (RuntimeAddin addin, ExtensionPoint ep)
 
191
                {
 
192
                        defaultContext.CreateExtensionPoint (ep);
 
193
                        foreach (ExtensionNodeType nt in ep.NodeSet.NodeTypes) {
 
194
                                if (nt.ObjectTypeName.Length > 0) {
 
195
                                        Type ntype = addin.GetType (nt.ObjectTypeName, true);
 
196
                                        RegisterAutoTypeExtensionPoint (ntype, ep.Path);
 
197
                                }
 
198
                        }
 
199
                }
 
200
                
 
201
                bool ResolveLoadDependencies (ArrayList addins, Stack depCheck, string id, bool optional)
 
202
                {
 
203
                        if (IsAddinLoaded (id))
 
204
                                return true;
 
205
                                
 
206
                        if (depCheck.Contains (id))
 
207
                                throw new InvalidOperationException ("A cyclic addin dependency has been detected.");
 
208
 
 
209
                        depCheck.Push (id);
 
210
 
 
211
                        Addin iad = AddinManager.Registry.GetAddin (id);
 
212
                        if (iad == null || !iad.Enabled) {
 
213
                                if (optional)
 
214
                                        return false;
 
215
                                else if (iad != null && !iad.Enabled)
 
216
                                        throw new MissingDependencyException (GettextCatalog.GetString ("The required addin '{0}' is disabled.", id));
 
217
                                else
 
218
                                        throw new MissingDependencyException (GettextCatalog.GetString ("The required addin '{0}' is not installed.", id));
 
219
                        }
 
220
 
 
221
                        // If this addin has already been requested, bring it to the head
 
222
                        // of the list, so it is loaded earlier than before.
 
223
                        addins.Remove (iad);
 
224
                        addins.Add (iad);
 
225
                        
 
226
                        foreach (Dependency dep in iad.AddinInfo.Dependencies) {
 
227
                                AddinDependency adep = dep as AddinDependency;
 
228
                                if (adep != null) {
 
229
                                        try {
 
230
                                                string adepid = Addin.GetFullId (iad.AddinInfo.Namespace, adep.AddinId, adep.Version);
 
231
                                                ResolveLoadDependencies (addins, depCheck, adepid, false);
 
232
                                        } catch (MissingDependencyException) {
 
233
                                                if (optional)
 
234
                                                        return false;
 
235
                                                else
 
236
                                                        throw;
 
237
                                        }
 
238
                                }
 
239
                        }
 
240
                        
 
241
                        if (iad.AddinInfo.OptionalDependencies != null) {
 
242
                                foreach (Dependency dep in iad.AddinInfo.OptionalDependencies) {
 
243
                                        AddinDependency adep = dep as AddinDependency;
 
244
                                        if (adep != null) {
 
245
                                                string adepid = Addin.GetFullId (iad.Namespace, adep.AddinId, adep.Version);
 
246
                                                if (!ResolveLoadDependencies (addins, depCheck, adepid, true))
 
247
                                                return false;
 
248
                                        }
 
249
                                }
 
250
                        }
 
251
                                
 
252
                        depCheck.Pop ();
 
253
                        return true;
 
254
                }
 
255
                
 
256
                public void RegisterNodeSet (ExtensionNodeSet nset)
 
257
                {
 
258
                        nodeSets [nset.Id] = nset;
 
259
                }
 
260
                
 
261
                public void UnregisterNodeSet (ExtensionNodeSet nset)
 
262
                {
 
263
                        nodeSets.Remove (nset.Id);
 
264
                }
 
265
                
 
266
                public string GetNodeTypeAddin (ExtensionNodeSet nset, string type, string callingAddinId)
 
267
                {
 
268
                        ExtensionNodeType nt = FindType (nset, type, callingAddinId);
 
269
                        if (nt != null)
 
270
                                return nt.AddinId;
 
271
                        else
 
272
                                return null;
 
273
                }
 
274
                
 
275
                internal ExtensionNodeType FindType (ExtensionNodeSet nset, string name, string callingAddinId)
 
276
                {
 
277
                        if (nset == null)
 
278
                                return null;
 
279
 
 
280
                        foreach (ExtensionNodeType nt in nset.NodeTypes) {
 
281
                                if (nt.Id == name)
 
282
                                        return nt;
 
283
                        }
 
284
                        
 
285
                        foreach (string ns in nset.NodeSets) {
 
286
                                ExtensionNodeSet regSet = (ExtensionNodeSet) nodeSets [ns];
 
287
                                if (regSet == null) {
 
288
                                        AddinManager.ReportError ("Unknown node set: " + ns, callingAddinId, null, false);
 
289
                                        return null;
 
290
                                }
 
291
                                ExtensionNodeType nt = FindType (regSet, name, callingAddinId);
 
292
                                if (nt != null)
 
293
                                        return nt;
 
294
                        }
 
295
                        return null;
 
296
                }
 
297
                
 
298
                public void RegisterAutoTypeExtensionPoint (Type type, string path)
 
299
                {
 
300
                        autoExtensionTypes [type] = path;
 
301
                }
 
302
 
 
303
                public void UnregisterAutoTypeExtensionPoint (Type type, string path)
 
304
                {
 
305
                        autoExtensionTypes.Remove (type);
 
306
                }
 
307
                
 
308
                public string GetAutoTypeExtensionPoint (Type type)
 
309
                {
 
310
                        return autoExtensionTypes [type] as string;
 
311
                }
 
312
 
 
313
                void OnAssemblyLoaded (object s, AssemblyLoadEventArgs a)
 
314
                {
 
315
                        CheckHostAssembly (a.LoadedAssembly);
 
316
                }
 
317
                
 
318
                internal void ActivateRoots ()
 
319
                {
 
320
                        foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies ())
 
321
                                CheckHostAssembly (asm);
 
322
                }
 
323
                
 
324
                void CheckHostAssembly (Assembly asm)
 
325
                {
 
326
                        if (AddinDatabase.RunningSetupProcess || asm is System.Reflection.Emit.AssemblyBuilder)
 
327
                                return;
 
328
                        string asmFile = new Uri (asm.CodeBase).LocalPath;
 
329
                        Addin ainfo = AddinManager.Registry.GetAddinForHostAssembly (asmFile);
 
330
                        if (ainfo != null && !IsAddinLoaded (ainfo.Id))
 
331
                                InsertAddin (null, ainfo);
 
332
                }
 
333
        }
 
334
                
 
335
}