KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > iiop > IiopStubLoader


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.iiop;
30
31 import com.caucho.config.ConfigException;
32 import com.caucho.ejb.AbstractStubLoader;
33 import com.caucho.loader.DynamicClassLoader;
34 import com.caucho.log.Log;
35 import com.caucho.server.util.CauchoSystem;
36 import com.caucho.util.CharBuffer;
37 import com.caucho.vfs.Path;
38
39 import java.net.URL JavaDoc;
40 import java.security.CodeSource JavaDoc;
41 import java.security.cert.Certificate JavaDoc;
42 import java.util.HashSet JavaDoc;
43 import java.util.logging.Level JavaDoc;
44 import java.util.logging.Logger JavaDoc;
45
46 /**
47  * Class loader which checks for changes in class files and automatically
48  * picks up new jars.
49  */

50 public class IiopStubLoader extends AbstractStubLoader {
51   private static final Logger JavaDoc log = Log.open(IiopStubLoader.class);
52
53   private HashSet JavaDoc<String JavaDoc> _stubClassNames = new HashSet JavaDoc<String JavaDoc>();
54
55   private CodeSource JavaDoc _codeSource;
56
57   /**
58    * Null constructor for the simple loader.
59    */

60   public IiopStubLoader()
61   {
62   }
63
64   /**
65    * Creates the simple loader with the specified path.
66    *
67    * @param path specifying the root of the resources
68    */

69   public IiopStubLoader(Path path)
70   {
71     setPath(path);
72   }
73
74   /**
75    * Sets the owning class loader.
76    */

77   public void setLoader(DynamicClassLoader loader)
78   {
79     super.setLoader(loader);
80
81     loader.addURL(getPath());
82   }
83
84   /**
85    * Initializes the loader.
86    */

87   public void init()
88     throws ConfigException
89   {
90     try {
91       _codeSource = new CodeSource JavaDoc(new URL JavaDoc(getPath().getURL()),
92                    (Certificate JavaDoc []) null);
93     } catch (Exception JavaDoc e) {
94       log.log(Level.FINE, e.toString(), e);
95     }
96   }
97
98   /**
99    * Adds a stub class.
100    */

101   public void addStubClass(String JavaDoc className)
102   {
103     System.out.println("PATH: " + getPath());
104     
105     int p = className.lastIndexOf('.');
106
107     if (p > 0) {
108       String JavaDoc tail = className.substring(p + 1);
109
110       tail = "_" + tail + "_Stub";
111
112       className = className.substring(0, p) + '.' + tail;
113     }
114     else
115       className = "_" + className + "_Stub";
116
117     className = "org.omg.stub." + className;
118
119     className = className.replace('.', '/') + ".class";
120     
121     System.out.println("CL: " + className);
122     
123     _stubClassNames.add(className);
124   }
125
126   /**
127    * Given a class or resource name, returns a patch to that resource.
128    *
129    * @param name the class or resource name.
130    *
131    * @return the path representing the class or resource.
132    */

133   public Path getPath(String JavaDoc name)
134   {
135     if (! _stubClassNames.contains(name))
136       return null;
137
138     Path stubClassPath = getPath().lookup(name);
139
140     System.out.println("PATH: " + stubClassPath.getNativePath());
141
142     if (stubClassPath.canRead())
143       return stubClassPath;
144
145     String JavaDoc fullClassName = name.substring(0, name.length() - ".class".length());
146     fullClassName = fullClassName.replace('/', '.');
147     
148     String JavaDoc className = fullClassName.substring("org.omg.stub.".length());
149     className = className.replace('/', '.');
150
151     int p = className.lastIndexOf('.');
152     if (p > 0) {
153       String JavaDoc tail = className.substring(p + 1);
154
155       tail = tail.substring(1, tail.length() - "_Stub".length());
156
157       className = className.substring(0, p) + '.' + tail;
158     }
159
160     Thread JavaDoc thread = Thread.currentThread();
161     ClassLoader JavaDoc loader = thread.getContextClassLoader();
162
163     try {
164       System.out.println("ZOOM: " + className);
165       
166       Class JavaDoc cl = Class.forName(className, false, loader);
167       System.out.println("CL: " + cl);
168
169       IiopStubCompiler compiler = new IiopStubCompiler(cl);
170       compiler.setFullClassName(fullClassName);
171       compiler.setClassDir(getPath());
172       
173       compiler.generate();
174       compiler.compileJava();
175     
176       System.out.println("OOK-OOK-OOK");
177       
178       if (stubClassPath.canRead())
179     return stubClassPath;
180     } catch (Exception JavaDoc e) {
181       e.printStackTrace();
182       log.log(Level.WARNING, e.toString(), e);
183     }
184
185     return null;
186   }
187
188   /**
189    * Returns the code source for the directory.
190    */

191   protected CodeSource JavaDoc getCodeSource(Path path)
192   {
193     return _codeSource;
194   }
195
196   /**
197    * Adds the class of this resource.
198    */

199   protected String JavaDoc getClassPath(String JavaDoc head)
200   {
201     CharBuffer cb = new CharBuffer();
202
203     if (! head.equals("")) {
204       cb.append(head);
205       cb.append(CauchoSystem.getPathSeparatorChar());
206     }
207     
208     if (getPath().isDirectory())
209       cb.append(getPath().getNativePath());
210
211     return cb.toString();
212   }
213
214   /**
215    * Returns a printable representation of the loader.
216    */

217   public String JavaDoc toString()
218   {
219     return "IiopStubLoader[" + getPath() + "]";
220   }
221 }
222
Popular Tags