List of usage examples for java.net URLClassLoader newInstance
public static URLClassLoader newInstance(final URL[] urls, final ClassLoader parent)
From source file:tvbrowser.core.PluginLoader.java
private Object loadJavaPlugin(File jarFile) throws TvBrowserException { Object plugin = null;/*from w w w .j a v a2 s.c o m*/ // Create a class loader for the plugin ClassLoader classLoader; ClassLoader classLoader2 = null; try { URL[] urls = new URL[] { jarFile.toURI().toURL() }; classLoader = URLClassLoader.newInstance(urls, ClassLoader.getSystemClassLoader()); try { if (!new File(PLUGIN_DIRECTORY).equals(jarFile.getParentFile()) && new File(PLUGIN_DIRECTORY, jarFile.getName()).isFile()) { urls = new URL[] { new File(PLUGIN_DIRECTORY, jarFile.getName()).toURI().toURL() }; classLoader2 = URLClassLoader.newInstance(urls, ClassLoader.getSystemClassLoader()); } } catch (MalformedURLException exc) { } } catch (MalformedURLException exc) { throw new TvBrowserException(getClass(), "error.1", "Loading Jar file of a plugin failed: {0}.", jarFile.getAbsolutePath(), exc); } // Get the plugin name String pluginName = jarFile.getName(); if (pluginName.endsWith(".jar")) { pluginName = pluginName.substring(0, pluginName.length() - 4); } boolean isBlockedDataService = false; // Create a plugin instance try { Class pluginClass = classLoader.loadClass(pluginName.toLowerCase() + "." + pluginName); Method getVersion = pluginClass.getMethod("getVersion", new Class[0]); Version version1 = null; try { version1 = (Version) getVersion.invoke(pluginClass, new Object[0]); } catch (Exception e) { } if (version1 == null || version1.toString().equals("0.0.0.0")) { mLog.warning("Did not load plugin " + pluginName + ", version is too old."); return null; } if (pluginClass.getSuperclass().equals(devplugin.AbstractTvDataService.class) || classLoader2 != null) { getVersion = pluginClass.getMethod("getVersion", new Class[0]); version1 = (Version) getVersion.invoke(pluginClass, new Object[0]); if (pluginClass.getSuperclass().equals(devplugin.AbstractTvDataService.class)) { isBlockedDataService = Settings.propBlockedPluginArray .isBlocked(pluginName.toLowerCase() + "." + pluginName, version1); } } if (classLoader2 != null) { try { Class pluginClass2 = classLoader2.loadClass(pluginName.toLowerCase() + "." + pluginName); Method getVersion2 = pluginClass2.getMethod("getVersion", new Class[0]); Version version2 = (Version) getVersion2.invoke(pluginClass2, new Object[0]); if (version2.compareTo(version1) > 0) { return null; } } catch (Throwable t) { } } try { Method preInstancing = pluginClass.getMethod("preInstancing", new Class[0]); preInstancing.invoke(pluginClass, new Object[0]); } catch (Throwable ti) { } if (!isBlockedDataService) { plugin = pluginClass.newInstance(); } } catch (Throwable thr) { throw new TvBrowserException(getClass(), "error.2", "Could not load plugin {0}.", jarFile.getAbsolutePath(), thr); } return plugin; }
From source file:org.apache.zeppelin.interpreter.InterpreterFactory.java
private Interpreter createRepl(String dirName, String className, Properties property) throws InterpreterException { logger.info("Create repl {} from {}", className, dirName); ClassLoader oldcl = Thread.currentThread().getContextClassLoader(); try {//from w ww .j a v a 2 s .com URLClassLoader ccl = cleanCl.get(dirName); if (ccl == null) { // classloader fallback ccl = URLClassLoader.newInstance(new URL[] {}, oldcl); } boolean separateCL = true; try { // check if server's classloader has driver already. Class cls = this.getClass().forName(className); if (cls != null) { separateCL = false; } } catch (Exception e) { logger.error("exception checking server classloader driver", e); } URLClassLoader cl; if (separateCL == true) { cl = URLClassLoader.newInstance(new URL[] {}, ccl); } else { cl = ccl; } Thread.currentThread().setContextClassLoader(cl); Class<Interpreter> replClass = (Class<Interpreter>) cl.loadClass(className); Constructor<Interpreter> constructor = replClass.getConstructor(new Class[] { Properties.class }); Interpreter repl = constructor.newInstance(property); repl.setClassloaderUrls(ccl.getURLs()); LazyOpenInterpreter intp = new LazyOpenInterpreter(new ClassloaderInterpreter(repl, cl)); return intp; } catch (SecurityException e) { throw new InterpreterException(e); } catch (NoSuchMethodException e) { throw new InterpreterException(e); } catch (IllegalArgumentException e) { throw new InterpreterException(e); } catch (InstantiationException e) { throw new InterpreterException(e); } catch (IllegalAccessException e) { throw new InterpreterException(e); } catch (InvocationTargetException e) { throw new InterpreterException(e); } catch (ClassNotFoundException e) { throw new InterpreterException(e); } finally { Thread.currentThread().setContextClassLoader(oldcl); } }
From source file:tvbrowser.core.PluginLoader.java
private void getBaseInfoOfPlugins(File[] plugins, ArrayList<PluginBaseInfo> availablePlugins) { for (File plugin : plugins) { URL[] urls;/*from ww w . ja v a 2 s . co m*/ // Get the plugin name String pluginName = plugin.getName(); pluginName = pluginName.substring(0, pluginName.length() - 4); Class pluginClass = null; try { urls = new URL[] { plugin.toURI().toURL() }; ClassLoader classLoader = URLClassLoader.newInstance(urls, ClassLoader.getSystemClassLoader()); pluginClass = classLoader.loadClass(pluginName.toLowerCase() + "." + pluginName); Method getVersion = pluginClass.getMethod("getVersion", new Class[0]); try { Version version = (Version) getVersion.invoke(pluginClass, new Object[0]); PluginBaseInfo baseInfo = new PluginBaseInfo("java." + pluginClass.getName(), version); if (!availablePlugins.contains(baseInfo)) { availablePlugins.add(baseInfo); } } catch (Exception e) { } } catch (Throwable t) { urls = null; pluginClass = null; System.gc(); PluginBaseInfo baseInfo = new PluginBaseInfo("java." + pluginName.toLowerCase() + "." + pluginName, new Version(0, 0)); if (!availablePlugins.contains(baseInfo)) { availablePlugins.add(baseInfo); } mLog.info("Could not load base info for plugin file '" + plugin.getAbsolutePath() + "'. Use default version instead."); } } }
From source file:org.entando.entando.plugins.jpcomponentinstaller.aps.system.services.installer.DefaultComponentInstaller.java
private URLClassLoader getURLClassLoader(List<URL> urlList, File[] files, ServletContext servletContext) { for (File input : files) { try {//from ww w . j a v a2 s . c om if (!urlList.contains(input.toURI().toURL())) { urlList.add(input.toURI().toURL()); } } catch (Exception e) { e.printStackTrace(); } } URL[] urls = new URL[urlList.size()]; for (int i = 0; i < urlList.size(); i++) { URL url = urlList.get(i); urls[i] = url; } URLClassLoader cl = URLClassLoader.newInstance(urls, Thread.currentThread().getContextClassLoader()); return cl; }
From source file:net.sourceforge.sqlexplorer.service.SqlexplorerService.java
private Driver createGenericJDBC(String driverJars, String driverName) throws Exception { Driver driver = null;//from w w w. j av a 2s. co m String[] driverJarPath = driverJars.split(SEMICOLON_STRING); try { int driverCount = 0; URL[] driverUrl = new URL[driverJarPath.length]; for (String dirverpath : driverJarPath) { driverUrl[driverCount++] = new File(dirverpath).toURL(); } URLClassLoader cl = URLClassLoader.newInstance(driverUrl, Thread.currentThread().getContextClassLoader()); Class c = cl.loadClass(driverName); driver = (Driver) c.newInstance(); } catch (Exception ex) { log.error(ex, ex); throw ex; } return driver; }
From source file:tvbrowser.ui.mainframe.MainFrame.java
@Override public void drop(DropTargetDropEvent dtde) { dtde.acceptDrop(dtde.getDropAction()); File[] files = getDragDropPlugins(dtde.getCurrentDataFlavors(), dtde.getTransferable()); try {// w w w .ja v a 2 s. c o m File tmpFile = File.createTempFile("plugins", ".txt"); StringBuilder alreadyInstalled = new StringBuilder(); StringBuilder notCompatiblePlugins = new StringBuilder(); for (File jarFile : files) { ClassLoader classLoader = null; try { URL[] urls = new URL[] { jarFile.toURI().toURL() }; classLoader = URLClassLoader.newInstance(urls, ClassLoader.getSystemClassLoader()); } catch (MalformedURLException exc) { } if (classLoader != null) { // Get the plugin name String pluginName = jarFile.getName(); pluginName = pluginName.substring(0, pluginName.length() - 4); try { String pluginId = "java." + pluginName.toLowerCase() + "." + pluginName; PluginProxy installedPlugin = PluginProxyManager.getInstance().getPluginForId(pluginId); TvDataServiceProxy service = TvDataServiceProxyManager.getInstance() .findDataServiceById(pluginName.toLowerCase() + '.' + pluginName); Class<?> pluginClass = classLoader.loadClass(pluginName.toLowerCase() + '.' + pluginName); Method getVersion = pluginClass.getMethod("getVersion", new Class[0]); Version version1 = null; try { version1 = (Version) getVersion.invoke(pluginClass, new Object[0]); } catch (Throwable t1) { t1.printStackTrace(); } if (installedPlugin != null && (installedPlugin.getInfo().getVersion().compareTo(version1) > 0 || (installedPlugin.getInfo().getVersion().compareTo(version1) == 0 && version1.isStable()))) { alreadyInstalled.append(installedPlugin.getInfo().getName()).append('\n'); } else if (service != null && (service.getInfo().getVersion().compareTo(version1) > 0 || (service.getInfo().getVersion().compareTo(version1) == 0 && version1.isStable()))) { alreadyInstalled.append(service.getInfo().getName()).append('\n'); } else { RandomAccessFile write = new RandomAccessFile(tmpFile, "rw"); String versionString = Integer.toString(version1.getMajor()) + '.' + (version1.getMinor() / 10) + (version1.getMinor() % 10) + '.' + version1.getSubMinor(); write.seek(write.length()); write.writeBytes("[plugin:" + pluginName + "]\n"); write.writeBytes("name_en=" + pluginName + "\n"); write.writeBytes("filename=" + jarFile.getName() + "\n"); write.writeBytes("version=" + versionString + "\n"); write.writeBytes("stable=" + version1.isStable() + "\n"); write.writeBytes("download=" + jarFile.toURI().toURL() + "\n"); write.writeBytes("category=unknown\n"); write.close(); } } catch (Exception e) { notCompatiblePlugins.append(jarFile.getName()).append("\n"); } } } if (alreadyInstalled.length() > 0) { showInfoTextMessage( mLocalizer.msg("update.alreadyInstalled", "The following Plugin in current version are already installed:"), alreadyInstalled.toString(), 400); } if (notCompatiblePlugins.length() > 0) { showInfoTextMessage( mLocalizer.msg("update.noTVBPlugin", "This following files are not TV-Browser Plugins:"), notCompatiblePlugins.toString(), 400); } if (tmpFile.length() > 0) { java.net.URL url = tmpFile.toURI().toURL(); SoftwareUpdater softwareUpdater = new SoftwareUpdater(url, false, true); mSoftwareUpdateItems = softwareUpdater.getAvailableSoftwareUpdateItems(); dtde.dropComplete(true); SoftwareUpdateDlg updateDlg = new SoftwareUpdateDlg(this, false, mSoftwareUpdateItems); updateDlg.setVisible(true); } else { dtde.rejectDrop(); dtde.dropComplete(false); } if (!tmpFile.delete()) { tmpFile.deleteOnExit(); } } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }