~ifolder-dev/simias/trunk-packaging

« back to all changes in this revision

Viewing changes to src/core/StoreProvider/.svn/text-base/Provider.cs.svn-base

  • Committer: Jorge O. Castro
  • Date: 2007-12-03 06:56:46 UTC
  • Revision ID: jorge@ubuntu.com-20071203065646-mupcnjcwgm5mnhyt
* Remove a bunch of .svn directories we no longer need.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/****************************************************************************
2
 
 |
3
 
 | Copyright (c) 2007 Novell, Inc.
4
 
 | All Rights Reserved.
5
 
 |
6
 
 | This program is free software; you can redistribute it and/or
7
 
 | modify it under the terms of version 2 of the GNU General Public License as
8
 
 | published by the Free Software Foundation.
9
 
 |
10
 
 | This program is distributed in the hope that it will be useful,
11
 
 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 
 | GNU General Public License for more details.
14
 
 |
15
 
 | You should have received a copy of the GNU General Public License
16
 
 | along with this program; if not, contact Novell, Inc.
17
 
 |
18
 
 | To contact Novell about this file by physical or electronic mail,
19
 
 | you may find current contact information at www.novell.com 
20
 
 |
21
 
 | Author: Russ Young
22
 
 |***************************************************************************/
23
 
 
24
 
 
25
 
using System;
26
 
using System.Xml;
27
 
using System.Text;
28
 
using System.IO;
29
 
using System.Collections;
30
 
using System.Reflection;
31
 
using System.Runtime.Remoting;
32
 
using System.Runtime.Remoting.Activation;
33
 
using System.Runtime.Remoting.Channels;
34
 
using System.Security.Principal;
35
 
using System.Threading;
36
 
using System.Diagnostics;
37
 
using Simias;
38
 
//using Simias.Storage.Provider.Flaim;
39
 
 
40
 
namespace Simias.Storage.Provider
41
 
