001: /*
002: * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
003: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
004: */
005: package com.sun.portal.monitoring;
006:
007: import java.io.ByteArrayOutputStream;
008: import java.io.File;
009: import java.io.FileInputStream;
010: import java.io.IOException;
011: import java.io.InputStream;
012: import java.util.ArrayList;
013: import java.util.List;
014: import java.util.StringTokenizer;
015: import java.util.zip.ZipEntry;
016: import java.util.zip.ZipFile;
017:
018: public class MonitoringClassLoader extends ClassLoader {
019: private String[] classPathElements;
020: private String[] delegatees;
021: private String[] nonDelegatees;
022:
023: private void parseClassPath(String classPath) {
024: List result = new ArrayList();
025: if (classPath != null) {
026: StringTokenizer st = new StringTokenizer(classPath, System
027: .getProperty("path.separator"));
028: while (st.hasMoreTokens()) {
029: result.add(st.nextToken());
030: }
031: }
032:
033: classPathElements = (result.size() == 0) ? null
034: : (String[]) result.toArray(new String[result.size()]);
035: }
036:
037: private void parseDelegatees(String delegatees) {
038: List result = new ArrayList();
039: if (delegatees != null) {
040: StringTokenizer st = new StringTokenizer(delegatees, System
041: .getProperty("path.separator"));
042: while (st.hasMoreTokens()) {
043: result.add(st.nextToken());
044: }
045: }
046:
047: this .delegatees = (result.size() == 0) ? null
048: : (String[]) result.toArray(new String[result.size()]);
049: }
050:
051: private void parseNonDelegatees(String nonDelegatees) {
052: List result = new ArrayList();
053: if (nonDelegatees != null) {
054: StringTokenizer st = new StringTokenizer(nonDelegatees,
055: System.getProperty("path.separator"));
056: while (st.hasMoreTokens()) {
057: result.add(st.nextToken());
058: }
059: }
060:
061: this .nonDelegatees = (result.size() == 0) ? null
062: : (String[]) result.toArray(new String[result.size()]);
063: }
064:
065: public MonitoringClassLoader(String classPath, ClassLoader parent,
066: String delegatees, String nonDelegatees) {
067: super (parent);
068:
069: parseClassPath(classPath);
070: parseDelegatees(delegatees);
071: parseNonDelegatees(nonDelegatees);
072: }
073:
074: private byte[] lookupClassData(String fullPathName)
075: throws ClassNotFoundException {
076: if (classPathElements != null) {
077: byte data[] = null;
078: for (int i = 0; i < classPathElements.length; i++) {
079: String classPathElement = classPathElements[i];
080: if (isJar(classPathElement)) {
081: data = loadJarData(classPathElement, fullPathName);
082: } else {
083: data = loadFileData(classPathElement, fullPathName);
084: }
085: if (data != null) {
086: return data;
087: }
088: }
089: }
090:
091: throw new ClassNotFoundException();
092: }
093:
094: boolean isJar(String pathEntry) {
095: return pathEntry.endsWith(".jar") || pathEntry.endsWith(".zip");
096: }
097:
098: private byte[] loadFileData(String path, String fileName) {
099: File file = new File(path, fileName);
100: if (file.exists()) {
101: return getClassData(file);
102: } else {
103: return null;
104: }
105: }
106:
107: private byte[] getClassData(File f) {
108: try {
109: FileInputStream stream = new FileInputStream(f);
110: ByteArrayOutputStream out = new ByteArrayOutputStream(1000);
111: byte b[] = new byte[1000];
112: int n;
113: while ((n = stream.read(b)) != -1) {
114: out.write(b, 0, n);
115: }
116: stream.close();
117: out.close();
118: return out.toByteArray();
119: } catch (IOException ioexception) {
120: return null;
121: }
122: }
123:
124: private byte[] loadJarData(String path, String fileName) {
125: ZipFile zipFile = null;
126: InputStream stream = null;
127: File archive = new File(path);
128: if (!archive.exists()) {
129: return null;
130: }
131: try {
132: zipFile = new ZipFile(archive);
133: } catch (IOException io) {
134: return null;
135: }
136: ZipEntry entry = zipFile.getEntry(fileName);
137: if (entry == null) {
138: return null;
139: }
140: int size = (int) entry.getSize();
141: try {
142: stream = zipFile.getInputStream(entry);
143: byte data[] = new byte[size];
144: int n;
145: for (int pos = 0; pos < size; pos += n) {
146: n = stream.read(data, pos, data.length - pos);
147: }
148:
149: zipFile.close();
150: byte abyte0[] = data;
151: return abyte0;
152: } catch (IOException ioexception) {
153: } finally {
154: try {
155: if (stream != null) {
156: stream.close();
157: }
158: } catch (IOException e) {
159: }
160: }
161: return null;
162: }
163:
164: private String encode(String name) {
165: return name + getClass().getName();
166: }
167:
168: private String decode(String name) {
169: return name.substring(0, name.indexOf(getClass().getName()));
170: }
171:
172: private boolean isEncoded(String name) {
173: return name.endsWith(getClass().getName());
174: }
175:
176: protected Class findClass(String name)
177: throws ClassNotFoundException {
178: Class clazz = null;
179:
180: if (isEncoded(name)) {
181: String decodedName = decode(name);
182: String fullPathName = decodedName.replace('.', '/').concat(
183: ".class");
184:
185: byte data[] = lookupClassData(fullPathName);
186: if (data == null) {
187: throw new ClassNotFoundException();
188: }
189:
190: clazz = defineClass(decodedName, data, 0, data.length);
191: }
192:
193: return clazz;
194: }
195:
196: private boolean isDelegatee(String name) {
197: boolean result = false;
198:
199: if (delegatees != null) {
200: for (int i = 0; i < delegatees.length; i++) {
201: if (name.equals(delegatees[i])) {
202: result = true;
203: break;
204: }
205: }
206: }
207:
208: return result;
209: }
210:
211: protected synchronized Class loadClass(String name, boolean resolve)
212: throws ClassNotFoundException {
213: Class clazz = findLoadedClass(name);
214: if (clazz == null) {
215: if (isDelegatee(name)) {
216: clazz = getParent().loadClass(name);
217: }
218:
219: if (clazz == null) {
220: if (nonDelegatees != null) {
221: if (nonDelegatees.length != 0) {
222: int i = 0;
223: do {
224: if (name.startsWith(nonDelegatees[i])) {
225: clazz = super.loadClass(encode(name),
226: resolve);
227: break;
228: }
229:
230: i++;
231: } while (i < nonDelegatees.length);
232: }
233: }
234: }
235:
236: if (clazz == null) {
237: clazz = getParent().loadClass(name);
238: }
239: }
240:
241: if (resolve) {
242: resolveClass(clazz);
243: }
244:
245: return clazz;
246: }
247: }
|