001package org.opengion.fukurou.model;
002
003import java.io.File;
004import java.util.Locale;
005import org.opengion.fukurou.util.StringUtil;
006
007
008/**
009 * ファイル操作のファクトリークラス
010 * 
011 * デフォルトはローカルのファイル操作を行うFileOperationクラスを生成します。
012 * 利用プラグイン、バケット、パス等を指定する事でクラウドのオブジェクトストレージに対応した
013 * クラスを生成します。
014 * 
015 * @og.rev 5.10.8.0 (2019/02/01) 新規作成
016 * @og.rev 5.10.9.0 (2019/03/01) 変更対応
017 * @author oota
018 * @since JDK7.0
019 */
020public class FileOperationFactory {
021        private static final int BUFFER_MIDDLE = 200;
022        
023        /**
024         * インスタンス生成
025         * 
026         * 引数を元に、ファイル操作インスタンスを生成します。
027         * ローカルのファイル操作を行うFileOperationクラスを返します。
028         * 
029         * @param path ファイルパス
030         * @return ファイル操作インスタンス
031         */
032        public static FileOperation newStorageOperation(final String path) {
033//              return newStorageOperation( (String)null, null, path.toString());
034                return newStorageOperation( (String)null, null, path);
035        }
036        
037        /**
038         * インスタンス生成
039         * 
040         * 引数を元に、ファイル操作クラスを生成します。
041         * プラグインとバケットを指定する事で、plugin.cloud内のクラウド用のクラスを返します。
042         * 
043         * ディレクトリとファイル名からパスを生成します。
044         * 
045         * @param plugin 利用プラグイン
046         * @param buket バケット名
047         * @param dir ディレクトリ
048         * @param fileName ファイル名
049         * @return ファイル操作インスタンス
050         */
051        public static FileOperation newStorageOperation(final String plugin, final String buket, final String dir, final String fileName) {
052                final StringBuilder path = new StringBuilder(BUFFER_MIDDLE);
053                path.append( dir );
054                
055                if(fileName != null) {
056                        path.append(File.separator).append(fileName);
057                }
058
059                return newStorageOperation(plugin, buket, path.toString());
060        }
061
062        /**
063         * インスタンス生成
064         * 
065         * 引数を元に、ファイル操作クラスを生成します。
066         * プラグインとバケットを指定する事で、plugin.cloud内のクラウド用のクラスを返します。
067         * プラグインがnull、もしくはDEFAULTの場合は標準のFileOperation(ローカルファイル用)を返します。
068         * 
069         * @param plugin 利用プラグイン
070         * @param buket バケット名
071         * @param path ファイルパス
072         * @return ファイル操作インスタンス
073         */
074        public static FileOperation newStorageOperation(final String plugin, final String buket, final String path) {
075                FileOperation rtn;
076                String cloudTarget = null;
077
078                final Object[] args = new Object[] { buket, path };
079
080                // 対象のクラウドサービスを取得(大文字化)。
081                // 未指定の場合は、ローカルディレクトリを利用。
082                if ( plugin != null && plugin.length() > 0 ) {
083                        cloudTarget = plugin.toUpperCase( Locale.JAPAN );
084                }
085
086                try {
087                        final StringBuilder sb = new StringBuilder(BUFFER_MIDDLE);
088
089                        if (StringUtil.isNull(cloudTarget) || "DEFAULT".equals(cloudTarget)) {
090                                sb.append("org.opengion.fukurou.model.FileOperation");
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                } catch (final Throwable th) {
102                        // キャッチしたエラー情報をスロー
103                        throw new RuntimeException(th);
104                }
105
106                return rtn;
107        }
108        
109        
110        /**
111         * インスタンス生成
112         * 
113         * 引数を元に、ファイル操作クラスを生成します。
114         * 与えたfileオブジェクトがFileOperationだった場合は、プラグインとバケットを取得して
115         * それに基づいたFileOperationを返します。
116         * 標準のFileの場合は、defaultのFileOperationを返します。
117         * 元がnullの場合はnullを返します。
118         * 
119         * @param file コピー元
120         * @param dir 親パス(ディレクトリ)
121         * @param fileName 子パス
122         * @return 設定をコピーしたのFileOperation
123         */
124        public static FileOperation newStorageOperation(final File file, final String dir, final String fileName) {
125                String plugin = null;
126                String buket = null;
127                
128                if( file == null) { return null; }
129                
130                // FileOperation型の場合にプラグインを判定する
131                if( file instanceof FileOperation ) {
132                        plugin = ((FileOperation)file).getPlugin();
133                        buket = ((FileOperation)file).getBucket();
134                }
135                
136                return newStorageOperation( plugin, buket, dir, fileName);
137        
138        }
139        
140        /**
141         * インスタンス生成。
142         * 
143         * コピーするタイプで、子パスを与えないパターンです。
144         * 
145         * @param file コピー元
146         * @param path パス
147         * @return 設定をコピーしたのFileOperation
148         */
149        public static FileOperation newStorageOperation(final File file, final String path) {
150                return newStorageOperation( file, path, null);
151        
152        }
153        
154}