// todo[r.sh] drop after migration on Java 7 private static void loadSnappyForJRockit() throws Exception { String vmName = System.getProperty("java.vm.name"); if (vmName == null || !vmName.toLowerCase().contains("jrockit")) { return; } byte[] bytes; InputStream in = StartupUtil.class.getResourceAsStream("/org/xerial/snappy/SnappyNativeLoader.bytecode"); try { bytes = FileUtil.loadBytes(in); } finally { in.close(); } ClassLoader classLoader = StartupUtil.class.getClassLoader(); Method defineClass = ClassLoader.class.getDeclaredMethod( "defineClass", String.class, byte[].class, int.class, int.class); defineClass.setAccessible(true); Class<?> loaderClass = (Class<?>) defineClass.invoke( classLoader, "org.xerial.snappy.SnappyNativeLoader", bytes, 0, bytes.length); loaderClass = classLoader.loadClass(loaderClass.getName()); String[] classes = { "org.xerial.snappy.SnappyNativeAPI", "org.xerial.snappy.SnappyNative", "org.xerial.snappy.SnappyErrorCode" }; for (String aClass : classes) { classLoader.loadClass(aClass); } Method loadNativeLibrary = SnappyLoader.class.getDeclaredMethod("loadNativeLibrary", Class.class); loadNativeLibrary.setAccessible(true); loadNativeLibrary.invoke(null, loaderClass); }
boolean tryDir(String d) { ClassLoader L = getClass().getClassLoader(); File p = new File(d, PACKAGE); // System.out.println("Try "+p); if (!p.exists() || !p.isDirectory()) return false; for (File f : p.listFiles()) { String s = f.getName(); if (!s.endsWith(".class")) continue; String name = s.substring(0, s.length() - 6); try { Class<?> c = L.loadClass(PACKAGE + "." + name); if (!Quotation.class.isAssignableFrom(c)) continue; Q.put(name, (Quotation) c.newInstance()); System.out.println(" " + name); // ClassNotFoundException InstantiationException IllegalAccessException } catch (Exception e) { continue; } } return Q.size() > 0; }
/** Look on disk for an editor with the class name 'ocName'. */ PluggableEditor loadEditorFromDisk(String ocName) { // if here, we need to look on disk for a pluggable editor class... log.finer("looking for ocName: " + ocName); try { Class c = myLoader.loadClass(ocName); Constructor constructor = c.getConstructor(new Class[0]); // XXX If the pluggable editor has an error in the constructor, under some // XXX circumstances it can fail so badly that this call never returns, and // XXX the thread hangs! It doesn't even get to the exception handler below... // XXX but sometimes if the constructor fails everything works as expected. Wierd. PluggableEditor editor = (PluggableEditor) constructor.newInstance(new Object[0]); editors.put(ocName, editor); // add the new editor to our list return editor; } catch (Exception e) // expected condition - just means no pluggable editor available { if (e instanceof InvocationTargetException) // rare exception - an error was encountered in the plugin's // constructor. { log.warning("unable to load special editor for: '" + ocName + "' " + e); if (JXConfig.debugLevel >= 1) { log.warning("Error loading plugin class: "); ((InvocationTargetException) e).getTargetException().printStackTrace(); } } log.log(Level.FINEST, "'Expected' Error loading " + ocName, e); editors.put(ocName, NONE); // add a blank place holder - we can't load // an editor for this, and there's no point looking again. (change if want dynamic loading, // i.e. look *every* time) } return null; // only here if an error has occured. }
public QSAdminGUI(QSAdminMain qsadminMain, JFrame parentFrame) { this.parentFrame = parentFrame; Container cp = this; qsadminMain.setGUI(this); cp.setLayout(new BorderLayout(5, 5)); headerPanel = new HeaderPanel(qsadminMain, parentFrame); mainCommandPanel = new MainCommandPanel(qsadminMain); cmdConsole = new CmdConsole(qsadminMain); propertiePanel = new PropertiePanel(qsadminMain); if (headerPanel == null || mainCommandPanel == null || cmdConsole == null || propertiePanel == null) { throw new RuntimeException("Loading of one of gui component failed."); } headerPanel.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 5)); cp.add(headerPanel, BorderLayout.NORTH); JScrollPane propertieScrollPane = new JScrollPane(propertiePanel); // JScrollPane commandScrollPane = new JScrollPane(mainCommandPanel); JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, true, mainCommandPanel, cmdConsole); splitPane.setOneTouchExpandable(false); splitPane.setDividerLocation(250); // splitPane.setDividerLocation(0.70); tabbedPane = new JTabbedPane(JTabbedPane.TOP); tabbedPane.addTab("Main", ball, splitPane, "Main Commands"); tabbedPane.addTab("Get/Set", ball, propertieScrollPane, "Properties Panel"); QSAdminPluginConfig qsAdminPluginConfig = null; PluginPanel pluginPanel = null; // -- start of loadPlugins try { File xmlFile = null; ClassLoader classLoader = null; Class mainClass = null; File file = new File(pluginDir); File dirs[] = null; if (file.canRead()) dirs = file.listFiles(new DirFileList()); for (int i = 0; dirs != null && i < dirs.length; i++) { xmlFile = new File(dirs[i].getAbsolutePath() + File.separator + "plugin.xml"); if (xmlFile.canRead()) { qsAdminPluginConfig = PluginConfigReader.read(xmlFile); if (qsAdminPluginConfig.getActive().equals("yes") && qsAdminPluginConfig.getType().equals("javax.swing.JPanel")) { classLoader = ClassUtil.getClassLoaderFromJars(dirs[i].getAbsolutePath()); mainClass = classLoader.loadClass(qsAdminPluginConfig.getMainClass()); logger.fine("Got PluginMainClass " + mainClass); pluginPanel = (PluginPanel) mainClass.newInstance(); if (JPanel.class.isInstance(pluginPanel) == true) { logger.info("Loading plugin : " + qsAdminPluginConfig.getName()); pluginPanelMap.put("" + (2 + i), pluginPanel); plugins.add(pluginPanel); tabbedPane.addTab( qsAdminPluginConfig.getName(), ball, (JPanel) pluginPanel, qsAdminPluginConfig.getDesc()); pluginPanel.setQSAdminMain(qsadminMain); pluginPanel.init(); } } else { logger.info( "Plugin " + dirs[i] + " is disabled so skipping " + qsAdminPluginConfig.getActive() + ":" + qsAdminPluginConfig.getType()); } } else { logger.info("No plugin configuration found in " + xmlFile + " so skipping"); } } } catch (Exception e) { logger.warning("Error loading plugin : " + e); logger.fine("StackTrace:\n" + MyString.getStackTrace(e)); } // -- end of loadPlugins tabbedPane.addChangeListener( new ChangeListener() { int selected = -1; int oldSelected = -1; public void stateChanged(ChangeEvent e) { // if plugin selected = tabbedPane.getSelectedIndex(); if (selected >= 2) { ((PluginPanel) pluginPanelMap.get("" + selected)).activated(); } if (oldSelected >= 2) { ((PluginPanel) pluginPanelMap.get("" + oldSelected)).deactivated(); } oldSelected = selected; } }); // tabbedPane.setBorder(BorderFactory.createEmptyBorder(0,5,5,5)); cp.add(tabbedPane, BorderLayout.CENTER); buildMenu(); }