Added container accessor to Castle.Core
[castle.git] / InversionOfControl / Castle.MicroKernel / SubSystems / Naming / ComponentName.cs
bloba42c1fb862c7d222f97081d0674237da54eade42
1 // Copyright 2004-2007 Castle Project - http://www.castleproject.org/
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.MicroKernel.SubSystems.Naming
17 using System;
18 using System.Text;
19 using System.Collections;
20 using System.Collections.Specialized;
21 using System.Runtime.Serialization;
23 [Serializable]
24 public class ComponentName : ISerializable
26 protected String internalService;
27 protected String internalliteralProperties = String.Empty;
28 protected HybridDictionary internalproperties;
29 protected bool allProperties;
31 /// <summary>
32 /// Creates a ComponentName using a name pattern like
33 /// "service:key=value,key2=value2"
34 /// </summary>
35 /// <param name="name">Complete name</param>
36 public ComponentName(String name)
38 Setup(name);
41 /// <summary>
42 /// Creates a ComponentName with specified service and
43 /// properties.
44 /// </summary>
45 /// <param name="service">Service name</param>
46 /// <param name="properties">Property list.</param>
47 public ComponentName(String service, String properties)
49 SetupService(service);
50 SetupProperties(properties);
53 internal IDictionary Properties
55 get { return internalproperties; }
58 /// <summary>
59 /// Serialization constructor.
60 /// </summary>
61 /// <param name="info"></param>
62 /// <param name="context"></param>
63 public ComponentName(SerializationInfo info, StreamingContext context)
65 String service = info.GetString("service");
66 String props = info.GetString("props");
67 SetupService(service);
69 if (props != String.Empty)
71 SetupProperties(props);
75 /// <summary>
76 /// Parses the full name extracting the service and properties.
77 /// </summary>
78 /// <param name="name">Full name.</param>
79 protected virtual void Setup(String name)
81 if (name == null)
83 throw new ArgumentNullException("name");
86 if (name.IndexOf(':') != -1)
88 String[] splitted = name.Split(new char[] {':'});
90 SetupService(splitted[0]);
91 SetupProperties(splitted[1]);
93 else
95 SetupService(name);
96 SetupProperties(String.Empty);
100 /// <summary>
101 /// Sets up the service. Can be empty but can't be null.
102 /// </summary>
103 /// <param name="service"></param>
104 protected virtual void SetupService(String service)
106 if (service == null)
108 throw new ArgumentNullException("service");
111 internalService = service;
114 /// <summary>
115 /// Parses and validate a properties list string like
116 /// "key=value,key2=value2" and so on.
117 /// </summary>
118 /// <param name="properties">Property list.</param>
119 protected virtual void SetupProperties(String properties)
121 if (properties == null)
123 throw new ArgumentNullException("properties");
125 if (properties.Equals("*"))
127 internalliteralProperties = "*";
128 allProperties = true;
129 return;
131 if (properties == String.Empty)
133 internalliteralProperties = "";
134 SetupProperties(new HybridDictionary(true));
135 return;
138 String[] props = properties.Split(new char[] {','});
140 HybridDictionary propsHash = new HybridDictionary(true);
142 foreach (String chunk in props)
144 if (chunk.IndexOf('=') == -1)
146 throw new ArgumentException("Invalid properties.");
149 String[] keyvalue = chunk.Split(new char[] {'='});
151 String key = keyvalue[0];
152 String value = keyvalue[1];
154 propsHash.Add(key, value);
157 SetupProperties(propsHash);
160 /// <summary>
161 /// Validates a properties Hashtable.
162 /// </summary>
163 /// <param name="properties">Property list.</param>
164 protected virtual void SetupProperties(IDictionary properties)
166 internalproperties = new HybridDictionary(true);
168 StringBuilder sb = new StringBuilder();
170 foreach (DictionaryEntry entry in properties)
172 if (sb.Length != 0)
174 sb.Append(",");
177 String key = null;
181 key = (String) entry.Key;
183 catch (InvalidCastException)
185 throw new ApplicationException("Key is not a String.");
188 String value = null;
192 value = (String) entry.Value;
194 catch (InvalidCastException)
196 throw new ApplicationException("Value is not a String.");
199 sb.AppendFormat("{0}={1}", key, value);
201 Properties[key] = value;
204 internalliteralProperties = sb.ToString();
207 public String Service
209 get { return internalService; }
212 public String LiteralProperties
214 get { return internalliteralProperties; }
217 public String this[String key]
221 if (key == null)
223 throw new ArgumentNullException("key");
226 return (String) internalproperties[key];
230 public override bool Equals(object obj)
232 ComponentName other = obj as ComponentName;
234 if (other != null)
236 return other.internalService.Equals(internalService) &&
237 other.internalliteralProperties.Equals(internalliteralProperties);
240 return false;
243 public override int GetHashCode()
245 return internalService.GetHashCode() ^ internalliteralProperties.GetHashCode();
248 public override string ToString()
250 return
251 String.Format("Service: {0} Properties: {1}",
252 internalService, internalliteralProperties);
255 #region ISerializable Members
257 public void GetObjectData(SerializationInfo info, StreamingContext context)
259 info.AddValue("service", internalService);
260 info.AddValue("props", internalliteralProperties);
263 #endregion