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