/** * Recursive function to enumerate classes inside a classpath folder * * @param superClass * @param classloader * @param classes * @param packagePath * @param packageName */ private static void enumerateDirectory( String prefix, Class superClass, ClassLoader classloader, LinkedList<Class> classes, File packagePath, String packageName) { File[] classFiles = packagePath.listFiles(); for (File classFile : classFiles) { if (classFile.isDirectory()) { enumerateDirectory( prefix, superClass, classloader, classes, classFile, packageName + classFile.getName() + "."); } else { if (classFile.getName().endsWith(".class") && (prefix == null || classFile.getName().startsWith(prefix))) { String classFileName = classFile.getName(); String className = packageName + classFileName.substring(0, classFileName.length() - 6); checkAndAddClass(classloader, superClass, classes, className); } } } }
/** * Find mod files in the "mods" folder * * @param modFolder Folder to search * @param modFiles List of mod files to load */ protected void findModFiles(File modFolder, LinkedList<File> modFiles) { List<String> supportedVerions = Arrays.asList(SUPPORTED_VERSIONS); for (File modFile : modFolder.listFiles(this)) { try { // Check for a version file ZipFile modZip = new ZipFile(modFile); ZipEntry version = modZip.getEntry("version.txt"); if (version != null) { // Read the version string InputStream versionStream = modZip.getInputStream(version); BufferedReader versionReader = new BufferedReader(new InputStreamReader(versionStream)); String strVersion = versionReader.readLine(); versionReader.close(); // Only add the mod if the version matches and we were able to successfully add it to the // class path if (supportedVerions.contains(strVersion) && addURLToClassPath(modFile.toURI().toURL())) { modFiles.add(modFile); } } modZip.close(); } catch (Exception ex) { logger.warning( "Error enumerating '" + modFile.getAbsolutePath() + "': Invalid zip file or error reading file"); } } }
public static String[] get_filelist(String path, boolean nodirs, boolean nofiles) { File folder = new File(path); if (folder.isDirectory()) { File[] listOfFiles = folder.listFiles(); java.util.Vector<String> r = new java.util.Vector<String>(); for (int i = 0; listOfFiles != null && i < listOfFiles.length; i++) { if ((listOfFiles[i].isFile() && !nofiles) || (listOfFiles[i].isDirectory() && !nodirs)) { r.add(listOfFiles[i].getName()); } } return r.toArray(new String[0]); } else { return null; // A: no existe o no es directorio } }
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(); }