red5-service分析

red5版本1.0.8-M12

1.下载red5的编译好的tar.gz版本red5-server-1.0.8-M12.tar.gz

2.执行red5.sh的脚本

    最后执行java命令其实是

 /usr/java/jdk1.8.0_05//bin/java 
-Dred5.root=/usr/local/red5-server 
-Djava.security.debug=failure 
-Xverify:none 
-XX:+TieredCompilation 
-XX:+UseBiasedLocking 
-XX:InitialCodeCacheSize=8m 
-XX:ReservedCodeCacheSize=32m 
-Dorg.terracotta.quartz.skipUpdateCheck=true 
-Dcatalina.home=/usr/local/red5-server 
-Dcatalina.useNaming=true 
-Djava.library.path=/usr/local/red5-server/lib/native 
-Dpython.home=lib 
-cp 
/usr/local/red5-server/red5-service.jar:/usr/local/red5-server/conf: org.red5.server.Bootstrap 9999
其实就是启动red5-service.jar的org.red5.server.Bootstrap类

带入red5.root=/usr/local/red5-server 

以及conf的文件

3.

package org.red5.server;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import org.red5.classloading.ClassLoaderBuilder;

/**
 * Boot-straps Red5 using the latest available jars found in <i>red5.home/lib</i> directory.
 *
 * @author The Red5 Project
 * @author Paul Gregoire (mondain@gmail.com)
 * @author Dominick Accattato (daccattato@gmail.com)
 */
public class Bootstrap {

    /**
     * BootStrapping entry point
     * 
     * @param args
     *            command line arguments
     * @throws Exception
     *             if error occurs
     */
    public static void main(String[] args) throws Exception {
        try {
            String root = getRed5Root();
            getConfigurationRoot(root);
            // bootstrap dependencies and start red5
            bootStrap();
            System.out.println("Bootstrap complete");
        } catch (Throwable t) {
            System.out.printf("Bootstrap exception: %s%n", t.getMessage());
            t.printStackTrace();
        } finally {
            System.out.println("Bootstrap exit");
        }
    }

    /**
     * Loads classloader with dependencies
     * 
     * @throws InstantiationException
     * @throws IllegalAccessException
     * @throws ClassNotFoundException
     * @throws NoSuchMethodException
     * @throws InvocationTargetException
     */
    private static void bootStrap() throws InstantiationException, IllegalAccessException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException {
        // print the classpath
        // String classPath = System.getProperty("java.class.path");
        // System.out.printf("JVM classpath: %s\n", classPath);
        System.setProperty("red5.deployment.type", "bootstrap");//设置部署的类
        System.setProperty("sun.lang.ClassLoader.allowArraySyntax", "true");
        // check system property before forcing out selector
        //if (System.getProperty("logback.ContextSelector") == null) {
            // set to use our logger
            //System.setProperty("logback.ContextSelector", "org.red5.logging.LoggingContextSelector");
        //}
        String policyFile = System.getProperty("java.security.policy");
        if (policyFile == null) {
            System.setProperty("java.security.debug", "all");
            System.setProperty("java.security.policy", String.format("%s/red5.policy", System.getProperty("red5.config_root")));
        }
        // set the temp directory if we're vista or later
        String os = System.getProperty("os.name").toLowerCase();
        // String arch = System.getProperty("os.arch").toLowerCase();
        // System.out.printf("OS: %s Arch: %s\n", os, arch);
        if (os.contains("vista") || os.contains("windows 7")) {//windows系统 暂时忽略
            String dir = System.getProperty("user.home");
            // detect base drive (c:\ etc)
            if (dir.length() == 3) {
                // use default
                dir += "Users\\Default\\AppData\\Red5";
                // make sure the directory exists
                File f = new File(dir);
                if (!f.exists()) {
                    f.mkdir();
                }
                f = null;
            } else {
                dir += "\\AppData\\localLow";
            }
            System.setProperty("java.io.tmpdir", dir);
            System.out.printf("Setting temp directory to %s%n", System.getProperty("java.io.tmpdir"));
        }
        /*
         * try { // Enable the security manager SecurityManager sm = new
         * SecurityManager(); System.setSecurityManager(sm); } catch
         * (SecurityException se) {
         * System.err.println("Security manager already set"); }
         */
        // get current loader
        ClassLoader baseLoader = Thread.currentThread().getContextClassLoader();
        // build a ClassLoader
        ClassLoader loader = ClassLoaderBuilder.build();//类加载器 加载lib plugins下
        // set new loader as the loader for this thread
        Thread.currentThread().setContextClassLoader(loader);
        // create a new instance of this class using new classloader
        Object boot = Class.forName("org.red5.server.Launcher", true, loader).newInstance();
        Method m1 = boot.getClass().getMethod("launch", (Class[]) null);
        m1.invoke(boot, (Object[]) null);//反射启动org.red5.server.Launcher
        // not that it matters, but set it back to the original loader
        Thread.currentThread().setContextClassLoader(baseLoader);
    }

