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.common;
017    
018    import org.opengion.fukurou.db.ConnectionFactory;
019    import org.opengion.fukurou.util.Cleanable;
020    import org.opengion.fukurou.util.HybsEntry;
021    import org.opengion.fukurou.util.URLConnect;
022    import org.opengion.fukurou.util.LogWriter;
023    import java.util.Date;
024    import java.util.Map;
025    
026    import javax.servlet.ServletContextListener;
027    import javax.servlet.ServletContextEvent;
028    import javax.servlet.ServletContext;
029    
030    import java.io.IOException;
031    
032    /**
033     * ServletContextListener を実è£?—ãŸã?コンãƒ?‚­ã‚¹ãƒˆã?監視オブジェクトã§ã™ã?
034     * ã“れã¯ã€ã‚³ãƒ³ãƒ?‚­ã‚¹ãƒ?Webアプリケーション)ã®èµ·å‹?シャãƒ?ƒˆãƒ?‚¦ãƒ³ã‚’監視ã§ãã‚‹ã€?
035     *
036     * ServletContextListener ã¯ã€?
037     *
038     *      ConnectionFactory ã®ã‚³ãƒã‚¯ã‚·ãƒ§ãƒ³ãƒ—ã?ルã¸ã®ã‚¢ã‚¯ã‚»ã‚¹?開放
039     *      ResourceFactory   ã®ãƒªã‚½ãƒ¼ã‚¹æƒ??ã¸ã®ã‚¢ã‚¯ã‚»ã‚¹?開放
040     *
041     * ã®ä½œæ¥­ã‚’行ã„ã¾ã™ã?
042     *
043     * ã“ã?リスナã?ã¯ã€WEB-INF/web.xml ã§ã€çµ?¿è¾¼ã¿ã¾ã™ã?
044     *
045     * ã€WEB-INF/web.xmlã€?
046     *
047     *     <listener>
048     *         <listener-class>
049     *             org.opengion.hayabusa.common.HybsContextListener
050     *         </listener-class>
051     *     </listener>
052     *
053     * @og.group åˆæœŸåŒ?
054     *
055     * @version  4.0
056     * @author   Kazuhiko Hasegawa
057     * @since    JDK5.0,
058     */
059    public class HybsContextListener implements ServletContextListener {
060            // 4.0.0.0 (2007/10/26) ConnectionFactoryã®hayabusaä¾å­˜ã‚’åˆ?‚‹ãŸã‚ã«ç§»å‹•ã—ã¦ããŸ
061            static {
062                    final Cleanable clr = new Cleanable() {
063                            public void clear() {
064                                    ConnectionFactory.realClose();
065                            }
066                    };
067                    SystemManager.addCleanable( clr );
068            }
069    
070            /**
071             *  ServletContextListener インターフェースã®å®Ÿè£?
072             *
073             * WebアプリケーションãŒãƒªã‚¯ã‚¨ã‚¹ãƒˆã‚’処ç?§ãる状態ã«ãªã£ãŸã“ã¨ã‚?
074             * リスナã?ã«é€šçŸ¥ã™ã‚‹ã€?
075             *
076             * @og.rev 3.0.0.0 (2002/12/25) ãƒã?ジョンãƒã‚§ãƒ?‚¯ã€HybsSystemåˆæœŸåŒ–追åŠ?
077             * @og.rev 3.4.0.0 (2003/09/01) Contextã®pathã«ã‚ˆã‚‹ã€ã‚·ã‚¹ãƒ?ƒ ãƒ‘ラメータ ã®åˆ?‚Šæ›¿ãˆå¯¾å¿?
078             * @og.rev 3.4.0.3 (2003/09/10) ServletContext ã®åç§°ã‚’ã?仮想パスåã¨ã™ã‚‹ã€?
079             * @og.rev 3.5.3.1 (2003/10/31) シスãƒ?ƒ ãƒ‘ラメータ ファイルã®èª­ã¿å–りタイミングをé…らã›ã¾ã™ã?
080             * @og.rev 4.0.0.0 (2005/01/31) Ver4 ã®ã‚·ã‚¹ãƒ?ƒ ãƒ‘ラメータæƒ??ã®å–å¾—å?ç?‚’追åŠ?—ã¾ã™ã?
081             * @og.rev 4.1.0.1 (2008/01/23) ログ出力å?ã®è¨­å®šå?ç?‚’追åŠ?
082             * @og.rev 4.3.4.1 (2008/12/08) ログã®ç’°å¢?¤‰æ•°å¯¾å¿?
083             *
084             * @param  event コンãƒ?‚­ã‚¹ãƒˆã‚¤ãƒ™ãƒ³ãƒ?
085             */
086            public void contextInitialized( final ServletContextEvent event ) {
087                    ServletContext context = event.getServletContext();
088    
089    //              // 4.0.0.0 (2007/10/26) ConnectionFactoryã®hayabusaä¾å­˜ã‚’åˆ?‚‹ãŸã‚ã«ç§»å‹•ã—ã¦ããŸ
090    //              final Cleanable clr = new Cleanable() {
091    //                      public void clear() {
092    //                              ConnectionFactory.realClose();
093    //                      }
094    //              };
095    //              SystemManager.addCleanable( clr );
096    
097                    Map<String,String> param = SystemParameter.makeSystemParameter( context );
098                    HybsSystem.setInitialData( param );                     // 4.0.0 (2005/01/31)
099    
100                    // 4.3.4.1 (2008/12/08) ログã®ç’°å¢?¤‰æ•°å¯¾å¿?
101    //              LogWriter.init( HybsSystem.url2dir( HybsSystem.sys( "SYS_LOG_URL" ) ) ); // 4.1.0.1 (2008/01/23)
102                    LogWriter.init( HybsSystem.url2dir( System.getProperty( "SYS_LOG_URL" ,HybsSystem.sys( "SYS_LOG_URL" ) ) ) );
103    
104                    // CONTEXT_INITIAL_CALL_URL ã§ç™»éŒ²ã•れãŸURLを実行ã—ã¾ã™ã?
105                    // 処ç??ã€contextInitialized ãŒçµ‚äº?—ã¦ã‹ã‚‰å®Ÿè¡Œã™ã‚‹å¿?¦ãŒã‚りã¾ã™ã?
106                    new Thread( new InitialCallURL() ).start();
107    
108                    System.out.println( "-------" );
109            }
110    
111            /**
112             *  ServletContextListener インターフェースã®å®Ÿè£?
113             *
114             * WebアプリケーションãŒã‚·ãƒ£ãƒ?ƒˆãƒ?‚¦ãƒ³ã•れるã“ã¨ã‚?
115             * リスナã?ã«é€šçŸ¥ã™ã‚‹ã€?
116             *
117             * @og.rev 3.1.1.1 (2003/04/03) キャãƒ?‚·ãƒ¥ã‚¯ãƒªã‚¢ãƒ¡ã‚½ãƒ?ƒ‰ã‚’æ–°è¦è¿½åŠ??
118             * @og.rev 3.3.3.3 (2003/08/06) HybsTimerTaskManager を終äº?™‚ã«ã‚­ãƒ£ãƒ³ã‚»ãƒ«ã™ã‚‹ãƒ­ã‚¸ãƒ?‚¯ã‚’追åŠ??
119             * @og.rev 3.5.2.1 (2003/10/27) リンクエラー対策:永続化セãƒ?‚·ãƒ§ãƒ³(SESSIONS.ser)ã‹ã‚‰ã‚ªãƒ–ジェクトを削除ã—ã¦ãŠãã€?
120             * @og.rev 3.6.0.0 (2004/09/17) CalendarFactory.clear() を追åŠ?—ã¾ã™ã?
121             * @og.rev 4.0.0.0 (2005/01/31) コンãƒ?‚­ã‚¹ãƒˆåã®å–り方を変更ã—ã¾ã™ã?
122             * @og.rev 4.0.0.0 (2005/01/31) Cleanable インターフェースã«ã‚ˆã‚‹åˆæœŸåŒ–å?ç?
123             * @og.rev 4.0.0.0 (2005/01/31) HybsTimerTaskManager ã¯ã€Cleanable インターフェースã«ã‚ˆã‚‹åˆæœŸåŒ?
124             * @og.rev 4.1.0.0 (2007/12/27) GE12ã®ã‚¯ãƒªã‚¢å‡¦ç?¿½åŠ?
125             * @og.rev 4.3.0.0 (2008/07/18) soffice.binã®ã‚¿ã‚¹ã‚¯ã‚’削除ã™ã‚‹å‡¦ç?‚’追åŠ?
126             * @og.rev 5.0.2.0 (2009/11/01) å†ç·¨æˆæ©Ÿè?追åŠ?
127             *
128             * @param  event コンãƒ?‚­ã‚¹ãƒˆã‚¤ãƒ™ãƒ³ãƒ?
129             */
130            public void contextDestroyed( final ServletContextEvent event ) {
131                    String name = HybsSystem.sys( "CONTEXT_NAME" );
132                    System.out.println( "Context Destroyed [" + name + "]  " + new Date() );
133    
134                    // 4.1.0.0 (2007/12/26) GE12ã‹ã‚‰SystemParameterã§è¨­å®šã—ãŸã‚³ãƒ³ãƒ?‚­ã‚¹ãƒˆé–¢ä¿‚ã?æƒ??
135                    SystemManager.clearGE12();
136    
137                    // 4.0.0 (2005/01/31) Cleanable インターフェースã«ã‚ˆã‚‹åˆæœŸåŒ–å?ç?
138                    SystemManager.allClear( true ) ;
139    
140                    // 4.3.0.0 (2008/07/18) soffice.binã‚’å?ã¦killã—ã¾ã?
141    //              SystemManager.sofficeKill();
142    
143                    SystemManager.sessionDestroyed();               // 3.5.2.1 (2003/10/27)
144    
145                    SystemManager.deleteGUIAccessInfo();    // 5.0.2.0 (2009/11/01)
146    
147                    System.out.println( "-------" );
148            }
149    
150            /**
151             * CONTEXT_INITIAL_CALL_URL ã‚’èµ·å‹•ã™ã‚‹ç‚ºã®ã‚¹ãƒ¬ãƒ?ƒ‰å†?ƒ¨ã‚¯ãƒ©ã‚¹ã§ã™ã?
152             *
153             * HybsContextListener ãŒæ­£å¸¸çµ‚äº?—ãªã?¨ã€TomcatãŒèµ·å‹•ã—ãŸã“ã¨ã«ãªã£ã¦ã?ªã?‚ºã€?
154             * 通常ã®JSP処ç?Œå‡ºæ¥ã¾ã›ã‚“ã€?
155             * ã“ã“ã§ã¯ã€Tomcat起動時ã«åˆæœŸå‡¦ç?RL(CONTEXT_INITIAL_CALL_URL)をコールã™ã‚‹ç‚ºã«ã€?
156             * 時間差を利用ã™ã‚‹ç‚ºã€ã‚¹ãƒ¬ãƒ?ƒ‰åŒ–ã—ã¦å®Ÿè¡Œã•ã›ã¾ã™ã?
157             * ã“ã?スレãƒ?ƒ‰ã¯ã€?¼’秒間スリープ後ã«ã€å?期å?ç?RLを呼ã³å‡ºã—ã¾ã™ã?
158             *
159             * @og.rev 4.2.2.0 (2008/05/22) åˆæœŸURLã®æŽ¥ç¶šãƒ¦ãƒ¼ã‚¶ãƒ¼ã‚’ã‚·ã‚¹ãƒ?ƒ ãƒªã‚½ãƒ¼ã‚¹ã‚ˆã‚Šå–å¾?
160             *
161             * @og.group ログイン制御
162             *
163             * @version  4.0
164             * @author   Kazuhiko Hasegawa
165             * @since    JDK5.0,
166             */
167            private static class InitialCallURL implements Runnable {
168                    /**
169                     * スレãƒ?ƒ‰ã®å‡¦ç?–‹å§‹ãƒ¡ã‚½ãƒ?ƒ‰
170                     *
171                     */
172                    public void run() {
173                            try {
174                                    Thread.sleep( 2000 );
175                            }
176                            catch ( InterruptedException ex) {
177                                    LogWriter.log( "InterruptedException:" + ex.getMessage() );
178                            }
179    
180                            HybsEntry[] urls = HybsSystem.sysEntry( "CONTEXT_INITIAL_CALL_URL" );
181    //                      String userPass = "SYSTEM:MANAGER" ;
182                            String userPass = HybsSystem.sys( "CONTEXT_INITIAL_CALL_USERPASS" );
183                            boolean isCall = false;
184                            for( int i=0; i<urls.length; i++ ) {
185                                    String url = urls[i].getValue();
186                                    if( url == null || url.length() == 0 ) { continue; }
187                                    URLConnect conn = new URLConnect( url,userPass );
188                                    try {
189                                            conn.connect();
190                                            String msg = conn.getCode() + ":" + conn.getMessage() ;
191                                            conn.disconnect();
192                                            System.out.println( "    URL[" + i + "]:" + url );
193                                            System.out.println( "           " + msg );
194                                            isCall = true ;
195                                    }
196                                    catch( IOException ex ) {
197                                            LogWriter.log( "    URL[" + i + "]:" + url );
198                                            LogWriter.log( "           " + ex.getMessage() );
199                                    }
200                            }
201                            if( isCall ) {
202                                    System.out.println( "  CONTEXT_INITIAL_CALL_URL" );
203                                    System.out.println( "-------" );
204                            }
205                    }
206            }
207    }