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