1 22 package org.jboss.aop.metadata; 23 24 import java.io.IOException ; 25 import java.util.HashMap ; 26 import java.util.HashSet ; 27 import java.util.Iterator ; 28 import org.jboss.aop.joinpoint.Invocation; 29 import org.jboss.aop.util.MarshalledValue; 30 import org.jboss.aop.util.PayloadKey; 31 32 36 public class SimpleMetaData implements MetaDataResolver, java.io.Externalizable 37 { 38 static final long serialVersionUID = -3873275588469743345L; 39 protected HashMap metaData = new HashMap (); 40 41 public class MetaDataValue implements java.io.Serializable 42 { 43 static final long serialVersionUID = -8024138149680591337L; 44 public final PayloadKey type; 45 public Object value; 46 47 public MetaDataValue(PayloadKey type, Object value) 48 { 49 this.type = type; 50 this.value = value; 51 } 52 53 public Object get() 54 throws java.io.IOException , ClassNotFoundException 55 { 56 if (value instanceof MarshalledValue) 57 { 58 value = ((MarshalledValue) value).get(); 59 } 60 return value; 61 } 62 63 public String toString() 64 { 65 StringBuffer sb = new StringBuffer (100); 66 sb.append("["); 67 sb.append("type=").append(type); 68 sb.append("value=").append(value); 69 sb.append("]"); 70 return sb.toString(); 71 } 72 73 } 74 75 public synchronized int size() 76 { 77 return metaData.size(); 78 } 79 80 public synchronized HashSet tags() 81 { 82 return new HashSet (metaData.keySet()); 83 } 84 85 public synchronized HashMap tag(String name) 86 { 87 HashMap map = (HashMap ) metaData.get(name); 88 if (map == null) return null; 89 return (HashMap ) map.clone(); 90 } 91 92 public synchronized boolean hasTag(String name) 93 { 94 return metaData.get(name) != null; 95 } 96 97 103 public void tag(Object tag) 104 { 105 addMetaData(tag, EMPTY_TAG, new Object (), PayloadKey.TRANSIENT); 106 } 107 108 public void addMetaData(Object tag, Object attr, Object value) 109 { 110 addMetaData(tag, attr, value, PayloadKey.MARSHALLED); 111 } 112 113 public synchronized void addMetaData(Object tag, Object attr, Object value, PayloadKey type) 114 { 115 HashMap groupData = (HashMap ) metaData.get(tag); 116 if (groupData == null) 117 { 118 groupData = new HashMap (); 119 metaData.put(tag, groupData); 120 } 121 MetaDataValue val = new MetaDataValue(type, value); 122 groupData.put(attr, val); 123 } 124 125 public synchronized Object getMetaData(Object tag, Object attr) 126 { 127 try 128 { 129 HashMap groupData = (HashMap ) metaData.get(tag); 130 if (groupData == null) return null; 131 MetaDataValue val = (MetaDataValue) groupData.get(attr); 132 if (val == null) return null; 133 return val.get(); 134 } 135 catch (IOException ioex) 136 { 137 throw new RuntimeException ("failed on MarshalledValue", ioex); 138 } 139 catch (ClassNotFoundException ex) 140 { 141 throw new RuntimeException ("failed on MarshalledValue", ex); 142 } 143 } 144 145 public synchronized void removeMetaData(Object tag, Object attr) 146 { 147 HashMap groupData = (HashMap ) metaData.get(tag); 148 if (groupData != null) 149 { 150 groupData.remove(attr); 151 } 152 } 153 154 public synchronized void removeGroupData(Object group) 155 { 156 metaData.remove(group); 157 } 158 159 public synchronized void clear() 160 { 161 metaData.clear(); 162 } 163 164 167 public synchronized void mergeIn(SimpleMetaData data) 168 { 169 Iterator it = data.metaData.keySet().iterator(); 170 while (it.hasNext()) 171 { 172 Object tag = it.next(); 173 HashMap attrs = (HashMap ) data.metaData.get(tag); 174 HashMap map = (HashMap ) metaData.get(tag); 175 if (map == null) 176 { 177 map = new HashMap (); 178 this.metaData.put(tag, map); 179 } 180 map.putAll(attrs); 181 } 182 } 183 184 public synchronized Object resolve(Invocation invocation, Object tag, Object attr) 185 { 186 return getMetaData(tag, attr); 187 } 188 189 public SimpleMetaData getAllMetaData(Invocation invocation) 190 { 191 return this; 192 } 193 194 public void writeExternal(java.io.ObjectOutput out) 195 throws IOException 196 { 197 Iterator it = metaData.keySet().iterator(); 199 while (it.hasNext()) 200 { 201 Object group = it.next(); 202 HashMap map = (HashMap ) metaData.get(group); 203 if (map != null && map.size() > 0) 205 { 206 boolean groupWritten = false; 207 Iterator attrs = map.keySet().iterator(); 208 while (attrs.hasNext()) 209 { 210 Object attr = attrs.next(); 211 MetaDataValue value = (MetaDataValue) map.get(attr); 213 if (value.type == PayloadKey.TRANSIENT) continue; 214 if (!groupWritten) 215 { 216 groupWritten = true; 217 out.writeObject(group); 218 } 219 out.writeObject(attr); 220 if (value.type == PayloadKey.AS_IS) 221 { 222 out.writeObject(value.value); 223 } 224 else 225 { 226 out.writeObject(new MarshalledValue(value.value)); 227 } 228 } 229 if (groupWritten) out.writeObject(null); } 231 } 232 out.writeObject(null); } 234 235 public void readExternal(java.io.ObjectInput in) 236 throws IOException , ClassNotFoundException 237 { 238 metaData = new HashMap (); 240 Object group; 241 while ((group = in.readObject()) != null) 242 { 243 HashMap map = new HashMap (); 245 metaData.put(group, map); 246 Object attr; 247 while ((attr = in.readObject()) != null) 248 { 249 Object obj = in.readObject(); 251 if (obj instanceof MarshalledValue) 252 { 253 map.put(attr, new MetaDataValue(PayloadKey.MARSHALLED, obj)); 254 } 255 else 256 { 257 map.put(attr, new MetaDataValue(PayloadKey.AS_IS, obj)); 258 } 259 } 260 } 261 } 262 263 public String toString() 264 { 265 StringBuffer sb = new StringBuffer (100); 266 sb.append("["); 267 sb.append("metaData=").append(metaData); 268 sb.append("]"); 269 return sb.toString(); 270 } 271 } 272 | Popular Tags |