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 */
016package org.opengion.hayabusa.taglib;
017
018import static org.opengion.fukurou.util.StringUtil.nval;
019
020import java.io.File;
021import java.io.PrintWriter;
022import java.sql.Connection;
023import java.sql.SQLException;
024
025import org.opengion.fukurou.db.ConnectionFactory;
026import org.opengion.fukurou.util.StringUtil;
027import org.opengion.hayabusa.common.HybsSystem;
028import org.opengion.hayabusa.common.HybsSystemException;
029import org.opengion.hayabusa.common.SystemInstaller;
030
031/**
032 * システムのインストールを行うためのタグです。
033 *
034 * システムインストール用のアーカイブ(ZIP)形式を指定することで、以下の設定を自動で行います。
035 * @webapps以下へのコンテキストファイルの展開T
036 * ATomcatのコンテキスト設定
037 * B各種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 * ●形式:<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 */
078public 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) または、2文字目が、
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}