ブラックボックスがホワイトボックスに変わります。Red 5プロジェクトを調整する時、Red 5容器内部の運行メカニズムを確認します。


「Red 5プラグインを使ってRed 5プロジェクトを作成するにはどうすればいいですか?」は、EclipseでRed 5プロジェクトを作成してデバッグする方法を紹介しています。Red 5アプリケーションをデバッグする時、Red 5 APIを見ながらRed 5容器内部の運行状況を見ることができます。Red 5ソースコードを修正して、運行状況を見てもいいですか?
        いいです。本稿ではBootstrapからRed 5のソースコードを「動かす」方法を紹介します。
        著者のRed 5バージョン:0.9.1 Final;Eclipseバージョン:3.5.
        まずソースをEclipseに入れます。
        Eclipseにsvnプラグインが搭載されているユーザーは公式サイトから直接検出できます。
        http://red5.googlecode.com/svn/java/server/tagsの中でRed 5のバージョンを選んで、作者の選択のは0〓です。9_1,Finishボタンをクリックします。サーバーは海外にあります。辛抱強く検出を待てばいいです。
        Eclipseにsvnプラグインがインストールされていないユーザーは先にダウンロードしてから導入できます。
        Red 5のホームページを開けて、Logo右側のDownloads->Red 5 Serverを選んで、Red 5のバージョンのリンクポイントを選んで入れて、作者は0.9.1 Finalを選んで、開いた新しいページの中でこのバージョンのZIPをクリックしてダウンロードを開始します。得られたred 5-0.9.1.zipファイルをダウンロードして解凍します。Eclipseを開けて、File->Import...->General->Existing Project into Workspaceを選んで、Select root directoryは先ほど解凍したred 5-0.9.1を選択して、Copy project into workspaceをチェックして、Finishは、作業台にred 5_があります。serverプロジェクトの生成。そしてEclipseのテーブルの下のred 5_serverディレクトリ下でsrc.zipを展開し、testディレクトリを新規作成します。Eclipseに戻りred 5_をリフレッシュします。serverプロジェクトは、多くのErrersがあります。Java Build Path->Libries->Add JARs...libの中のすべてのjarカバンをチェックしたら、Errersはなくなります。
        作者は後者を採用しています。OKです。今はRed 5ソースコードを見てデバッグできます。
        見ました。Javaファイルだけで2.5 MBがあります。こんなに大きいプロジェクトはどこから始まりますか?
        一つの言語を理解して、ハロルドから始まります。プログラムを理解するには、main関数から始まります。
        Red 5のインストールディレクトリの下のred 5.batを見てください。次のような文があります。
if NOT DEFINED RED5_MAINCLASS set RED5_MAINCLASS=org.red5.server.Bootstrap
:launchRed5
echo Starting Red5
"%JAVA_HOME%\bin\java" %JYTHON_OPTS% %JAVA_OPTS% -cp "%RED5_CLASSPATH%" %RED5_MAINCLASS% %RED5_OPTS%
        Red 5のスタートはorg.red.5 server.Bootstrapです。じゃ、私たちはBootstrapから見ましょう。
        R 3379バージョン以降、Red 5はorg.red.5 server.Bootstrapをプログラムエントリクラスとして使用している。これはorg.red.5 server.Standarloneが廃棄されることを意味します。
        org.red.5 server.BootstrapはRed 5の公式APIに現れていません。red.5 jarでは見つけられません。それは単独でboot.jarに圧縮されて、Red 5の起動に使われます。
        Bootstrapのmain関数の有効コードは三つだけです。
	/**
	 * 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 properties
		String root = getRed5Root();		
		getConfigurationRoot(root);
		
		//bootstrap dependencies and start red5
		bootStrap();
			
		System.out.println("Bootstrap complete");
	}
        私たちは一つ一つ確認します。最初の文で呼び出されたgetRed 5 Root関数を見てください。
	/**
	 * Gets the Red5 root
	 * 
	 * @return
	 * @throws IOException
	 */
	private static String getRed5Root() throws IOException {
		// look for red5 root first as a system property
		String root = System.getProperty("red5.root");
		// 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
", 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
", root); return root; }
        これはRed 5を見つけるルートディレクトリです。ソースのコメントは非常に詳細で、もはや詳細です。これらの文を走り終えた後、著者のEclipseコンソールは次のように印刷しました。
       
Red 5 root:D:/javaproject/red 5_server
        get ConfigrationRootの方法を見てください。
	/**
	 * Gets the configuration root
	 * 
	 * @param root
	 * @return
	 */
	private 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";
		}

		//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
", conf); return conf; }
        これはRed 5プロファイルのディレクトリを探しています。ソースのコメントは非常に詳細で、もはや詳細です。これらの文を走り終えた後、著者のEclipseコンソールは次のように印刷しました。
       
Configation root:D:/javaproject/red 5_server/conf
        最後にboot Strap文を見にきましたが、これはbootstrapメソッドを呼び出します。この方法の前半には起動前の属性設定が記載されており、コメントの詳細は省略されている。後半から見て、後半のソースコードは以下の通りです。
		//get current loader
		ClassLoader baseLoader = Thread.currentThread().getContextClassLoader();
		
		// build a ClassLoader
		ClassLoader loader = ClassLoaderBuilder.build();

		//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);

		//not that it matters, but set it back to the original loader
		Thread.currentThread().setContextClassLoader(baseLoader);
        このコードの意味は、現在のスレッドのクラスリーダーをClass Loader BuilderカスタムのクラスLoaderに置き換え、後者の主な役割はRed 5を依存する第三者クラスをロードすることである。その後反射機構を用いてorg.red.5 server.Launcher.launch法を動的に起動する。最後に現在のスレッドのClass Loaderを置換前のクラスLoaderに還元します。以上です
        取り上げるべきはorg.red 5.server.Launcher.launch方法です。この方法でやるべきことは、FileSystemXmlAppleication Contectを初期化することです。Springをよく知っている友達はこの種類に慣れていないはずです。ここではred 5.xmlの構成によってRed 5容器を初期化します。
        OKです。現在は「Red 5プラグインを使ってRed 5プロジェクトを作成するにはどうすればいいですか?」のRed 5インストールディレクトリをEclipseテーブルの下のred 5_に設定します。Serverディレクトリ(Red 5プロジェクトを新規作成する場合は、ステップ4とステップ8の関連配置を調整すれば良い)、Red 5プロジェクトをデバッグする場合は、Red 5のソースコードをブレークポイントトレースできます。ブラックボックスの操作はホワイトボックス操作になります。