001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.iiop;
030:
031: import com.caucho.config.ConfigException;
032: import com.caucho.ejb.AbstractStubLoader;
033: import com.caucho.loader.DynamicClassLoader;
034: import com.caucho.log.Log;
035: import com.caucho.server.util.CauchoSystem;
036: import com.caucho.util.CharBuffer;
037: import com.caucho.vfs.Path;
038:
039: import java.net.URL;
040: import java.security.CodeSource;
041: import java.security.cert.Certificate;
042: import java.util.HashSet;
043: import java.util.logging.Level;
044: import java.util.logging.Logger;
045:
046: /**
047: * Class loader which checks for changes in class files and automatically
048: * picks up new jars.
049: */
050: public class IiopStubLoader extends AbstractStubLoader {
051: private static final Logger log = Log.open(IiopStubLoader.class);
052:
053: private HashSet<String> _stubClassNames = new HashSet<String>();
054:
055: private CodeSource _codeSource;
056:
057: /**
058: * Null constructor for the simple loader.
059: */
060: public IiopStubLoader() {
061: }
062:
063: /**
064: * Creates the simple loader with the specified path.
065: *
066: * @param path specifying the root of the resources
067: */
068: public IiopStubLoader(Path path) {
069: setPath(path);
070: }
071:
072: /**
073: * Sets the owning class loader.
074: */
075: public void setLoader(DynamicClassLoader loader) {
076: super .setLoader(loader);
077:
078: loader.addURL(getPath());
079: }
080:
081: /**
082: * Initializes the loader.
083: */
084: public void init() throws ConfigException {
085: try {
086: _codeSource = new CodeSource(new URL(getPath().getURL()),
087: (Certificate[]) null);
088: } catch (Exception e) {
089: log.log(Level.FINE, e.toString(), e);
090: }
091: }
092:
093: /**
094: * Adds a stub class.
095: */
096: public void addStubClass(String className) {
097: System.out.println("PATH: " + getPath());
098:
099: int p = className.lastIndexOf('.');
100:
101: if (p > 0) {
102: String tail = className.substring(p + 1);
103:
104: tail = "_" + tail + "_Stub";
105:
106: className = className.substring(0, p) + '.' + tail;
107: } else
108: className = "_" + className + "_Stub";
109:
110: className = "org.omg.stub." + className;
111:
112: className = className.replace('.', '/') + ".class";
113:
114: System.out.println("CL: " + className);
115:
116: _stubClassNames.add(className);
117: }
118:
119: /**
120: * Given a class or resource name, returns a patch to that resource.
121: *
122: * @param name the class or resource name.
123: *
124: * @return the path representing the class or resource.
125: */
126: public Path getPath(String name) {
127: if (!_stubClassNames.contains(name))
128: return null;
129:
130: Path stubClassPath = getPath().lookup(name);
131:
132: System.out.println("PATH: " + stubClassPath.getNativePath());
133:
134: if (stubClassPath.canRead())
135: return stubClassPath;
136:
137: String fullClassName = name.substring(0, name.length()
138: - ".class".length());
139: fullClassName = fullClassName.replace('/', '.');
140:
141: String className = fullClassName.substring("org.omg.stub."
142: .length());
143: className = className.replace('/', '.');
144:
145: int p = className.lastIndexOf('.');
146: if (p > 0) {
147: String tail = className.substring(p + 1);
148:
149: tail = tail.substring(1, tail.length() - "_Stub".length());
150:
151: className = className.substring(0, p) + '.' + tail;
152: }
153:
154: Thread thread = Thread.currentThread();
155: ClassLoader loader = thread.getContextClassLoader();
156:
157: try {
158: System.out.println("ZOOM: " + className);
159:
160: Class cl = Class.forName(className, false, loader);
161: System.out.println("CL: " + cl);
162:
163: IiopStubCompiler compiler = new IiopStubCompiler(cl);
164: compiler.setFullClassName(fullClassName);
165: compiler.setClassDir(getPath());
166:
167: compiler.generate();
168: compiler.compileJava();
169:
170: System.out.println("OOK-OOK-OOK");
171:
172: if (stubClassPath.canRead())
173: return stubClassPath;
174: } catch (Exception e) {
175: e.printStackTrace();
176: log.log(Level.WARNING, e.toString(), e);
177: }
178:
179: return null;
180: }
181:
182: /**
183: * Returns the code source for the directory.
184: */
185: protected CodeSource getCodeSource(Path path) {
186: return _codeSource;
187: }
188:
189: /**
190: * Adds the class of this resource.
191: */
192: protected String getClassPath(String head) {
193: CharBuffer cb = new CharBuffer();
194:
195: if (!head.equals("")) {
196: cb.append(head);
197: cb.append(CauchoSystem.getPathSeparatorChar());
198: }
199:
200: if (getPath().isDirectory())
201: cb.append(getPath().getNativePath());
202:
203: return cb.toString();
204: }
205:
206: /**
207: * Returns a printable representation of the loader.
208: */
209: public String toString() {
210: return "IiopStubLoader[" + getPath() + "]";
211: }
212: }
|