use of org.apache.karaf.shell.impl.console.loader.JarInJarURLStreamHandlerFactory in project karaf by apache.
the class Main method run.
private void run(ThreadIO threadio, String[] args, InputStream in, PrintStream out, PrintStream err) throws Exception {
StringBuilder sb = new StringBuilder();
String classpath = null;
boolean batch = false;
String file = null;
for (int i = 0; i < args.length; i++) {
String arg = args[i];
if (arg.startsWith("--classpath=")) {
classpath = arg.substring("--classpath=".length());
} else if (arg.startsWith("-c=")) {
classpath = arg.substring("-c=".length());
} else if (arg.equals("--classpath") || arg.equals("-c")) {
classpath = args[++i];
} else if (arg.equals("-b") || arg.equals("--batch")) {
batch = true;
} else if (arg.startsWith("--file=")) {
file = arg.substring("--file=".length());
} else if (arg.startsWith("-f=")) {
file = arg.substring("-f=".length());
} else if (arg.equals("--file") || arg.equals("-f")) {
file = args[++i];
} else {
sb.append(arg);
sb.append(' ');
}
}
if (file != null) {
try (Reader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)))) {
sb.setLength(0);
for (int c = reader.read(); c >= 0; c = reader.read()) {
sb.append((char) c);
}
}
} else if (batch) {
Reader reader = new BufferedReader(new InputStreamReader(System.in));
sb.setLength(0);
for (int c = reader.read(); c >= 0; reader.read()) {
sb.append((char) c);
}
}
ClassLoader cl = Main.class.getClassLoader();
if (classpath != null) {
List<URL> urls = getFiles(new File(classpath));
// Load jars in class path to be able to load jars inside them
ClassLoader tmpClassLoader = new URLClassLoader(urls.toArray(new URL[urls.size()]), cl);
URL.setURLStreamHandlerFactory(new JarInJarURLStreamHandlerFactory(tmpClassLoader));
List<URL> jarsInJars = getJarsInJars(urls);
// Create ClassLoader with jars in jars and parent main ClassLoader
cl = new URLClassLoader(jarsInJars.toArray(new URL[jarsInJars.size()]), cl);
// Load original Jars with jarsInJars ClassLoader as parent.
// This is needed cause if you load Class from main jar which depends on class in inner jar.
// The loaded class has its class loader and resolve dependant classes in its or parent classloaders
// which exclude jarInJar classloader.
cl = new URLClassLoader(urls.toArray(new URL[urls.size()]), cl);
}
SessionFactory sessionFactory = createSessionFactory(threadio);
run(sessionFactory, sb.toString(), in, out, err, cl);
}
Aggregations