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    }