Skip to content

Commit

Permalink
Cleanup and doc
Browse files Browse the repository at this point in the history
  • Loading branch information
Thrameos committed Nov 30, 2024
1 parent 3a5d118 commit e95010f
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 32 deletions.
39 changes: 34 additions & 5 deletions native/java/org/jpype/JPypeClassLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,13 @@
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.net.URLDecoder;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
* Class loader for JPype.
*
* This is augmented to manage directory resources, allow for late loading,
* and handling of resources on non-ASCII paths.
*/
public class JPypeClassLoader extends URLClassLoader
{

Expand All @@ -42,6 +46,11 @@ public JPypeClassLoader(ClassLoader parent)
super(initial(), parent);
}

/**
* Used to keep the cache up to date.
*
* @return
*/
public int getCode()
{
return code;
Expand All @@ -58,13 +67,14 @@ public int getCode()
*/
private static URL[] initial()
{
// Check to see if we have a late loaded path
String cp = System.getProperty("jpype.class.path");
if (cp == null)
return new URL[0];

try
{
cp = URLDecoder.decode(cp, "UTF-8");
cp = URLDecoder.decode(cp, "UTF-8");
} catch (UnsupportedEncodingException ex)
{
// ignored
Expand Down Expand Up @@ -100,7 +110,7 @@ private static URL[] initial()
return path.toArray(new URL[0]);
}

// this is required to add a Java agent even if it is already in the path
// This is required to add a Java agent even if it is already in the path
@SuppressWarnings("unused")
private void appendToClassPathForInstrumentation(String path) throws Throwable
{
Expand Down Expand Up @@ -143,6 +153,12 @@ public FileVisitResult visitFileFailed(Path file, IOException exc)

}

/**
* Add a path to the loader after the JVM is started.
*
* @param path
* @throws FileNotFoundException
*/
public void addPath(Path path) throws FileNotFoundException
{
try
Expand Down Expand Up @@ -229,6 +245,15 @@ public Enumeration<URL> findResources(String name) throws IOException
return Collections.enumeration(out);
}

/**
* Add a resource to the search.
*
* Many jar files lack directory support which is needed for the packaging
* import.
*
* @param name
* @param url
*/
public void addResource(String name, URL url)
{
if (!this.map.containsKey(name))
Expand All @@ -241,7 +266,11 @@ public void addURL(URL url)
{
// Mark our cache as dirty
code = code * 98745623 + url.hashCode();

// add to the search tree
super.addURL(url);

// See if it is a path
Path path;
try
{
Expand Down
61 changes: 34 additions & 27 deletions native/java/org/jpype/JPypeContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,39 +95,46 @@ static public JPypeContext getInstance()
* @param loader is the classloader holding JPype resources.
* @return the created context.
*/
static JPypeContext createContext(long context, ClassLoader loader, String nativeLib, boolean interrupt)
private static JPypeContext createContext(long context, ClassLoader loader, String nativeLib, boolean interrupt) throws Throwable
{
if (nativeLib != null)
{
System.load(nativeLib);
}
INSTANCE.context = context;
INSTANCE.classLoader = (JPypeClassLoader) loader;
INSTANCE.typeFactory = new TypeFactoryNative();
INSTANCE.typeManager = new TypeManager(context, INSTANCE.typeFactory);
INSTANCE.initialize(interrupt);

try
{
INSTANCE.reflector = (JPypeReflector) Class.forName("org.jpype.Reflector0", true, loader)
.getConstructor()
.newInstance();
} catch (ClassNotFoundException | NoSuchMethodException | SecurityException
| InstantiationException | IllegalAccessException
| IllegalArgumentException | InvocationTargetException ex)
if (nativeLib != null)
{
System.load(nativeLib);
}
INSTANCE.context = context;
INSTANCE.classLoader = (JPypeClassLoader) loader;
INSTANCE.typeFactory = new TypeFactoryNative();
INSTANCE.typeManager = new TypeManager(context, INSTANCE.typeFactory);
INSTANCE.initialize(interrupt);

try
{
INSTANCE.reflector = (JPypeReflector) Class.forName("org.jpype.Reflector0", true, loader)
.getConstructor()
.newInstance();
} catch (ClassNotFoundException | NoSuchMethodException | SecurityException
| InstantiationException | IllegalAccessException
| IllegalArgumentException | InvocationTargetException ex)
{
throw new RuntimeException("Unable to create reflector "+ ex.getMessage(), ex);
}

scanExistingJars();
return INSTANCE;
} catch (Throwable ex)
{
System.err.println("Unable to create reflector "+ ex);
ex.printStackTrace(System.err);
throw ex;
}

scanExistingJars();
return INSTANCE;
}

private JPypeContext()
{
}

void initialize(boolean interrupt)
private void initialize(boolean interrupt)
{
// Okay everything is setup so lets give it a go.
this.typeManager.init();
Expand Down Expand Up @@ -278,7 +285,7 @@ private void shutdown()

}

static native void onShutdown(long ctxt);
private static native void onShutdown(long ctxt);

public void addShutdownHook(Thread th)
{
Expand Down Expand Up @@ -331,7 +338,7 @@ public void _addPost(Runnable run)
{
this.postHooks.add(run);
}

/**
* Helper function for collect rectangular,
*/
Expand Down Expand Up @@ -426,7 +433,7 @@ private Object unpack(int size, Object parts)
return a1;
}

public Object assemble(int[] dims, Object parts)
private Object assemble(int[] dims, Object parts)
{
int n = dims.length;
if (n == 1)
Expand Down Expand Up @@ -497,12 +504,12 @@ public long getExcValue(Throwable th)
return 0;
}

public Exception createException(long l0, long l1)
private Exception createException(long l0, long l1)
{
return new PyExceptionProxy(l0, l1);
}

public boolean order(Buffer b)
private boolean order(Buffer b)
{
if (b instanceof java.nio.ByteBuffer)
return ((java.nio.ByteBuffer) b).order() == ByteOrder.LITTLE_ENDIAN;
Expand Down

0 comments on commit e95010f

Please sign in to comment.