KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > aop > metadata > SimpleMetaData


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.aop.metadata;
23
24 import java.io.IOException JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.HashSet JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import org.jboss.aop.joinpoint.Invocation;
29 import org.jboss.aop.util.MarshalledValue;
30 import org.jboss.aop.util.PayloadKey;
31
32 /**
33  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
34  * @version $Revision: 37406 $
35  */

36 public class SimpleMetaData implements MetaDataResolver, java.io.Externalizable JavaDoc
37 {
38    static final long serialVersionUID = -3873275588469743345L;
39    protected HashMap JavaDoc metaData = new HashMap JavaDoc();
40
41    public class MetaDataValue implements java.io.Serializable JavaDoc
42    {
43       static final long serialVersionUID = -8024138149680591337L;
44       public final PayloadKey type;
45       public Object JavaDoc value;
46
47       public MetaDataValue(PayloadKey type, Object JavaDoc value)
48       {
49          this.type = type;
50          this.value = value;
51       }
52
53       public Object JavaDoc get()
54       throws java.io.IOException JavaDoc, ClassNotFoundException JavaDoc
55       {
56          if (value instanceof MarshalledValue)
57          {
58             value = ((MarshalledValue) value).get();
59          }
60          return value;
61       }
62       
63       public String JavaDoc toString()
64       {
65          StringBuffer JavaDoc sb = new StringBuffer JavaDoc(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 JavaDoc tags()
81    {
82       return new HashSet JavaDoc(metaData.keySet());
83    }
84
85    public synchronized HashMap JavaDoc tag(String JavaDoc name)
86    {
87       HashMap JavaDoc map = (HashMap JavaDoc) metaData.get(name);
88       if (map == null) return null;
89       return (HashMap JavaDoc) map.clone();
90    }
91
92    public synchronized boolean hasTag(String JavaDoc name)
93    {
94       return metaData.get(name) != null;
95    }
96
97    /**
98     * Tag metadata to structure. Use for tags with no attributes
99     * i.e. @Singleton, etc...
100     *
101     * @param group
102     */

103    public void tag(Object JavaDoc tag)
104    {
105       addMetaData(tag, EMPTY_TAG, new Object JavaDoc(), PayloadKey.TRANSIENT);
106    }
107
108    public void addMetaData(Object JavaDoc tag, Object JavaDoc attr, Object JavaDoc value)
109    {
110       addMetaData(tag, attr, value, PayloadKey.MARSHALLED);
111    }
112
113    public synchronized void addMetaData(Object JavaDoc tag, Object JavaDoc attr, Object JavaDoc value, PayloadKey type)
114    {
115       HashMap JavaDoc groupData = (HashMap JavaDoc) metaData.get(tag);
116       if (groupData == null)
117       {
118          groupData = new HashMap JavaDoc();
119          metaData.put(tag, groupData);
120       }
121       MetaDataValue val = new MetaDataValue(type, value);
122       groupData.put(attr, val);
123    }
124
125    public synchronized Object JavaDoc getMetaData(Object JavaDoc tag, Object JavaDoc attr)
126    {
127       try
128       {
129          HashMap JavaDoc groupData = (HashMap JavaDoc) 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 JavaDoc ioex)
136       {
137          throw new RuntimeException JavaDoc("failed on MarshalledValue", ioex);
138       }
139       catch (ClassNotFoundException JavaDoc ex)
140       {
141          throw new RuntimeException JavaDoc("failed on MarshalledValue", ex);
142       }
143    }
144
145    public synchronized void removeMetaData(Object JavaDoc tag, Object JavaDoc attr)
146    {
147       HashMap JavaDoc groupData = (HashMap JavaDoc) metaData.get(tag);
148       if (groupData != null)
149       {
150          groupData.remove(attr);
151       }
152    }
153
154    public synchronized void removeGroupData(Object JavaDoc group)
155    {
156       metaData.remove(group);
157    }
158
159    public synchronized void clear()
160    {
161       metaData.clear();
162    }
163
164    /**
165     * merges incoming data. Incoming data overrides existing data
166     */

167    public synchronized void mergeIn(SimpleMetaData data)
168    {
169       Iterator JavaDoc it = data.metaData.keySet().iterator();
170       while (it.hasNext())
171       {
172          Object JavaDoc tag = it.next();
173          HashMap JavaDoc attrs = (HashMap JavaDoc) data.metaData.get(tag);
174          HashMap JavaDoc map = (HashMap JavaDoc) metaData.get(tag);
175          if (map == null)
176          {
177             map = new HashMap JavaDoc();
178             this.metaData.put(tag, map);
179          }
180          map.putAll(attrs);
181       }
182    }
183
184    public synchronized Object JavaDoc resolve(Invocation invocation, Object JavaDoc tag, Object JavaDoc 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 JavaDoc out)
195    throws IOException JavaDoc
196    {
197       //System.out.println("******** marshalling metadata");
198
Iterator JavaDoc it = metaData.keySet().iterator();
199       while (it.hasNext())
200       {
201          Object JavaDoc group = it.next();
202          HashMap JavaDoc map = (HashMap JavaDoc) metaData.get(group);
203          //System.out.println("******** marshalling group " + group + " size = " + map.size());
204
if (map != null && map.size() > 0)
205          {
206             boolean groupWritten = false;
207             Iterator JavaDoc attrs = map.keySet().iterator();
208             while (attrs.hasNext())
209             {
210                Object JavaDoc attr = attrs.next();
211                //System.out.println("******** marshalling attr: " + group + "." + attr);
212
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); // placeholder for end of attributes
230
}
231       }
232       out.writeObject(null); // place holder for end of marshall
233
}
234
235    public void readExternal(java.io.ObjectInput JavaDoc in)
236    throws IOException JavaDoc, ClassNotFoundException JavaDoc
237    {
238       //System.out.println("******** unmarshalling metadata");
239
metaData = new HashMap JavaDoc();
240       Object JavaDoc group;
241       while ((group = in.readObject()) != null)
242       {
243          //System.out.println("******** unmarshalling group: " + group);
244
HashMap JavaDoc map = new HashMap JavaDoc();
245          metaData.put(group, map);
246          Object JavaDoc attr;
247          while ((attr = in.readObject()) != null)
248          {
249             //System.out.println("******** unmarshalling attr: " + group + "." + attr);
250
Object JavaDoc 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 JavaDoc toString()
264    {
265       StringBuffer JavaDoc sb = new StringBuffer JavaDoc(100);
266       sb.append("[");
267       sb.append("metaData=").append(metaData);
268       sb.append("]");
269       return sb.toString();
270    }
271 }
272
Popular Tags