001: package org.drools.eclipse.flow.common.editor;
002:
003: /*
004: * Copyright 2005 JBoss Inc
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: import java.io.IOException;
020: import java.io.InputStream;
021: import java.io.ObjectInputStream;
022: import java.io.ObjectStreamClass;
023: import java.io.StreamCorruptedException;
024: import java.lang.reflect.Array;
025:
026: /**
027: * Implementation of an ObjectInputStream that has a custom classloader.
028: *
029: * @author <a href="mailto:kris_verlaenen@hotmail.com">Kris Verlaenen</a>
030: */
031: public class ObjectInputStreamWithLoader extends ObjectInputStream {
032:
033: private ClassLoader loader;
034:
035: /**
036: * Loader must be non-null;
037: */
038: public ObjectInputStreamWithLoader(InputStream in,
039: ClassLoader loader) throws IOException,
040: StreamCorruptedException {
041: super (in);
042: if (loader == null) {
043: throw new IllegalArgumentException(
044: "Illegal null argument to ObjectInputStreamWithLoader");
045: }
046: this .loader = loader;
047: }
048:
049: /**
050: * Make a primitive array class
051: */
052: private Class primitiveType(char type) {
053: switch (type) {
054: case 'B':
055: return byte.class;
056: case 'C':
057: return char.class;
058: case 'D':
059: return double.class;
060: case 'F':
061: return float.class;
062: case 'I':
063: return int.class;
064: case 'J':
065: return long.class;
066: case 'S':
067: return short.class;
068: case 'Z':
069: return boolean.class;
070: default:
071: return null;
072: }
073: }
074:
075: /**
076: * Use the given ClassLoader rather than using the system class
077: */
078: protected Class resolveClass(ObjectStreamClass classDesc)
079: throws IOException, ClassNotFoundException {
080: String cname = classDesc.getName();
081: if (cname.startsWith("[")) {
082: // An array
083: Class component = null; // component class
084: int dcount; // dimension
085: for (dcount = 1; cname.charAt(dcount) == '['; dcount++)
086: ;
087: if (cname.charAt(dcount) == 'L') {
088: String className = cname.substring(dcount + 1, cname
089: .length() - 1);
090: component = loader.loadClass(className);
091: } else {
092: if (cname.length() != dcount + 1) {
093: throw new ClassNotFoundException(cname);// malformed
094: }
095: component = primitiveType(cname.charAt(dcount));
096: }
097: int dim[] = new int[dcount];
098: for (int i = 0; i < dcount; i++) {
099: dim[i] = 0;
100: }
101: return Array.newInstance(component, dim).getClass();
102: }
103: return loader.loadClass(cname);
104: }
105: }
|