1 package org.jacorb.notification.util; 2 3 23 24 import java.util.ArrayList ; 25 import java.util.Collections ; 26 import java.util.HashMap ; 27 import java.util.HashSet ; 28 import java.util.Iterator ; 29 import java.util.List ; 30 import java.util.Map ; 31 import java.util.Set ; 32 33 import org.apache.avalon.framework.logger.Logger; 34 import org.omg.CORBA.Any ; 35 import org.omg.CORBA.ORB ; 36 import org.omg.CosNotification.Property; 37 import org.omg.CosNotification.PropertyError; 38 import org.omg.CosNotification.PropertyRange; 39 import org.omg.CosNotification.QoSError_code; 40 41 45 46 public abstract class PropertySet 47 { 48 protected static final ORB sORB = ORB.init(); 49 50 protected static final PropertyError[] PROPERTY_ERROR_ARRAY_TEMPLATE = new PropertyError[0]; 51 52 protected static final PropertyRange EMPTY_PROPERTY_RANGE = new PropertyRange( 53 sORB.create_any(), sORB.create_any()); 54 55 57 protected final Logger logger_ = LogUtil.getLogger(getClass().getName()); 58 59 private final Map listeners_ = new HashMap (); 60 61 private boolean modified_ = true; 62 63 private final Map properties_ = new HashMap (); 64 65 private Property[] arrayView_ = null; 66 67 69 public void addPropertySetListener(String [] props, PropertySetListener listener) 70 { 71 for (int x = 0; x < props.length; ++x) 72 { 73 addPropertySetListener(props[x], listener); 74 } 75 } 76 77 public void addPropertySetListener(String property, PropertySetListener listener) 78 { 79 List _list; 80 81 if (!listeners_.containsKey(property)) 82 { 83 _list = new ArrayList (); 84 listeners_.put(property, _list); 85 } 86 else 87 { 88 _list = (List ) listeners_.get(property); 89 } 90 91 _list.add(listener); 92 } 93 94 public synchronized Property[] toArray() 95 { 96 if (arrayView_ == null || modified_) 97 { 98 Property[] _props = new Property[properties_.size()]; 99 100 Iterator i = properties_.keySet().iterator(); 101 int x = 0; 102 while (i.hasNext()) 103 { 104 String _key = (String ) i.next(); 105 _props[x++] = new Property(_key, (Any ) properties_.get(_key)); 106 } 107 arrayView_ = _props; 108 modified_ = false; 109 } 110 111 return arrayView_; 112 } 113 114 public Map toMap() 115 { 116 return Collections.unmodifiableMap(properties_); 117 } 118 119 public String toString() 120 { 121 return properties_.toString(); 122 } 123 124 public boolean containsKey(String propertyName) 125 { 126 return properties_.containsKey(propertyName); 127 } 128 129 public Any get(String propertyName) 130 { 131 return (Any ) properties_.get(propertyName); 132 } 133 134 protected void set_properties(Property[] props) 135 { 136 HashSet _toBeNotified = new HashSet (); 137 138 for (int x = 0; x < props.length; ++x) 139 { 140 Any _oldValue = null; 141 142 if (properties_.containsKey(props[x].name)) 143 { 144 _oldValue = (Any ) properties_.get(props[x].name); 145 } 146 147 properties_.put(props[x].name, props[x].value); 148 149 if (listeners_.containsKey(props[x].name)) 150 { 151 if (!props[x].value.equals(_oldValue)) 152 { 153 _toBeNotified.addAll((List ) listeners_.get(props[x].name)); 154 } 155 } 156 } 157 158 synchronized (this) 159 { 160 modified_ = true; 161 } 162 163 Iterator i = _toBeNotified.iterator(); 164 while (i.hasNext()) 165 { 166 try 167 { 168 ((PropertySetListener) i.next()).actionPropertySetChanged(this); 169 } catch (Exception e) 170 { 171 logger_.error("exception in listener", e); 172 } 173 } 174 } 175 176 abstract Set getValidNames(); 177 178 protected void checkPropertyExistence(Property[] props, List errorList) 179 { 180 for (int x = 0; x < props.length; ++x) 181 { 182 if (!getValidNames().contains(props[x].name)) 183 { 184 errorList.add(badProperty(props[x].name)); 185 } 186 } 187 } 188 189 protected PropertyError badProperty(String name) 190 { 191 return new PropertyError(QoSError_code.BAD_PROPERTY, name, EMPTY_PROPERTY_RANGE); 192 } 193 194 protected PropertyError badType(String name) 195 { 196 return new PropertyError(QoSError_code.BAD_TYPE, name, EMPTY_PROPERTY_RANGE); 197 } 198 199 201 public static Property[] map2Props(Map props) 202 { 203 Property[] _ps = new Property[props.size()]; 204 205 Iterator i = props.keySet().iterator(); 206 int x = 0; 207 while (i.hasNext()) 208 { 209 String _key = (String ) i.next(); 210 _ps[x++] = new Property(_key, (Any ) props.get(_key)); 211 } 212 return _ps; 213 } 214 } | Popular Tags |