Continer容器-tomcat 6.xソース読み

12406 ワード

2013-09-21
容器はtomcatの基礎であり、tomcatは容器を通してtomcatコンポーネントを管理して維持しています。一番外側の容器はServerで、一番内側の容器はWrapperで、容器はいくつかの基礎的な機能を提供しています。
コンタイナーコンタイナーはtomcatの容器インターフェースであり、容器が持つ方法とtomcatの容器の仕様を定義しています。容器インターフェースは、親容器とサブ容器をセットし、コンテナを検索し、監視容器イベントのモニターを追加するなど、格納容器を操作する方法を提供し、HTTPプロトコル要求を処理する方法である。
ConttainerBase Continer BaseはConttainerインターフェースの基本的な実現であり、抽象的な種類であり、種類の中でConttainerの大多数の方法の実現を提供して、いくつかの容器を公共的にContinerBaseに実現します。ConttainerBaseの機能は、Conttainerインターフェースの多くの方法を実現し、サブクラスが必要とする共通の方法をベースクラスで実現し、サブクラスでの重複符号化を回避し、サブクラスはContinerBaseクラスを継承することによって完成される。
Lifecycle Support ContinerBaseの抽象類における属性、Lifecycleインターフェースの実現類、機能は容器に登録されたLifecycleEvent監聴類、監聴器のライフサイクルの変更、集中管理モニターを担当する。
Listeners ContinerBaseの属性は、容器モニター配列、ユーザーが容器自身のイベントを傍受し、傍受しているのはContinerEventイベントで、LifecycleEventとは違っています。
Loader ContinerBaseの属性は、tomcatが自分でカプセル化したクラスローダーで、webappの安全なクラスローディングに対して、webappのクラスローディングを管理しています。javaのクラスローダーシステムでは、同じ種類のローダーではない種類のインスタンスは同時にアクセスできません。
Manager ContinerBaseの属性は、Managerが容器の上にあるSession池を管理し、Sessionのライフサイクルを担当し、生成から廃棄までを管理する方法の一つである。
Cluuster Contacter Baseの属性は、クラスタと関連があります。
pipeline:Pipeline ConttainerBaseの属性は、非常に重要です。Valveチェーンです。機能はValveを管理しています。また、情報伝達チェーンを要求しています。最終目的はServletWrapperです。pipelineはValveの順番を決定し、制御要求情報は、コンテナ間で要求情報を伝達する橋です。
support:PropertyChange Support ContinerBaseのプロパティは、コンテナ属性更新モニター集中管理は、Lifecycle Support機能と同じです。
background Process()Conttainerインターフェース方法は、Continerがこの方法を定義する目的はバックグラウンドプロセスを有効にして、いくつかの論理を処理することである。例えば、設定を再読み込みし、Sessionが無効になると判断するなど。
ContinerBackground Processor ContinerBase基本容器の内部クラスは、Runnableインターフェースを実現し、具体的な役割はバックグラウンドプログラムのタイミングとしてbackground Processをトリガするバックグラウンド処理方法です。
    /**
     * Private thread class to invoke the backgroundProcess method of this
     * container and its children after a fixed delay.
     */
    protected class ContainerBackgroundProcessor implements Runnable {

        public void run() {
            while (!threadDone) {
                try {
                    Thread.sleep(backgroundProcessorDelay * 1000L);//  
                } catch (InterruptedException e) {
                    ;
                }
                if (!threadDone) {
                    //     ,                 
                    Container parent = (Container) getMappingObject();
                    ClassLoader cl = Thread.currentThread()
                            .getContextClassLoader();
                    if (parent.getLoader() != null) {
                        cl = parent.getLoader().getClassLoader();
                    }
                    processChildren(parent, cl);//     backgroundProcess()  
                }
            }
        }

        /**
         *       
         * @param container
         * @param cl
         */
        protected void processChildren(Container container, ClassLoader cl) {
            try {
                if (container.getLoader() != null) {
                    Thread.currentThread().setContextClassLoader(
                            container.getLoader().getClassLoader());
                }
                container.backgroundProcess();//        
            } catch (Throwable t) {
                log.error("Exception invoking periodic operation: ", t);
            } finally {
                Thread.currentThread().setContextClassLoader(cl);
            }
            Container[] children = container.findChildren();
            for (int i = 0; i < children.length; i++) {
                if (children[i].getBackgroundProcessorDelay() <= 0) {
                    processChildren(children[i], cl);//     
                }
            }
        }

    }
