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