1 17 package org.apache.geronimo.gbean; 18 19 import java.io.Serializable ; 20 import java.util.Arrays ; 21 import java.util.Comparator ; 22 import java.util.HashMap ; 23 import java.util.Hashtable ; 24 import java.util.Iterator ; 25 import java.util.Map ; 26 import javax.management.MalformedObjectNameException ; 27 import javax.management.ObjectName ; 28 29 30 41 public final class GBeanName implements Serializable { 42 private static final long serialVersionUID = 8571821054715922993L; 43 44 47 private final String name; 48 private final transient String domain; 49 private final transient HashMap props; 50 private final transient int hashCode; 51 52 59 public GBeanName(String domain, Map props) { 60 if (domain == null) { 61 throw new IllegalArgumentException ("domain is null"); 62 } else if (props == null) { 63 throw new IllegalArgumentException ("props is null"); 64 } else if (props.isEmpty()) { 65 throw new IllegalArgumentException ("props is empty"); 66 } 67 this.domain = domain; 68 this.props = new HashMap (props); 69 this.name = buildName(domain, props); 70 this.hashCode = domain.hashCode() + 37 * props.hashCode(); 71 } 72 73 private static String buildName(String domain, Map props) { 74 StringBuffer buf = new StringBuffer (128); 75 buf.append(domain).append(':'); 76 Iterator i = props.entrySet().iterator(); 77 Map.Entry entry = (Map.Entry ) i.next(); 78 buf.append(entry.getKey()).append('=').append(entry.getValue()); 79 while (i.hasNext()) { 80 entry = (Map.Entry ) i.next(); 81 buf.append(',').append(entry.getKey()).append('=').append(entry.getValue()); 82 } 83 return buf.toString(); 84 } 85 86 91 public GBeanName(String name) { 92 int idx = name.indexOf(':'); 93 if (idx == -1) { 94 throw new IllegalArgumentException ("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 parseName(String name) { 103 if (name.endsWith(",")) { 104 throw new IllegalArgumentException ("Missing last property pair"); 105 } 106 HashMap props = new HashMap (); 107 String [] pairs = name.split(","); 108 for (int i = 0; i < pairs.length; i++) { 109 String pair = pairs[i]; 110 int idx = pair.indexOf('='); 111 if (idx == -1) { 112 throw new IllegalArgumentException ("Invalid property pair: " + pair); 113 } 114 String key = pair.substring(0, idx); 115 String value = pair.substring(idx + 1); 116 if (props.put(key, value) != null) { 117 throw new IllegalArgumentException ("Duplicate property: " + key); 118 } 119 } 120 return props; 121 } 122 123 138 public boolean matches(String domain, Map 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 i = pattern.entrySet().iterator(); i.hasNext();) { 146 Map.Entry entry = (Map.Entry ) i.next(); 147 String key = (String ) entry.getKey(); 148 String ourValue = (String ) props.get(key); 149 if (ourValue == null || !ourValue.equals(entry.getValue())) { 150 return false; 151 } 152 } 153 } 154 return true; 155 } 156 157 165 public boolean equals(Object 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 183 public String toString() { 184 return name; 185 } 186 187 195 public String toString(Comparator keySorter) { 196 String [] keyList = (String []) props.keySet().toArray(new String [props.keySet().size()]); 197 Arrays.sort(keyList, keySorter); 198 199 StringBuffer buf = new StringBuffer (128); 200 buf.append(domain).append(':'); 201 String 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 readResolve() { 211 return new GBeanName(name); 212 } 213 214 216 219 public ObjectName getObjectName() throws MalformedObjectNameException { 220 return new ObjectName (domain, new Hashtable (props)); 221 } 222 223 226 public GBeanName(ObjectName name) { 227 this.name = name.toString(); 228 this.domain = name.getDomain(); 229 this.props = new HashMap (name.getKeyPropertyList()); 230 this.hashCode = domain.hashCode() + 37 * props.hashCode(); 231 } 232 } | Popular Tags |