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 */ 020// public class FileOperationFactory { 021public final class FileOperationFactory { // 7.2.9.4 (2020/11/20) PMD:A class which only has private constructors should be final 022 private static final int BUFFER_MIDDLE = 200; 023 024 /** 025 * オブジェクトを作らせない為の、private コンストラクタ 026 * 027 * @og.rev 7.2.9.4 (2020/11/20) オブジェクトを作らせない為の、private コンストラクタ 028 */ 029 private FileOperationFactory() {} 030 031 /** 032 * インスタンス生成 033 * 034 * 引数を元に、ファイル操作インスタンスを生成します。 035 * ローカルのファイル操作を行うFileOperationクラスを返します。 036 * 037 * @param path ファイルパス 038 * @return ファイル操作インスタンス 039 */ 040 public static FileOperation newStorageOperation(final String path) { 041// return newStorageOperation( (String)null, null, path.toString()); 042 return newStorageOperation( (String)null, null, path); 043 } 044 045 /** 046 * インスタンス生成 047 * 048 * 引数を元に、ファイル操作クラスを生成します。 049 * プラグインとバケットを指定する事で、plugin.cloud内のクラウド用のクラスを返します。 050 * 051 * ディレクトリとファイル名からパスを生成します。 052 * 053 * @param plugin 利用プラグイン 054 * @param buket バケット名 055 * @param dir ディレクトリ 056 * @param fileName ファイル名 057 * @return ファイル操作インスタンス 058 */ 059 public static FileOperation newStorageOperation(final String plugin, final String buket, final String dir, final String fileName) { 060 final StringBuilder path = new StringBuilder(BUFFER_MIDDLE); 061 path.append( dir ); 062 063 if(fileName != null) { 064 path.append(File.separator).append(fileName); 065 } 066 067 return newStorageOperation(plugin, buket, path.toString()); 068 } 069 070 /** 071 * インスタンス生成 072 * 073 * 引数を元に、ファイル操作クラスを生成します。 074 * プラグインとバケットを指定する事で、plugin.cloud内のクラウド用のクラスを返します。 075 * プラグインがnull、もしくはDEFAULTの場合は標準のFileOperation(ローカルファイル用)を返します。 076 * 077 * @param plugin 利用プラグイン 078 * @param buket バケット名 079 * @param path ファイルパス 080 * @return ファイル操作インスタンス 081 */ 082 public static FileOperation newStorageOperation(final String plugin, final String buket, final String path) { 083 FileOperation rtn; 084 String cloudTarget = null; 085 086 final Object[] args = new Object[] { buket, path }; 087 088 // 対象のクラウドサービスを取得(大文字化)。 089 // 未指定の場合は、ローカルディレクトリを利用。 090 if ( plugin != null && plugin.length() > 0 ) { 091 cloudTarget = plugin.toUpperCase( Locale.JAPAN ); 092 } 093 094 try { 095 final StringBuilder sb = new StringBuilder(BUFFER_MIDDLE); 096 097 if (StringUtil.isNull(cloudTarget) || "DEFAULT".equals(cloudTarget)) { 098 sb.append("org.opengion.fukurou.model.FileOperation"); 099 } else { 100 sb.append("org.opengion.plugin.cloud.") 101 .append("FileOperation_") 102 .append(cloudTarget); 103 } 104 105 rtn = (FileOperation) Class.forName(sb.toString()) 106 .getConstructor(String.class, String.class) 107 .newInstance(args); 108// } catch (Exception e) { 109 } catch (final Throwable th) { 110 // キャッチしたエラー情報をスロー 111 throw new RuntimeException(th); 112 } 113 114 return rtn; 115 } 116 117 /** 118 * インスタンス生成 119 * 120 * 引数を元に、ファイル操作クラスを生成します。 121 * 与えたfileオブジェクトがFileOperationだった場合は、プラグインとバケットを取得して 122 * それに基づいたFileOperationを返します。 123 * 標準のFileの場合は、defaultのFileOperationを返します。 124 * 元がnullの場合はnullを返します。 125 * 126 * @og.rev 7.2.9.4 (2020/11/20) PMD:Avoid declaring a variable if it is unreferenced before a possible exit point. 127 * 128 * @param file コピー元 129 * @param dir 親パス(ディレクトリ) 130 * @param fileName 子パス 131 * @return 設定をコピーしたのFileOperation 132 */ 133 public static FileOperation newStorageOperation(final File file, final String dir, final String fileName) { 134 if( file == null) { return null; } 135 136 String plugin = null; 137 String buket = null; 138 139// if( file == null) { return null; } // PMD:Avoid declaring a variable if it is unreferenced before a possible exit point. 140 141 // FileOperation型の場合にプラグインを判定する 142 if( file instanceof FileOperation ) { 143 plugin = ((FileOperation)file).getPlugin(); 144 buket = ((FileOperation)file).getBucket(); 145 } 146 147 return newStorageOperation( plugin, buket, dir, fileName); 148 } 149 150 /** 151 * インスタンス生成。 152 * 153 * コピーするタイプで、子パスを与えないパターンです。 154 * 155 * @param file コピー元 156 * @param path パス 157 * @return 設定をコピーしたのFileOperation 158 */ 159 public static FileOperation newStorageOperation(final File file, final String path) { 160 return newStorageOperation( file, path, null); 161 } 162}