Search in sources :

Example 1 with JarInJarURLStreamHandlerFactory

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);
}
Also used : SessionFactory(org.apache.karaf.shell.api.console.SessionFactory) InputStreamReader(java.io.InputStreamReader) JarInJarURLStreamHandlerFactory(org.apache.karaf.shell.impl.console.loader.JarInJarURLStreamHandlerFactory) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) FileInputStream(java.io.FileInputStream) URL(java.net.URL) URLClassLoader(java.net.URLClassLoader) BufferedReader(java.io.BufferedReader) URLClassLoader(java.net.URLClassLoader) JarFile(java.util.jar.JarFile) File(java.io.File)

Aggregations

BufferedReader (java.io.BufferedReader)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 URL (java.net.URL)1 URLClassLoader (java.net.URLClassLoader)1 JarFile (java.util.jar.JarFile)1 SessionFactory (org.apache.karaf.shell.api.console.SessionFactory)1 JarInJarURLStreamHandlerFactory (org.apache.karaf.shell.impl.console.loader.JarInJarURLStreamHandlerFactory)1