This repository has been archived by the owner on Mar 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
70 changed files
with
5,339 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
12 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
12 |
Binary file not shown.
36 changes: 36 additions & 0 deletions
36
.svn/pristine/00/006af0660ae56e2862b649bc8053c892fa9dd5fd.svn-base
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** | ||
* | ||
*/ | ||
package com.zhazhapan.qiniu.model; | ||
|
||
/** | ||
* @author pantao | ||
* | ||
*/ | ||
public class Key { | ||
|
||
private String accessKey; | ||
|
||
private String secretKey; | ||
|
||
public Key(String accessKey, String secretKey) { | ||
this.accessKey = accessKey; | ||
this.secretKey = secretKey; | ||
} | ||
|
||
public String getAccessKey() { | ||
return accessKey; | ||
} | ||
|
||
public void setAccessKey(String accessKey) { | ||
this.accessKey = accessKey; | ||
} | ||
|
||
public String getSecretKey() { | ||
return secretKey; | ||
} | ||
|
||
public void setSecretKey(String secretKey) { | ||
this.secretKey = secretKey; | ||
} | ||
} |
201 changes: 201 additions & 0 deletions
201
.svn/pristine/02/0202245c029a442c5bbb0c56e48a969e021b037a.svn-base
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,201 @@ | ||
/** | ||
* | ||
*/ | ||
package com.zhazhapan.qiniu.config; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.BufferedWriter; | ||
import java.io.File; | ||
import java.io.FileReader; | ||
import java.io.FileWriter; | ||
import java.io.IOException; | ||
import java.util.Map; | ||
|
||
import org.apache.log4j.Logger; | ||
|
||
import com.google.gson.Gson; | ||
import com.google.gson.JsonArray; | ||
import com.google.gson.JsonElement; | ||
import com.google.gson.JsonObject; | ||
import com.google.gson.JsonParser; | ||
import com.zhazhapan.qiniu.QiniuApplication; | ||
import com.zhazhapan.qiniu.controller.MainWindowController; | ||
import com.zhazhapan.qiniu.model.Key; | ||
import com.zhazhapan.qiniu.modules.constant.Values; | ||
import com.zhazhapan.qiniu.util.Checker; | ||
import com.zhazhapan.qiniu.util.Formatter; | ||
import com.zhazhapan.qiniu.view.Dialogs; | ||
|
||
import javafx.application.Platform; | ||
|
||
/** | ||
* @author pantao | ||
* | ||
*/ | ||
public class ConfigLoader { | ||
|
||
private static Logger logger = Logger.getLogger(ConfigLoader.class); | ||
|
||
public static String configPath = null; | ||
|
||
/* | ||
* 加载配置文件 | ||
*/ | ||
public static void loadConfig() { | ||
logger.info("start to load configuration"); | ||
File config = new File(configPath); | ||
if (config.exists()) { | ||
// 读取配置文件内容 | ||
JsonObject json = readConfig(); | ||
if (Checker.isNull(json)) { | ||
showInputKeyDialog(); | ||
} else { | ||
// 读取Key | ||
try { | ||
String ak = json.get("accesskey").getAsString(); | ||
String sk = json.get("secretkey").getAsString(); | ||
QiniuApplication.key = new Key(ak, sk); | ||
new QConfig().createAuth(ak, sk); | ||
} catch (Exception e) { | ||
logger.error("read key from configuration failed, message: " + e.getMessage()); | ||
Dialogs.showException(Values.LOAD_CONFIG_ERROR, e); | ||
showInputKeyDialog(); | ||
} | ||
// 读取Bucket | ||
JsonElement buckets = json.get("buckets"); | ||
if (Checker.isNotNull(buckets)) { | ||
logger.info("load buckets into memory"); | ||
JsonArray array = buckets.getAsJsonArray(); | ||
boolean setvalue = true; | ||
for (JsonElement element : array) { | ||
JsonObject obj = (JsonObject) element; | ||
String bucket = obj.get("bucket").getAsString(); | ||
JsonElement url = obj.get("url"); | ||
String zone = obj.get("zone").getAsString(); | ||
String zones = zone + " " + (Checker.isNull(url) ? "" : url.getAsString()); | ||
QiniuApplication.buckets.put(bucket, zones); | ||
Platform.runLater(() -> MainWindowController.getInstance().addItem(bucket)); | ||
if (setvalue) { | ||
Platform.runLater(() -> { | ||
MainWindowController.getInstance().bucketChoiceCombo.setValue(bucket); | ||
MainWindowController.getInstance().zoneText.setText(zone); | ||
}); | ||
setvalue = false; | ||
} | ||
} | ||
} | ||
// 读取文件前缀 | ||
JsonElement prefix = json.get("prefix"); | ||
if (Checker.isNotNull(prefix)) { | ||
JsonArray array = prefix.getAsJsonArray(); | ||
Platform.runLater(() -> { | ||
for (JsonElement element : array) { | ||
String string = element.getAsString(); | ||
QiniuApplication.prefix.add(string); | ||
MainWindowController.getInstance().filePrefixCombo.getItems().add(string); | ||
} | ||
}); | ||
} | ||
} | ||
} else { | ||
logger.info("there is no configuration file, starting to create"); | ||
checkWorkPath(); | ||
// 创建配置文件 | ||
try { | ||
config.createNewFile(); | ||
logger.info("create file 'qiniu.conf' success"); | ||
} catch (IOException e) { | ||
logger.error("create configuration file failed, messages: " + e.getMessage()); | ||
Dialogs.showFatalError(Values.LOAD_CONFIG_ERROR, e); | ||
} | ||
showInputKeyDialog(); | ||
} | ||
// 如果Key是Null则退出程序 | ||
if (Checker.isNull(QiniuApplication.key)) { | ||
System.exit(0); | ||
} | ||
} | ||
|
||
/* | ||
* 检测工作文件夹是否存在 | ||
*/ | ||
public static void checkWorkPath() { | ||
File dir = new File(QiniuApplication.workDir); | ||
if (!dir.exists()) { | ||
dir.mkdirs(); | ||
logger.info("mkdir '/tmp/qiniu/tool' success"); | ||
} | ||
} | ||
|
||
/* | ||
* 初始状态时要求用户输入AccessKey和SecretKey,否则退出程序 | ||
*/ | ||
public static void showInputKeyDialog() { | ||
logger.info("show dialog to require user input key"); | ||
new Dialogs().showInputKeyDialog(); | ||
} | ||
|
||
public static void writeKey(String accessKey, String secretKey) { | ||
QiniuApplication.key = new Key(accessKey, secretKey); | ||
writeConfig(); | ||
} | ||
|
||
public static JsonObject readConfig() { | ||
StringBuilder config = new StringBuilder(); | ||
JsonObject jsonObject = null; | ||
try { | ||
logger.info("load configuration into memory"); | ||
BufferedReader reader = new BufferedReader(new FileReader(configPath)); | ||
String line; | ||
while ((line = reader.readLine()) != null) { | ||
config.append(line); | ||
} | ||
reader.close(); | ||
jsonObject = new JsonParser().parse(config.toString()).getAsJsonObject(); | ||
} catch (IOException e) { | ||
logger.error("load configuration error, messages: " + e.getMessage()); | ||
Dialogs.showFatalError(Values.LOAD_CONFIG_ERROR, e); | ||
} catch (Exception e) { | ||
logger.error("convert json string to json object failed, app'll reset"); | ||
Dialogs.showException(Values.JSON_TO_OBJECT_ERROR, e); | ||
} | ||
return jsonObject; | ||
} | ||
|
||
public static void writeConfig() { | ||
JsonObject config = new JsonObject(); | ||
// Key | ||
config.addProperty("accesskey", QiniuApplication.key.getAccessKey()); | ||
config.addProperty("secretkey", QiniuApplication.key.getSecretKey()); | ||
JsonArray buckets = new JsonArray(); | ||
// Bucket | ||
for (Map.Entry<String, String> entry : QiniuApplication.buckets.entrySet()) { | ||
JsonObject json = new JsonObject(); | ||
json.addProperty("bucket", entry.getKey()); | ||
String[] zones = entry.getValue().split(" "); | ||
json.addProperty("zone", zones[0]); | ||
json.addProperty("url", zones.length > 1 ? zones[1] : ""); | ||
buckets.add(json); | ||
} | ||
config.add("buckets", buckets); | ||
// Prefix | ||
JsonArray prefix = new JsonArray(); | ||
for (String string : QiniuApplication.prefix) { | ||
prefix.add(string); | ||
} | ||
config.add("prefix", prefix); | ||
writeConfig(new Gson().toJson(config)); | ||
} | ||
|
||
public static void writeConfig(String configJson) { | ||
try { | ||
BufferedWriter out = new BufferedWriter(new FileWriter(configPath, false)); | ||
out.write(Formatter.jsonFormat(configJson)); | ||
out.close(); | ||
logger.info("rewrite configuration success"); | ||
} catch (IOException e) { | ||
logger.error("rewrite configuration file failed, messages: " + e.getMessage()); | ||
Dialogs.showFatalError(Values.LOAD_CONFIG_ERROR, e); | ||
} | ||
} | ||
} |
Oops, something went wrong.