001package org.opengion.fukurou.model;
002
003import java.io.File;
004
005import org.opengion.fukurou.util.StringUtil;
006
007
008/**
009 * ファイル操作インスタンスのファクトリークラス
010 * 
011 * システムリソースの「CLOUD_TARGET」を参照して、
012 * 処理対象のファイル操作クラスを生成します。
013 * 
014 * デフォルトはローカルサーバのファイル操作を行う、
015 * DefaultFileOperationクラスを生成します。
016 * 
017 * @og.rev 5.10.8.0 (2019/02/01) 新規作成
018 * @sinse JDK7.0
019 */
020public class FileOperationFactory {
021        private static final int BUFFER_MIDDLE = 200;
022        
023        /**
024         * 引数を元に、ファイル操作クラスを生成します。
025         * 
026         * @param path ファイルパス
027         * @return ファイル操作インスタンス
028         */
029        public static FileOperation newStorageOperation(String path) {
030                return newStorageOperation(null, null, path.toString());
031        }
032        
033        /**
034         * 引数を元に、ファイル操作クラスを生成します。
035         * 
036         * @param plugin 利用プラグイン
037         * @param buket バケット名
038         * @param dir ディレクトリ
039         * @param fileName ファイル名
040         * @return ファイル操作インスタンス
041         */
042        public static FileOperation newStorageOperation(String plugin, String buket, String dir, String fileName) {
043                StringBuilder path = new StringBuilder(BUFFER_MIDDLE);
044                path.append(dir).append(File.separator).append(fileName);
045
046                return newStorageOperation(path.toString());
047        }
048
049        /**
050         * 引数を元に、ファイル操作クラスを生成します。
051         * 
052         * @param plugin 利用プラグイン
053         * @param buket バケット名
054         * @param file ファイル情報
055         * @param fileName ファイル名
056         * @return ファイル操作インスタンス
057         */
058        public static FileOperation newStorageOperation(String plugin, String buket, FileOperation file, String fileName) {
059                StringBuilder path = new StringBuilder(BUFFER_MIDDLE);
060                path.append(file.getPath()).append(File.separator).append(fileName);
061
062                return newStorageOperation(path.toString());
063        }
064
065        /**
066         * 引数を元に、ファイル操作クラスを生成します。
067         * CLOUD_TARGETがNULLの場合はローカルファイル用のクラスを利用します。
068         * 
069         * @param plugin 利用プラグイン
070         * @param buket バケット名
071         * @param path ファイルパス
072         * @return ファイル操作インスタンス
073         */
074        public static FileOperation newStorageOperation(String plugin, String buket, String path) {
075                FileOperation rtn;
076                String cloudTarget = null;
077
078                Object[] args = new Object[] { buket, path };
079
080                // 対象のクラウドサービスを取得(大文字化)。
081                // 未指定の場合は、ローカルディレクトリを利用。
082                if ( plugin != null && plugin.length() > 0 ) {
083                        cloudTarget = plugin.toUpperCase();
084                }
085
086                try {
087                        StringBuilder sb = new StringBuilder(BUFFER_MIDDLE);
088
089                        if (StringUtil.isNull(cloudTarget)) {
090                                sb.append("org.opengion.fukurou.model.DefaultFileOperation");
091                        } else {
092                                sb.append("org.opengion.plugin.cloud.");
093                                sb.append("FileOperation_");
094                                sb.append(cloudTarget);
095                        }
096
097                        rtn = (FileOperation) Class.forName(sb.toString())
098                                        .getConstructor(String.class, String.class)
099                                        .newInstance(args);
100                } catch (Exception e) {
101                        StringBuilder errMsg = new StringBuilder(BUFFER_MIDDLE);
102                        errMsg.append("ファイルストレージの操作クラス生成に失敗しました。target:").append(cloudTarget);
103                        errMsg.append(" システムエラー情報:").append(e.getMessage());
104                        throw new RuntimeException(errMsg.toString());
105                }
106
107                return rtn;
108        }
109}