    /**
     * Gets the configuration root
     * 
     * @param root
     * @return
     */
    static String getConfigurationRoot(String root) {
        // look for config dir
        String conf = System.getProperty("red5.config_root");
        // if root is not null and conf is null then default it
        if (root != null && conf == null) {
            conf = root + "/conf";//获取conf的路径
        }
        // flip slashes only if windows based os
        if (File.separatorChar != '/') {
            conf = conf.replaceAll("\\\\", "/");
        }
        // set conf sysprop
        System.setProperty("red5.config_root", conf);
        System.out.printf("Configuation root: %s%n", conf);
        return conf;
    }

    /**
     * Gets the Red5 root
     * 
     * @return
     * @throws IOException
     */
    static String getRed5Root() throws IOException {
        // look for red5 root first as a system property
        String root = System.getProperty("red5.root");//获取带入的值red5.root=/usr/local/red5-server 
        // if root is null check environmental
        if (root == null) {
            // check for env variable
            root = System.getenv("RED5_HOME");
        }
        // if root is null find out current directory and use it as root
        if (root == null || ".".equals(root)) {
            root = System.getProperty("user.dir");
            // System.out.printf("Current directory: %s%n", root);
        }
        // if were on a windows based os flip the slashes
        if (File.separatorChar != '/') {
            root = root.replaceAll("\\\\", "/");
        }
        // drop last slash if exists
        if (root.charAt(root.length() - 1) == '/') {
            root = root.substring(0, root.length() - 1);
        }
        // set/reset property
        System.setProperty("red5.root", root);
        System.out.printf("Red5 root: %s%n", root);
        return root;
    }

}


