Просмотр исходного кода

- add support to add external ClassLoaders to BinaryImporter for loading classes

git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@7559 75d07b2b-3a1a-0410-a2c5-0572b91ccdca
nor..67 14 лет назад
Родитель
Сommit
e9fb72dcf2

+ 35 - 0
engine/src/core-plugins/com/jme3/export/binary/BinaryClassLoader.java

@@ -37,6 +37,8 @@ import java.util.logging.Logger;
 
 import com.jme3.export.InputCapsule;
 import com.jme3.export.Savable;
+import java.util.Iterator;
+import java.util.List;
 
 /**
  * This class is mis-named and is located in an inappropriate package:
@@ -80,4 +82,37 @@ public class BinaryClassLoader {
         }
     }
 
+    public static Savable fromName(String className, InputCapsule inputCapsule, List<ClassLoader> loaders) throws InstantiationException, 
+        IllegalAccessException, ClassNotFoundException, IOException {
+        if(loaders == null){
+            return fromName(className, inputCapsule);
+        }
+        for (Iterator<ClassLoader> it = loaders.iterator(); it.hasNext();) {
+            ClassLoader classLoader = it.next();
+            try {
+                return (Savable)classLoader.loadClass(className).newInstance();
+            }
+            catch (InstantiationException e) {
+            }
+            catch (IllegalAccessException e) {
+            }
+            
+        }
+        
+        try {
+            return (Savable)Class.forName(className).newInstance();
+        }
+        catch (InstantiationException e) {
+        	Logger.getLogger(BinaryClassLoader.class.getName()).severe(
+        			"Could not access constructor of class '" + className + "'! \n" +
+        			"Some types need to have the BinaryImporter set up in a special way. Please doublecheck the setup.");
+        	throw e;
+        }
+        catch (IllegalAccessException e) {
+        	Logger.getLogger(BinaryClassLoader.class.getName()).severe(
+        			e.getMessage() + " \n" +
+                    "Some types need to have the BinaryImporter set up in a special way. Please doublecheck the setup.");
+        	throw e;
+        }
+    }
 }

+ 12 - 2
engine/src/core-plugins/com/jme3/export/binary/BinaryImporter.java

@@ -48,8 +48,10 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.net.URL;
 import java.nio.ByteOrder;
+import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.IdentityHashMap;
+import java.util.List;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
@@ -81,14 +83,22 @@ public final class BinaryImporter implements JmeImporter {
     private int aliasWidth;
 
     private static final boolean fastRead = ByteOrder.nativeOrder() == ByteOrder.LITTLE_ENDIAN;
+    
+    private List<ClassLoader> loaders;
 
     public BinaryImporter() {
     }
-
+    
     public static boolean canUseFastBuffers(){
         return fastRead;
     }
 
+    public void addClassLoader(ClassLoader loader){
+        if(loaders == null)
+            loaders = new ArrayList<ClassLoader>();
+        loaders.add(loader);
+    }
+
     public static BinaryImporter getInstance() {
         return new BinaryImporter();
     }
@@ -284,7 +294,7 @@ public final class BinaryImporter implements JmeImporter {
             BinaryInputCapsule cap = new BinaryInputCapsule(this, bco);
             cap.setContent(dataArray, loc, loc+dataLength);
 
-            Savable out = BinaryClassLoader.fromName(bco.className, cap);
+            Savable out = BinaryClassLoader.fromName(bco.className, cap, loaders);
 
             capsuleTable.put(out, cap);
             contentTable.put(id, out);