KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.jboss.aop.joinpoint.Invocation;
25 import org.jboss.aop.util.PayloadKey;
26
27
28 /**
29  * Metadata can be associated with a Thread of execution.
30  * This class manages this thread metadata. It is a singleton
31  * instance wrapped by a ThreadLocal. Invocations usually
32  * have this repository as part of their MetaDataResolver chain
33  *
34  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
35  * @version $Revision: 37406 $
36  *
37  */

38 public class ThreadMetaData implements MetaDataResolver
39 {
40    ThreadLocal JavaDoc metaData = new ThreadLocal JavaDoc();
41
42    public void addMetaData(Object JavaDoc tag, Object JavaDoc attr, Object JavaDoc value)
43    {
44       addMetaData(tag, attr, value, PayloadKey.MARSHALLED);
45    }
46
47    public void addMetaData(Object JavaDoc tag, Object JavaDoc attr, Object JavaDoc value, PayloadKey type)
48    {
49       SimpleMetaData data = (SimpleMetaData)metaData.get();
50       if (data == null)
51       {
52          data = new SimpleMetaData();
53          metaData.set(data);
54       }
55       data.addMetaData(tag, attr, value, type);
56    }
57
58    public Object JavaDoc getMetaData(Object JavaDoc tag, Object JavaDoc attr)
59    {
60       SimpleMetaData data = (SimpleMetaData)metaData.get();
61       if (data == null) return null;
62       return data.getMetaData(tag, attr);
63    }
64
65    public synchronized void removeMetaData(Object JavaDoc tag, Object JavaDoc attr)
66    {
67       SimpleMetaData data = (SimpleMetaData)metaData.get();
68       if (data == null) return;
69       data.removeMetaData(tag, attr);
70    }
71
72    public synchronized void removeGroupData(Object JavaDoc tag)
73    {
74       SimpleMetaData data = (SimpleMetaData)metaData.get();
75       if (data == null) return;
76       data.removeGroupData(tag);
77    }
78
79    public Object JavaDoc resolve(Invocation invocation, Object JavaDoc tag, Object JavaDoc attr)
80    {
81       return getMetaData(tag, attr);
82    }
83
84    public SimpleMetaData getAllMetaData(Invocation invocation)
85    {
86       return (SimpleMetaData)metaData.get();
87    }
88    public void clear()
89    {
90       metaData.set(null);
91    }
92
93
94    private static final ThreadMetaData me = new ThreadMetaData();
95    public static ThreadMetaData instance() { return me; }
96 }
97
Popular Tags