1 // Copyright 2004-2007 Castle Project - http://www.castleproject.org/
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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
19 using System
.Collections
;
20 using System
.Collections
.Specialized
;
21 using System
.Runtime
.Serialization
;
24 public class ComponentName
: ISerializable
26 protected String internalService
;
27 protected String internalliteralProperties
= String
.Empty
;
28 protected HybridDictionary internalproperties
;
29 protected bool allProperties
;
32 /// Creates a ComponentName using a name pattern like
33 /// "service:key=value,key2=value2"
35 /// <param name="name">Complete name</param>
36 public ComponentName(String name
)
42 /// Creates a ComponentName with specified service and
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; }
59 /// Serialization constructor.
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
);
76 /// Parses the full name extracting the service and properties.
78 /// <param name="name">Full name.</param>
79 protected virtual void Setup(String name
)
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]);
96 SetupProperties(String
.Empty
);
101 /// Sets up the service. Can be empty but can't be null.
103 /// <param name="service"></param>
104 protected virtual void SetupService(String service
)
108 throw new ArgumentNullException("service");
111 internalService
= service
;
115 /// Parses and validate a properties list string like
116 /// "key=value,key2=value2" and so on.
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;
131 if (properties
== String
.Empty
)
133 internalliteralProperties
= "";
134 SetupProperties(new HybridDictionary(true));
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
);
161 /// Validates a properties Hashtable.
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
)
181 key
= (String
) entry
.Key
;
183 catch (InvalidCastException
)
185 throw new ApplicationException("Key is not a String.");
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
]
223 throw new ArgumentNullException("key");
226 return (String
) internalproperties
[key
];
230 public override bool Equals(object obj
)
232 ComponentName other
= obj
as ComponentName
;
236 return other
.internalService
.Equals(internalService
) &&
237 other
.internalliteralProperties
.Equals(internalliteralProperties
);
243 public override int GetHashCode()
245 return internalService
.GetHashCode() ^ internalliteralProperties
.GetHashCode();
248 public override string ToString()
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
);