Black box to white box-check the internal running mechanism of the red5 container when debugging the red5 Project

Source: Internet
Author: User

How to Use the red5 plug-in to create a red5 project? This section describes how to create and debug the red5 project in eclipse. Some people asked, "I want to see the red5 API while debugging the red5 application.
Internal running status of the red5 container. It is best to modify the red5 source code and check its running status. Can this be done ?"
Yes. This article starts with bootstrap and introduces how to make red5 source code "dynamic ".
The author's red5 version: 0.9.1 final; eclipse version: 3.5.0 (eclipse-jee-indigo-win32 version ).
The source code must be written into eclipse first.
Users with SVN plug-ins installed in eclipse can directly check out from the official website:
Select red5 in the http://red5.googlecode.com/svn/java/server/,tags, the author selects 0_9_1, click the finish button. The server is abroad, so be patient and wait for detection.
Users who do not have the svn plug-in installed in eclipse can download it before importing it:
Go to the red5 homepage, select downloads-> red5 server on the right of the logo, and select a link for the red5 version. The author selects 0.9.1 final, on the new page, click zip to download the version. (You can also select the source file to download the source code directly, but the third-party jar package on which the source code depends must be downloaded separately ). Download the obtained red5-0.9.1.zip file and decompress it. Open eclipse and select
File-> Import... -> General-> existing projects into workspace, select root directory, select the extracted red5-0.9.1, check copy projects into workspace, finish, there is a red5_server project generated on the workbench. Decompress src.zip under the red5_server directory under the eclipse workbench and create the test directory. Go back to eclipse to refresh the red5_server project, there will be a lot
Errors, Java build path-> libraries-> Add jars... check all jar packages in Lib, and errors will be gone.
The author uses the latter one. OK. Now we can see and debug the red5 source code.
After reading it, only the Java file is 2.5 MB. Where can I start with a large project?
Understand a language, start with helloworld, understand a program, and start with the main function.
View red5.bat in the red5 installation directory:

if NOT DEFINED RED5_MAINCLASS set RED5_MAINCLASS=org.red5.server.Bootstrap:launchRed5echo Starting Red5"%JAVA_HOME%\bin\java" %JYTHON_OPTS% %JAVA_OPTS% -cp "%RED5_CLASSPATH%" %RED5_MAINCLASS% %RED5_OPTS%

We can see that red5 is started by org. red5.server. Bootstrap. Let's start with bootstrap.
Since r3379, red5 uses org. red5.server. Bootstrap as the program entry class. This means org. red5.server. standalone has been deprecated.
Org. red5.server. Bootstrap does not appear in
In the official red5 API, we cannot find it in red5.jar. It is compressed to boot. Jar separately for red5 startup.
The main function of Bootstrap has only three valid codes:

/** * BootStrapping entry point *  * @param args command line arguments * @throws Exception if error occurs */public static void main(String[] args) throws Exception {//retrieve path elements from system propertiesString root = getRed5Root();getConfigurationRoot(root);//bootstrap dependencies and start red5bootStrap();System.out.println("Bootstrap complete");}

View them one by one. First look at the getred5root function called In the first sentence:

/** * Gets the Red5 root *  * @return * @throws IOException */private static String getRed5Root() throws IOException {// look for red5 root first as a system propertyString root = System.getProperty("red5.root");// if root is null check environmentalif (root == null) {//check for env variableroot = System.getenv("RED5_HOME");}// if root is null find out current directory and use it as rootif (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 slashesif (File.separatorChar != '/') {root = root.replaceAll("\\\\", "/");}//drop last slash if existsif (root.charAt(root.length()-1) == '/') {root = root.substring(0, root.length() - 1);}//set/reset propertySystem.setProperty("red5.root", root);System.out.printf("Red5 root: %s\n", root);return root;}

This is the root directory of red5. The source code comments are very detailed and will not be repeated. After running these statements, the author's eclipse console prints the result as follows:
Red5 root: D:/javaprojects/red5_server
Let's look at the getconfigurationroot method:

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

This is the directory of the red5 configuration file. The source code comments are very detailed and will not be repeated. After running these statements, the author's eclipse console prints the result as follows:
Configuation root: D:/javaprojects/red5_server/Conf

Finally, let's look at the bootstrap sentence, which calls the bootstrap method. The first half of this method writes some attribute settings before startup. The annotations are detailed and will not be repeated. The source code is as follows:

//get current loaderClassLoader baseLoader = Thread.currentThread().getContextClassLoader();// build a ClassLoaderClassLoader loader = ClassLoaderBuilder.build();//set new loader as the loader for this threadThread.currentThread().setContextClassLoader(loader);// create a new instance of this class using new classloaderObject boot = Class.forName("org.red5.server.Launcher", true, loader).newInstance();Method m1 = boot.getClass().getMethod("launch", (Class[]) null);m1.invoke(boot, (Object[]) null);//not that it matters, but set it back to the original loaderThread.currentThread().setContextClassLoader(baseLoader);

This code first replaces the classloader of the current thread with the classloaderbuilder's custom classloader. The latter mainly serves to load the third-party class libraries on which red5 depends. Then, the reflection mechanism is used to dynamically call the org. red5.server. launcher. Launch method. Finally, the classloader of the current thread is restored to the classloader before replacement. That's all.
It is worth mentioning that the org. red5.server. launcher. Launch method. The main task of this method is to initialize filesystemxmlapplicationcontext. Friends who are familiar with spring will not be unfamiliar with this class. Here, it is responsible for initializing the red5 container according to the configuration in red5.xml.
OK. Now we can use the red5 plug-in to create a red5 project. The red5 installation directory in is set to the red5_server directory under the eclipse workbench (adjust the configuration in step 4 and Step 8 when creating the red5 Project). When debugging the red5 project, you can also perform breakpoint tracking on the source code of red5, and black box operations can be converted to white box operations!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.