Author: andygumbrecht
Date: Thu Dec 6 10:48:39 2012
New Revision: 1417802
URL: http://svn.apache.org/viewvc?rev=1417802&view=rev
Log:
Reuse ByteArrayOutputStream.
Finals.
Modified:
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/core/TempClassLoader.java
openejb/trunk/openejb/container/openejb-core/src/test/java/org/apache/openejb/core/TempClassLoaderTest.java
Modified:
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/core/TempClassLoader.java
URL:
http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/core/TempClassLoader.java?rev=1417802&r1=1417801&r2=1417802&view=diff
==============================================================================
---
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/core/TempClassLoader.java
(original)
+++
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/core/TempClassLoader.java
Thu Dec 6 10:48:39 2012
@@ -46,26 +46,31 @@ import java.util.Set;
*/
// Note: this class is a fork from OpenJPA
public class TempClassLoader extends URLClassLoader {
+
private final Set<Skip> skip;
private final ClassLoader system;
private final boolean embedded;
- public TempClassLoader(ClassLoader parent) {
+ // 80% of class files are smaller then 6k
+ private final ByteArrayOutputStream bout = new ByteArrayOutputStream(6 *
1024);
+
+ public TempClassLoader(final ClassLoader parent) {
super(new URL[0], parent);
- skip =
SystemInstance.get().getOptions().getAll("openejb.tempclassloader.skip",
Skip.NONE);
- system = ClassLoader.getSystemClassLoader();
- embedded = getClass().getClassLoader() == system;
+ this.skip =
SystemInstance.get().getOptions().getAll("openejb.tempclassloader.skip",
Skip.NONE);
+ this.system = ClassLoader.getSystemClassLoader();
+ this.embedded = this.getClass().getClassLoader() == this.system;
}
/*
* Needed for testing
*/
- public void skip(Skip s) {
+ public void skip(final Skip s) {
this.skip.add(s);
}
- public Class loadClass(String name) throws ClassNotFoundException {
- return loadClass(name, false);
+ @Override
+ public Class loadClass(final String name) throws ClassNotFoundException {
+ return this.loadClass(name, false);
}
@Override
@@ -73,11 +78,12 @@ public class TempClassLoader extends URL
return URLClassLoaderFirst.filterResources(name,
super.getResources(name));
}
- protected synchronized Class loadClass(String name, boolean resolve)
throws ClassNotFoundException {
+ @Override
+ protected synchronized Class loadClass(final String name, final boolean
resolve) throws ClassNotFoundException {
if (name == null) throw new NullPointerException("name cannot be
null");
-
+
// see if we've already loaded it
- Class c = findLoadedClass(name);
+ Class c = this.findLoadedClass(name);
if (c != null) {
return c;
}
@@ -96,16 +102,16 @@ public class TempClassLoader extends URL
* 2. Since this class loader uses Class.forName to load classes
starting with java, javax or sun, it cannot load javax.faces.FacesServlet
* 3. Result is , AnnotationDeployer throws a ClassNotFoundException
*/
- if (skip(name)) {
- return Class.forName(name, resolve, getClass().getClassLoader());
+ if (this.skip(name)) {
+ return Class.forName(name, resolve,
this.getClass().getClassLoader());
}
// don't load classes from app classloader
// we do it after the previous one since it will probably result to
the same
// Class and the previous one is faster than this one
- if (!embedded && URLClassLoaderFirst.canBeLoadedFromSystem(name)) {
+ if (!this.embedded && URLClassLoaderFirst.canBeLoadedFromSystem(name))
{
try {
- c = system.loadClass(name);
+ c = this.system.loadClass(name);
if (c != null) {
return c;
}
@@ -115,23 +121,28 @@ public class TempClassLoader extends URL
}
// ( && !name.startsWith("javax.faces.") )||
- String resourceName = name.replace('.', '/') + ".class";
- InputStream in = getResourceAsStream(resourceName);
- if (in != null && !(in instanceof BufferedInputStream)) {
- in = new BufferedInputStream(in);
- }
- if (in == null) {
- throw new ClassNotFoundException(name);
- }
+ final String resourceName = name.replace('.', '/') + ".class";
- // 80% of class files are smaller then 6k
- ByteArrayOutputStream bout = new ByteArrayOutputStream(8 * 1024);
+ //Copy the input stream into a byte array
+ final byte[] bytes;
+ this.bout.reset();
+ InputStream in = null;
- // copy the input stream into a byte array
- byte[] bytes;
try {
- IO.copy(in, bout);
- bytes = bout.toByteArray();
+
+ in = this.getResourceAsStream(resourceName);
+
+ if (in != null && !(in instanceof BufferedInputStream)) {
+ in = new BufferedInputStream(in);
+ }
+
+ if (in == null) {
+ throw new ClassNotFoundException(name);
+ }
+
+ IO.copy(in, this.bout);
+ bytes = this.bout.toByteArray();
+
} catch (IOException e) {
throw new ClassNotFoundException(name, e);
} finally {
@@ -140,26 +151,26 @@ public class TempClassLoader extends URL
// Annotation classes must be loaded by the normal classloader
// So must Enum classes to prevent problems with the sun jdk.
- if (skip.contains(Skip.ANNOTATIONS) && isAnnotationClass(bytes)) {
- return Class.forName(name, resolve, getClass().getClassLoader());
+ if (this.skip.contains(Skip.ANNOTATIONS) && isAnnotationClass(bytes)) {
+ return Class.forName(name, resolve,
this.getClass().getClassLoader());
}
- if (skip.contains(Skip.ENUMS) && isEnum(bytes)) {
- return Class.forName(name, resolve, getClass().getClassLoader());
+ if (this.skip.contains(Skip.ENUMS) && isEnum(bytes)) {
+ return Class.forName(name, resolve,
this.getClass().getClassLoader());
}
// define the package
- int packageEndIndex = name.lastIndexOf('.');
+ final int packageEndIndex = name.lastIndexOf('.');
if (packageEndIndex != -1) {
- String packageName = name.substring(0, packageEndIndex);
- if (getPackage(packageName) == null) {
- definePackage(packageName, null, null, null, null, null, null,
null);
+ final String packageName = name.substring(0, packageEndIndex);
+ if (this.getPackage(packageName) == null) {
+ this.definePackage(packageName, null, null, null, null, null,
null, null);
}
}
// define the class
try {
- return defineClass(name, bytes, 0, bytes.length);
+ return this.defineClass(name, bytes, 0, bytes.length);
} catch (SecurityException e) {
// possible prohibited package: defer to the parent
return super.loadClass(name, resolve);
@@ -170,8 +181,8 @@ public class TempClassLoader extends URL
}
// TODO: for jsf it can be useful to include commons-logging and
openwebbeans...
- private boolean skip(String name) {
- return skip.contains(Skip.ALL) || URLClassLoaderFirst.shouldSkip(name);
+ private boolean skip(final String name) {
+ return this.skip.contains(Skip.ALL) ||
URLClassLoaderFirst.shouldSkip(name);
}
public static enum Skip {
@@ -182,9 +193,9 @@ public class TempClassLoader extends URL
* Fast-parse the given class bytecode to determine if it is an
* enum class.
*/
- private static boolean isEnum(byte[] bytes) {
- IsEnumVisitor isEnumVisitor = new IsEnumVisitor();
- ClassReader classReader = new ClassReader(bytes);
+ private static boolean isEnum(final byte[] bytes) {
+ final IsEnumVisitor isEnumVisitor = new IsEnumVisitor();
+ final ClassReader classReader = new ClassReader(bytes);
classReader.accept(isEnumVisitor, ClassReader.SKIP_DEBUG);
return isEnumVisitor.isEnum;
}
@@ -193,9 +204,9 @@ public class TempClassLoader extends URL
* Fast-parse the given class bytecode to determine if it is an
* annotation class.
*/
- private static boolean isAnnotationClass(byte[] bytes) {
- IsAnnotationVisitor isAnnotationVisitor = new IsAnnotationVisitor();
- ClassReader classReader = new ClassReader(bytes);
+ private static boolean isAnnotationClass(final byte[] bytes) {
+ final IsAnnotationVisitor isAnnotationVisitor = new
IsAnnotationVisitor();
+ final ClassReader classReader = new ClassReader(bytes);
classReader.accept(isAnnotationVisitor, ClassReader.SKIP_DEBUG);
return isAnnotationVisitor.isAnnotation;
}
@@ -203,17 +214,19 @@ public class TempClassLoader extends URL
public static class IsAnnotationVisitor extends EmptyVisitor {
public boolean isAnnotation = false;
- public void visit(int version, int access, String name, String
signature, String superName, String[] interfaces) {
- isAnnotation = (access & Opcodes.ACC_ANNOTATION) != 0;
+ @Override
+ public void visit(final int version, final int access, final String
name, final String signature, final String superName, final String[]
interfaces) {
+ this.isAnnotation = (access & Opcodes.ACC_ANNOTATION) != 0;
}
}
-
+
public static class IsEnumVisitor extends EmptyVisitor {
public boolean isEnum = false;
- public void visit(int version, int access, String name, String
signature, String superName, String[] interfaces) {
- isEnum = (access & Opcodes.ACC_ENUM) != 0;
+ @Override
+ public void visit(final int version, final int access, final String
name, final String signature, final String superName, final String[]
interfaces) {
+ this.isEnum = (access & Opcodes.ACC_ENUM) != 0;
}
}
Modified:
openejb/trunk/openejb/container/openejb-core/src/test/java/org/apache/openejb/core/TempClassLoaderTest.java
URL:
http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-core/src/test/java/org/apache/openejb/core/TempClassLoaderTest.java?rev=1417802&r1=1417801&r2=1417802&view=diff
==============================================================================
---
openejb/trunk/openejb/container/openejb-core/src/test/java/org/apache/openejb/core/TempClassLoaderTest.java
(original)
+++
openejb/trunk/openejb/container/openejb-core/src/test/java/org/apache/openejb/core/TempClassLoaderTest.java
Thu Dec 6 10:48:39 2012
@@ -41,7 +41,7 @@ public class TempClassLoaderTest {
@Test
public void test() throws Exception {
- ClassLoader tempCL = new TempClassLoader(getClass().getClassLoader());
+ final ClassLoader tempCL = new
TempClassLoader(this.getClass().getClassLoader());
Class<?> clazz;
// normal classes should be loaded by the temp class loader
@@ -63,7 +63,7 @@ public class TempClassLoaderTest {
@Test
public void testHackEnabled() throws Exception {
- TempClassLoader tempCL = new
TempClassLoader(getClass().getClassLoader());
+ final TempClassLoader tempCL = new
TempClassLoader(this.getClass().getClassLoader());
tempCL.skip(TempClassLoader.Skip.ANNOTATIONS);
Class<?> clazz;