KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > invocation > MarshalledValueInputStream


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.invocation;
23
24 import java.io.InputStream JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.ObjectInputStream JavaDoc;
27 import java.io.ObjectStreamClass JavaDoc;
28 import java.lang.reflect.Proxy JavaDoc;
29 import org.jboss.util.collection.WeakValueHashMap;
30
31 import org.jboss.logging.Logger;
32
33 /**
34  * An ObjectInputStream subclass used by the MarshalledValue class to
35  * ensure the classes and proxies are loaded using the thread context
36  * class loader.
37  *
38  * @author Scott.Stark@jboss.org
39  * @version $Revision: 37459 $
40  */

41 public class MarshalledValueInputStream
42    extends ObjectInputStream JavaDoc
43 {
44    private static Logger log = Logger.getLogger(MarshalledValueInputStream.class);
45    /** A class wide cache of proxy classes populated by resolveProxyClass */
46    private static WeakValueHashMap classCache;
47
48    /** Enable local caching of resolved proxy classes. This can only be used
49     * if there is a single ULR and no redeployment of the proxy classes.
50     *
51     * @param flag true to enable caching, false to disable it
52     */

53    public static void useClassCache(boolean flag)
54    {
55       if( flag == true )
56          classCache = new WeakValueHashMap();
57       else
58          classCache = null;
59    }
60
61    /** Clear the current proxy cache.
62     *
63     */

64    public static void flushClassCache()
65    {
66       classCache.clear();
67    }
68
69    /**
70     * Creates a new instance of MarshalledValueOutputStream
71     */

72    public MarshalledValueInputStream(InputStream JavaDoc is) throws IOException JavaDoc
73    {
74       super(is);
75    }
76
77    /**
78     * Use the thread context class loader to resolve the class
79     *
80     * @throws IOException Any exception thrown by the underlying OutputStream.
81     */

82    protected Class JavaDoc resolveClass(ObjectStreamClass JavaDoc v)
83       throws IOException JavaDoc, ClassNotFoundException JavaDoc
84    {
85       String JavaDoc className = v.getName();
86       Class JavaDoc resolvedClass = null;
87       // Check the class cache first if it exists
88
if( classCache != null )
89       {
90          synchronized( classCache )
91          {
92             resolvedClass = (Class JavaDoc) classCache.get(className);
93          }
94       }
95
96       if( resolvedClass == null )
97       {
98          ClassLoader JavaDoc loader = SecurityActions.getContextClassLoader();
99          try
100          {
101             resolvedClass = loader.loadClass(className);
102          }
103          catch(ClassNotFoundException JavaDoc e)
104          {
105             /* Use the super.resolveClass() call which will resolve array
106             classes and primitives. We do not use this by default as this can
107             result in caching of stale values across redeployments.
108             */

109             resolvedClass = super.resolveClass(v);
110          }
111          if( classCache != null )
112          {
113             synchronized( classCache )
114             {
115                classCache.put(className, resolvedClass);
116             }
117          }
118       }
119       return resolvedClass;
120    }
121
122    protected Class JavaDoc resolveProxyClass(String JavaDoc[] interfaces)
123       throws IOException JavaDoc, ClassNotFoundException JavaDoc
124    {
125       if( log.isTraceEnabled() )
126       {
127          StringBuffer JavaDoc tmp = new StringBuffer JavaDoc("[");
128          for(int i = 0; i < interfaces.length; i ++)
129          {
130             if( i > 0 )
131                tmp.append(',');
132             tmp.append(interfaces[i]);
133          }
134          tmp.append(']');
135          log.trace("resolveProxyClass called, ifaces="+tmp.toString());
136       }
137
138       // Load the interfaces from the cache or thread context class loader
139
ClassLoader JavaDoc loader = null;
140       Class JavaDoc[] ifaceClasses = new Class JavaDoc[interfaces.length];
141       for (int i = 0; i < interfaces.length; i++)
142       {
143          Class JavaDoc iface = null;
144          String JavaDoc className = interfaces[i];
145          // Check the proxy cache if it exists
146
if( classCache != null )
147          {
148             synchronized( classCache )
149             {
150                iface = (Class JavaDoc) classCache.get(className);
151             }
152          }
153
154          // Load the interface class using the thread context ClassLoader
155
if( iface == null )
156          {
157             if( loader == null )
158                loader = Thread.currentThread().getContextClassLoader();
159             iface = loader.loadClass(className);
160             if( classCache != null )
161             {
162                synchronized( classCache )
163                {
164                   classCache.put(className, iface);
165                }
166             }
167          }
168          ifaceClasses[i] = iface;
169       }
170
171       return Proxy.getProxyClass(loader, ifaceClasses);
172    }
173 }
174
Popular Tags