Added generalized installation capabilities to Windsor (analogous to Kernel registration)
[castle.git] / Tools / ManagedExtensions / ManagementExtensions / ManagedObjectName.cs
blob24a7ba97ea749c494781df98a9a7007088b0dfe7
1 // Copyright 2003-2004 DigitalCraftsmen - http://www.digitalcraftsmen.com.br/
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle.ManagementExtensions
17 using System;
18 using System.Text;
19 using System.Collections;
20 using System.Runtime.Serialization;
22 /// <summary>
23 /// Represents a ManagedObject's Name.
24 /// TODO: Supports query semantic.
25 /// </summary>
26 [Serializable]
27 public class ManagedObjectName : ISerializable
29 protected String domain;
30 protected String literalProperties = String.Empty;
31 protected Hashtable properties;
32 protected bool allProperties;
34 /// <summary>
35 /// Creates a ManagedObjectName using a name pattern like
36 /// "domain:key=value,key2=value2"
37 /// </summary>
38 /// <param name="name">Complete name</param>
39 public ManagedObjectName(String name)
41 Setup(name);
44 /// <summary>
45 /// Creates a ManagedObjectName with specified domain and
46 /// properties.
47 /// </summary>
48 /// <param name="domain">Domain name</param>
49 /// <param name="properties">Property list.</param>
50 public ManagedObjectName(String domain, String properties)
52 SetupDomain(domain);
53 SetupProperties(properties);
56 /// <summary>
57 /// Creates a ManagedObjectName with specified domain and
58 /// properties.
59 /// </summary>
60 /// <param name="domain">Domain name</param>
61 /// <param name="properties">Property list.</param>
62 public ManagedObjectName(String domain, Hashtable properties)
64 SetupDomain(domain);
65 SetupProperties(properties);
68 /// <summary>
69 /// Serialization constructor.
70 /// </summary>
71 /// <param name="info"></param>
72 /// <param name="context"></param>
73 public ManagedObjectName(SerializationInfo info, StreamingContext context)
75 String domain = info.GetString("domain");
76 String props = info.GetString("props");
77 SetupDomain(domain);
79 if (props != String.Empty)
81 SetupProperties(props);
85 /// <summary>
86 /// Parses the full name extracting the domain and properties.
87 /// </summary>
88 /// <param name="name">Full name.</param>
89 protected virtual void Setup(String name)
91 if (name == null)
93 throw new ArgumentNullException("name");
96 if (name.IndexOf(':') != -1)
98 String[] splitted = name.Split(new char[] { ':' });
100 SetupDomain(splitted[0]);
101 SetupProperties(splitted[1]);
103 else
105 SetupDomain(name);
109 /// <summary>
110 /// Sets up the domain. Can be empty but can't be null.
111 /// </summary>
112 /// <param name="domain"></param>
113 protected virtual void SetupDomain(String domain)
115 if (domain == null)
117 throw new ArgumentNullException("domain");
120 this.domain = domain;
123 /// <summary>
124 /// Parses and validate a properties list string like
125 /// "key=value,key2=value2" and so on.
126 /// </summary>
127 /// <param name="properties">Property list.</param>
128 protected virtual void SetupProperties(String properties)
130 if (properties == null)
132 throw new ArgumentNullException("properties");
134 if (properties.Equals("*"))
136 literalProperties = "*";
137 allProperties = true;
138 return;
141 String [] props = properties.Split( new char[] { ',' } );
143 Hashtable propsHash = new Hashtable(
144 CaseInsensitiveHashCodeProvider.Default,
145 CaseInsensitiveComparer.Default);
147 foreach(String chunk in props)
149 if (chunk.IndexOf('=') == -1)
151 throw new InvalidManagedObjectName("Invalid properties.");
154 String[] keyvalue = chunk.Split( new char[] { '=' } );
156 String key = keyvalue[0];
157 String value = keyvalue[1];
159 propsHash.Add(key, value);
162 SetupProperties(propsHash);
165 /// <summary>
166 /// Validates a properties Hashtable.
167 /// </summary>
168 /// <param name="properties">Property list.</param>
169 protected virtual void SetupProperties(Hashtable properties)
171 StringBuilder sb = new StringBuilder();
173 foreach(DictionaryEntry entry in properties)
175 if (sb.Length != 0)
177 sb.Append(",");
180 String key = null;
184 key = (String) entry.Key;
186 catch(InvalidCastException)
188 throw new InvalidManagedObjectName("Key is not a String.");
191 String value = null;
195 value = (String) entry.Value;
197 catch(InvalidCastException)
199 throw new InvalidManagedObjectName("Value is not a String.");
202 sb.AppendFormat("{0}={1}", key, value);
205 this.literalProperties = sb.ToString();
206 this.properties = new Hashtable(properties);
209 public String Domain
213 return domain;
217 public String LiteralProperties
221 return literalProperties;
225 public String this[ String key ]
229 if (key == null)
231 throw new ArgumentNullException("key");
234 return (String) this.properties[key];
238 public override bool Equals(object obj)
240 ManagedObjectName other = obj as ManagedObjectName;
242 if (other != null)
244 return other.domain.Equals(domain) &&
245 other.literalProperties.Equals(literalProperties);
248 return false;
251 public override int GetHashCode()
253 return domain.GetHashCode() ^ literalProperties.GetHashCode();
256 public override string ToString()
258 return
259 String.Format("Domain: {0} Properties: {1}",
260 domain, literalProperties);
263 #region ISerializable Members
265 public void GetObjectData(SerializationInfo info, StreamingContext context)
267 info.AddValue("domain", domain);
268 info.AddValue("props", literalProperties);
271 #endregion