Skip to content

Instantly share code, notes, and snippets.

@solomax
Created July 27, 2016 02:48
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save solomax/1783a36d678011c077100c2905a24a8c to your computer and use it in GitHub Desktop.
Save solomax/1783a36d678011c077100c2905a24a8c to your computer and use it in GitHub Desktop.
In-memory Jar Class Loader
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, 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.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.openmeetings.screen.webstart;
import java.io.ByteArrayInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.net.URL;
import java.net.URLClassLoader;
import java.net.URLConnection;
import java.net.URLStreamHandler;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.security.Provider;
import java.security.Security;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.jar.JarEntry;
import java.util.jar.JarInputStream;
public class Loader {
public static void main(String[] args) {
try {
final Map<String, byte[]> map = new HashMap<>();
try (JarInputStream is = new JarInputStream(CoreScreenShare.class.getResourceAsStream("/lib/bcprov-jdk15on-1.54.jar"))) {
JarEntry nextEntry = null;
while ((nextEntry = is.getNextJarEntry()) != null) {
final int est = (int) nextEntry.getSize();
byte[] data = new byte[est > 0 ? est : 1024];
int real = 0;
for (int r = is.read(data); r > 0; r = is.read(data, real, data.length - real)) {
if (data.length == (real += r)) {
data = Arrays.copyOf(data, data.length * 2);
}
}
if (real != data.length) {
data = Arrays.copyOf(data, real);
}
map.put("/" + nextEntry.getName(), data);
}
}
URL u = new URL("x-buffer", null, -1, "/", new URLStreamHandler() {
@Override
protected URLConnection openConnection(URL u) throws IOException {
final byte[] data = map.get(u.getFile());
if (data == null) {
throw new FileNotFoundException(u.getFile());
}
return new URLConnection(u) {
@Override
public void connect() throws IOException {
}
@Override
public InputStream getInputStream() throws IOException {
return new ByteArrayInputStream(data);
}
};
}
});
try (URLClassLoader cl = new URLClassLoader(new URL[] { u })) {
final Class<?> bcp = Class.forName("org.bouncycastle.jce.provider.BouncyCastleProvider", true, cl);
AccessController.doPrivileged(new PrivilegedAction<Void>() {
@Override
public Void run() {
try {
Security.addProvider((Provider) bcp.newInstance());
} catch (InstantiationException | IllegalAccessException e) {
e.printStackTrace();
}
return null;
}
});
//bcp.newInstance();
/*
Class<?> c = Class.forName(CoreScreenShare.class.getName(), true, cl);
@SuppressWarnings("unchecked")
Constructor<CoreScreenShare> con = (Constructor<CoreScreenShare>)c.getConstructor(String[].class);
con.newInstance(new Object[]{args});
*/
}
} catch (Exception e) {
e.printStackTrace();;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment