KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > gbean > GBeanName


1 /**
2  *
3  * Copyright 2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.geronimo.gbean;
18
19 import java.io.Serializable JavaDoc;
20 import java.util.Arrays JavaDoc;
21 import java.util.Comparator JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.Hashtable JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.Map JavaDoc;
26 import javax.management.MalformedObjectNameException JavaDoc;
27 import javax.management.ObjectName JavaDoc;
28
29
30 /**
31  * Class that represents the name for a GBean.
32  * A name is comprised of a domain combined with one or more properties.
33  * The domain is a fixed base name, properties qualify that as necessary.
34  * Two names are equal if they have the same name and the same properties.
35  * The String representation of a name can be written as domain:key1=value1,key2=value2,...
36  * Values are case sensitive, spaces are significant and there is no escaping mechanism;
37  * this is intended to be fast rather than allow lax syntax.
38  *
39  * @version $Rev$ $Date$
40  */

41 public final class GBeanName implements Serializable JavaDoc {
42     private static final long serialVersionUID = 8571821054715922993L;
43
44     /**
45      * Original name preserved; used for toString and Serialized form
46      */

47     private final String JavaDoc name;
48     private final transient String JavaDoc domain;
49     private final transient HashMap JavaDoc props;
50     private final transient int hashCode;
51
52     /**
53      * Construct a GBeanName by combining a domain with explicit properties.
54      * The string representation of this name is generated by combining the properties in iteration order.
55      *
56      * @param domain the domain
57      * @param props the properties used to qualify this name; a Map<String,String>
58      */

59     public GBeanName(String JavaDoc domain, Map JavaDoc props) {
60         if (domain == null) {
61             throw new IllegalArgumentException JavaDoc("domain is null");
62         } else if (props == null) {
63             throw new IllegalArgumentException JavaDoc("props is null");
64         } else if (props.isEmpty()) {
65             throw new IllegalArgumentException JavaDoc("props is empty");
66         }
67         this.domain = domain;
68         this.props = new HashMap JavaDoc(props);
69         this.name = buildName(domain, props);
70         this.hashCode = domain.hashCode() + 37 * props.hashCode();
71     }
72
73     private static String JavaDoc buildName(String JavaDoc domain, Map JavaDoc props) {
74         StringBuffer JavaDoc buf = new StringBuffer JavaDoc(128);
75         buf.append(domain).append(':');
76         Iterator JavaDoc i = props.entrySet().iterator();
77         Map.Entry JavaDoc entry = (Map.Entry JavaDoc) i.next();
78         buf.append(entry.getKey()).append('=').append(entry.getValue());
79         while (i.hasNext()) {
80             entry = (Map.Entry JavaDoc) i.next();
81             buf.append(',').append(entry.getKey()).append('=').append(entry.getValue());
82         }
83         return buf.toString();
84     }
85
86     /**
87      * Construct a GBeanName by parsing a string.
88      *
89      * @param name the name to parse
90      */

91     public GBeanName(String JavaDoc name) {
92         int idx = name.indexOf(':');
93         if (idx == -1) {
94             throw new IllegalArgumentException JavaDoc("Missing ':' for domain: " + name);
95         }
96         this.name = name;
97         this.domain = name.substring(0, idx);
98         this.props = parseName(name.substring(idx + 1));
99         this.hashCode = domain.hashCode() + 37 * props.hashCode();
100     }
101
102     private static HashMap JavaDoc parseName(String JavaDoc name) {
103         if (name.endsWith(",")) {
104             throw new IllegalArgumentException JavaDoc("Missing last property pair");
105         }
106         HashMap JavaDoc props = new HashMap JavaDoc();
107         String JavaDoc[] pairs = name.split(",");
108         for (int i = 0; i < pairs.length; i++) {
109             String JavaDoc pair = pairs[i];
110             int idx = pair.indexOf('=');
111             if (idx == -1) {
112                 throw new IllegalArgumentException JavaDoc("Invalid property pair: " + pair);
113             }
114             String JavaDoc key = pair.substring(0, idx);
115             String JavaDoc value = pair.substring(idx + 1);
116             if (props.put(key, value) != null) {
117                 throw new IllegalArgumentException JavaDoc("Duplicate property: " + key);
118             }
119         }
120         return props;
121     }
122
123     /**
124      * Determine if this name matches the supplied pattern.
125      * This performs a fast but simplistic pattern match which is true if:
126      * <ul>
127      * <li>The domains are equal</li>
128      * <li>If this instance has all the supplied properties with equal values</li>
129      * <ul>
130      * A null domain and a null or empty properties object are considered wildcards
131      * and always match; in other words GBeanName.match(null, new Properties()) will
132      * always evaluate to true.
133      *
134      * @param domain the domain to match
135      * @param pattern the set properties to match; a Map<String,String>
136      * @return true if this instance matches the pattern
137      */

138     public boolean matches(String JavaDoc domain, Map JavaDoc pattern) {
139         if (domain != null) {
140             if (!this.domain.equals(domain)) {
141                 return false;
142             }
143         }
144         if (pattern != null && !pattern.isEmpty()) {
145             for (Iterator JavaDoc i = pattern.entrySet().iterator(); i.hasNext();) {
146                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) i.next();
147                 String JavaDoc key = (String JavaDoc) entry.getKey();
148                 String JavaDoc ourValue = (String JavaDoc) props.get(key);
149                 if (ourValue == null || !ourValue.equals(entry.getValue())) {
150                     return false;
151                 }
152             }
153         }
154         return true;
155     }
156
157     /**
158      * Test for equality.
159      * This instance will be equal if the supplied object is a GBeanName with
160      * equal domain and properties.
161      *
162      * @param obj
163      * @return
164      */