4.

 public static ClassLoader build(File path, int mode, ClassLoader parent) {
        JarFileFilter jarFileFilter = new JarFileFilter();
        List<URL> urlList = new ArrayList<URL>(31);//获得的jar文件
        // the class loader to return
        ClassLoader loader = null;
        // urls to load resources / classes from
        URL[] urls = null;

        if (mode == USE_RED5_LIB) {
            // get red5 home
            // look for red5 home as a system property
            String home = System.getProperty("red5.root");
            // if home is null check environmental
            if (home == null) {
                // check for env variable
                home = System.getenv("RED5_HOME");
            }
            // if home is null or equal to "current" directory
            if (home == null || ".".equals(home)) {
                // if home is still null look it up via this classes loader
                String classLocation = ClassLoaderBuilder.class.getProtectionDomain().getCodeSource().getLocation().toString();
                // System.out.printf("Classloader location: %s\n",
                // classLocation);
                // snip off anything beyond the last slash
                home = classLocation.substring(0, classLocation.lastIndexOf('/'));
            }

            try {
                // add red5.jar to the classpath
                File red5jar = new File(home, "red5-server.jar");//1.加载red5-server.jar
                if (!red5jar.exists()) {
                    System.out.println("Red5 server jar was not found, using fallback.");
                    red5jar = new File(home, "red5.jar");
                } else {
                    System.out.println("Red5 server jar was found");
                }
                urlList.add(red5jar.toURI().toURL());
            } catch (MalformedURLException e1) {
                e1.printStackTrace();
            }
            System.out.printf("URL list: %s\n", urlList);//URL list: [file:/usr/local/red5-server/red5-server.jar]

            // get red5 lib system property, if not found build it
            String libPath = System.getProperty("red5.lib_root");
            if (libPath == null) {
                // construct the lib path
                libPath = home + "/lib";
            }
            // System.out.printf("Library path: %s\n", libPath);

            // try {
            // // add red5-server-common jar to the classpath
            // File red5commonjar = new File(home, "red5-server-common.jar");
            // if (!red5commonjar.exists()) {
            // System.out.println("Red5 server common jar was not found");
            // } else {
            // System.out.println("Red5 server common jar was found");
            // urlList.add(red5commonjar.toURI().toURL());
            // }
            // } catch (MalformedURLException e1) {
            // e1.printStackTrace();
            // }

            // grab the urls for all the jars in "lib"
            File libDir = new File(libPath);
            // if we are on osx with spaces in our path this may occur
            if (libDir == null) {
                libDir = new File(home, "lib");
            }
            File[] libFiles = libDir.listFiles(jarFileFilter);//2.获取lib下所有的jar文件
            for (File lib : libFiles) {
                try {
                    urlList.add(lib.toURI().toURL());
                } catch (MalformedURLException e) {
                    System.err.printf("Exception %s\n", e);
                }
            }

            // look over the libraries and remove the old versions
            scrubURLList(urlList);//移除lib里的旧版本

            // get config dir
            String conf = System.getProperty("red5.config_root");
            if (conf == null) {
                conf = home + "/conf";
            }
            // add config dir
            try {
                URL confUrl = new File(conf).toURI().toURL();//3.增加conf dir
                if (!urlList.contains(confUrl)) {
                    urlList.add(confUrl);
                }
            } catch (MalformedURLException e) {
                System.err.printf("Exception %s\n", e);
            }

            // add the plugins

            // get red5 lib system property, if not found build it
            String pluginsPath = System.getProperty("red5.plugins_root");
            if (pluginsPath == null) {
                // construct the plugins path
                pluginsPath = home + "/plugins";
                // update the property
                System.setProperty("red5.plugins_root", pluginsPath);
            }
            // create the directory if it doesnt exist
            File pluginsDir = new File(pluginsPath);
            // if we are on osx with spaces in our path this may occur
            if (pluginsDir == null) {
                pluginsDir = new File(home, "plugins");
                // create the dir
                pluginsDir.mkdirs();
            }
            // add the plugin directory to the path so that configs
            // will be resolved and not have to be copied to conf
            try {
                URL pluginsUrl = pluginsDir.toURI().toURL();//4.增加plugins dir
                if (!urlList.contains(pluginsUrl)) {
                    urlList.add(pluginsUrl);
                }
            } catch (MalformedURLException e) {
                System.err.printf("Exception %s\n", e);
            }
            // get all the plugin jars
            File[] pluginsFiles = pluginsDir.listFiles(jarFileFilter);//5.获取plugins下所有的jar文件
            // this can be null if the dir doesnt exist
            if (pluginsFiles != null) {
                for (File plugin : pluginsFiles) {
                    try {
                        urlList.add(plugin.toURI().toURL());
                    } catch (MalformedURLException e) {
                        System.err.printf("Exception %s\n", e);
                    }
                }
            }

            // create the url array that the classloader wants
            urls = urlList.toArray(new URL[0]);
            System.out.printf("Selected libraries: (%s items)\n", urls.length);//Selected libraries: (67 items)
            for (URL url : urls) {
                System.out.println(url);
            }
            System.out.println();
            // instance a url classloader using the selected jars
            if (parent == null) {
                loader = new URLClassLoader(urls);//得到classloader
            } else {
                loader = new URLClassLoader(urls, parent);
            }

        } else {

            List<String> standardLibs = new ArrayList<String>(7);

            if (path != null) {
                try {
                    urlList.add(path.toURI().toURL());
                    URL classesURL = new URL("jar:file:" + path.getAbsolutePath().replace(File.separatorChar, '/') + "!/WEB-INF/classes/");
                    urlList.add(classesURL);
                } catch (MalformedURLException e1) {
                    e1.printStackTrace();
                }
            }

            if (mode == USE_CLASSPATH_LIB) {
                String classPath = System.getProperty("java.class.path");
                StringTokenizer stClassPath = new StringTokenizer(classPath, File.pathSeparator);
                while (stClassPath.hasMoreTokens()) {
                    String nextPath = stClassPath.nextToken();
                    if (nextPath.toLowerCase().endsWith(".jar")) {
                        standardLibs.add(nextPath.substring(nextPath.lastIndexOf(File.separatorChar) + 1));
                    }
                    try {
                        urlList.add(new File(nextPath).toURI().toURL());
                    } catch (MalformedURLException e) {
                        System.err.printf("Exception %s\n", e);
                    }
                }
            }
            if (mode == USE_WAR_LIB) {
                if (path.isDirectory()) {
                    File libDir = new File(path, "WEB-INF/lib");
                    // this should not be null but it can happen
                    if (libDir != null && libDir.canRead()) {
                        File[] libs = libDir.listFiles(jarFileFilter);
                        // System.out.printf("Webapp lib count: %s\n",
                        // libs.length);
                        for (File lib : libs) {
                            try {
                                urlList.add(lib.toURI().toURL());
                            } catch (MalformedURLException e) {
                                System.err.printf("Exception %s\n", e);
                            }
                        }
                    }
                } else {
                    try {
                        JarInputStream jarStream = new JarInputStream(new FileInputStream(path));
                        JarEntry entry = jarStream.getNextJarEntry();
                        while (entry != null) {
                            String entryName = entry.getName();
                            if (entryName.startsWith("WEB-INF/lib/") && entryName.endsWith(".jar") && !standardLibs.contains(entryName.substring(12))) {
                                File tempJarFile = unpack(jarStream, entryName);
                                urlList.add(tempJarFile.toURI().toURL());
                            }
                            entry = jarStream.getNextJarEntry();
                        }
                        jarStream.close();
                    } catch (IOException e) {
                        System.err.printf("Exception %s\n", e);
                    }
                }
            }
            urls = urlList.toArray(new URL[0]);
            loader = new ChildFirstClassLoader(urls, parent);
        }
        Thread.currentThread().setContextClassLoader(loader);
        // loop thru all the current urls
        // System.out.printf("Classpath for %s:\n", loader);
        // for (URL url : urls) {
        // System.out.println(url.toExternalForm());
        // }
        return loader;
    }

