|
| 1 | +/** |
| 2 | + * |
| 3 | + */ |
| 4 | +package com.zhazhapan.qiniu.config; |
| 5 | + |
| 6 | +import java.io.BufferedReader; |
| 7 | +import java.io.BufferedWriter; |
| 8 | +import java.io.File; |
| 9 | +import java.io.FileReader; |
| 10 | +import java.io.FileWriter; |
| 11 | +import java.io.IOException; |
| 12 | +import java.util.Map; |
| 13 | + |
| 14 | +import org.apache.log4j.Logger; |
| 15 | + |
| 16 | +import com.google.gson.Gson; |
| 17 | +import com.google.gson.JsonArray; |
| 18 | +import com.google.gson.JsonElement; |
| 19 | +import com.google.gson.JsonObject; |
| 20 | +import com.google.gson.JsonParser; |
| 21 | +import com.zhazhapan.qiniu.QiniuApplication; |
| 22 | +import com.zhazhapan.qiniu.controller.MainWindowController; |
| 23 | +import com.zhazhapan.qiniu.model.Key; |
| 24 | +import com.zhazhapan.qiniu.modules.constant.Values; |
| 25 | +import com.zhazhapan.qiniu.util.Checker; |
| 26 | +import com.zhazhapan.qiniu.util.Formatter; |
| 27 | +import com.zhazhapan.qiniu.view.Dialogs; |
| 28 | + |
| 29 | +import javafx.application.Platform; |
| 30 | + |
| 31 | +/** |
| 32 | + * @author pantao |
| 33 | + * |
| 34 | + */ |
| 35 | +public class ConfigLoader { |
| 36 | + |
| 37 | + private static Logger logger = Logger.getLogger(ConfigLoader.class); |
| 38 | + |
| 39 | + public static String configPath = null; |
| 40 | + |
| 41 | + /* |
| 42 | + * 加载配置文件 |
| 43 | + */ |
| 44 | + public static void loadConfig() { |
| 45 | + logger.info("start to load configuration"); |
| 46 | + File config = new File(configPath); |
| 47 | + if (config.exists()) { |
| 48 | + // 读取配置文件内容 |
| 49 | + JsonObject json = readConfig(); |
| 50 | + if (Checker.isNull(json)) { |
| 51 | + showInputKeyDialog(); |
| 52 | + } else { |
| 53 | + // 读取Key |
| 54 | + try { |
| 55 | + String ak = json.get("accesskey").getAsString(); |
| 56 | + String sk = json.get("secretkey").getAsString(); |
| 57 | + QiniuApplication.key = new Key(ak, sk); |
| 58 | + new QConfig().createAuth(ak, sk); |
| 59 | + } catch (Exception e) { |
| 60 | + logger.error("read key from configuration failed, message: " + e.getMessage()); |
| 61 | + Dialogs.showException(Values.LOAD_CONFIG_ERROR, e); |
| 62 | + showInputKeyDialog(); |
| 63 | + } |
| 64 | + // 读取Bucket |
| 65 | + JsonElement buckets = json.get("buckets"); |
| 66 | + if (Checker.isNotNull(buckets)) { |
| 67 | + logger.info("load buckets into memory"); |
| 68 | + JsonArray array = buckets.getAsJsonArray(); |
| 69 | + boolean setvalue = true; |
| 70 | + for (JsonElement element : array) { |
| 71 | + JsonObject obj = (JsonObject) element; |
| 72 | + String bucket = obj.get("bucket").getAsString(); |
| 73 | + JsonElement url = obj.get("url"); |
| 74 | + String zone = obj.get("zone").getAsString(); |
| 75 | + String zones = zone + " " + (Checker.isNull(url) ? "" : url.getAsString()); |
| 76 | + QiniuApplication.buckets.put(bucket, zones); |
| 77 | + Platform.runLater(() -> MainWindowController.getInstance().addItem(bucket)); |
| 78 | + if (setvalue) { |
| 79 | + Platform.runLater(() -> { |
| 80 | + MainWindowController.getInstance().bucketChoiceCombo.setValue(bucket); |
| 81 | + MainWindowController.getInstance().zoneText.setText(zone); |
| 82 | + }); |
| 83 | + setvalue = false; |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + // 读取文件前缀 |
| 88 | + JsonElement prefix = json.get("prefix"); |
| 89 | + if (Checker.isNotNull(prefix)) { |
| 90 | + JsonArray array = prefix.getAsJsonArray(); |
| 91 | + Platform.runLater(() -> { |
| 92 | + for (JsonElement element : array) { |
| 93 | + String string = element.getAsString(); |
| 94 | + QiniuApplication.prefix.add(string); |
| 95 | + MainWindowController.getInstance().filePrefixCombo.getItems().add(string); |
| 96 | + } |
| 97 | + }); |
| 98 | + } |
| 99 | + } |
| 100 | + } else { |
| 101 | + logger.info("there is no configuration file, starting to create"); |
| 102 | + checkWorkPath(); |
| 103 | + // 创建配置文件 |
| 104 | + try { |
| 105 | + config.createNewFile(); |
| 106 | + logger.info("create file 'qiniu.conf' success"); |
| 107 | + } catch (IOException e) { |
| 108 | + logger.error("create configuration file failed, messages: " + e.getMessage()); |
| 109 | + Dialogs.showFatalError(Values.LOAD_CONFIG_ERROR, e); |
| 110 | + } |
| 111 | + showInputKeyDialog(); |
| 112 | + } |
| 113 | + // 如果Key是Null则退出程序 |
| 114 | + if (Checker.isNull(QiniuApplication.key)) { |
| 115 | + System.exit(0); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + /* |
| 120 | + * 检测工作文件夹是否存在 |
| 121 | + */ |
| 122 | + public static void checkWorkPath() { |
| 123 | + File dir = new File(QiniuApplication.workDir); |
| 124 | + if (!dir.exists()) { |
| 125 | + dir.mkdirs(); |
| 126 | + logger.info("mkdir '/tmp/qiniu/tool' success"); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + /* |
| 131 | + * 初始状态时要求用户输入AccessKey和SecretKey,否则退出程序 |
| 132 | + */ |
| 133 | + public static void showInputKeyDialog() { |
| 134 | + logger.info("show dialog to require user input key"); |
| 135 | + new Dialogs().showInputKeyDialog(); |
| 136 | + } |
| 137 | + |
| 138 | + public static void writeKey(String accessKey, String secretKey) { |
| 139 | + QiniuApplication.key = new Key(accessKey, secretKey); |
| 140 | + writeConfig(); |
| 141 | + } |
| 142 | + |
| 143 | + public static JsonObject readConfig() { |
| 144 | + StringBuilder config = new StringBuilder(); |
| 145 | + JsonObject jsonObject = null; |
| 146 | + try { |
| 147 | + logger.info("load configuration into memory"); |
| 148 | + BufferedReader reader = new BufferedReader(new FileReader(configPath)); |
| 149 | + String line; |
| 150 | + while ((line = reader.readLine()) != null) { |
| 151 | + config.append(line); |
| 152 | + } |
| 153 | + reader.close(); |
| 154 | + jsonObject = new JsonParser().parse(config.toString()).getAsJsonObject(); |
| 155 | + } catch (IOException e) { |
| 156 | + logger.error("load configuration error, messages: " + e.getMessage()); |
| 157 | + Dialogs.showFatalError(Values.LOAD_CONFIG_ERROR, e); |
| 158 | + } catch (Exception e) { |
| 159 | + logger.error("convert json string to json object failed, app'll reset"); |
| 160 | + Dialogs.showException(Values.JSON_TO_OBJECT_ERROR, e); |
| 161 | + } |
| 162 | + return jsonObject; |
| 163 | + } |
| 164 | + |
| 165 | + public static void writeConfig() { |
| 166 | + JsonObject config = new JsonObject(); |
| 167 | + // Key |
| 168 | + config.addProperty("accesskey", QiniuApplication.key.getAccessKey()); |
| 169 | + config.addProperty("secretkey", QiniuApplication.key.getSecretKey()); |
| 170 | + JsonArray buckets = new JsonArray(); |
| 171 | + // Bucket |
| 172 | + for (Map.Entry<String, String> entry : QiniuApplication.buckets.entrySet()) { |
| 173 | + JsonObject json = new JsonObject(); |
| 174 | + json.addProperty("bucket", entry.getKey()); |
| 175 | + String[] zones = entry.getValue().split(" "); |
| 176 | + json.addProperty("zone", zones[0]); |
| 177 | + json.addProperty("url", zones.length > 1 ? zones[1] : ""); |
| 178 | + buckets.add(json); |
| 179 | + } |
| 180 | + config.add("buckets", buckets); |
| 181 | + // Prefix |
| 182 | + JsonArray prefix = new JsonArray(); |
| 183 | + for (String string : QiniuApplication.prefix) { |
| 184 | + prefix.add(string); |
| 185 | + } |
| 186 | + config.add("prefix", prefix); |
| 187 | + writeConfig(new Gson().toJson(config)); |
| 188 | + } |
| 189 | + |
| 190 | + public static void writeConfig(String configJson) { |
| 191 | + try { |
| 192 | + BufferedWriter out = new BufferedWriter(new FileWriter(configPath, false)); |
| 193 | + out.write(Formatter.jsonFormat(configJson)); |
| 194 | + out.close(); |
| 195 | + logger.info("rewrite configuration success"); |
| 196 | + } catch (IOException e) { |
| 197 | + logger.error("rewrite configuration file failed, messages: " + e.getMessage()); |
| 198 | + Dialogs.showFatalError(Values.LOAD_CONFIG_ERROR, e); |
| 199 | + } |
| 200 | + } |
| 201 | +} |
0 commit comments