KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > iiop > WebCL


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.iiop;
23
24 import java.util.Collections JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.WeakHashMap JavaDoc;
27 import javax.management.ObjectName JavaDoc;
28
29 import org.jboss.logging.Logger;
30 import org.jboss.mx.loading.RepositoryClassLoader;
31 import org.jboss.proxy.compiler.IIOPStubCompiler;
32 import org.jboss.web.WebClassLoader;
33
34 /**
35  * A subclass of WebClassLoader that does IIOP bytecode generation on the fly.
36  *
37  * @author <a HREF="mailto:reverbel@ime.usp.br">Francisco Reverbel</a>
38  * @version $Revision: 37459 $
39 */

40 public class WebCL extends WebClassLoader
41 {
42     /** Logger for trace messages */
43     static Logger logger = Logger.getLogger(WebCL.class);
44
45     /** Map from stub classes into bytecode arrays (stub bytecode cache) */
46     private Map JavaDoc loadedStubMap = Collections.synchronizedMap(new WeakHashMap JavaDoc());
47
48     /** Creates new WebCL */
49     public WebCL(ObjectName JavaDoc container, RepositoryClassLoader parent)
50     {
51         super(container, parent);
52         logger.debug("Constructed WebCL " + this.toString());
53         logger.debug(" parent " + parent.toString());
54
55         // Turn standard loading back on (we do classloading)
56
standard = true;
57     }
58
59     /** Gets a string key used as the key into the WebServer's loaderMap. */
60     public String JavaDoc getKey()
61     {
62         String JavaDoc className = getClass().getName();
63         int dot = className.lastIndexOf('.');
64         if( dot >= 0 )
65             className = className.substring(dot+1);
66         String JavaDoc jndiName = getContainer().getKeyProperty("jndiName");
67         String JavaDoc key = className + '[' + jndiName + ']';
68         return key;
69     }
70
71     /** Gets the bytecodes for a given stub class. */
72     public byte[] getBytes(Class JavaDoc clz) {
73         byte[] code = (byte[])loadedStubMap.get(clz);
74         return (code == null) ? null : (byte[])code.clone();
75     }
76    
77     protected Class JavaDoc findClass(String JavaDoc name)
78         throws ClassNotFoundException JavaDoc
79     {
80         if (logger.isTraceEnabled()) {
81             logger.trace("findClass(" + name + ") called");
82         }
83         if (name.endsWith("_Stub")) {
84             int start = name.lastIndexOf('.') + 1;
85             if (name.charAt(start) == '_') {
86                 String JavaDoc pkg = name.substring(0, start);
87                 String JavaDoc interfaceName = pkg + name.substring(start + 1,
88                                                             name.length() - 5);
89
90                 // This is a workaround for a problem in the RMI/IIOP
91
// stub loading code in SUN JDK 1.4.x, which prepends
92
// "org.omg.stub." to classes in certain name spaces,
93
// such as "com.sun". This non-compliant behavior
94
// results in failures when deploying SUN example code,
95
// including ECPerf and PetStore, so we remove the prefix.
96
if (interfaceName.startsWith("org.omg.stub.com.sun."))
97                     interfaceName = interfaceName.substring(13);
98
99                 Class JavaDoc intf = super.loadClass(interfaceName);
100                 if (logger.isTraceEnabled()) {
101                     logger.trace("loaded class " + interfaceName);
102                 }
103                 
104                 try {
105                     byte[] code =
106                         IIOPStubCompiler.compile(intf, name);
107                
108                     if (logger.isTraceEnabled()) {
109                         logger.trace("compiled stub class for "
110                                      + interfaceName);
111                     }
112                     Class JavaDoc clz = defineClass(name, code, 0, code.length);
113                     if (logger.isTraceEnabled()) {
114                         logger.trace("defined stub class for "
115                                      + interfaceName);
116                     }
117                     resolveClass(clz);
118                     try {
119                         clz.newInstance();
120                     }
121                     catch (Throwable JavaDoc t) {
122                         //t.printStackTrace();
123
throw new org.jboss.util.NestedRuntimeException(t);
124                     }
125                     if (logger.isTraceEnabled()) {
126                         logger.trace("resolved stub class for "
127                                      + interfaceName);
128                     }
129                     loadedStubMap.put(clz, code);
130                     return clz;
131                 }
132                 catch (RuntimeException JavaDoc e) {
133                     logger.error("failed finding class " + name, e);
134                     //throw e;
135
return super.findClass(name);
136                 }
137             }
138             else {
139                 return super.findClass(name);
140             }
141         }
142         else {
143             return super.findClass(name);
144         }
145     }
146
147 }
148
Popular Tags