165     public boolean equals(Object JavaDoc obj) {
166         if (obj == this) return true;
167         if (obj instanceof GBeanName == false) return false;
168         final GBeanName other = (GBeanName) obj;
169         return this.domain.equals(other.domain) && this.props.equals(other.props);
170     }
171
172     public int hashCode() {
173         return hashCode;
174     }
175
176     /**
177      * Return a human readable version of this GBeanName. If the instance was created
178      * by parsing a String, this will be the supplied value; it it was created by
179      * supplying properties, the name will contain properties in an unspecified order.
180      *
181      * @return a readable name
182      */

183     public String JavaDoc toString() {
184         return name;
185     }
186
187     /**
188      * Return a String representation of ths GBeanName.
189      * The format will be <domain> ':' <key> '=' <value> ( ',' <key> '=' <value> )*
190      * Keys are appended in the order determined by the supplied Comparator.
191      *
192      * @param keySorter the Comparator to use to order the keys.
193      * @return a String representation of this GBean
194      */

195     public String JavaDoc toString(Comparator JavaDoc keySorter) {
196         String JavaDoc[] keyList = (String JavaDoc[]) props.keySet().toArray(new String JavaDoc[props.keySet().size()]);
197         Arrays.sort(keyList, keySorter);
198
199         StringBuffer JavaDoc buf = new StringBuffer JavaDoc(128);
200         buf.append(domain).append(':');
201         String JavaDoc key = keyList[0];
202         buf.append(key).append('=').append(props.get(key));
203         for (int i = 1; i < keyList.length; i++) {
204             key = keyList[i];
205             buf.append(',').append(key).append('=').append(props.get(key));
206         }
207         return buf.toString();
208     }
209
210     private Object JavaDoc readResolve() {
211         return new GBeanName(name);
212     }
213
214     // utility methods to support conversion from ObjectName to GBeanName
215

216     /**
217      * @deprecated
218      */

219     public ObjectName JavaDoc getObjectName() throws MalformedObjectNameException JavaDoc {
220         return new ObjectName JavaDoc(domain, new Hashtable JavaDoc(props));
221     }
222
223     /**
224      * @deprecated
225      */

226     public GBeanName(ObjectName JavaDoc name) {
227         this.name = name.toString();
228         this.domain = name.getDomain();
229         this.props = new HashMap JavaDoc(name.getKeyPropertyList());
230         this.hashCode = domain.hashCode() + 37 * props.hashCode();
231     }
232 }
Popular Tags