You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@xalan.apache.org by Martin Klang <ma...@pingdynasty.com> on 2000/11/30 10:18:54 UTC

ContextClassLoader for java extensions

hiyall,

we had a discussion a couple of weeks ago regarding the way xalan loads
extension classes. i believe the consensus was that it would be nice
if xalan could use the ContextClassLoader introduced in jdk1.2, without
breaking jdk1.1 compatibility. and so here's the patch :)

it follows what was said in the previous discussion, and should be
quite straightforward - basically i've mimicked what tomcat does.
it's been tested with ibms jdk1.1.8 and blackdowns 1.2.2 for linux - i
can't see any reason why it shouldn't work on all previously supported
jdks/platforms.

the patch is against xalanJ 1.2 D02 - if you really want i'll set up anon 
cvs and redo it, provided you tell what module/tag to diff against.

regards,

/martin


--- src/org/apache/xalan/xpath/XSLTJavaClassEngine.java	Thu Nov 30
08:55:57 2000+++
original/src/org/apache/xalan/xpath/XSLTJavaClassEngine.java	Thu Aug 24
18:19:53 2000
@@ -76,11 +76,6 @@
 
 public class XSLTJavaClassEngine extends BSFEngineImpl 
 {
-    // static so we don't have to create new objects in getClassForName
-    private static final Object noObjs[] = new Object[0];
-    private static final Class noParams[] = new Class[0];
-    private static boolean tryContextClassLoader = true;
-
   /**
    * This is used by an application to evaluate an object containing
    * some expression - clearly not possible for compiled code ..
@@ -92,34 +87,6 @@
       "Java bytecode engine can't evaluate expressions");
   }
 
-  protected Class getClassForName(String className)
-      throws ClassNotFoundException
-  {
-      Class result = null;
-      if(tryContextClassLoader){
-          try{
-              Thread currentThread = Thread.currentThread();
-              java.lang.reflect.Method getCCL = currentThread.getClass()
-                  .getMethod("getContextClassLoader", noParams);
-              ClassLoader contextClassLoader =
-                  (ClassLoader)getCCL.invoke(currentThread, noObjs);
-              result = Class.forName(className, true,
contextClassLoader);
-          }catch(InvocationTargetException e){
-              // something went wrong... should log this
-              tryContextClassLoader=false;
-          }catch(NoSuchMethodException e){
-              // we don't have the methods (jdk<1.2), don't try again
-              tryContextClassLoader=false;
-          }catch(IllegalAccessException e){
-              // we don't have the methods (jdk<1.2), don't try again
-              tryContextClassLoader=false;
-          }
-      }
-      if(result == null)
-          result = Class.forName(className);
-      return result;
-  }
-
   /**
    * call the named method on the object that was loaded by eval. 
    * The method selection stuff is very XSLT-specific, hence the
@@ -154,7 +121,7 @@
       isNew = method.equals ("new");
       try 
       {
-          object = getClassForName(className);
+        object = Class.forName (className);
       }
       catch (ClassNotFoundException e) 
       {



Re: ContextClassLoader for java extensions

Posted by Gary L Peskin <ga...@firstech.com>.
Martin Klang wrote:
> 
> hiyall,
> 
> we had a discussion a couple of weeks ago regarding the way xalan loads
> extension classes. i believe the consensus was that it would be nice
> if xalan could use the ContextClassLoader introduced in jdk1.2, without
> breaking jdk1.1 compatibility. and so here's the patch :)

Thanks, Martin.  This looks great!

To summarize:

XalanJ will now load extension functions written in java using the
ContextClassLoader for the current thread rather than the classloader
used to load Xalan itself.  This change will only be effective for Java
2 since the ContextClassLoader is not available under Java 1.1.x.  Even
under Java 2, this change will only affect those multi-threaded
applications where Xalan is loaded by a different classloader than the
one asssociated with the thread doing the transformation.

If anyone does NOT want this change, speak up before 5pm PST Friday
1-Dec-00.

Otherwise, I'll commit Martin's code to XalanJ1 and retrofit
(forwardfit?) it for XalanJ2.

Gary

Re: ContextClassLoader for java extensions

Posted by Martin Klang <ma...@pingdynasty.com>.
On Sun, 3 Dec 2000, Gary L Peskin wrote:
> 
> I had to make one small change.  The Class.forName method with three
> arguments was not supported until Java2.  Thus, Martin's change required
> that XalanJ1 be compiled only under Java2, even though it would execute
> just fine with Java 1.1.x.
> 
> I changed Class.forName to a call to contextClassLoader.loadClass().  I
> think that in the way XalanJ uses this, this shouldn't be a problem.
> 
> Since I don't have Tomcat (or any servlet engine) installed, I would
> appreciate people testing this, when convenient, in both XalanJ1 and
> XalanJ2.

good stuff - i didn't try compiling it on jdk1.1 so i wasn't aware of
this. i'll try to test it with tomcat and an ejb-server sometime today
(at least xalanJ 1), but i shouldn't think there'd be any problems.

> 
> Thanks again to Martin for this change.

my pleasure - glad to be able to contribute :>

/m


Re: ContextClassLoader for java extensions

Posted by Gary L Peskin <ga...@firstech.com>.
Martin Klang wrote:
> 
> hiyall,
> 
> we had a discussion a couple of weeks ago regarding the way xalan loads
> extension classes. i believe the consensus was that it would be nice
> if xalan could use the ContextClassLoader introduced in jdk1.2, without
> breaking jdk1.1 compatibility. and so here's the patch :)

Martin's change is now committed to CVS.  Thanks, Martin.  I've also
made the corresponding changes in XalanJ2.

I had to make one small change.  The Class.forName method with three
arguments was not supported until Java2.  Thus, Martin's change required
that XalanJ1 be compiled only under Java2, even though it would execute
just fine with Java 1.1.x.

I changed Class.forName to a call to contextClassLoader.loadClass().  I
think that in the way XalanJ uses this, this shouldn't be a problem.

Since I don't have Tomcat (or any servlet engine) installed, I would
appreciate people testing this, when convenient, in both XalanJ1 and
XalanJ2.

Thanks again to Martin for this change.

Gary