001    /*
002     * Copyright (c) 2009 The openGion Project.
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     *     http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
013     * either express or implied. See the License for the specific language
014     * governing permissions and limitations under the License.
015     */
016    package org.opengion.hayabusa.report2;
017    
018    import java.util.HashMap;
019    import java.util.Map;
020    
021    /**
022     * 帳票処ç?‚’行ã†å?‚¹ãƒ¬ãƒ?ƒ‰ã‚’管ç?™ã‚‹ã‚¯ãƒ©ã‚¹ã§ã™ã?
023     *
024     * å?‚¹ãƒ¬ãƒ?ƒ‰ã¯ã€å?部çš?«ãƒ—ã?ルã•れã¾ã™ã?
025     * スレãƒ?ƒ‰ã®IDã¯OOoQueue#getThreadId()ã§è¿”ã•れる値ã§ã™ã?
026     * スレãƒ?ƒ‰ãŒç”Ÿæˆã•れるタイミングã¯ã€ãã®IDã§åˆã‚ã¦ã‚¹ã‚¿ãƒ?‚¯è¦æ±‚ãŒæ¥ã?insertQueue()ãŒå‘¼ã°ã‚ŒãŸ)時ã§ã™ã?
027     *
028     * æŒ?®šã?スレãƒ?ƒ‰ã‚’終äº?™ã‚‹ã«ã¯ã€funishThread( key )を呼ã³å‡ºã—ã¾ã™ã?
029     * å…¨ã¦ã®ã‚¹ãƒ¬ãƒ?ƒ‰ã‚’終äº?™ã‚‹ã«ã¯ã€funishAllThreads()を呼ã³å‡ºã—ã¾ã™ã?
030     *
031     * ç¾æ™‚点ã§ã®å®Ÿè£?§ã¯ã€ç”Ÿæˆã•れãŸã‚¹ãƒ¬ãƒ?ƒ‰ã«å¯¾ã—ã¦ã®ç›£è¦–ã?行ã£ã¦ã?¾ã›ã‚“ã€?
032     * ã“れã¯ã€ç‰¹å®šã?スレãƒ?ƒ‰ãŒãƒ•リーズã—ãŸéš›ã?外部ã‹ã‚‰å¼·åˆ¶çµ‚äº?‚’行ãŠã?¨ã™ã‚‹ã¨ã€?
033     * 監視スレãƒ?ƒ‰ã‚‚フリーズã—ã¦ã—ã¾ã?•題ãŒã‚ã‚‹ãŸã‚ã§ã™ã?
034     * (ä½?—ã€?ã¤ã®soffice.binã®ãƒ—ロセスã«å¯¾ã—ã¦ã‚·ãƒªã‚¢ãƒ«ã«å¯¾ã—ã¦å‡¦ç?—ã¦ã?‚‹é™ã‚Šã§ã¯ã€?
035     *  フリーズå•題ã?発生ã—ãªã?‚ˆã?§ã?
036     *
037     * @og.group 帳票シスãƒ?ƒ 
038     *
039     * @version  4.0
040     * @author   Hiroki.Nakamura
041     * @since    JDK1.6
042     */
043    public final class ExecThreadManager {
044    
045            /**
046             * スレãƒ?ƒ‰ãƒ—ã?ル
047             */
048            private static final Map<String, ExecThread>      pool    = new HashMap<String, ExecThread>();
049            private static boolean debug    = false; // 4.3.0.0 (2008/07/15) ãƒ?ƒãƒ?‚°è¿½åŠ?
050    
051    //      /**
052    //       * ãƒã‚§ãƒ?‚¯ã‚¹ãƒ¬ãƒ?ƒ‰ã®å®Ÿè¡Œé–“éš?
053    //       */
054    //      private static final int REPORT_THREAD_CHECK_INTERVAL = 1000 * 2;
055    
056    //      /**
057    //       * OOoãŒã‚¯ãƒ©ãƒ?‚·ãƒ¥ã—ã¦ã?‚‹ã¨åˆ¤æ–­ã•ã‚Œã‚‹åœæ­¢æ™‚é–“
058    //       */
059    //      private static final int REPORT_THREAD_CRASH_TIME = 1000 * 2;
060    
061    //      /**
062    //       * ãƒã‚§ãƒ?‚¯ç”¨ã®ã‚¹ãƒ¬ãƒ?ƒ‰ã‚’é–‹å§‹ã—ã¾ã?
063    //       */
064    //      static {
065    //              checkThreads();
066    //      }
067    
068            /**
069             * オブジェクトã?生æ?ã‚’ç¦æ­¢ã—ã¾ã?
070             */
071            private ExecThreadManager() {}
072    
073            /**
074             * キューを該当ã™ã‚‹ã‚¹ãƒ¬ãƒ?ƒ‰ã«ã‚¹ã‚¿ãƒ?‚¯ã™ã‚‹
075             *
076             * @og.rev 4.3.0.0 (2008/07/15) スレãƒ?ƒ‰IDã«ã‚·ã‚¹ãƒ?ƒ IDを付加
077             *
078             * @param       queue   ExecQueueオブジェク�
079             */
080            public static void insertQueue( final ExecQueue queue ) {
081                    // 4.3.3.6 (2008/11/15) ã“ã?部åˆ??ä¸è¦ãªã®ã§å…?«æˆ»ã—ã¾ã?
082                    ExecThread oet = getExecThread( queue.getThreadId() );
083    //              ExecThread oet = getExecThread( queue.getSystemId() + "_" + queue.getThreadId() ); // 4.3.0.0 (2008/07/15) シスãƒ?ƒ IDをスレãƒ?ƒ‰IDã«ä»˜åŠ ã—ã¾ã™ã?
084                    oet.stackQueue( queue );
085            }
086    
087            /**
088             * キューを該当ã™ã‚‹ã‚¹ãƒ¬ãƒ?ƒ‰ã«ã‚¹ã‚¿ãƒ?‚¯ã™ã‚‹
089             *
090             * ã“ã?メソãƒ?ƒ‰ã§ã¯ã€æ—¢ã«åŒã˜ã‚¹ãƒ¬ãƒ?ƒ‰ãŒå­˜åœ¨ã™ã‚‹ã‹ã©ã?‹ã‚’ãƒã‚§ãƒ?‚¯ã›ãšã«å¿?š
091             * æ–°ã—ã„スレãƒ?ƒ‰ã‚’生æˆã—ã€ã‚­ãƒ¥ãƒ¼ã‚’å?ç?—ã¾ã™ã?
092             * ã¾ãŸã?処ç?Œå®Œäº?—ãŸå¾Œã?ãã?スレãƒ?ƒ‰ã¯ã€WAITã™ã‚‹ã“ã¨ãªã終äº?—ã¾ã™ã?
093             *
094             * @og.rev 5.1.6.0 (2010/05/01) æ–°è¦ä½œæ?
095             *
096             * @param       queue   ExecQueueオブジェク�
097             */
098            public static void insertQueueOnNewThread( final ExecQueue queue ) {
099                    ExecThread oet = new ExecThread( queue.getThreadId(), debug );
100                    oet.start();
101                    System.out.println( "[INFO]THREAD CREATED:THREAD-ID=" + queue.getThreadId() );
102                    oet.stackQueue( queue );
103                    oet.finishAfterExec();
104            }
105    
106            /**
107             * 該当ã™ã‚‹ã‚¹ãƒ¬ãƒ?ƒ‰IDã‚’æŒã¤ã‚¹ãƒ¬ãƒ?ƒ‰ã‚’å–å¾—ã—ã¾ã™ã?
108             * スレãƒ?ƒ‰ãƒ—ã?ルã«å­˜åœ¨ã—ãªã??åˆã?ã€æ–°è¦ã«ä½œæ?ã•れã¾ã™ã?
109             *
110             * @param       threadId        スレãƒ?ƒ‰ID
111             *
112             * @return      ExecThreadスレãƒ?ƒ‰
113             */
114            private static ExecThread getExecThread( final String threadId ) {
115                    ExecThread oet = null;
116                    synchronized( pool ) {
117                            oet = pool.get( threadId );
118    
119                            // スレãƒ?ƒ‰ãŒçµ‚äº?—ã¦ã?‚‹å ´åˆã?ã€å?度作æ?
120                            if( oet != null && !oet.isAlive() ) {
121                                    pool.remove( threadId );
122                                    oet = null;
123                            }
124    
125                            if( oet == null ) {
126                                    // oet = new ExecThread( threadId );
127                                    oet = new ExecThread( threadId, debug );
128                                    oet.start();
129                                    System.out.println( "[INFO]THREAD CREATED:THREAD-ID=" + threadId );
130                                    pool.put( threadId, oet );
131                            }
132                    }
133    
134                    return oet;
135            }
136    
137            /**
138             * å…¨ã¦ã®ã‚¹ãƒ¬ãƒ?ƒ‰ã‚’終äº?—ã¾ã™ã?
139             *
140             */
141            public static void finishAllThreads() {
142                    synchronized( pool ) {
143                            for( ExecThread oet : pool.values() ) {
144                                    oet.finish();
145                            }
146                            pool.clear();
147                            System.out.println( "[INFO]ALL THREADS FINISHED" );
148                    }
149            }
150    
151            /**
152             * æŒ?®šã?スレãƒ?ƒ‰ã‚’終äº?—ã¾ã™ã?
153             *
154             * @param threadId スレãƒ?ƒ‰ID
155             */
156            public static void finishThread( final String threadId ) {
157                    synchronized( pool ) {
158                            ExecThread oet = pool.remove( threadId );
159                            if( oet != null ) {
160                                    oet.finish();
161                                    System.out.println( "[INFO]THREAD CREATED:THREAD-ID=" + threadId );
162                            }
163                    }
164            }
165    
166            /**
167             * スレãƒ?ƒ‰æƒ??ã®ãƒžãƒƒãƒ—ã‚’è¿”ã—ã¾ã™ã?
168             *
169             * @return      スレãƒ?ƒ‰æƒ??ã®ãƒžãƒƒãƒ?
170             */
171            public static Map<String, String> getThreadInfo() {
172                    Map<String, String> info = new HashMap<String, String>();
173                    synchronized( pool ) {
174                            for( Map.Entry<String, ExecThread> entry : pool.entrySet() ) {
175                                    info.put( entry.getKey(), entry.getValue().toString() );
176                            }
177                    }
178                    return info;
179            }
180    
181            /**
182             * ãƒ?ƒãƒ?‚°ãƒ•ラグã®è¨­å®?
183             *
184             * @og.rev  4.3.0.0 (2008/07/15) ãƒ?ƒãƒ?‚°è¿½åŠ?
185             *
186             * @param   flag ãƒ?ƒãƒ?‚°ãƒ•ラグ [true:ãƒ?ƒãƒ?‚°/false:通常]
187             */
188            public static void setDebug ( final boolean flag ){
189                    debug = flag;
190            }
191    
192    //      /**
193    //       * 監視用ã®ã‚¹ãƒ¬ãƒ?ƒ‰ã‚’é–‹å§‹ã—ã¾ã™ã?
194    //       *
195    //       */
196    //      private static void checkThreads() {
197    //              new Thread() {
198    //                      public void run() {
199    //
200    //                              while( true ) {
201    //                                      try {
202    //                                              Thread.sleep( REPORT_THREAD_CHECK_INTERVAL );
203    //                                      }
204    //                                      catch( InterruptedException e ) {}
205    //
206    //                                      synchronized( pool ) {
207    //                                              for( Map.Entry<String, ExecThread> entry : pool.entrySet() ) {
208    //                                                      String id = entry.getKey();
209    //                                                      ExecThread oet = entry.getValue();
210    //
211    //                                                      System.out.println(oet.getExecTime());
212    //
213    //                                                      if( oet.getExecTime() > REPORT_THREAD_CRASH_TIME ) {
214    //                                                              System.out.println( "ã€ç›£è¦–ã?スレãƒ?ƒ‰ID=" + id + "ãŒã‚¯ãƒ©ãƒ?‚·ãƒ¥ã—ã¦ã?‚‹å¯èƒ½æ€§ãŒã‚りã¾ã™ã?ã§ã€å‰²è¾¼è¦æ±‚を発行ã—ã¾ã™ã?(処ç?™‚é–?" + String.valueOf( oet.getExecTime() ) + ")" );
215    //                                                              oet.interrupt();
216    //                                                      }
217    //                                              }
218    //                                      }
219    //                              }
220    //
221    //                      }
222    //              }.start();
223    //      }
224    
225    }