OSDN Git Service

use absolute path.
authorOlyutorskii <olyutorskii@users.osdn.me>
Thu, 28 May 2020 14:41:40 +0000 (23:41 +0900)
committerOlyutorskii <olyutorskii@users.osdn.me>
Thu, 28 May 2020 14:41:40 +0000 (23:41 +0900)
src/main/java/jp/sfjp/jindolf/config/AppSetting.java
src/main/java/jp/sfjp/jindolf/config/ConfigDirUtils.java
src/main/java/jp/sfjp/jindolf/config/ConfigStore.java
src/main/java/jp/sfjp/jindolf/config/FileUtils.java

index 5e8f94c..87bff85 100644 (file)
@@ -101,7 +101,7 @@ public class AppSetting{
                                                   CmdOption.OPT_NOCONF );
         ConfigStore result;
         if(opt == null){
-            result = new ConfigStore(true, null);
+            result = new ConfigStore(null);
         }else{
             switch(opt){
             case OPT_NOCONF:
@@ -111,7 +111,7 @@ public class AppSetting{
                 String optArg = option.getStringArg(opt);
                 Path configPath = Paths.get(optArg);
                 configPath = configPath.toAbsolutePath();
-                result = new ConfigStore(true, configPath);
+                result = new ConfigStore(configPath);
                 break;
             default:
                 result = null;
index 238773e..882662a 100644 (file)
@@ -36,8 +36,6 @@ public final class ConfigDirUtils{
     private static final String TITLE_BUILDCONF =
             VerInfo.TITLE + "設定格納ディレクトリの設定";
 
-    private static final Path JINCONF     = Paths.get("Jindolf");
-    private static final Path JINCONF_DOT = Paths.get(".jindolf");
     private static final Path FILE_README = Paths.get("README.txt");
     private static final Path FILE_AVATARJSON = Paths.get("avatarCache.json");
 
@@ -287,73 +285,6 @@ public final class ConfigDirUtils{
     }
 
     /**
-     * アプリケーション設定ディレクトリを返す。
-     *
-     * <p>存在の有無、アクセスの可否は関知しない。
-     *
-     * <p>WindowsやLinuxではホームディレクトリ。
-     * Mac OS X ではさらにホームディレクトリの下の
-     * "Library/Application Support/"
-     *
-     * @return アプリケーション設定ディレクトリ
-     */
-    public static Path getAppSetDir(){
-        Path home = FileUtils.getHomeDirectory();
-
-        Path result = home;
-
-        if(FileUtils.isMacOSXFs()){
-            result = result.resolve("Library");
-            result = result.resolve("Application Support");
-        }
-
-        return result;
-    }
-
-    /**
-     * 暗黙的な設定格納ディレクトリを返す。
-     *
-     * <ul>
-     *
-     * <li>起動元JARファイルと同じディレクトリに、
-     * アクセス可能なディレクトリ"Jindolf"が
-     * すでに存在していればそれを返す。
-     *
-     * <li>起動元JARファイルおよび"Jindolf"が発見できなければ、
-     * MacOSX環境の場合"~/Library/Application Support/Jindolf/"を返す。
-     * Windows環境の場合"%USERPROFILE%\Jindolf\"を返す。
-     *
-     * <li>それ以外の環境(Linux,etc?)の場合"~/.jindolf/"を返す。
-     *
-     * </ul>
-     *
-     * <p>返すディレクトリが存在しているか否か、
-     * アクセス可能か否かは呼び出し元で判断せよ。
-     *
-     * @return 設定格納ディレクトリ
-     */
-    public static Path getDefaultConfDirPath(){
-        Path jarParent = FileUtils.getJarDirectory();
-        if(FileUtils.isAccessibleDirectory(jarParent)){
-            Path confPath = jarParent.resolve(JINCONF);
-            if(FileUtils.isAccessibleDirectory(confPath)){
-                return confPath;
-            }
-        }
-
-        Path appset = getAppSetDir();
-
-        Path result;
-        if(FileUtils.isMacOSXFs() || FileUtils.isWindowsOSFs()){
-            result = appset.resolve(JINCONF);
-        }else{
-            result = appset.resolve(JINCONF_DOT);
-        }
-
-        return result;
-    }
-
-    /**
      * まだ存在しない設定格納ディレクトリを新規に作成する。
      *
      * <p>エラーがあればダイアログ提示とともにVM終了する。
index 33f2d77..287ad0f 100644 (file)
@@ -13,22 +13,35 @@ import java.nio.file.Path;
 import java.nio.file.Paths;
 
 /**
- * Jindolf設定ディレクトリ以下に配置される各種ファイル資源の管理を行う。
+ * Jindolf設定ディレクトリの管理を行う。
  *
- * <p>管理対象は
+ * <p>デフォルトの設定ディレクトリやコマンドライン引数から構成された、
+ * 設定ディレクトリに関する情報が保持管理される。
+ *
+ * <p>設定ディレクトリには
  *
  * <ul>
- * <li>JSON設定ファイル格納ディレクトリ
+ * <li>ロックファイル
+ * <li>JSON設定ファイル
  * <li>Avatar代替イメージ格納ディレクトリ
- * <li>ロックファイルの獲得/解放。
  * </ul>
+ *
+ * <p>などが配置される。
+ *
+ * <p>コンストラクタに与えられるディレクトリは
+ * 絶対パスでなければならない。
+ *
+ * <p>ロックファイル取得の失敗、
+ * およびその後のユーザインタラクションによっては、
+ * 設定ディレクトリを使わない設定に上書きされた後
+ * 起動処理が続行される場合がありうる。
  */
 public class ConfigStore {
 
-    /** ローカル画像格納ディレクトリ。 */
-    public static final Path LOCALIMG_DIR = Paths.get("img");
-
-    private static final String LOCKFILE = "lock";
+    private static final Path JINCONF      = Paths.get("Jindolf");
+    private static final Path JINCONF_DOT  = Paths.get(".jindolf");
+    private static final Path LOCKFILE     = Paths.get("lock");
+    private static final Path LOCALIMG_DIR = Paths.get("img");
 
 
     private boolean useStoreFile;
@@ -49,9 +62,15 @@ public class ConfigStore {
     /**
      * コンストラクタ。
      *
-     * @param configDirPath 設定ディレクトリ。
+     * <p>このインスタンスでは、
+     * 設定ディレクトリへの入出力を行うが、
+     * デフォルトではない明示されたディレクトリが用いられる。
+     *
+     * @param configDirPath 設定ディレクトリの絶対パス。
+     *     nullの場合はデフォルトの設定ディレクトリが用いられる。
+     * @throws IllegalArgumentException 絶対パスではない。
      */
-    public ConfigStore(Path configDirPath){
+    public ConfigStore(Path configDirPath) throws IllegalArgumentException{
         this(true, configDirPath);
         return;
     }
@@ -61,23 +80,28 @@ public class ConfigStore {
      *
      * @param useStoreFile 設定ディレクトリ内への
      *     入出力機能を使うならtrue
-     * @param configDirPath 設定ディレクトリ。
+     * @param configDirPath è¨­å®\9aã\83\87ã\82£ã\83¬ã\82¯ã\83\88ã\83ªã\81®çµ¶å¯¾ã\83\91ã\82¹ã\80\82
      *     設定ディレクトリを使わない場合は無視される。
      *     この時点でのディレクトリ存在の有無は関知しない。
      *     既存ディレクトリの各種属性チェックは後にチェックするものとする。
      *     nullの場合デフォルトの設定ディレクトリが用いられる。
+     * @throws IllegalArgumentException 絶対パスではない。
      */
-    public ConfigStore(boolean useStoreFile,
-                       Path configDirPath ){
+    protected ConfigStore(boolean useStoreFile,
+                          Path configDirPath )
+            throws IllegalArgumentException{
         super();
 
         this.useStoreFile = useStoreFile;
 
         if(this.useStoreFile){
             if(configDirPath != null){
+                if( ! configDirPath.isAbsolute()){
+                    throw new IllegalArgumentException();
+                }
                 this.configDir = configDirPath;
             }else{
-                this.configDir = ConfigDirUtils.getDefaultConfDirPath();
+                this.configDir = getDefaultConfDirPath();
             }
         }else{
             this.configDir = null;
@@ -91,6 +115,93 @@ public class ConfigStore {
 
 
     /**
+     * 暗黙的な設定格納ディレクトリを絶対パスで返す。
+     *
+     * <ul>
+     *
+     * <li>起動元JARファイルと同じディレクトリに、
+     * アクセス可能なディレクトリ"Jindolf"が
+     * すでに存在していればそれを返す。
+     *
+     * <li>起動元JARファイルおよび"Jindolf"が発見できなければ、
+     * MacOSX環境の場合"~/Library/Application Support/Jindolf/"を返す。
+     * Windows環境の場合"%USERPROFILE%\Jindolf\"を返す。
+     *
+     * <li>それ以外の環境(Linux,etc?)の場合"~/.jindolf/"を返す。
+     *
+     * </ul>
+     *
+     * <p>返すディレクトリが存在しているか否か、
+     * アクセス可能か否かは呼び出し元で判断せよ。
+     *
+     * @return 設定格納ディレクトリの絶対パス
+     */
+    public static Path getDefaultConfDirPath(){
+        Path jarParent = FileUtils.getJarDirectory();
+        if(FileUtils.isAccessibleDirectory(jarParent)){
+            Path confPath = jarParent.resolve(JINCONF);
+            if(FileUtils.isAccessibleDirectory(confPath)){
+                assert confPath.isAbsolute();
+                return confPath;
+            }
+        }
+
+        Path appset = getAppSetDir();
+
+        Path leaf;
+        if(FileUtils.isMacOSXFs() || FileUtils.isWindowsOSFs()){
+            leaf = JINCONF;
+        }else{
+            leaf = JINCONF_DOT;
+        }
+
+        Path result = appset.resolve(leaf);
+        assert result.isAbsolute();
+
+        return result;
+    }
+
+    /**
+     * アプリケーション設定ディレクトリを絶対パスで返す。
+     *
+     * <p>存在の有無、アクセスの可否は関知しない。
+     *
+     * <p>WindowsやLinuxではホームディレクトリ。
+     * Mac OS X ではさらにホームディレクトリの下の
+     * "Library/Application Support/"
+     *
+     * @return アプリケーション設定ディレクトリの絶対パス
+     */
+    private static Path getAppSetDir(){
+        Path home = getHomeDirectory();
+
+        Path result = home;
+
+        if(FileUtils.isMacOSXFs()){
+            result = result.resolve("Library");
+            result = result.resolve("Application Support");
+        }
+
+        return result;
+    }
+
+    /**
+     * ホームディレクトリを得る。
+     *
+     * <p>システムプロパティuser.homeで示されたホームディレクトリを
+     * 絶対パスで返す。
+     *
+     * @return ホームディレクトリの絶対パス。
+     */
+    private static Path getHomeDirectory(){
+        String homeProp = System.getProperty("user.home");
+        Path result = Paths.get(homeProp);
+        result = result.toAbsolutePath();
+        return result;
+    }
+
+
+    /**
      * 設定ディレクトリを使うか否か判定する。
      *
      * @return 設定ディレクトリを使うならtrue。
@@ -100,18 +211,20 @@ public class ConfigStore {
     }
 
     /**
-     * 設定ディレクトリを返す。
+     * 設定ディレクトリを絶対パスで返す。
      *
-     * @return 設定ディレクトリ。設定ディレクトリを使わない場合はnull
+     * @return 設定ディレクトリの絶対パス。
+     * 設定ディレクトリを使わない場合はnull
      */
     public Path getConfigDir(){
         return this.configDir;
     }
 
     /**
-     * ローカル画像格納ディレクトリを返す。
+     * ローカル画像格納ディレクトリを絶対パスで返す。
      *
-     * @return 格納ディレクトリ。格納ディレクトリを使わない場合はnull
+     * @return 格納ディレクトリの絶対パス。
+     * 格納ディレクトリを使わない場合はnull
      */
     public Path getLocalImgDir(){
         if( ! this.useStoreFile ) return null;
@@ -123,6 +236,21 @@ public class ConfigStore {
     }
 
     /**
+     * ロックファイルを絶対パスで返す。
+     *
+     * @return ロックファイルの絶対パス。
+     * 格納ディレクトリを使わない場合はnull
+     */
+    public Path getLockFile(){
+        if( ! this.useStoreFile ) return null;
+        if(this.configDir == null) return null;
+
+        Path lockFile = this.configDir.resolve(LOCKFILE);
+
+        return lockFile;
+    }
+
+    /**
      * 設定ディレクトリの存在を確認し、なければ作る。
      *
      * <p>設定ディレクトリを使わない場合は何もしない。
@@ -155,7 +283,7 @@ public class ConfigStore {
     public void tryLock(){
         if( ! this.useStoreFile ) return;
 
-        File lockFile = new File(this.configDir.toFile(), LOCKFILE);
+        File lockFile = getLockFile().toFile();
         InterVMLock lock = new InterVMLock(lockFile);
 
         lock.tryLock();
index 4513f67..2bf135c 100644 (file)
@@ -93,10 +93,10 @@ public final class FileUtils{
 
     /**
      * クラスがローカルファイルからロードされたのであれば
-     * そのファイルを返す。
+     * その絶対パスを返す。
      *
      * @param klass 任意のクラス
-     * @return ロード元ファイル。見つからなければnull。
+     * @return ã\83­ã\83¼ã\83\89å\85\83ã\83\95ã\82¡ã\82¤ã\83«ã\81®çµ¶å¯¾ã\83\91ã\82¹ã\80\82è¦\8bã\81¤ã\81\8bã\82\89ã\81ªã\81\91ã\82\8cã\81°nullã\80\82
      */
     public static Path getClassSourcePath(Class<?> klass){
         URL location = getClassSourceUrl(klass);
@@ -114,6 +114,7 @@ public final class FileUtils{
         }
 
         Path result = Paths.get(uri);
+        result = result.toAbsolutePath();
 
         return result;
     }
@@ -142,10 +143,10 @@ public final class FileUtils{
 
     /**
      * クラスがローカルJARファイルからロードされたのであれば
-     * その格納ディレクトリを返す。
+     * ã\81\9dã\81®æ ¼ç´\8dã\83\87ã\82£ã\83¬ã\82¯ã\83\88ã\83ªã\81®çµ¶å¯¾ã\83\91ã\82¹ã\82\92è¿\94ã\81\99ã\80\82
      *
      * @param klass 任意のクラス
-     * @return ロード元JARファイルの格納ディレクトリ。
+     * @return ã\83­ã\83¼ã\83\89å\85\83JARã\83\95ã\82¡ã\82¤ã\83«ã\81®æ ¼ç´\8dã\83\87ã\82£ã\83¬ã\82¯ã\83\88ã\83ªã\81®çµ¶å¯¾ã\83\91ã\82¹ã\80\82
      *     JARが見つからない、もしくはロード元がJARファイルでなければnull。
      */
     public static Path getJarDirectory(Class<?> klass){
@@ -156,14 +157,17 @@ public final class FileUtils{
             return null;
         }
 
-        return jarFile.getParent();
+        Path result = jarFile.getParent();
+        assert result.isAbsolute();
+
+        return result;
     }
 
     /**
      * このクラスがローカルJARファイルからロードされたのであれば
-     * その格納ディレクトリを返す。
+     * ã\81\9dã\81®æ ¼ç´\8dã\83\87ã\82£ã\83¬ã\82¯ã\83\88ã\83ªã\81®çµ¶å¯¾ã\83\91ã\82¹ã\82\92è¿\94ã\81\99ã\80\82
      *
-     * @return ロード元JARファイルの格納ディレクトリ。
+     * @return ã\83­ã\83¼ã\83\89å\85\83JARã\83\95ã\82¡ã\82¤ã\83«ã\81®æ ¼ç´\8dã\83\87ã\82£ã\83¬ã\82¯ã\83\88ã\83ªã\81®çµ¶å¯¾ã\83\91ã\82¹ã\80\82
      *     JARが見つからない、もしくはロード元がJARファイルでなければnull。
      */
     public static Path getJarDirectory(){
@@ -171,19 +175,6 @@ public final class FileUtils{
     }
 
     /**
-     * ホームディレクトリを得る。
-     *
-     * <p>システムプロパティuser.homeで示されたホームディレクトリを返す。
-     *
-     * @return ホームディレクトリ。
-     */
-    public static Path getHomeDirectory(){
-        String homeProp = System.getProperty("user.home");
-        Path result = Paths.get(homeProp);
-        return result;
-    }
-
-    /**
      * MacOSX環境か否か判定する。
      *
      * @return MacOSX環境ならtrue