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;
023
024import org.opengion.fukurou.db.ConnectionFactory;
025import org.opengion.fukurou.util.StringUtil;
026import org.opengion.hayabusa.common.HybsSystem;
027import org.opengion.hayabusa.common.HybsSystemException;
028import org.opengion.hayabusa.common.SystemInstaller;
029
030/**
031 * システムのインストールを行うためのタグです。
032 *
033 * システムインストール用のアーカイブ(ZIP)形式を指定することで、以下の設定を自動で行います。
034 * ①webapps以下へのコンテキストファイルの展開T
035 * ②Tomcatのコンテキスト設定
036 * ③各種DB環境のインストール
037 *
038 * 5.6.7.0 (2013/07/27) 
039 * ※ インストールするデータベースは、dbid パラメータで指定できますが、リソースを登録するデータベースは
040 * 実行している コンテキストの RESOURCE_DBID で、外部から指定できません。ご注意ください。
041 *
042 * このアーカイブは、ルートディレクトリにコンテキストパスのドキュメントベースが配置されている必要があります。
043 *
044 * [アーカイブの構成]
045 * xx.zip - gf - db - ...               (DB環境インストール用のスクリプト)
046 *             - filetemp - ...         (一時ファイルの保存場所 ※中身は空)
047 *             - jsp - ...              (画面JSP)
048 *             - log - ...              (ログファイルの保存場所 ※中身は空)
049 *             - WEB-INF -  ...         (接続先情報、openGionのjarファイル)
050 *                       - [CONTEXT].xml(コンテキスト設定ファイル)
051 * 
052 * ※WEB-INF直下の[CONTEXT].xmlは、Tomcatのconfディレクトリ以下に、コンテキスト設定ファイルとして、コピーされます。
053 *   なお、この[CONTEXT].xmlがアーカイブに含まれていない場合、インストールは行われません。
054 *
055 * @og.formSample
056 * ●形式:<og:sysInstall fileURL="…" insFile="…" />
057 * ●body:なし
058 *
059 * ●Tag定義:
060 *   <og:sysInstall
061 *       insFile          ○【TAG】インストールファイルを指定します(必須)。
062 *       fileURL            【TAG】操作するファイルのディレクトリを指定します (初期値:FILE_URL[=filetemp/])
063 *       dbid               【TAG】(通常は使いません)インストールを行うDB接続IDを指定します。
064 *       debug              【TAG】デバッグ情報を出力するかどうか[true/false]を指定します(初期値:false)
065 *   />
066 *
067 * ●使用例
068 *    ・Calc(ods)ファイルをPDFに変換
069 *        <og:sysInstall insFile="context.zip" />
070 *
071 * @og.group その他部品
072 *
073 * @version  4.0
074 * @author       Hiroki Nakamura
075 * @since    JDK5.0,
076 */
077public class SysInstallTag extends CommonTagSupport {
078        //* このプログラムのVERSION文字列を設定します。   {@value} */
079        private static final String VERSION = "5.6.7.0 (2013/07/27)";
080
081        private static final long serialVersionUID = 567020130727L ;
082
083        private String  insFile         = null;
084        private String  fileURL         = HybsSystem.sys( "FILE_URL" );
085        private String  dbid            = null;                         // 5.5.4.5 (2012/07/27) 新規追加
086
087        private final String RESOURCE_DBID = HybsSystem.sys( "RESOURCE_DBID" );         // 5.6.7.0 (2013/07/27) リソース系DBID
088
089        /**
090         * Taglibの開始タグが見つかったときに処理する doStartTag() を オーバーライドします。
091         *
092         * @return      後続処理の指示( SKIP_BODY )
093         */
094        @Override
095        public int doStartTag() {
096                return SKIP_BODY ;
097        }
098
099        /**
100         * Taglibの終了タグが見つかったときに処理する doEndTag() を オーバーライドします。
101         *
102         * @og.rev 5.5.4.5 (2012/07/27) dbid 新規追加
103         * @og.rev 5.6.7.0 (2013/07/27) リソース登録用とアプリケーション登録用のコネクションを分ける
104         *
105         * @return      後続処理の指示
106         */
107        @Override
108        public int doEndTag() {
109                debugPrint();
110
111                String directory = HybsSystem.url2dir( fileURL );
112                File buildFile = new File( StringUtil.urlAppend( directory,insFile ) );
113
114                // 5.6.7.0 (2013/07/27) リソース登録用とアプリケーション登録用のコネクションを分ける
115                // エラー処理も合わせて、修正しておきます。
116                Connection defConn = null;
117                Connection rscConn = null;
118                boolean errFlag = true;
119                try {
120                        defConn = ConnectionFactory.connection( dbid, getApplicationInfo() );
121                        rscConn = ConnectionFactory.connection( RESOURCE_DBID, getApplicationInfo() );
122                        PrintWriter out = new PrintWriter( pageContext.getOut() );
123                        SystemInstaller installer = new SystemInstaller( defConn , rscConn , out );             // 5.6.7.0 (2013/07/27)
124                        installer.install( buildFile );
125                        errFlag = false;                // エラーではない
126                }
127                catch( Throwable ex ) {
128                        String errMsg = "インストール中に、DB処理でエラーが発生しました。"
129                                                + ex.getMessage()  + HybsSystem.CR ;
130                        throw new HybsSystemException( errMsg, ex );
131                }
132                finally {
133                        // 以下のコーディングの見直しを考える必要有り。
134                        // Connection はプールしている為、close() しても再利用されるだけ。
135                        if( errFlag ) {
136                                ConnectionFactory.remove( defConn,dbid );                       // 削除
137                                ConnectionFactory.remove( rscConn,RESOURCE_DBID );
138                        }
139                        else {
140                                ConnectionFactory.close( defConn,dbid );                        // 返却
141                                ConnectionFactory.close( rscConn,RESOURCE_DBID );
142                        }
143                }
144
145                return EVAL_PAGE ;
146        }
147
148        /**
149         * タグリブオブジェクトをリリースします。
150         * キャッシュされて再利用されるので、フィールドの初期設定を行います。
151         *
152         * @og.rev 5.5.4.5 (2012/07/27) dbid 新規追加
153         */
154        @Override
155        protected void release2() {
156                super.release2();
157                insFile         = null;
158                fileURL         = HybsSystem.sys( "FILE_URL" );
159                dbid            = null;                         // 5.5.4.5 (2012/07/27) 新規追加
160        }
161
162        /**
163         * 【TAG】操作するファイルのディレクトリを指定します
164         *              (初期値:FILE_URL[={@og.value org.opengion.hayabusa.common.SystemData#FILE_URL}])。
165         *
166         * @og.tag
167         * この属性で指定されるディレクトリのファイルを操作します。
168         * 指定方法は、通常の fileURL 属性と同様に、先頭が、'/' (UNIX) または、2文字目が、
169         * ":" (Windows)の場合は、指定のURLそのままのディレクトリに、そうでない場合は、
170         * (初期値:システム定数のFILE_URL[={@og.value org.opengion.hayabusa.common.SystemData#FILE_URL}])。
171         *
172         * @param       url ファイルURL
173         * @see         org.opengion.hayabusa.common.SystemData#FILE_URL
174         */
175        public void setFileURL( final String url ) {
176                String furl = nval( getRequestParameter( url ),null );
177                if( furl != null ) {
178                        char ch = furl.charAt( furl.length()-1 );
179                        if( ch != '/' && ch != '\\' ) { furl = furl + "/"; }
180                        fileURL = StringUtil.urlAppend( fileURL,furl );
181                }
182        }
183
184        /**
185         * 【TAG】インストールファイルを指定します。
186         *
187         * @og.tag
188         * インストールファイルを指定します。
189         * 各コンテキストのインストール用アーカイブを指定する必要があります。
190         *
191         * @param       fname 出力ファイル名
192         */
193        public void setInsFile( final String fname ) {
194                insFile = nval( getRequestParameter( fname ),insFile );
195        }
196
197        /**
198         * 【TAG】(通常は使いません)インストールを行うDB接続IDを指定します。
199         *
200         * @og.tag Queryオブジェクトを作成する時のDB接続IDを指定します。
201         *
202         * @og.rev 5.5.4.5 (2012/07/27) dbid 新規追加
203         *
204         * @param       id データベース接続ID
205         */
206        public void setDbid( final String id ) {
207                dbid = nval( getRequestParameter( id ),dbid );
208        }
209
210        /**
211         * このオブジェクトの文字列表現を返します。
212         * 基本的にデバッグ目的に使用します。
213         *
214         * @return このクラスの文字列表現
215         */
216        @Override
217        public String toString() {
218                return org.opengion.fukurou.util.ToString.title( this.getClass().getName() )
219                                .println( "VERSION"             ,VERSION        )
220                                .println( "insFile"             ,insFile        )
221                                .println( "fileURL"             ,fileURL        )
222                                .println( "dbid"                ,dbid           )                               // 5.5.4.5 (2012/07/27) 新規追加
223                                .println( "Other..."    ,getAttributes().getAttribute() )
224                                .fixForm().toString() ;
225        }
226}