KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > interceptor > ClientInterceptorUtil


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.ejb3.interceptor;
23
24 import java.io.IOException JavaDoc;
25 import java.util.Collection JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.Set JavaDoc;
29
30 import org.jboss.aop.joinpoint.Invocation;
31 import org.jboss.aop.metadata.SimpleMetaData;
32 import org.jboss.aop.util.PayloadKey;
33
34 /**
35  * Utility class for placing the client interceptor data under a given metadata tag
36  *
37  * @author <a HREF="kabir.khan@jboss.com">Kabir Khan</a>
38  * @version $Revision: 43261 $
39  */

40 public class ClientInterceptorUtil
41 {
42    public static final String JavaDoc CLIENT_METADATA = "jboss.ejb3.client.invocation.metadata";
43    
44    public static void addMetadata(Invocation invocation, Object JavaDoc key, Object JavaDoc value, PayloadKey payload)
45    {
46       invocation.getMetaData().addMetaData(CLIENT_METADATA, key, value, payload);
47    }
48    
49    public static void addMetadata(Invocation invocation, Object JavaDoc key, Object JavaDoc value)
50    {
51       invocation.getMetaData().addMetaData(CLIENT_METADATA, key, value);
52    }
53    
54    public static Object JavaDoc getMetadata(Invocation invocation, Object JavaDoc key)
55    {
56       return invocation.getMetaData().getMetaData(CLIENT_METADATA, key);
57    }
58    
59    static Map JavaDoc getClientMetadataMap(Invocation invocation)
60    {
61       Map JavaDoc map = invocation.getMetaData().tag(CLIENT_METADATA);
62       if (map != null)
63       {
64          return new ClientValueMap(map);
65       }
66       return null;
67    }
68
69    /**
70     * Wraps map containing data set by client interceptorss and lazily unmarshals it
71     */

72          
73    private static class ClientValueMap implements Map JavaDoc
74    {
75       Map JavaDoc marshalledMap;
76       boolean haveUnmarshalledAllEntries;
77
78       private ClientValueMap(Map JavaDoc marshalledMap)
79       {
80          this.marshalledMap = marshalledMap;
81       }
82       
83       public void clear()
84       {
85          marshalledMap.clear();
86       }
87
88       public boolean containsKey(Object JavaDoc key)
89       {
90          return marshalledMap.containsKey(key);
91       }
92
93       public boolean containsValue(Object JavaDoc value)
94       {
95          unmarshallAllEntries();
96          return marshalledMap.containsValue(value);
97       }
98
99       public Set JavaDoc entrySet()
100       {
101          unmarshallAllEntries();
102          return marshalledMap.entrySet();
103       }
104
105       public boolean equals(Object JavaDoc o)
106       {
107          return marshalledMap.equals(o);
108       }
109
110       public Object JavaDoc get(Object JavaDoc key)
111       {
112          return unmarshallEntry(key);
113       }
114
115       public int hashCode()
116       {
117          return marshalledMap.hashCode();
118       }
119
120       public boolean isEmpty()
121       {
122          return marshalledMap.isEmpty();
123       }
124
125       public Set JavaDoc keySet()
126       {
127          return marshalledMap.keySet();
128       }
129
130       public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value)
131       {
132          return marshalledMap.put(key, value);
133       }
134
135       public void putAll(Map JavaDoc t)
136       {
137          marshalledMap.putAll(t);
138       }
139
140       public Object JavaDoc remove(Object JavaDoc key)
141       {
142          return marshalledMap.remove(key);
143       }
144
145       public int size()
146       {
147          return marshalledMap.size();
148       }
149
150       public Collection JavaDoc values()
151       {
152          unmarshallAllEntries();
153          return marshalledMap.values();
154       }
155       
156       private void unmarshallAllEntries()
157       {
158          if (haveUnmarshalledAllEntries)
159          {
160             return;
161          }
162          
163          Iterator JavaDoc keys = marshalledMap.keySet().iterator();
164          while (keys.hasNext())
165          {
166             //Make sure that each entry gets unmarshalled
167
unmarshallEntry(keys.next());
168          }
169          haveUnmarshalledAllEntries = true;
170       }
171       
172       private Object JavaDoc unmarshallEntry(Object JavaDoc key)
173       {
174          try
175          {
176             Object JavaDoc obj = marshalledMap.get(key);
177             if (obj instanceof SimpleMetaData.MetaDataValue)
178             {
179                Object JavaDoc realObj = ((SimpleMetaData.MetaDataValue)obj).get();
180                marshalledMap.put(key, realObj);
181                return realObj;
182             }
183             return obj;
184          }
185          catch (IOException JavaDoc e)
186          {
187             throw new RuntimeException JavaDoc(e);
188          }
189          catch (ClassNotFoundException JavaDoc e)
190          {
191             throw new RuntimeException JavaDoc(e);
192          }
193       }
194    }
195 }
196
Popular Tags