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 if( StringUtil.isNull( plugin,bucket ) 108 || FileOperation.LOCAL.equalsIgnoreCase(plugin) 109 || FileOperation.LOCAL.equalsIgnoreCase(bucket) ) { 110 return new FileOperation( path ); // ローカルFile 111 } 112 113 try { 114 final Object[] args = new Object[] { bucket, path }; 115 116 final String cloudTarget = plugin.toUpperCase( Locale.JAPAN ); // 先に null 判定済み 117 return (FileOperation)Class.forName( CLOUD_PLUGIN + cloudTarget ) 118 .getConstructor(String.class, String.class) 119 .newInstance( args ); 120 } 121 catch ( final Throwable th ) { 122 final String errMsg = "FileOperation 生成で、失敗しました。" + CR 123 + " plugin=" + plugin + " , bucket=" + bucket + CR 124 + " path=" + path + CR ; 125 126 throw new RuntimeException( errMsg,th ); 127 } 128 } 129 130 /** 131 * 引数を元に、ファイル操作クラスのアドレスを解決した新しいオブジェクトを返します。 132 * 133 * 与えたfileオブジェクトがFileOperationだった場合は、プラグインとバケットを取得して 134 * それに基づいたFileOperationを返します。 135 * 標準のFileの場合は、defaultのFileOperationを返します。 136 * 元がnullの場合はnullを返します。 137 * new File( dir,fileName ).getPath() で求めたパスで、生成します。 138 * ※ ファイルのコピーは行いません。 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 * @og.rev 8.0.0.1 (2021/10/08) クラウド修正 142 * 143 * @param file コピー元 144 * @param dir 親パス(ディレクトリ) 145 * @param fileName 子パス 146 * @return 設定をコピーしたのFileOperation 147 */ 148 public static FileOperation resolveFile(final File file, final String dir, final String fileName) { 149 return resolveFile( file, new File(dir,fileName).getPath() ); 150 } 151 152 /** 153 * 引数を元に、ファイル操作クラスのアドレスを解決した新しいオブジェクトを返します。 154 * 155 * 与えたfileオブジェクトがFileOperationだった場合は、プラグインとバケットを取得して 156 * それに基づいた新しいFileOperationを返します。 157 * 標準のFileの場合は、defaultのFileOperationを返します。 158 * 元がnullの場合はnullを返します。 159 * ※ ファイルのコピーは行いません。 160 * 161 * @og.rev 8.0.0.1 (2021/10/08) クラウド修正 162 * 163 * @param file コピー元 164 * @param path パス 165 * @return 設定をコピーしたFileOperation 166 */ 167 public static FileOperation resolveFile(final File file, final String path) { 168 if( file == null) { return null; } // 元がnullの場合はnullを返します。 169 170 // FileOperation型の場合にプラグインを判定する 171 if( file instanceof FileOperation ) { 172 final String plugin = ((FileOperation)file).getPlugin(); 173 final String bucket = ((FileOperation)file).getBucket(); 174 175 return newStorageOperation( plugin, bucket, path ); // クラウドFile/(ローカルFile もありうる) 176 } 177 else { 178 return newStorageOperation( path ); // ローカルFile 179 } 180 } 181}