1 18 19 package org.osgi.service.event; 20 21 import java.util.*; 22 23 import org.osgi.framework.Filter; 24 25 33 public class Event { 34 37 String topic; 38 42 Hashtable properties; 43 44 52 public Event(String topic, Dictionary properties) { 53 this.topic = topic; 54 validateTopicName(); 55 this.properties = new Hashtable(); 56 if (properties != null) { 57 for (Enumeration e = properties.keys(); e.hasMoreElements();) { 58 String key = (String ) e.nextElement(); 59 Object value = properties.get(key); 60 this.properties.put(key, value); 61 } 62 } 63 this.properties.put(EventConstants.EVENT_TOPIC, topic); 64 } 65 66 73 public final Object getProperty(String name) { 74 return properties.get(name); 75 } 76 77 82 public final String [] getPropertyNames() { 83 String [] names = new String [properties.size()]; 84 Enumeration keys = properties.keys(); 85 for (int i = 0; keys.hasMoreElements(); i++) { 86 names[i] = (String ) keys.nextElement(); 87 } 88 return names; 89 } 90 91 96 public final String getTopic() { 97 return topic; 98 } 99 100 108 public final boolean matches(Filter filter) { 109 return filter.matchCase(properties); 110 } 111 112 124 public boolean equals(Object object) { 125 if (object == this) { return true; 127 } 128 129 if (!(object instanceof Event)) { 130 return false; 131 } 132 133 Event event = (Event) object; 134 return topic.equals(event.topic) && properties.equals(event.properties); 135 } 136 137 142 public int hashCode() { 143 return topic.hashCode() ^ properties.hashCode(); 144 } 145 146 151 public String toString() { 152 return getClass().getName() + " [topic=" + topic + "]"; } 154 155 private static final String SEPARATOR = "/"; 157 162 private void validateTopicName() { 163 try { 164 StringTokenizer st = new StringTokenizer(topic, SEPARATOR, true); 165 validateToken(st.nextToken()); 166 167 while (st.hasMoreTokens()) { 168 st.nextToken(); validateToken(st.nextToken()); 170 } 171 } 172 catch (NoSuchElementException e) { 173 throw new IllegalArgumentException ("invalid topic"); } 175 } 176 177 private static final String tokenAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-"; 179 184 private void validateToken(String token) { 185 int length = token.length(); 186 if (length < 1) { throw new IllegalArgumentException ("invalid topic"); } 189 for (int i = 0; i < length; i++) { if (tokenAlphabet.indexOf(token.charAt(i)) == -1) { throw new IllegalArgumentException ("invalid topic"); } 193 } 194 } 195 } 196 | Popular Tags |