{       
42
 
        /// <summary>
43
 
        /// Represents a Collection Store Provider.
44
 
        /// </summary>
45
 
        public abstract class Provider
46
 
        {
47
 
                private static readonly ISimiasLog logger = SimiasLogManager.GetLogger(typeof(Provider));
48
 
 
49
 
                #region Static Methods.
50
 
 
51
 
                /// <summary>
52
 
                /// Connect to the store using the provided configuration.
53
 
                /// </summary>
54
 
                /// <param name="conf"></param>
55
 
                /// <param name="created"></param>
56
 
                /// <returns></returns>
57
 
                public static IProvider Connect(ProviderConfig conf, out bool created)
58
 
                {
59
 
                        string assembly = conf.Assembly;
60
 
                        string providerType = conf.TypeName;
61
 
                        created = false;
62
 
                        IProvider provider = null;
63
 
 
64
 
                        // Load the assembly and find our provider.
65
 
                        Assembly pAssembly = AppDomain.CurrentDomain.Load(Path.GetFileNameWithoutExtension(assembly));
66
 
                        object[] args = {conf};
67
 
                        object[] activationAttrs = null;
68
 
                        
69
 
                        provider = (IProvider)pAssembly.CreateInstance(providerType, false, 0, null, args, null, activationAttrs);
70
 
                        if (provider != null)
71
 
                        {
72
 
                                try
73
 
                                {
74
 
                                        provider.OpenStore();
75
 
                                }
76
 
                                catch
77
 
                                {
78
 
                                        provider.CreateStore();
79
 
                                        created = true;
80
 
                                        logger.Info("Created new store {0}.", conf.Path);
81
 
                                }
82
 
                        }
83
 
                        
84
 
                        return (provider);
85
 
                }
86
 
 
87
 
                /// <summary>
88
 
                /// Called to remove the Database path.
89
 
                /// </summary>
90
 
                public static void Delete(string path)
91
 
                {
92
 
                        if (Directory.Exists(path))
93
 
                        {
94
 
                                Directory.Delete(path, true);
95
 
                                logger.Info("Deleted store {0}.", path);
96
 
                        }
97
 
                }
98
 
                
99
 
                #endregion
100
 
        }
101
 
 
102
 
        /// <summary>
103
 
        /// Store configuration class.
104
 
        /// </summary>
105
 
        public class ProviderConfig
106
 
        {
107
 
                private const string CFG_Section = "StoreProvider";
108
 
                private const string CFG_Path = "Path";
109
 
                private const string CFG_Assembly = "Assembly";
110
 
                private const string CFG_TypeName = "Type";
111
 
                private const string StoreName = "simias";
112
 
                private string assembly = null;
113
 
                private string typeName = null;
114
 
                private Hashtable settingTable = new Hashtable();
115
 
 
116
 
                private Simias.Configuration conf;
117
 
                private string storePath;
118
 
                
119
 
 
120
 
                /// <summary>
121
 
                /// Default Constructor
122
 
                /// </summary>
123
 
                public ProviderConfig(Configuration config, string storePath)
124
 
                {
125
 
                        this.conf = config;
126
 
                        this.storePath = storePath;
127
 
                }
128
 
 
129
 
                /// <summary>
130
 
                /// Gets and Sets the DB Path.
131
 
                /// </summary>
132
 
                public string Path
133
 
                {
134
 
                        get
135
 
                        {
136
 
                                return storePath;
137
 
                        }
138
 
                }
139
 
 
140
 
 
141
 
                /// <summary>
142
 
                /// Gets and Sets the Assembly that implements the provider instance used.
143
 
                /// </summary>
144
 
                public string Assembly
145
 
                {
146
 
                        get
147
 
                        {
148
 
                                lock(this)
149
 
                                {
150
 
                                        if (assembly == null)
151
 
                                        {
152
 
                                                assembly = conf.Get(CFG_Section, CFG_Assembly);
153
 
                                                if ( assembly == null )
154
 
                                                {
155
 
                                                        assembly = "SimiasLib.dll";
156
 
                                                }
157
 
                                        }
158
 
                                }
159
 
                                return assembly;
160
 
                        }
161
 
                }
162
 
 
163
 
                /// <summary>
164
 
                /// Gets and Sets the Class Type of the implemented provider.
165
 
                /// </summary>
166
 
                public string TypeName
167
 
                {
168
 
                        get
169
 
                        {
170
 
                                lock(this)
171
 
                                {
172
 
                                        if (typeName == null)
173
 
                                        {
174
 
                                                typeName = conf.Get(CFG_Section, CFG_TypeName);
175
 
                                                if ( typeName == null )
176
 
                                                {
177
 
                                                        typeName = "Simias.Storage.Provider.Flaim.FlaimProvider";
178
 
                                                }
179
 
                                        }
180
 
                                }
181
 
                                return typeName;
182
 
                        }
183
 
                }
184
 
 
185
 
                /// <summary>
186
 
                /// Get the specified configuration setting.
187
 
                /// </summary>
188
 
                /// <param name="key">The setting to retrieve.</param>
189
 
                /// <param name="defaultValue">The default setting.</param>
190
 
                /// <returns>The stored setting.</returns>
191
 
                public string Get(string key, string defaultValue)
192
 
                {
193
 
                        string setting;
194
 
                        lock (this)
195
 
                        {
196
 
                                if (!settingTable.Contains(key))
197
 
                                {
198
 
                                        setting = conf.Get(CFG_Section, key);
199
 
                                        if ( setting == null )
200
 
                                        {
201
 
                                                setting = defaultValue;
202
 
                                        }
203
 
 
204
 
                                        settingTable[key] = setting;
205
 
                                }
206
 
                        }
207
 
                        return (string)settingTable[key];
208
 
                }
209
 
        }
210
 
 
211
 
        /// <summary>
212
 
        ///
213
 
        /// </summary>
214
 
        public class CommitException : SimiasException
215
 
        {
216
 
                /// <summary>
217
 
                ///
218
 
                /// </summary>
219
 
                public XmlDocument CreateDoc;
220
 
                /// <summary>
221
 
                ///
222
 
                /// </summary>
223
 
                public XmlDocument DeleteDoc;
224
 
 
225
 
                /// <summary>
226
 
                ///
227
 
                /// </summary>
228
 
                /// <param name="createDoc"></param>
229
 
                /// <param name="deleteDoc"></param>
230
 
                /// <param name="ex"></param>
231
 
                public CommitException(XmlDocument createDoc, XmlDocument deleteDoc, Exception ex) :
232
 
                        base("Failed to commite Records", ex)
233
 
                {
234
 
                        CreateDoc = createDoc;
235
 
                        DeleteDoc = deleteDoc;
236
 
                }
237
 
 
238
 
                /// <summary>
239
 
                ///
240
 
                /// </summary>
241
 
                public override string Message
242
 
                {
243
 
                        get
244
 
                        {
245
 
                StringBuilder sb = new StringBuilder();
246
 
 
247
 
                                if (CreateDoc != null)
248
 
                                {
249
 
                                        sb.Append("Failed to create:" + Environment.NewLine);
250
 
                                        XmlElement root = CreateDoc.DocumentElement;
251
 
                                        XmlNodeList recordList = root.SelectNodes(XmlTags.ObjectTag);
252
 
                                        foreach (XmlElement recordEl in recordList)
253
 
                                        {
254
 
                                                sb.Append(String.Format("{0}, {1}{2}", recordEl.GetAttribute(XmlTags.IdAttr), recordEl.GetAttribute(XmlTags.NameAttr), Environment.NewLine));
255
 
                                        }
256
 
                                }
257
 
 
258
 
                                if (DeleteDoc != null)
259
 
                                {
260
 
                                        sb.Append("Failed to delete:" + Environment.NewLine);
261
 
                                        XmlElement root = DeleteDoc.DocumentElement;
262
 
                                        XmlNodeList recordList = root.SelectNodes(XmlTags.ObjectTag);
263
 
                                        foreach (XmlElement recordEl in recordList)
264
 
                                        {
265
 
                                                sb.Append(String.Format("{0}, {1}{2}", recordEl.GetAttribute(XmlTags.IdAttr), recordEl.GetAttribute(XmlTags.NameAttr), Environment.NewLine));
266
 
                                        }
267
 
                                }
268
 
                                return (sb.ToString());
269
 
                        }
270
 
                }
271
 
 
272
 
        }
273
 
}