org.beigesoft.android.ajetty.WebAppClassLoaderAndroid Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of a-jetty Show documentation
Show all versions of a-jetty Show documentation
A-Jetty for Android. It is Jetty 9.2 adapted for Android
configured as minimum server with WebAppDeployer that can deploy ordinal WAR (JSP/JSTL must be pre-compiled into servlets by A-Tomcat).
It's only for tests purposes. It doesn't comply to the latest Android policy (loading executable binaries from outside)!
package org.beigesoft.android.ajetty;
/*
* Copyright (c) 2016 Beigesoft ™
*
* Licensed under the GNU General Public License (GPL), Version 2.0
* (the "License");
* you may not use this file except in compliance with the License.
*
* You may obtain a copy of the License at
*
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html
*/
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.Enumeration;
import dalvik.system.DexClassLoader;
import org.eclipse.jetty.webapp.WebAppClassLoader;
import org.eclipse.jetty.webapp.WebAppContext;
import org.beigesoft.lang.IUrlClassLoader;
/**
* WebAppClassLoader for Android.
*
* @author Yury Demidenko
*/
public class WebAppClassLoaderAndroid implements IUrlClassLoader {
/**
* DexClassLoader.
**/
private DexClassLoader delegate = null;
/**
* WebApp Context.
**/
private WebAppContext context;
/**
* dexPath for DexClassLoader.
**/
private String dexPath = "";
/**
* optimizedDirectory for DexClassLoader.
**/
private String optimizedDirectory;
/**
* Only constructor.
* @param pContext WebAppClassLoader.Context
* @throws Exception an Exception
**/
public WebAppClassLoaderAndroid(
final WebAppClassLoader.Context pContext) throws Exception {
context = (WebAppContext) pContext;
}
/**
* Initialize DexClassLoader.
* @throws Exception an Exception
**/
public final void init() throws Exception {
String libPath = context.getWar() + File.separator + "WEB-INF/lib/";
File dirLib = new File(libPath);
addJars(dirLib);
String classesPath = context.getWar() + File.separator + "WEB-INF/classes/";
File dirClasses = new File(classesPath);
addJars(dirClasses);
this.delegate = new DexClassLoader(this.dexPath, this.optimizedDirectory,
null, WebAppClassLoaderAndroid.class.getClassLoader());
}
/**
* Add jars into dexPath for DexClassLoader.
* @param pLib directory
* @throws Exception an Exception
**/
public final void addJars(final File pLib) throws Exception {
if (pLib.exists() && pLib.isDirectory()) {
File[] files = pLib.listFiles();
for (int i = 0; files != null && i < files.length; i++) {
String fname = files[i].getName().toLowerCase();
if (!files[i].isDirectory() && fname.endsWith(".jar")) {
String jar = files[i].getAbsolutePath();
if ("".equals(this.dexPath)) {
this.dexPath = jar;
} else {
this.dexPath += File.pathSeparator + jar;
}
}
}
}
}
/**
* Just stub for find class.
* @param pName name
* @return Class> class
* @throws ClassNotFoundException always
**/
@Override
public final Class> findClass(
final String pName) throws ClassNotFoundException {
throw new ClassNotFoundException();
}
/**
* Just stub for add URL.
* @param pUrl URL
**/
@Override
public final void addUrl(final URL pUrl) {
// nothing
}
/**
* Just stub for get resources.
* @param pName name
* @return Enumeration URL set
* @throws IOException always
**/
@Override
public final Enumeration getResources(
final String pName) throws IOException {
throw new IOException();
}
/**
* Just stub for get resource.
* @param pName name
* @return URL URL
**/
@Override
public final URL getResource(final String pName) {
return null;
}
/**
* Load classr.
* @param pName name
* @return Class> class
* @throws ClassNotFoundException if not found
**/
@Override
public final Class> loadClass(
final String pName) throws ClassNotFoundException {
return this.delegate.loadClass(pName);
}
//Simple getters and setters:
/**
* Getter for delegate.
* @return DexClassLoader
**/
public final DexClassLoader getDelegate() {
return this.delegate;
}
/**
* Setter for delegate.
* @param pDelegate reference
**/
public final void setDelegate(final DexClassLoader pDelegate) {
this.delegate = pDelegate;
}
/**
* Getter for dexPath.
* @return String
**/
public final String getDexPath() {
return this.dexPath;
}
/**
* Setter for dexPath.
* @param pDexPath reference
**/
public final void setDexPath(final String pDexPath) {
this.dexPath = pDexPath;
}
/**
* Getter for optimizedDirectory.
* @return String
**/
public final String getOptimizedDirectory() {
return this.optimizedDirectory;
}
/**
* Setter for optimizedDirectory.
* @param pOptimizedDirectory reference
**/
public final void setOptimizedDirectory(final String pOptimizedDirectory) {
this.optimizedDirectory = pOptimizedDirectory;
}
}