init()は容器の初期化を担当しています。主に次のステップがあります。
  • MBeanServer
  • に登録します。
  • タグが初期化されました。
        /**
         * Init method, part of the MBean lifecycle. If the container was added via
         * JMX, it'll register itself with the parent, using the ObjectName
         * conventions to locate the parent.
         * 
         * If the container was added directly and it doesn't have an ObjectName,
         * it'll create a name and register itself with the JMX console. On
         * destroy(), the object will unregister.
         * 
         * @throws Exception
         */
        public void init() throws Exception {
    
            if (this.getParent() == null) {
                // "Life" update
                ObjectName parentName = getParentName();
    
                // log.info("Register " + parentName );
                if (parentName != null && mserver.isRegistered(parentName)) {
                    mserver.invoke(parentName, "addChild", new Object[] { this },
                            new String[] { "org.apache.catalina.Container" });
                }
            }
            initialized = true;
        }
    
    start()は容器の起動作業を担当しています。主に以下のステップがあります。
  • は起動したかどうかを判断する
  • は、BEFOREE_をトリガする。START_イベント、タグはすでに起動されました。
  • ブート類キャリアloader、ログレコーダlogger、sessionマネージャmanager、クラスタノードcluster、権限マネージャrealm、ディレクトリリソースサービスreourcess
  • サブ容器を起動する
  • Valveチェーンpipelineを起動する
  • トリガSTART_イベント
  • バックグラウンドタスク処理プロセスを開始する
  • トリガAFTER_START_イベント
  •     /**
         * Prepare for active use of the public methods of this Component.
         *         
         * @exception LifecycleException
         *                if this component detects a fatal error that prevents it
         *                from being started
         */
        public synchronized void start() throws LifecycleException {
    
            // Validate and update our current component state
            if (started) {
                if (log.isInfoEnabled())
                    log.info(sm
                            .getString("containerBase.alreadyStarted", logName()));
                return;
            }
    
            // Notify our interested LifecycleListeners
            //        
            lifecycle.fireLifecycleEvent(BEFORE_START_EVENT, null);
    
            started = true;
    
            // Start our subordinate components, if any
            if ((loader != null) && (loader instanceof Lifecycle))
                ((Lifecycle) loader).start();//     
            logger = null;
            getLogger();
            if ((logger != null) && (logger instanceof Lifecycle))
                ((Lifecycle) logger).start();
            if ((manager != null) && (manager instanceof Lifecycle))
                ((Lifecycle) manager).start();//     
            if ((cluster != null) && (cluster instanceof Lifecycle))
                ((Lifecycle) cluster).start();//       
            if ((realm != null) && (realm instanceof Lifecycle))
                ((Lifecycle) realm).start();
            if ((resources != null) && (resources instanceof Lifecycle))
                ((Lifecycle) resources).start();//    
    
            // Start our child containers, if any
            Container children[] = findChildren();
            for (int i = 0; i < children.length; i++) {
                if (children[i] instanceof Lifecycle)
                    ((Lifecycle) children[i]).start();//     
            }
    
            // Start the Valves in our pipeline (including the basic), if any
            if (pipeline instanceof Lifecycle)
                ((Lifecycle) pipeline).start();//  pipe  
    
            // Notify our interested LifecycleListeners
            //        
            lifecycle.fireLifecycleEvent(START_EVENT, null);
    
            // Start our thread
            threadStart();//    
    
            // Notify our interested LifecycleListeners
            //        
            lifecycle.fireLifecycleEvent(AFTER_START_EVENT, null);
    
        }
    
    stop()は容器の停止を担当していますが、作業はstart()方式で部品が起動する順番に逆に停止します。
        /**
         * Gracefully shut down active use of the public methods of this Component.
         *            
         * @exception LifecycleException
         *                if this component detects a fatal error that needs to be
         *                reported
         */
        public synchronized void stop() throws LifecycleException {
    
            // Validate and update our current component state
            if (!started) {
                if (log.isInfoEnabled())
                    log.info(sm.getString("containerBase.notStarted", logName()));
                return;
            }
    
            // Notify our interested LifecycleListeners
            //          
            lifecycle.fireLifecycleEvent(BEFORE_STOP_EVENT, null);
    
            // Stop our thread
            threadStop();//    
    
            // Notify our interested LifecycleListeners
            //        
            lifecycle.fireLifecycleEvent(STOP_EVENT, null);
            started = false;
    
            // Stop the Valves in our pipeline (including the basic), if any
            if (pipeline instanceof Lifecycle) {
                ((Lifecycle) pipeline).stop();//pipe  
            }
    
            // Stop our child containers, if any
            Container children[] = findChildren();
            for (int i = 0; i < children.length; i++) {
                if (children[i] instanceof Lifecycle)
                    ((Lifecycle) children[i]).stop();//     
            }
            // Remove children - so next start can work
            children = findChildren();
            for (int i = 0; i < children.length; i++) {
                removeChild(children[i]);//     
            }
    
            // Stop our subordinate components, if any
            if ((resources != null) && (resources instanceof Lifecycle)) {
                ((Lifecycle) resources).stop();//    
            }
            if ((realm != null) && (realm instanceof Lifecycle)) {
                ((Lifecycle) realm).stop();
            }
            if ((cluster != null) && (cluster instanceof Lifecycle)) {
                ((Lifecycle) cluster).stop();//       
            }
            if ((manager != null) && (manager instanceof Lifecycle)) {
                ((Lifecycle) manager).stop();//     
            }
            if ((logger != null) && (logger instanceof Lifecycle)) {
                ((Lifecycle) logger).stop();
            }
            if ((loader != null) && (loader instanceof Lifecycle)) {
                ((Lifecycle) loader).stop();
            }
    
            // Notify our interested LifecycleListeners
            //          
            lifecycle.fireLifecycleEvent(AFTER_STOP_EVENT, null);
    
        }
    
    destroy()は容器の廃棄を担当しています。主な仕事はMBeanServerをキャンセルし、父の容器参照を除去し、容器を除去します。
        /**
         *     
         * @throws Exception
         */
        public void destroy() throws Exception {
            if (started) {
                stop();
            }
            initialized = false;
    
            // unregister this component
            if (oname != null) {
                try {
                    if (controller == oname) {
                        Registry.getRegistry(null, null).unregisterComponent(oname);
                        if (log.isDebugEnabled())
                            log.debug("unregistering " + oname);
                    }
                } catch (Throwable t) {
                    log.error("Error unregistering ", t);
                }
            }
    
            if (parent != null) {
                parent.removeChild(this);
            }
    
            // Stop our child containers, if any
            Container children[] = findChildren();
            for (int i = 0; i < children.length; i++) {
                removeChild(children[i]);
            }
    
        }
    
    TrreadStartは、コンテナを起動するバックグラウンドタスクプログラムを担当します。
        /**
         * Start the background thread that will periodically check for session
         * timeouts.
         *            session  
         */
        protected void threadStart() {
    
            if (thread != null)
                return;
            if (backgroundProcessorDelay <= 0)
                return;
    
            threadDone = false;
            String threadName = "ContainerBackgroundProcessor[" + toString() + "]";
            thread = new Thread(new ContainerBackgroundProcessor(), threadName);
            thread.setDaemon(true);//    
            thread.start();
    
        }
    
    threadStop()はバックグラウンドプロセスの実行を停止する責任があります。
        /**
         * Stop the background thread that is periodically checking for session
         * timeouts.
         *     
         */
        protected void threadStop() {
    
            if (thread == null)
                return;
    
            threadDone = true;
            thread.interrupt();//      
            try {
                thread.join();
            } catch (InterruptedException e) {
                ;
            }
    
            thread = null;
    
        }
    
    上のコンポーネントと方法から、容器の完成が必要な機能、あるいは備えられている機能があります。
  • 親容器を設置し、管理子容器
  • を追加する。
  • 管理サービス
  • 権限管理
  • バックグラウンドタスク処理
  • ライフサイクルイベント
  • 属性変更イベントモニタ
  • Valveチェーン、フィルタ要求及び応答情報
  • 約束したことは堅持して、いつもとぎれとぎれです。