You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ro...@apache.org on 2012/01/29 18:18:56 UTC

svn commit: r1237362 - /commons/proper/bsf/trunk/src/main/java/org/apache/bsf/BSFManager.java

Author: rony
Date: Sun Jan 29 17:18:56 2012
New Revision: 1237362

URL: http://svn.apache.org/viewvc?rev=1237362&view=rev
Log:
issue [BSF-38] in full, partially [BSF-15]

Modified:
    commons/proper/bsf/trunk/src/main/java/org/apache/bsf/BSFManager.java

Modified: commons/proper/bsf/trunk/src/main/java/org/apache/bsf/BSFManager.java
URL: http://svn.apache.org/viewvc/commons/proper/bsf/trunk/src/main/java/org/apache/bsf/BSFManager.java?rev=1237362&r1=1237361&r2=1237362&view=diff
==============================================================================
--- commons/proper/bsf/trunk/src/main/java/org/apache/bsf/BSFManager.java (original)
+++ commons/proper/bsf/trunk/src/main/java/org/apache/bsf/BSFManager.java Sun Jan 29 17:18:56 2012
@@ -68,6 +68,9 @@ import org.apache.bsf.util.ObjectRegistr
                                  load not found, then
                                - use the BSFManager's defining ClassLoader instead, if it is
                                  different to the context ClassLoader
