⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 shellcmds.java

📁 有关java 的p2p应用,是一般很好的教程,有兴趣的朋友应该好好阅读一下
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
     *     * @return	an array of the names of all the available shell commands.     */    public String[] list() {        List<String> list = cmdlist();                return list.toArray(new String[list.size()]);    }        /**     * Returns a list of all available Shell commands.     *     * <p/>The current implementation tries to locate commands on the classpath     * pf the classloader     *     * @return	 list of the names of all the available shell commands.     */    public List<String> cmdlist() {        List<URL> classpath = new ArrayList<URL>();        // Retrieve the list of paths representing jar-files.        classpath.addAll( Arrays.asList( parseClassPath(System.getProperty( "java.class.path" ))) );        classpath.addAll( Arrays.asList( getInstJars() ) );                // Initialize a ClassLoader that also searches jar-files for classes.        URLClassLoader scl = new URLClassLoader( new URL[0], this.getClass().getClassLoader() );                List<String> cmds = new ArrayList<String>();                classpath.addAll( Arrays.asList( scl.getURLs()) );                for (URL aURL : classpath) {    try {        if (aURL.toString().endsWith("/")) {            if (!"file".equalsIgnoreCase(aURL.getProtocol())) {                continue;            }            File dir = new File(new File(aURL.getPath()), BIN_PATH);            if (dir.exists()) {                File[] dirCmds = dir.listFiles();                Iterator eachDirCmds = Arrays.asList(dirCmds).iterator();                while (eachDirCmds.hasNext()) {                    try {                        File aDir = (File)eachDirCmds.next();                        if (aDir.isDirectory()) {                            File[] someCmds = aDir.listFiles();                            Iterator eachSomeCmds = Arrays.asList(someCmds).iterator();                            while (eachSomeCmds.hasNext()) {                                File aFile = (File)eachSomeCmds.next();                                if (aFile.getName().endsWith(".class")) {                                    String filename = aFile.getName();                                    filename = filename.substring(0, filename.length() - 6);                                    if (aDir.getName().equals(filename)) {                                        if (null != getInstance(filename)) {                                            cmds.add(filename);                                        }                                    } else {                                        if (null != getInstance(aDir.getName() + "." + filename)) {                                            cmds.add(aDir.getName() + "." + filename);                                        }                                    }                                }                            }                        }                    } catch (Exception ignored) {                        ;                    }                }            }        } else {            String entryName = aURL.getPath();            if (entryName.endsWith(".class")) {                entryName = entryName.substring(0, entryName.length() - 6);                int cmdPath = entryName.indexOf(BIN_PATH);                if (-1 != cmdPath) {                    String cmdName = entryName.substring(cmdPath + BIN_PATH_LEN);                    cmds.add(cmdName);                }            } else {                JarFile jarfile = null;                try {                    if ("jar".equalsIgnoreCase(aURL.getProtocol())) {                        JarURLConnection jar = (JarURLConnection)aURL.openConnection();                        jarfile = jar.getJarFile();                    } else if ("file".equalsIgnoreCase(aURL.getProtocol())) {                        jarfile = new JarFile(new File(new URI(aURL.toString())));                    }                    Enumeration allEntries = jarfile.entries();                    while (allEntries.hasMoreElements()) {                        JarEntry anEntry = (JarEntry)allEntries.nextElement();                        entryName = anEntry.getName();                        if (entryName.endsWith(".class")) {                            entryName = entryName.substring(0, entryName.length() - 6);                            int cmdPath = entryName.indexOf(BIN_PATH);                            if (-1 != cmdPath) {                                String leafPath = entryName.substring(cmdPath + BIN_PATH_LEN);                                String pkgName = leafPath.substring(0, leafPath.indexOf('/'));                                String cmdName = leafPath.substring(leafPath.indexOf('/') + 1);                                if (pkgName.equals(cmdName)) {                                    cmds.add(cmdName);                                } else {                                    cmds.add(pkgName + "." + cmdName);                                }                            }                        }                    }                } finally {                    if (null != jarfile) {                        try {                            jarfile.close();                        } catch (IOException ignored) {                            ;                        }                    }                }            }        }    } catch (Exception failure) {        if (LOG.isLoggable(Level.WARNING)) {            LOG.log(Level.WARNING, " Failure processing : " + ((null != aURL) ? aURL.toString() : ""), failure);        }    }}                Collections.sort( cmds );                return cmds;    }        /**     * Instantiates a given Command and returns its reference. For this purpose this implementation     * tries to load the class 'net.jxta.impl.shell.bin.pkg.cmd'. This class is then instantiated through     * the standard constructor. No other initialisation is done, so the caller of this method should     * set the group, outputPipe, etc.     *     * @param cmd The command to execute. In the form <pkg> "." <cmd>.     * @return	an instance of the specified Shell command.     * @throws	an exception if command could not be found or instantiated.     */    public ShellApp getInstance(String cmd) {        URL [] pathURLs = getInstJars();                // Initialize a ClassLoader that also searches jar-files for classes.        URLClassLoader scl = new URLClassLoader( pathURLs, this.getClass().getClassLoader() );                // Make sure that the Context loader of the current thread        // is properly set, so if the shell commands creates its own        // thread, the Shell class loader will also be used.        Thread.currentThread().setContextClassLoader(scl);                // try just loading the class by name        Class appClass = null;                try {            appClass = scl.loadClass( cmd );        } catch( NoClassDefFoundError notFound ) {;} catch( ClassNotFoundException notFound ) {;}                if (null == appClass) {            String cmdPkg;            String cmdcmdName;            if (-1 == cmd.indexOf('.')) {                cmdPkg = cmd;                cmdcmdName = cmd;            }            else {                cmdPkg = cmd.substring(0, cmd.lastIndexOf('.'));                cmdcmdName = cmd.substring(cmd.lastIndexOf('.') + 1);            }            String cmdclassName = BIN_PACKAGE + "." + cmdPkg + "." + cmdcmdName;            if (LOG.isLoggable(java.util.logging.Level.FINE))                LOG.finer("Loading command : " + cmdclassName);            // Load the class with the help of scl.            try {                appClass = Class.forName(cmdclassName, true, scl);            }            catch (ClassNotFoundException failed) {                return null;            }            if (!ShellApp.class.isAssignableFrom(appClass)) {                return null;            }            try {                if (null == appClass.getConstructor()) {                    // missing required constructor.                    return null;                }            }            catch (NoSuchMethodException missingConstructor) {                return null;            }        }                // Instantiate loaded command class and return it.        try {            ShellApp app = (ShellApp) appClass.newInstance();                        return app;        } catch( java.lang.InstantiationException failed ) {            throw new UndeclaredThrowableException( failed );        } catch( java.lang.IllegalAccessException failed ) {            return null;        }    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -