1 // Copyright 2003-2004 DigitalCraftsmen - http://www.digitalcraftsmen.com.br/
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
.ManagementExtensions
19 using System
.Collections
;
20 using System
.Runtime
.Serialization
;
23 /// Represents a ManagedObject's Name.
24 /// TODO: Supports query semantic.
27 public class ManagedObjectName
: ISerializable
29 protected String domain
;
30 protected String literalProperties
= String
.Empty
;
31 protected Hashtable properties
;
32 protected bool allProperties
;
35 /// Creates a ManagedObjectName using a name pattern like
36 /// "domain:key=value,key2=value2"
38 /// <param name="name">Complete name</param>
39 public ManagedObjectName(String name
)
45 /// Creates a ManagedObjectName with specified domain and
48 /// <param name="domain">Domain name</param>
49 /// <param name="properties">Property list.</param>
50 public ManagedObjectName(String domain
, String properties
)
53 SetupProperties(properties
);
57 /// Creates a ManagedObjectName with specified domain and
60 /// <param name="domain">Domain name</param>
61 /// <param name="properties">Property list.</param>
62 public ManagedObjectName(String domain
, Hashtable properties
)
65 SetupProperties(properties
);
69 /// Serialization constructor.
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");
79 if (props
!= String
.Empty
)
81 SetupProperties(props
);
86 /// Parses the full name extracting the domain and properties.
88 /// <param name="name">Full name.</param>
89 protected virtual void Setup(String name
)
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]);
110 /// Sets up the domain. Can be empty but can't be null.
112 /// <param name="domain"></param>
113 protected virtual void SetupDomain(String domain
)
117 throw new ArgumentNullException("domain");
120 this.domain
= domain
;
124 /// Parses and validate a properties list string like
125 /// "key=value,key2=value2" and so on.
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;
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
);
166 /// Validates a properties Hashtable.
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
)
184 key
= (String
) entry
.Key
;
186 catch(InvalidCastException
)
188 throw new InvalidManagedObjectName("Key is not a String.");
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
);
217 public String LiteralProperties
221 return literalProperties
;
225 public String
this[ String key
]
231 throw new ArgumentNullException("key");
234 return (String
) this.properties
[key
];
238 public override bool Equals(object obj
)
240 ManagedObjectName other
= obj
as ManagedObjectName
;
244 return other
.domain
.Equals(domain
) &&
245 other
.literalProperties
.Equals(literalProperties
);
251 public override int GetHashCode()
253 return domain
.GetHashCode() ^ literalProperties
.GetHashCode();
256 public override string ToString()
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
);