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.taglib;
017    
018    import static org.opengion.fukurou.util.StringUtil.nval;
019    
020    import java.io.File;
021    import java.io.PrintWriter;
022    import java.sql.Connection;
023    import java.sql.SQLException;
024    
025    import org.opengion.fukurou.db.ConnectionFactory;
026    import org.opengion.fukurou.util.StringUtil;
027    import org.opengion.hayabusa.common.HybsSystem;
028    import org.opengion.hayabusa.common.HybsSystemException;
029    import org.opengion.hayabusa.common.SystemInstaller;
030    
031    /**
032     * シスãƒ?ƒ ã®ã‚¤ãƒ³ã‚¹ãƒˆã?ルを行ã†ãŸã‚ã®ã‚¿ã‚°ã§ã™ã?
033     *
034     * シスãƒ?ƒ ã‚¤ãƒ³ã‚¹ãƒˆã?ル用ã®ã‚¢ãƒ¼ã‚«ã‚¤ãƒ?ZIP)å½¢å¼ã‚’æŒ?®šã™ã‚‹ã“ã¨ã§ã€ä»¥ä¸‹ã?設定を自動ã§è¡Œã„ã¾ã™ã?
035     * â‘?ebapps以下ã¸ã®ã‚³ãƒ³ãƒ?‚­ã‚¹ãƒˆãƒ•ァイルã®å±•é–‹T
036     * â‘¡Tomcatã®ã‚³ãƒ³ãƒ?‚­ã‚¹ãƒˆè¨­å®?
037     * â‘¢å?¨®DBç’°å¢??インストã?ル
038     *
039     * 5.6.7.0 (2013/07/27) 
040     * ※ インストã?ルã™ã‚‹ãƒ??タベã?スã¯ã€dbid ãƒ‘ãƒ©ãƒ¡ãƒ¼ã‚¿ã§æŒ?®šã§ãã¾ã™ãŒã€ãƒªã‚½ãƒ¼ã‚¹ã‚’登録ã™ã‚‹ãƒ??タベã?スã¯
041     * 実行ã—ã¦ã?‚‹ コンãƒ?‚­ã‚¹ãƒˆã? RESOURCE_DBID ã§ã€å¤–部ã‹ã‚‰æŒ?®šã§ãã¾ã›ã‚“ã€‚ã”æ³¨æ„ãã?•ã??
042     *
043     * ã“ã?アーカイブã?ã€ãƒ«ãƒ¼ãƒˆãƒ‡ã‚£ãƒ¬ã‚¯ãƒˆãƒªã«ã‚³ãƒ³ãƒ?‚­ã‚¹ãƒˆãƒ‘スã®ãƒ‰ã‚­ãƒ¥ãƒ¡ãƒ³ãƒˆã?ースãŒé?ç½®ã•れã¦ã?‚‹å¿?¦ãŒã‚りã¾ã™ã?
044     *
045     * [アーカイブã?æ§‹æ?]
046     * xx.zip - gf - db - ...               (DBç’°å¢?‚¤ãƒ³ã‚¹ãƒˆã?ル用ã®ã‚¹ã‚¯ãƒªãƒ—ト)
047     *             - filetemp - ...         (ä¸?™‚ファイルã®ä¿å­˜å?æ‰?※中身ã¯ç©º)
048     *             - jsp - ...              (ç”»é¢JSP)
049     *             - log - ...              (ログファイルã®ä¿å­˜å?æ‰?※中身ã¯ç©º)
050     *             - WEB-INF -  ...         (接続å?æƒ??ã€openGionã®jarファイル)
051     *                       - [CONTEXT].xml(コンãƒ?‚­ã‚¹ãƒˆè¨­å®šãƒ•ァイル)
052     *�
053     * ※WEB-INF直下ã?[CONTEXT].xmlã¯ã€Tomcatã®confãƒ?‚£ãƒ¬ã‚¯ãƒˆãƒªä»¥ä¸‹ã«ã€ã‚³ãƒ³ãƒ?‚­ã‚¹ãƒˆè¨­å®šãƒ•ァイルã¨ã—ã¦ã€ã‚³ãƒ”ã?ã•れã¾ã™ã?
054     *   ãªãŠã?ã“ã?[CONTEXT].xmlãŒã‚¢ãƒ¼ã‚«ã‚¤ãƒ–ã«å«ã¾ã‚Œã¦ã?ªã??åˆã?インストã?ルã¯è¡Œã‚れã¾ã›ã‚“ã€?
055     *
056     * @og.formSample
057     * â—å½¢å¼ï¼?lt;og:sysInstall fileURL="…" insFile="…" />
058     * â—body?šãªã?
059     *
060     * â—Tag定義??
061     *   <og:sysInstall
062     *       insFile          â—‹ã?TAG】インストã?ルファイルを指定ã—ã¾ã™ã?(å¿??)
063     *       fileURL            ã€TAG】æ“作ã™ã‚‹ãƒ•ァイルã®ãƒ?‚£ãƒ¬ã‚¯ãƒˆãƒªã‚’指定ã—ã¾ã?(åˆæœŸå€¤:FILE_URL[=filetemp/])
064     *       dbid               ã€TAGã€?通常ã¯ä½¿ã?¾ã›ã‚“)インストã?ルを行ã†DB接続IDを指定ã—ã¾ã™ã?
065     *       debug              ã€TAG】デãƒãƒƒã‚°æƒ??ã‚’å?力ã™ã‚‹ã‹ã©ã?‹[true/false]を指定ã—ã¾ã?åˆæœŸå€¤:false)
066     *   />
067     *
068     * â—使用ä¾?
069     *    ・Calc(ods)ファイルをPDFã«å¤‰æ›
070     *        <og:sysInstall insFile="context.zip" />
071     *
072     * @og.group ãã?他部å“?
073     *
074     * @version  4.0
075     * @author       Hiroki Nakamura
076     * @since    JDK5.0,
077     */
078    public class SysInstallTag extends CommonTagSupport {
079            //* ã“ã?プログラãƒ??VERSIONæ–?­—å?を設定ã—ã¾ã™ã?       {@value} */
080            private static final String VERSION = "5.6.7.0 (2013/07/27)";
081    
082            private static final long serialVersionUID = 567020130727L ;
083    
084            private String  insFile         = null;
085            private String  fileURL         = HybsSystem.sys( "FILE_URL" );
086            private String  dbid            = null;                         // 5.5.4.5 (2012/07/27) æ–°è¦è¿½åŠ?
087    
088            private final String RESOURCE_DBID = HybsSystem.sys( "RESOURCE_DBID" );         // 5.6.7.0 (2013/07/27) リソース系DBID
089    
090            /**
091             * Taglibã®é–‹å§‹ã‚¿ã‚°ãŒè¦‹ã¤ã‹ã£ãŸã¨ãã«å‡¦ç?™ã‚?doStartTag() ã‚?オーãƒã?ライドã—ã¾ã™ã?
092             *
093             * @return      後続å?ç??æŒ?¤º( SKIP_BODY )
094             */
095            @Override
096            public int doStartTag() {
097                    return ( SKIP_BODY );
098            }
099    
100            /**
101             * Taglibã®çµ‚äº?‚¿ã‚°ãŒè¦‹ã¤ã‹ã£ãŸã¨ãã«å‡¦ç?™ã‚?doEndTag() ã‚?オーãƒã?ライドã—ã¾ã™ã?
102             *
103             * @og.rev 5.5.4.5 (2012/07/27) dbid æ–°è¦è¿½åŠ?
104             * @og.rev 5.6.7.0 (2013/07/27) リソース登録用ã¨ã‚¢ãƒ—リケーション登録用ã®ã‚³ãƒã‚¯ã‚·ãƒ§ãƒ³ã‚’å?ã‘ã‚‹
105             *
106             * @return      後続å?ç??æŒ?¤º
107             */
108            @Override
109            public int doEndTag() {
110                    debugPrint();
111    
112                    String directory = HybsSystem.url2dir( fileURL );
113                    File buildFile = new File( StringUtil.urlAppend( directory,insFile ) );
114    
115    //              Connection conn = ConnectionFactory.connection( null, null );
116    //              Connection conn = ConnectionFactory.connection( dbid, getApplicationInfo() );   // 5.5.4.5 (2012/07/27)
117    //              PrintWriter out = new PrintWriter( pageContext.getOut() );
118    //              SystemInstaller installer = new SystemInstaller( conn, out );
119    
120    //              try {
121    //                      installer.install( buildFile );
122    //              }
123    ////            catch( SQLException ex ) {
124    //              catch( Throwable ex ) {
125    //                      String msg = "インストã?ル中ã«ã€DB処ç?§ã‚¨ãƒ©ãƒ¼ãŒç™ºç”Ÿã—ã¾ã—ãŸã€?;
126    //                      throw new HybsSystemException( msg, ex );
127    //              }
128    
129                    // 5.6.7.0 (2013/07/27) リソース登録用ã¨ã‚¢ãƒ—リケーション登録用ã®ã‚³ãƒã‚¯ã‚·ãƒ§ãƒ³ã‚’å?ã‘ã‚‹
130                    // エラー処ç?‚‚åˆã‚ã›ã¦ã€ä¿®æ­£ã—ã¦ãŠãã¾ã™ã?
131                    Connection defConn = null;
132                    Connection rscConn = null;
133                    boolean errFlag = true;
134                    try {
135                            defConn = ConnectionFactory.connection( dbid, getApplicationInfo() );
136                            rscConn = ConnectionFactory.connection( RESOURCE_DBID, getApplicationInfo() );
137                            PrintWriter out = new PrintWriter( pageContext.getOut() );
138                            SystemInstaller installer = new SystemInstaller( defConn , rscConn , out );             // 5.6.7.0 (2013/07/27)
139                            installer.install( buildFile );
140                            errFlag = false;                // エラーã§ã¯ãªã?
141                    }
142                    catch( Throwable ex ) {
143                            String errMsg = "インストã?ル中ã«ã€DB処ç?§ã‚¨ãƒ©ãƒ¼ãŒç™ºç”Ÿã—ã¾ã—ãŸã€?
144                                                    + ex.getMessage()  + HybsSystem.CR ;
145                            throw new HybsSystemException( errMsg, ex );
146                    }
147                    finally {
148                            // 以下ã?コーãƒ?‚£ãƒ³ã‚°ã®è¦‹ç›´ã—ã‚’è€?ˆã‚‹å¿?¦æœ‰ã‚Šã?
149                            // Connection ã¯ãƒ—ã?ルã—ã¦ã?‚‹ç‚ºã€close() ã—ã¦ã‚‚å?利用ã•れるã ã‘ã?
150                            if( errFlag ) {
151                                    ConnectionFactory.remove( defConn,dbid );                       // 削除
152                                    ConnectionFactory.remove( rscConn,RESOURCE_DBID );
153                            }
154                            else {
155                                    ConnectionFactory.close( defConn,dbid );                        // è¿”å´
156                                    ConnectionFactory.close( rscConn,RESOURCE_DBID );
157                            }
158                    }
159    
160                    return( EVAL_PAGE );
161            }
162    
163            /**
164             * タグリブオブジェクトをリリースã—ã¾ã™ã?
165             * キャãƒ?‚·ãƒ¥ã•れã¦å†åˆ©ç”¨ã•れるã?ã§ã€ãƒ•ィールドã?åˆæœŸè¨­å®šã‚’行ã„ã¾ã™ã?
166             *
167             * @og.rev 5.5.4.5 (2012/07/27) dbid æ–°è¦è¿½åŠ?
168             */
169            @Override
170            protected void release2() {
171                    super.release2();
172                    insFile         = null;
173                    fileURL         = HybsSystem.sys( "FILE_URL" );
174                    dbid            = null;                         // 5.5.4.5 (2012/07/27) æ–°è¦è¿½åŠ?
175            }
176    
177            /**
178             * ã€TAG】æ“作ã™ã‚‹ãƒ•ァイルã®ãƒ?‚£ãƒ¬ã‚¯ãƒˆãƒªã‚’指定ã—ã¾ã?
179             *              (åˆæœŸå€¤:FILE_URL[={@og.value org.opengion.hayabusa.common.SystemData#FILE_URL}])ã€?
180             *
181             * @og.tag
182             * ã“ã?属æ?ã§æŒ?®šã•れるãƒ?‚£ãƒ¬ã‚¯ãƒˆãƒªã®ãƒ•ァイルをæ“作ã—ã¾ã™ã?
183             * æŒ?®šæ–¹æ³•ã?ã€??常㮠fileURL 属æ?ã¨åŒæ§˜ã«ã€å?é ­ãŒã?'/' (UNIX) ã¾ãŸã?ã€?¼’文字目ãŒã?
184             * ":" (Windows)ã®å ´åˆã?ã€æŒ‡å®šã?URLãã?ã¾ã¾ã®ãƒ?‚£ãƒ¬ã‚¯ãƒˆãƒªã«ã€ãã?§ãªã??åˆã?ã€?
185             * (åˆæœŸå€¤:シスãƒ?ƒ å®šæ•°ã®FILE_URL[={@og.value org.opengion.hayabusa.common.SystemData#FILE_URL}])ã€?
186             *
187             * @param       url ファイルURL
188             * @see         org.opengion.hayabusa.common.SystemData#FILE_URL
189             */
190            public void setFileURL( final String url ) {
191                    String furl = nval( getRequestParameter( url ),null );
192                    if( furl != null ) {
193                            char ch = furl.charAt( furl.length()-1 );
194                            if( ch != '/' && ch != '\\' ) { furl = furl + "/"; }
195                            fileURL = StringUtil.urlAppend( fileURL,furl );
196                    }
197            }
198    
199            /**
200             * ã€TAG】インストã?ルファイルを指定ã—ã¾ã™ã?
201             *
202             * @og.tag
203             * インストã?ルファイルを指定ã—ã¾ã™ã?
204             * å?‚³ãƒ³ãƒ?‚­ã‚¹ãƒˆã?インストã?ル用アーカイブをæŒ?®šã™ã‚‹å¿?¦ãŒã‚りã¾ã™ã?
205             *
206             * @param       fname 出力ファイルå?
207             */
208            public void setInsFile( final String fname ) {
209                    insFile = nval( getRequestParameter( fname ),insFile );
210            }
211    
212            /**
213             * ã€TAGã€?通常ã¯ä½¿ã?¾ã›ã‚“)インストã?ルを行ã†DB接続IDを指定ã—ã¾ã™ã?
214             *
215             * @og.tag Queryオブジェクトを作æ?ã™ã‚‹æ™‚ã?DB接続IDを指定ã—ã¾ã™ã?
216             *
217             * @og.rev 5.5.4.5 (2012/07/27) dbid æ–°è¦è¿½åŠ?
218             *
219             * @param       id ãƒ??タベã?ス接続ID
220             */
221            public void setDbid( final String id ) {
222                    dbid = nval( getRequestParameter( id ),dbid );
223            }
224    
225            /**
226             * ã“ã?オブジェクトã?æ–?­—å?表ç¾ã‚’è¿”ã—ã¾ã™ã?
227             * 基本çš?«ãƒ?ƒãƒ?‚°ç›®çš?«ä½¿ç”¨ã—ã¾ã™ã?
228             *
229             * @return ã“ã?ã‚¯ãƒ©ã‚¹ã®æ–?­—å?表ç¾
230             */
231            @Override
232            public String toString() {
233                    return org.opengion.fukurou.util.ToString.title( this.getClass().getName() )
234                                    .println( "VERSION"             ,VERSION        )
235                                    .println( "insFile"             ,insFile        )
236                                    .println( "fileURL"             ,fileURL        )
237                                    .println( "dbid"                ,dbid           )                               // 5.5.4.5 (2012/07/27) æ–°è¦è¿½åŠ?
238                                    .println( "Other..."    ,getAttributes().getAttribute() )
239                                    .fixForm().toString() ;
240            }
241    }