List of usage examples for java.lang ClassLoader getClass
@HotSpotIntrinsicCandidate public final native Class<?> getClass();
From source file:org.apache.zeppelin.spark.SparkInterpreter.java
@Override public void open() { // set properties and do login before creating any spark stuff for secured cluster if (isYarnMode()) { System.setProperty("SPARK_YARN_MODE", "true"); }/* w w w.j a v a 2 s . co m*/ if (getProperty().containsKey("spark.yarn.keytab") && getProperty().containsKey("spark.yarn.principal")) { try { String keytab = getProperty().getProperty("spark.yarn.keytab"); String principal = getProperty().getProperty("spark.yarn.principal"); UserGroupInformation.loginUserFromKeytab(principal, keytab); } catch (IOException e) { throw new RuntimeException("Can not pass kerberos authentication", e); } } conf = new SparkConf(); URL[] urls = getClassloaderUrls(); // Very nice discussion about how scala compiler handle classpath // https://groups.google.com/forum/#!topic/scala-user/MlVwo2xCCI0 /* * > val env = new nsc.Settings(errLogger) > env.usejavacp.value = true > val p = new * Interpreter(env) > p.setContextClassLoader > Alternatively you can set the class path through * nsc.Settings.classpath. * * >> val settings = new Settings() >> settings.usejavacp.value = true >> * settings.classpath.value += File.pathSeparator + >> System.getProperty("java.class.path") >> * val in = new Interpreter(settings) { >> override protected def parentClassLoader = * getClass.getClassLoader >> } >> in.setContextClassLoader() */ Settings settings = new Settings(); // process args String args = getProperty("args"); if (args == null) { args = ""; } String[] argsArray = args.split(" "); LinkedList<String> argList = new LinkedList<>(); for (String arg : argsArray) { argList.add(arg); } DepInterpreter depInterpreter = getDepInterpreter(); String depInterpreterClasspath = ""; if (depInterpreter != null) { SparkDependencyContext depc = depInterpreter.getDependencyContext(); if (depc != null) { List<File> files = depc.getFiles(); if (files != null) { for (File f : files) { if (depInterpreterClasspath.length() > 0) { depInterpreterClasspath += File.pathSeparator; } depInterpreterClasspath += f.getAbsolutePath(); } } } } if (Utils.isScala2_10()) { scala.collection.immutable.List<String> list = JavaConversions.asScalaBuffer(argList).toList(); Object sparkCommandLine = Utils.instantiateClass("org.apache.spark.repl.SparkCommandLine", new Class[] { scala.collection.immutable.List.class }, new Object[] { list }); settings = (Settings) Utils.invokeMethod(sparkCommandLine, "settings"); } else { String sparkReplClassDir = getProperty("spark.repl.classdir"); if (sparkReplClassDir == null) { sparkReplClassDir = System.getProperty("spark.repl.classdir"); } if (sparkReplClassDir == null) { sparkReplClassDir = System.getProperty("java.io.tmpdir"); } synchronized (sharedInterpreterLock) { if (outputDir == null) { outputDir = createTempDir(sparkReplClassDir); } } argList.add("-Yrepl-class-based"); argList.add("-Yrepl-outdir"); argList.add(outputDir.getAbsolutePath()); String classpath = ""; if (conf.contains("spark.jars")) { classpath = StringUtils.join(conf.get("spark.jars").split(","), File.separator); } if (!depInterpreterClasspath.isEmpty()) { if (!classpath.isEmpty()) { classpath += File.separator; } classpath += depInterpreterClasspath; } if (!classpath.isEmpty()) { argList.add("-classpath"); argList.add(classpath); } scala.collection.immutable.List<String> list = JavaConversions.asScalaBuffer(argList).toList(); settings.processArguments(list, true); } // set classpath for scala compiler PathSetting pathSettings = settings.classpath(); String classpath = ""; List<File> paths = currentClassPath(); for (File f : paths) { if (classpath.length() > 0) { classpath += File.pathSeparator; } classpath += f.getAbsolutePath(); } if (urls != null) { for (URL u : urls) { if (classpath.length() > 0) { classpath += File.pathSeparator; } classpath += u.getFile(); } } // add dependency from DepInterpreter if (classpath.length() > 0) { classpath += File.pathSeparator; } classpath += depInterpreterClasspath; // add dependency from local repo String localRepo = getProperty("zeppelin.interpreter.localRepo"); if (localRepo != null) { File localRepoDir = new File(localRepo); if (localRepoDir.exists()) { File[] files = localRepoDir.listFiles(); if (files != null) { for (File f : files) { if (classpath.length() > 0) { classpath += File.pathSeparator; } classpath += f.getAbsolutePath(); } } } } pathSettings.v_$eq(classpath); settings.scala$tools$nsc$settings$ScalaSettings$_setter_$classpath_$eq(pathSettings); // set classloader for scala compiler settings.explicitParentLoader_$eq(new Some<>(Thread.currentThread().getContextClassLoader())); BooleanSetting b = (BooleanSetting) settings.usejavacp(); b.v_$eq(true); settings.scala$tools$nsc$settings$StandardScalaSettings$_setter_$usejavacp_$eq(b); /* Required for scoped mode. * In scoped mode multiple scala compiler (repl) generates class in the same directory. * Class names is not randomly generated and look like '$line12.$read$$iw$$iw' * Therefore it's possible to generated class conflict(overwrite) with other repl generated * class. * * To prevent generated class name conflict, * change prefix of generated class name from each scala compiler (repl) instance. * * In Spark 2.x, REPL generated wrapper class name should compatible with the pattern * ^(\$line(?:\d+)\.\$read)(?:\$\$iw)+$ * * As hashCode() can return a negative integer value and the minus character '-' is invalid * in a package name we change it to a numeric value '0' which still conforms to the regexp. * */ System.setProperty("scala.repl.name.line", ("$line" + this.hashCode()).replace('-', '0')); // To prevent 'File name too long' error on some file system. MutableSettings.IntSetting numClassFileSetting = settings.maxClassfileName(); numClassFileSetting.v_$eq(128); settings.scala$tools$nsc$settings$ScalaSettings$_setter_$maxClassfileName_$eq(numClassFileSetting); synchronized (sharedInterpreterLock) { /* create scala repl */ if (printREPLOutput()) { this.interpreter = new SparkILoop((java.io.BufferedReader) null, new PrintWriter(out)); } else { this.interpreter = new SparkILoop((java.io.BufferedReader) null, new PrintWriter(Console.out(), false)); } interpreter.settings_$eq(settings); interpreter.createInterpreter(); intp = Utils.invokeMethod(interpreter, "intp"); Utils.invokeMethod(intp, "setContextClassLoader"); Utils.invokeMethod(intp, "initializeSynchronous"); if (Utils.isScala2_10()) { if (classOutputDir == null) { classOutputDir = settings.outputDirs().getSingleOutput().get(); } else { // change SparkIMain class output dir settings.outputDirs().setSingleOutput(classOutputDir); ClassLoader cl = (ClassLoader) Utils.invokeMethod(intp, "classLoader"); try { Field rootField = cl.getClass().getSuperclass().getDeclaredField("root"); rootField.setAccessible(true); rootField.set(cl, classOutputDir); } catch (NoSuchFieldException | IllegalAccessException e) { logger.error(e.getMessage(), e); } } } if (Utils.findClass("org.apache.spark.repl.SparkJLineCompletion", true) != null) { completer = Utils.instantiateClass("org.apache.spark.repl.SparkJLineCompletion", new Class[] { Utils.findClass("org.apache.spark.repl.SparkIMain") }, new Object[] { intp }); } else if (Utils.findClass("scala.tools.nsc.interpreter.PresentationCompilerCompleter", true) != null) { completer = Utils.instantiateClass("scala.tools.nsc.interpreter.PresentationCompilerCompleter", new Class[] { IMain.class }, new Object[] { intp }); } else if (Utils.findClass("scala.tools.nsc.interpreter.JLineCompletion", true) != null) { completer = Utils.instantiateClass("scala.tools.nsc.interpreter.JLineCompletion", new Class[] { IMain.class }, new Object[] { intp }); } if (Utils.isSpark2()) { sparkSession = getSparkSession(); } sc = getSparkContext(); if (sc.getPoolForName("fair").isEmpty()) { Value schedulingMode = org.apache.spark.scheduler.SchedulingMode.FAIR(); int minimumShare = 0; int weight = 1; Pool pool = new Pool("fair", schedulingMode, minimumShare, weight); sc.taskScheduler().rootPool().addSchedulable(pool); } sparkVersion = SparkVersion.fromVersionString(sc.version()); sqlc = getSQLContext(); dep = getDependencyResolver(); hooks = getInterpreterGroup().getInterpreterHookRegistry(); z = new ZeppelinContext(sc, sqlc, null, dep, hooks, Integer.parseInt(getProperty("zeppelin.spark.maxResult"))); interpret("@transient val _binder = new java.util.HashMap[String, Object]()"); Map<String, Object> binder; if (Utils.isScala2_10()) { binder = (Map<String, Object>) getValue("_binder"); } else { binder = (Map<String, Object>) getLastObject(); } binder.put("sc", sc); binder.put("sqlc", sqlc); binder.put("z", z); if (Utils.isSpark2()) { binder.put("spark", sparkSession); } interpret("@transient val z = " + "_binder.get(\"z\").asInstanceOf[org.apache.zeppelin.spark.ZeppelinContext]"); interpret("@transient val sc = " + "_binder.get(\"sc\").asInstanceOf[org.apache.spark.SparkContext]"); interpret("@transient val sqlc = " + "_binder.get(\"sqlc\").asInstanceOf[org.apache.spark.sql.SQLContext]"); interpret("@transient val sqlContext = " + "_binder.get(\"sqlc\").asInstanceOf[org.apache.spark.sql.SQLContext]"); if (Utils.isSpark2()) { interpret("@transient val spark = " + "_binder.get(\"spark\").asInstanceOf[org.apache.spark.sql.SparkSession]"); } interpret("import org.apache.spark.SparkContext._"); if (importImplicit()) { if (Utils.isSpark2()) { interpret("import spark.implicits._"); interpret("import spark.sql"); interpret("import org.apache.spark.sql.functions._"); } else { if (sparkVersion.oldSqlContextImplicits()) { interpret("import sqlContext._"); } else { interpret("import sqlContext.implicits._"); interpret("import sqlContext.sql"); interpret("import org.apache.spark.sql.functions._"); } } } } /* Temporary disabling DisplayUtils. see https://issues.apache.org/jira/browse/ZEPPELIN-127 * // Utility functions for display intp.interpret("import org.apache.zeppelin.spark.utils.DisplayUtils._"); // Scala implicit value for spark.maxResult intp.interpret("import org.apache.zeppelin.spark.utils.SparkMaxResult"); intp.interpret("implicit val sparkMaxResult = new SparkMaxResult(" + Integer.parseInt(getProperty("zeppelin.spark.maxResult")) + ")"); */ if (Utils.isScala2_10()) { try { if (sparkVersion.oldLoadFilesMethodName()) { Method loadFiles = this.interpreter.getClass().getMethod("loadFiles", Settings.class); loadFiles.invoke(this.interpreter, settings); } else { Method loadFiles = this.interpreter.getClass() .getMethod("org$apache$spark$repl$SparkILoop$$loadFiles", Settings.class); loadFiles.invoke(this.interpreter, settings); } } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { throw new InterpreterException(e); } } // add jar from DepInterpreter if (depInterpreter != null) { SparkDependencyContext depc = depInterpreter.getDependencyContext(); if (depc != null) { List<File> files = depc.getFilesDist(); if (files != null) { for (File f : files) { if (f.getName().toLowerCase().endsWith(".jar")) { sc.addJar(f.getAbsolutePath()); logger.info("sc.addJar(" + f.getAbsolutePath() + ")"); } else { sc.addFile(f.getAbsolutePath()); logger.info("sc.addFile(" + f.getAbsolutePath() + ")"); } } } } } // add jar from local repo if (localRepo != null) { File localRepoDir = new File(localRepo); if (localRepoDir.exists()) { File[] files = localRepoDir.listFiles(); if (files != null) { for (File f : files) { if (f.getName().toLowerCase().endsWith(".jar")) { sc.addJar(f.getAbsolutePath()); logger.info("sc.addJar(" + f.getAbsolutePath() + ")"); } else { sc.addFile(f.getAbsolutePath()); logger.info("sc.addFile(" + f.getAbsolutePath() + ")"); } } } } } numReferenceOfSparkContext.incrementAndGet(); }
From source file:org.carracoo.maven.assist.AssistProcessor.java
private void debugClassLoader(final ClassPool classPool) { if (!logger.isLoggable(Level.INFO)) { return;//from w w w. jav a2 s .c om } logger.log(Level.FINE, String.format(" - classPool: %s", classPool.toString())); ClassLoader classLoader = classPool.getClassLoader(); while (classLoader != null) { logger.log(Level.INFO, String.format(" -- %s : %s", classLoader.getClass().getName(), classLoader.toString())); if (classLoader instanceof URLClassLoader) { logger.log(Level.INFO, String.format(" --- urls: %s ", Arrays.deepToString(((URLClassLoader) classLoader).getURLs()))); } classLoader = classLoader.getParent(); } }
From source file:org.dhatim.classpath.Scanner.java
public void scanClasspath(ClassLoader classLoader) throws IOException { if (!(classLoader instanceof URLClassLoader)) { logger.warn("Not scanning classpath for ClassLoader '" + classLoader.getClass().getName() + "'. ClassLoader must implement '" + URLClassLoader.class.getName() + "'."); return;//from ww w. ja v a 2 s . c o m } URL[] urls = ((URLClassLoader) classLoader).getURLs(); Set<String> alreadyScanned = new HashSet<String>(); for (URL url : urls) { String urlPath = url.getFile(); urlPath = URLDecoder.decode(urlPath, "UTF-8"); if (urlPath.startsWith("file:")) { urlPath = urlPath.substring(5); } if (urlPath.indexOf('!') > 0) { urlPath = urlPath.substring(0, urlPath.indexOf('!')); } File file = new File(urlPath); if (alreadyScanned.contains(file.getAbsolutePath())) { logger.debug("Ignoring classpath URL '" + file.getAbsolutePath() + "'. Already scanned this URL."); continue; } if (file.isDirectory()) { handleDirectory(file, null); } else { handleArchive(file); } alreadyScanned.add(file.getAbsolutePath()); } }
From source file:org.ebayopensource.turmeric.plugins.maven.resources.ResourceLocator.java
private Location lookInClasspath(String pathref) throws MojoExecutionException { log.debug("Looking for resource in project classpath: " + pathref); if (log.isDebugEnabled()) { StringBuilder dbg = new StringBuilder(); dbg.append("System.getProperty('java.class.path')="); String rawcp = System.getProperty("java.class.path"); for (String cp : rawcp.split(File.pathSeparator)) { dbg.append("\n ").append(cp); }//ww w. java 2 s. c o m log.debug(dbg.toString()); ClassLoader cl = this.getClass().getClassLoader(); if (cl instanceof URLClassLoader) { dbg = new StringBuilder(); dbg.append("URLClassLoader("); dbg.append(cl.getClass().getName()); dbg.append("):"); URLClassLoader ucl = (URLClassLoader) cl; for (URL url : ucl.getURLs()) { dbg.append("\n ").append(url.toExternalForm()); } log.debug(dbg.toString()); } } List<URL> resources = new ArrayList<URL>(); try { Enumeration<URL> enurls = ClassLoader.getSystemResources(pathref); if (enurls != null) { while (enurls.hasMoreElements()) { URL url = enurls.nextElement(); if (!resources.contains(url)) { resources.add(url); } } } addFoundResource(resources, pathref, Thread.currentThread().getContextClassLoader()); addFoundResource(resources, pathref, this.getClass().getClassLoader()); if (resources.isEmpty()) { log.debug("NOT FOUND in project classpath"); return null; } if (resources.size() > 1) { log.warn("Found more than 1 classpath entry for: " + pathref); for (URL url : resources) { log.warn(" + " + url.toExternalForm()); } } URI uri = resources.get(0).toURI(); log.debug("FOUND resource in project classpath: " + uri); return new Location(uri, project); } catch (IOException e) { throw new MojoExecutionException( "Unable to process resource lookup in project classpath: " + e.getMessage(), e); } catch (URISyntaxException e) { throw new MojoExecutionException( "Unable to process resource lookup in project classpath: " + e.getMessage(), e); } }
From source file:org.ebayopensource.turmeric.tools.codegen.AbstractServiceGeneratorTestCase.java
private void dumpClassLoader(String indent, ClassLoader cl) { if (cl == null) { return;/*w ww . j av a 2 s . c om*/ } System.out.printf("%sClassLoader: %s: %s%n", indent, cl.getClass().getName(), cl.toString()); if (cl instanceof URLClassLoader) { URLClassLoader ucl = (URLClassLoader) cl; System.out.printf("%s(URLClassLoader)%n", indent); URL urls[] = ucl.getURLs(); for (URL url : urls) { System.out.printf("%s* %s%n", indent, url); } } ClassLoader parent = cl.getParent(); dumpClassLoader(indent + " ", parent); }
From source file:org.eclipse.ecr.testlib.NXRuntimeTestCase.java
protected void initUrls() throws Exception { ClassLoader classLoader = NXRuntimeTestCase.class.getClassLoader(); if (classLoader instanceof URLClassLoader) { urls = ((URLClassLoader) classLoader).getURLs(); } else if (classLoader.getClass().getName().equals("org.apache.tools.ant.AntClassLoader")) { Method method = classLoader.getClass().getMethod("getClasspath"); String cp = (String) method.invoke(classLoader); String[] paths = cp.split(File.pathSeparator); urls = new URL[paths.length]; for (int i = 0; i < paths.length; i++) { urls[i] = new URL("file:" + paths[i]); }/*from w w w .j av a 2s .co m*/ } else { log.warn("Unknow classloader type: " + classLoader.getClass().getName() + "\nWon't be able to load OSGI bundles"); return; } // special case for maven surefire with useManifestOnlyJar if (urls.length == 1) { try { URI uri = urls[0].toURI(); if (uri.getScheme().equals("file") && uri.getPath().contains("surefirebooter")) { JarFile jar = new JarFile(new File(uri)); try { String cp = jar.getManifest().getMainAttributes().getValue(Attributes.Name.CLASS_PATH); if (cp != null) { String[] cpe = cp.split(" "); URL[] newUrls = new URL[cpe.length]; for (int i = 0; i < cpe.length; i++) { // Don't need to add 'file:' with maven surefire // >= 2.4.2 String newUrl = cpe[i].startsWith("file:") ? cpe[i] : "file:" + cpe[i]; newUrls[i] = new URL(newUrl); } urls = newUrls; } } finally { jar.close(); } } } catch (Exception e) { // skip } } StringBuilder sb = new StringBuilder(); sb.append("URLs on the classpath: "); for (URL url : urls) { sb.append(url.toString()); sb.append('\n'); } log.debug(sb.toString()); readUris = new HashSet<URI>(); bundles = new HashMap<String, BundleFile>(); }
From source file:org.eclipse.equinox.http.servlet.tests.ServletTest.java
public void test_RegistrationTCCL1() { final Set<String> filterTCCL = Collections.synchronizedSet(new HashSet<String>()); final Set<String> servletTCCL = Collections.synchronizedSet(new HashSet<String>()); Filter tcclFilter = new Filter() { @Override//from w ww . j av a 2s. co m public void init(FilterConfig filterConfig) throws ServletException { filterTCCL.add(Thread.currentThread().getContextClassLoader().getClass().getName()); } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { filterTCCL.add(Thread.currentThread().getContextClassLoader().getClass().getName()); chain.doFilter(request, response); } @Override public void destroy() { filterTCCL.add(Thread.currentThread().getContextClassLoader().getClass().getName()); } }; HttpServlet tcclServlet = new HttpServlet() { @Override public void destroy() { super.destroy(); servletTCCL.add(Thread.currentThread().getContextClassLoader().getClass().getName()); } @Override public void init(ServletConfig config) throws ServletException { super.init(config); servletTCCL.add(Thread.currentThread().getContextClassLoader().getClass().getName()); } private static final long serialVersionUID = 1L; @Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { servletTCCL.add(Thread.currentThread().getContextClassLoader().getClass().getName()); response.getWriter().print(Thread.currentThread().getContextClassLoader().getClass().getName()); } }; ClassLoader originalTCCL = Thread.currentThread().getContextClassLoader(); ClassLoader dummy = new ClassLoader() { }; Thread.currentThread().setContextClassLoader(dummy); String expected = dummy.getClass().getName(); String actual = null; ExtendedHttpService extendedHttpService = (ExtendedHttpService) getHttpService(); try { extendedHttpService.registerFilter("/tccl", tcclFilter, null, null); extendedHttpService.registerServlet("/tccl", tcclServlet, null, null); actual = requestAdvisor.request("tccl"); } catch (Exception e) { fail("Unexpected exception: " + e); } finally { Thread.currentThread().setContextClassLoader(originalTCCL); try { extendedHttpService.unregister("/tccl"); extendedHttpService.unregisterFilter(tcclFilter); } catch (IllegalArgumentException e) { // ignore } } assertEquals(expected, actual); assertEquals("Wrong filterTCCL size: " + filterTCCL, 1, filterTCCL.size()); assertTrue("Wrong filterTCCL: " + filterTCCL, filterTCCL.contains(expected)); assertEquals("Wrong httpTCCL size: " + servletTCCL, 1, servletTCCL.size()); assertTrue("Wrong servletTCCL: " + servletTCCL, servletTCCL.contains(expected)); }
From source file:org.eclipse.mylyn.commons.sdk.util.CommonTestUtil.java
private static boolean isOsgiVersion310orNewer(ClassLoader classLoader) { return classLoader.getClass().getName().equals("org.eclipse.osgi.internal.loader.ModuleClassLoader") // user before 4.4M4 || classLoader.getClass().getName().equals("org.eclipse.osgi.internal.loader.EquinoxClassLoader"); }
From source file:org.fuin.utils4j.Utils4JTest.java
/** * @testng.test/* w ww. j a va 2s . c o m*/ */ public final void testAddToClasspathString() throws MalformedURLException { final ClassLoader classLoader = Utils4J.class.getClassLoader(); if (!(classLoader instanceof URLClassLoader)) { throw new IllegalStateException( "Classloader is not an URL classloader! [" + classLoader.getClass().getName() + "]"); } final URLClassLoader urlClassLoader = (URLClassLoader) classLoader; final URL url = new URL("file:/test1.jar"); Assert.assertFalse(Utils4J.containsURL(urlClassLoader.getURLs(), url)); Utils4J.addToClasspath(url.toExternalForm()); Assert.assertTrue(Utils4J.containsURL(urlClassLoader.getURLs(), url)); }
From source file:org.fuin.utils4j.Utils4JTest.java
/** * @testng.test/*from www . ja v a 2s . c om*/ */ public final void testAddToClasspathURL() throws MalformedURLException { final ClassLoader classLoader = Utils4J.class.getClassLoader(); if (!(classLoader instanceof URLClassLoader)) { throw new IllegalStateException( "Classloader is not an URL classloader! [" + classLoader.getClass().getName() + "]"); } final URLClassLoader urlClassLoader = (URLClassLoader) classLoader; final URL url = new URL("file:/test2.jar"); Assert.assertFalse(Utils4J.containsURL(urlClassLoader.getURLs(), url)); Utils4J.addToClasspath(url); Assert.assertTrue(Utils4J.containsURL(urlClassLoader.getURLs(), url)); }