001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: /*
043: * JavaClassLoader.java
044: *
045: * Created on January 18, 2007, 1:07 PM
046: *
047: * To change this template, choose Tools | Template Manager
048: * and open the template in the editor.
049: */
050:
051: package org.netbeans.test.umllib.project;
052:
053: import java.io.File;
054: import java.io.FileInputStream;
055: import java.io.IOException;
056: import java.io.InputStream;
057: import java.util.HashMap;
058:
059: /**
060: *
061: * @author Alexandr Scherbatiy
062: */
063: public class JavaClassLoader extends ClassLoader {
064:
065: private boolean showLog = false;
066: private int INITIAL_SIZE = 1024;
067:
068: private String rootPath;
069: private int rootIndex;
070:
071: private HashMap<String, File> hashMap = new HashMap<String, File>();
072:
073: /** Creates a new instance of JavaClassLoader */
074: public JavaClassLoader(String rootPath) {
075: this .rootPath = rootPath;
076: this .rootIndex = rootPath.length() + 1;
077: loadClassList();
078: }
079:
080: private void loadClassList() {
081: File rootDir = new File(rootPath);
082: loadClassList(rootDir);
083:
084: showLog("[Show HashMap]");
085: for (String name : hashMap.keySet()) {
086: showLog("file: \"" + name + "\"");
087: }
088: showLog("");
089:
090: }
091:
092: private void loadClassList(File file) {
093:
094: showLog("[file] " + file.getAbsolutePath());
095:
096: if (file.isDirectory()) {
097: for (File f : file.listFiles()) {
098: loadClassList(f);
099: }
100: } else {
101:
102: String name = file.getPath();
103:
104: if (name.endsWith(".class")) {
105:
106: name = name.substring(rootIndex, name.length()
107: - ".class".length());
108: name = name.replace("/", ".");
109: name = name.replace("\\", ".");
110:
111: hashMap.put(name, file);
112: }
113: }
114:
115: }
116:
117: protected Class<?> findClass(String name)
118: throws ClassNotFoundException {
119:
120: showLog("[find class] " + "\"" + name + "\"");
121:
122: if (hashMap.keySet().contains(name)) {
123: return loadClassFromFile(name);
124: }
125:
126: Class result = super .findSystemClass(name);
127: return result;
128:
129: }
130:
131: public synchronized Class loadClassFromFile(String className)
132: throws ClassNotFoundException {
133:
134: System.out.println("[loadClassFromFile] " + className);
135:
136: try {
137:
138: showLog("load entry = \"" + className + "\"");
139:
140: File file = hashMap.get(className);
141:
142: InputStream inputStream = new FileInputStream(file);
143:
144: int available = inputStream.available();
145:
146: byte[] buffer = new byte[INITIAL_SIZE];
147:
148: int len = inputStream.read(buffer);
149: int size = len;
150:
151: while (true) {
152:
153: byte[] temp = new byte[buffer.length + INITIAL_SIZE];
154: System.arraycopy(buffer, 0, temp, 0, size);
155: buffer = temp;
156:
157: len = inputStream.read(buffer, size, buffer.length
158: - size - 1);
159:
160: if (len < 0) {
161: break;
162: }
163:
164: size += len;
165:
166: showLog("buff len = " + buffer.length);
167: }
168:
169: Class defClass = defineClass(className, buffer, 0, size);
170:
171: return defClass;
172:
173: } catch (IOException e) {
174: throw new ClassNotFoundException("class \"" + className
175: + "\" is not loaded from zip!", e);
176: }
177:
178: }
179:
180: public void showLog(String log) {
181: if (showLog) {
182: System.out.println("[class loader] " + log + "\n");
183: }
184:
185: }
186:
187: }
|