+
+           2012-01-29, ---rgf, - context class loader may not be set, account for it (2009-09-10)
+                               - static constructor: fixed logic error in fallback code for getResources() (2011-01-08)
 */
 
 public class BSFManager {
@@ -75,8 +78,8 @@ public class BSFManager {
     // "abc" represents a dewey decimal number (three levels, each between 0 and 9),
     // and "yyyy" a four digit year, "mm" a two digit month, "dd" a two digit day.
     //
-    // Example: "242.20070921" stands for: BSF version "2.4.3" as of "2007-09-21"
-    protected static String version="243.20070921";
+    // Example: "250.20120129" stands for: BSF version "2.5.0" as of "2012-01-29"
+    protected static String version="250.20120129";
 
     // table of registered scripting engines
     protected static Hashtable registeredEngines = new Hashtable();
@@ -150,21 +153,19 @@ public class BSFManager {
         try {
             definedClassLoader=BSFManager.class.getClassLoader();   // get defining ClassLoader
 
-/*
-            RGFInfo.newSection(strInfo);
-            RGFInfo.dumpClassLoaderInfos(BSFManager.class, "org.apache.bsf.BSFManager");
-            RGFInfo.append("BSFManager.staticBlock, registering BSF scripting engines:\n");
-*/
-
-            // Enumeration e = BSFManager.class.getClassLoader().getResources("org/apache/bsf/Languages.properties");
-            // use the Thread's context class loader to locate the resources
             String resourceName="org/apache/bsf/Languages.properties";
-            Enumeration e = Thread.currentThread().getContextClassLoader().getResources(resourceName);
 
-            if (!e.hasMoreElements()) // 20070917: if no resources (BSF engines!), then use defCL
+            Enumeration e = null;
+            // use the Thread's context class loader to locate the resources
+            ClassLoader tccl=Thread.currentThread().getContextClassLoader();    // try to get the context class loader
+            if (tccl!=null)                         // no context class loader available!
+            {
+                e=tccl.getResources(resourceName);
+            }
+            else  // fallback
             {
-//                RGFInfo.append("==> no BSF engines found from context ClassLoader, now using defCL !\n");
                 e=definedClassLoader.getResources(resourceName);
+		Thread.currentThread().setContextClassLoader(definedClassLoader); // set Thread context class loader
             }
 
             while (e.hasMoreElements()) {
@@ -186,42 +187,43 @@ public class BSFManager {
                     StringTokenizer st = new StringTokenizer(exts, "|");
                     String[] extensions = new String[st.countTokens()];
 
-//                    RGFInfo.append("\tregistering BSF engine: key=["+key+"]\tclassName=["+className+"] nr. of extensions=["+extensions.length+"\n");
                     for (int i = 0; st.hasMoreTokens(); i++) {
                         extensions[i] = st.nextToken().trim();
-//                        RGFInfo.append("\t\t--->extension=["+extensions[i]+"]\n");
                     }
 
                     registerScriptingEngine(key, className, extensions);
                 }
             }
         } catch (IOException ex) {
+            BSF_Log logger = BSF_LogFactory.getLog(BSFManager.class.getName());
+            logger.debug("[BSFManager] static {...}");
+            logger.error("[BSFManager] Error reading Languages file, exception :",ex);
 
+               // TODO: leave in case only a no-op-logger is available or remove next two statements?
             ex.printStackTrace();
             System.err.println("Error reading Languages file " + ex);
-//            RGFInfo.append("\t*ERROR* reading Language file ["+ex+"]\n");
         } catch (NoSuchElementException nsee) {
+            BSF_Log logger = BSF_LogFactory.getLog(BSFManager.class.getName());
+            logger.debug("[BSFManager] static {...}");
+            logger.error("[BSFManager] Syntax error in Languages resource bundle, exception :",nsee);
 
+            // TODO: leave in case only a no-op-logger is available or remove next two statements?
             nsee.printStackTrace();
             System.err.println("Syntax error in Languages resource bundle");
-//            RGFInfo.append("\t*ERROR* Syntax error in Languages resourcebundle ["+nsee+"]\n");
         } catch (MissingResourceException mre) {
+            BSF_Log logger = BSF_LogFactory.getLog(BSFManager.class.getName());
+            logger.debug("[BSFManager] static {...}");
+            logger.error("[BSFManager] Initialization error, exception :",mre);
 
+            // TODO: leave in case only a no-op-logger is available or remove next two statements?
             mre.printStackTrace();
             System.err.println("Initialization error: " + mre.toString());
-//            RGFInfo.append("\t*ERROR* initializing ["+mre+"]\n");
-        }
-        finally
-        {
-//            RGFInfo.dumpEnvironment();
-//            RGFInfo.endSection(strInfo);
         }
     }
 
     public BSFManager() {
         pcs = new PropertyChangeSupport(this);
             // handle logger
-        // logger = LogFactory.getLog(this.getClass().getName());
         logger = BSF_LogFactory.getLog(this.getClass().getName());
     }
 
@@ -233,8 +235,8 @@ public class BSFManager {
        "yyyy" a four digit year, "mm" a two digit month,
        "dd" a two digit day.
     *
-       <br>Example: &quot;<code>241.20061208</code>&quot;
-       stands for: BSF version <code>2.4.2</code> as of <code>2007-01-28</code>.
+       <br>Example: &quot;<code>250.20120129</code>&quot;
+       stands for: BSF version <code>2.5.0</code> as of <code>2012-01-29</code>.
     *
     *
      * @since 2006-01-17
@@ -658,6 +660,8 @@ public class BSFManager {
             int index, loops = 0;
 
             if (langval != null) {
+                ClassLoader tccl=Thread.currentThread().getContextClassLoader();    // rgf, 2009-09-10
+
                 while ((index = langval.indexOf(":", 0)) != -1) {
                     // Great. Multiple language engines registered
                     // for this extension.
@@ -671,18 +675,29 @@ public class BSFManager {
                     try {
                         engineName =
                             (String) registeredEngines.get(lang);
-                        // Class.forName(engineName);
-                        Thread.currentThread().getContextClassLoader().loadClass (engineName); // rgf, 2007-01-28
-                    } catch (ClassNotFoundException cnfe) { // 20070917: hmm, maybe defCL can load it ?
-                        try {
-                            definedClassLoader.loadClass(engineName);
-                        }
-                        catch (ClassNotFoundException cnfe2) {
-                            // Bummer.
-                            lang = langval;
-                            continue;
+
+                        boolean bTryDefinedClassLoader=false;
+                        if (tccl!=null)     // context CL available, try it first
+                        {
+                            try
+                            {
+                                tccl.loadClass (engineName);
+                            }
+                            catch (ClassNotFoundException cnfe)
+                            {
+                                bTryDefinedClassLoader=true;
+                            }
                         }
 
+                        if (bTryDefinedClassLoader || tccl==null)   // not found, try defined CL next
+                        {
+                            definedClassLoader.loadClass(engineName);
+                        }
+                    }
+                    catch (ClassNotFoundException cnfe2) {
+                        // Bummer.
+                        lang = langval;
+                        continue;
                     }
 
                     // Got past that? Good.
@@ -767,19 +782,22 @@ public class BSFManager {
         // except.
         try {
 
-            Class engineClass;
-            try {  // 20070917
-                engineClass = Thread.currentThread().getContextClassLoader().loadClass (engineClassName);
+            Class engineClass=null;
+
+            ClassLoader tccl=Thread.currentThread().getContextClassLoader();
+            if (tccl!=null) {
+                try {
+                    engineClass = tccl.loadClass (engineClassName);
+                }
+                catch (ClassNotFoundException cnfe)
+                {}
             }
-            catch (ClassNotFoundException cnfe) {
+
+            if (engineClass==null)      // not found, try the defined classLoader
+            {
                 engineClass = definedClassLoader.loadClass (engineClassName);
             }
 
-               // (classLoader == null)
-               //  ? // Class.forName(engineClassName)
-               //    Thread.currentThread().getContextClassLoader().loadClass (engineClassName) // rgf, 2007-01-28
-               //  : classLoader.loadClass(engineClassName);
-
             final BSFEngine engf = (BSFEngine) engineClass.newInstance();
             final BSFManager thisf = this;
             final String langf = lang;