4 * License : The MIT License
5 * Copyright(c) 2012 olyutorskii
8 package jp.sfjp.jindolf.config;
10 import java.nio.file.Files;
11 import java.nio.file.Path;
12 import java.nio.file.Paths;
15 * Jindolf設定ディレクトリの管理を行う。
18 * アプリのコマンドライン引数から構成された、
19 * 設定ディレクトリに関する情報が保持管理される。
21 * <p>基本的に1アプリのみが設定ディレクトリへの入出力を許される。
24 * 設定ディレクトリを使わない設定に変更することが可能。
32 * <li>Avatar代替イメージ格納ディレクトリ
37 * <p>コンストラクタに与えられるディレクトリは
41 * およびその後のユーザインタラクションによっては、
42 * 設定ディレクトリを使わない設定に上書きされた後
45 public class ConfigStore {
47 private static final Path JINCONF = Paths.get("Jindolf");
48 private static final Path JINCONF_DOT = Paths.get(".jindolf");
49 private static final Path LOCKFILE = Paths.get("lock");
50 private static final Path LOCALIMG_DIR = Paths.get("img");
52 private static final Path MAC_LIB = Paths.get("Library");
53 private static final Path MAC_APPSUPP = Paths.get("Application Support");
56 private boolean useStoreFile;
57 private Path configDir;
76 * デフォルトではない明示されたディレクトリが用いられる。
78 * @param configDirPath 設定ディレクトリの絶対パス。
79 * nullの場合はデフォルトの設定ディレクトリが用いられる。
80 * @throws IllegalArgumentException 絶対パスではない。
82 public ConfigStore(Path configDirPath) throws IllegalArgumentException{
83 this(true, configDirPath);
90 * @param useStoreFile 設定ディレクトリ内への
92 * @param configDirPath 設定ディレクトリの絶対パス。
93 * 設定ディレクトリを使わない場合は無視される。
94 * この時点でのディレクトリ存在の有無は関知しない。
95 * 既存ディレクトリの各種属性チェックは後にチェックするものとする。
96 * nullの場合デフォルトの設定ディレクトリが用いられる。
97 * @throws IllegalArgumentException 絶対パスではない。
99 protected ConfigStore(boolean useStoreFile,
101 throws IllegalArgumentException{
104 this.useStoreFile = useStoreFile;
106 if(this.useStoreFile){
107 if(configDirPath != null){
108 if( ! configDirPath.isAbsolute()){
109 throw new IllegalArgumentException();
111 this.configDir = configDirPath;
113 this.configDir = getDefaultConfDirPath();
116 this.configDir = null;
119 assert ( this.useStoreFile && this.configDir != null)
120 || ( ( ! this.useStoreFile) && this.configDir == null);
127 * 暗黙的な設定格納ディレクトリを絶対パスで返す。
131 * <li>起動元JARファイルと同じディレクトリに、
132 * アクセス可能なディレクトリ"Jindolf"が
135 * <li>起動元JARファイルおよび"Jindolf"が発見できなければ、
136 * MacOSX環境の場合"~/Library/Application Support/Jindolf/"を返す。
137 * Windows環境の場合"%USERPROFILE%\Jindolf\"を返す。
139 * <li>それ以外の環境(Linux,etc?)の場合"~/.jindolf/"を返す。
143 * <p>返すディレクトリが存在しているか否か、
144 * アクセス可能か否かは呼び出し元で判断せよ。
146 * @return 設定格納ディレクトリの絶対パス
148 public static Path getDefaultConfDirPath(){
149 Path jarParent = FileUtils.getJarDirectory();
150 if(FileUtils.isAccessibleDirectory(jarParent)){
151 Path confPath = jarParent.resolve(JINCONF);
152 if(FileUtils.isAccessibleDirectory(confPath)){
153 assert confPath.isAbsolute();
158 Path appset = getAppSetDir();
161 if(FileUtils.isMacOSXFs() || FileUtils.isWindowsOSFs()){
167 Path result = appset.resolve(leaf);
168 assert result.isAbsolute();
174 * アプリケーション設定ディレクトリを絶対パスで返す。
176 * <p>存在の有無、アクセスの可否は関知しない。
178 * <p>WindowsやLinuxではホームディレクトリ。
179 * Mac OS X ではさらにホームディレクトリの下の
180 * "Library/Application Support/"
182 * @return アプリケーション設定ディレクトリの絶対パス
184 private static Path getAppSetDir(){
185 Path home = getHomeDirectory();
189 if(FileUtils.isMacOSXFs()){
190 result = result.resolve(MAC_LIB);
191 result = result.resolve(MAC_APPSUPP);
200 * <p>システムプロパティuser.homeで示されたホームディレクトリを
203 * @return ホームディレクトリの絶対パス。
205 private static Path getHomeDirectory(){
206 String homeProp = System.getProperty("user.home");
207 Path result = Paths.get(homeProp);
208 result = result.toAbsolutePath();
214 * 設定ディレクトリを使うか否か判定する。
216 * @return 設定ディレクトリを使うならtrue。
218 public boolean useStoreFile(){
219 return this.useStoreFile;
225 * @return 設定ディレクトリの絶対パス。
226 * 設定ディレクトリを使わない場合はnull
228 public Path getConfigDir(){
229 return this.configDir;
233 * 設定ディレクトリを使わない設定に変更する。
235 public void setNoConf(){
236 this.useStoreFile = false;
237 this.configDir = null;
242 * ローカル画像格納ディレクトリを絶対パスで返す。
244 * @return 格納ディレクトリの絶対パス。
245 * 格納ディレクトリを使わない場合はnull
247 public Path getLocalImgDir(){
248 if( ! this.useStoreFile ) return null;
249 if(this.configDir == null) return null;
251 Path result = this.configDir.resolve(LOCALIMG_DIR);
259 * @return ロックファイルの絶対パス。
260 * 格納ディレクトリを使わない場合はnull
262 public Path getLockFile(){
263 if( ! this.useStoreFile ) return null;
264 if(this.configDir == null) return null;
266 Path lockFile = this.configDir.resolve(LOCKFILE);
272 * 設定ディレクトリの存在を確認し、なければ作る。
274 * <p>設定ディレクトリを使わない場合は何もしない。
276 public void prepareConfigDir(){
277 if( ! this.useStoreFile ) return;
279 if( ! Files.exists(this.configDir) ){
281 ConfigDirUtils.buildConfDirPath(this.configDir);
282 ConfigDirUtils.checkDirPerm(created);
284 ConfigDirUtils.checkDirPerm(this.configDir);
287 Path imgDir = this.configDir.resolve("img");
288 if( ! Files.exists(imgDir) ){
289 ConfigDirUtils.buildImageCacheDir(imgDir);