1.加载red5-server.jar

2.获取lib下所有的jar文件  

3.增加conf dir 

4.增加plugins dir  

5.获取plugins下所有的jar文件  

最后输出

file:/usr/local/red5-server/red5-server.jar
file:/usr/local/red5-server/lib/commons-io-2.4.jar
file:/usr/local/red5-server/lib/log4j-over-slf4j-1.7.21.jar
file:/usr/local/red5-server/lib/boilerpipe-1.1.0.jar
file:/usr/local/red5-server/lib/spring-core-4.3.2.RELEASE.jar
file:/usr/local/red5-server/lib/apache-mime4j-core-0.7.2.jar
file:/usr/local/red5-server/lib/apache-mime4j-dom-0.7.2.jar
file:/usr/local/red5-server/lib/spring-web-4.3.2.RELEASE.jar
file:/usr/local/red5-server/lib/ognl-3.1.10.jar
file:/usr/local/red5-server/lib/cors-filter-2.5.jar
file:/usr/local/red5-server/lib/httpclient-4.5.2.jar
file:/usr/local/red5-server/lib/rome-1.5.1.jar
file:/usr/local/red5-server/lib/bcprov-jdk15on-1.55.jar
file:/usr/local/red5-server/lib/jempbox-1.8.12.jar
file:/usr/local/red5-server/lib/tagsoup-1.2.1.jar
file:/usr/local/red5-server/lib/logback-core-1.1.7.jar
file:/usr/local/red5-server/lib/spring-webmvc-4.3.2.RELEASE.jar
file:/usr/local/red5-server/lib/asm-5.0.4.jar
file:/usr/local/red5-server/lib/spring-context-support-4.3.2.RELEASE.jar
file:/usr/local/red5-server/lib/spring-beans-4.3.2.RELEASE.jar
file:/usr/local/red5-server/lib/pdfbox-debugger-2.0.1.jar
file:/usr/local/red5-server/lib/slf4j-api-1.7.21.jar
file:/usr/local/red5-server/lib/httpcore-4.4.4.jar
file:/usr/local/red5-server/lib/commons-compress-1.11.jar
file:/usr/local/red5-server/lib/xml-apis-1.4.01.jar
file:/usr/local/red5-server/lib/quartz-2.2.2.jar
file:/usr/local/red5-server/lib/red5-io-1.0.8-M12.jar
file:/usr/local/red5-server/lib/spring-expression-4.3.2.RELEASE.jar
file:/usr/local/red5-server/lib/red5-server-common-1.0.8-M12.jar
file:/usr/local/red5-server/lib/mina-integration-jmx-2.0.14.jar
file:/usr/local/red5-server/lib/spring-aop-4.3.2.RELEASE.jar
file:/usr/local/red5-server/lib/gson-2.2.4.jar
file:/usr/local/red5-server/lib/jackson-core-2.7.1.jar
file:/usr/local/red5-server/lib/c3p0-0.9.1.1.jar
file:/usr/local/red5-server/lib/jcl-over-slf4j-1.7.21.jar
file:/usr/local/red5-server/lib/commons-daemon-1.0.15.jar
file:/usr/local/red5-server/lib/isoparser-1.1.17.jar
file:/usr/local/red5-server/lib/juniversalchardet-1.0.3.jar
file:/usr/local/red5-server/lib/java-property-utils-1.9.1.jar
file:/usr/local/red5-server/lib/tika-core-1.13.jar
file:/usr/local/red5-server/lib/pdfbox-tools-2.0.1.jar
file:/usr/local/red5-server/lib/commons-codec-1.10.jar
file:/usr/local/red5-server/lib/rome-utils-1.5.1.jar
file:/usr/local/red5-server/lib/spring-context-4.3.2.RELEASE.jar
file:/usr/local/red5-server/lib/commons-lang3-3.4.jar
file:/usr/local/red5-server/lib/javassist-3.20.0-GA.jar
file:/usr/local/red5-server/lib/metadata-extractor-2.8.1.jar
file:/usr/local/red5-server/lib/mina-core-2.0.14.jar
file:/usr/local/red5-server/lib/ehcache-core-2.6.11.jar
file:/usr/local/red5-server/lib/tika-parsers-1.13.jar
file:/usr/local/red5-server/lib/commons-beanutils-1.9.2.jar
file:/usr/local/red5-server/lib/jul-to-slf4j-1.7.21.jar
file:/usr/local/red5-server/lib/logback-classic-1.1.7.jar
file:/usr/local/red5-server/lib/xmpcore-5.1.2.jar
file:/usr/local/red5-server/lib/commons-collections-3.2.2.jar
file:/usr/local/red5-server/lib/mina-integration-beans-2.0.14.jar
file:/usr/local/red5-server/lib/xercesImpl-2.11.0.jar
file:/usr/local/red5-server/conf/
file:/usr/local/red5-server/plugins/
file:/usr/local/red5-server/plugins/cors-filter-2.5.jar
file:/usr/local/red5-server/plugins/tomcat-embed-el-8.5.5.jar
file:/usr/local/red5-server/plugins/websocket-1.6.jar
file:/usr/local/red5-server/plugins/tomcatplugin-1.16.jar
file:/usr/local/red5-server/plugins/tomcat-embed-core-8.5.5.jar
file:/usr/local/red5-server/plugins/java-property-utils-1.9.1.jar
file:/usr/local/red5-server/plugins/tomcat-embed-jasper-8.5.5.jar
file:/usr/local/red5-server/plugins/ecj-4.5.1.jar

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值