jyonkheel jyonkheel - 3 months ago 43
Java Question

jetty set resource base within an external java lib (a.k.a jar file) while in Android

I had success deploying jetty 7 embedded server on Android the problem is i cannot set it to access folder within my jar file (jar file runs perfectly on PC and has self contained dependencies, which is added on android as an external lib). i wish to set this folder as a resourceBase for my embedded web app

here is the full code

System.out.println(" server init attempt on port:"+curPort);
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
Server server = new Server(curPort);
server.setHandler(context);
context.setContextPath("/");
System.out.println(" servlet main initing");
DSSServer servlet = new DSSServer();
context.addServlet(new ServletHolder(servlet), "/DSS");
ResourceHandler res = new ResourceHandler();
System.out.println(" getting resBase:");
//[code above works fine]

//[!ATTENTION HERE!]
String resDir = "/dss_core/HTML5/webapp";
//This runs on PC fine but creates a null pointer exception in Android
String resurl = DSSServer.class.getResource(resDir).toExternalForm();
//Here is another non-working varient
//String resurl = servlet.getClass().getClassLoader().getResource(resDir).toExternalForm();

//[code below works fine]
res.setResourceBase(resurl);
res.setDirectoriesListed(true);
context.setHandler(res);
System.out.println(" servlet main inited");


this is the code block that makes the null pointer exception

String resDir = "/dss_core/HTML5/webapp";
//This runs on PC fine but creates a null pointer exception in Android
String resurl = DSSServer.class.getResource(resDir).toExternalForm();
//Here is another non-working variant
//String resurl = servlet.getClass().getClassLoader().getResource(resDir).toExternalForm();


what is the correct way to access a external java library resource folder in Android, i know Android has it's own resource management system, but i cannot put native Android code in my external java lib as it is a jar file specifically for PC.

an alternative method i may do is extract my resource base to a local folder, but i'd rather not do that as it leaves tons of temporary files

BTW Android has no problem accessing one file at a time from inside my java lib

//Accessing individual files work on Android
InputStream input = DatabaseManager.class.getResourceAsStream("/h2db.mv.db");

Answer

it appears such is not possible, i ended up extracting the files somewhere in Android (like /mnt/sdcard/.myApp/temp) and manually copying each and every single file there

String resDir = "/dss_core/HTML5/webapp";
String resurl = dss_core.util.DRYcode.platformIsAndroid()?"file:/mnt/sdcard/protected/"+servletName:
                DRYcodeWeb.class.getResource(resDir).toString();
if (dss_core.util.DRYcode.platformIsAndroid())
    for (String str:resourceList)
        DRYcode.copy_("/dss_core/HTML5/webapp/"+str,"/mnt/sdcard/protected/"+servletName+"/"+str);