1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.whitesource.teamcity.server;
17
18 import com.thoughtworks.xstream.XStream;
19 import jetbrains.buildServer.log.Loggers;
20 import jetbrains.buildServer.serverSide.ServerPaths;
21 import jetbrains.buildServer.serverSide.crypt.RSACipher;
22 import jetbrains.buildServer.util.FileUtil;
23 import jetbrains.buildServer.util.StringUtil;
24 import org.jetbrains.annotations.NotNull;
25 import org.whitesource.teamcity.common.WssUtils;
26
27 import java.io.*;
28
29
30
31
32
33
34 public class GlobalSettingsManager {
35
36
37
38 private static final String LOG_COMPONENT = "GlobalConfig";
39
40 private final static String CONFIG_FILE_NAME = "whitesource-config.xml";
41
42
43
44 private File configFile;
45
46 private XStream xStream;
47
48 private GlobalSettings globalSettings;
49
50
51
52
53
54
55
56
57 public GlobalSettingsManager(@NotNull ServerPaths serverPaths) {
58 xStream = new XStream();
59 xStream.processAnnotations(GlobalSettings.class);
60 xStream.setClassLoader(GlobalSettings.class.getClassLoader());
61
62 configFile = new File(serverPaths.getConfigDir(), CONFIG_FILE_NAME);
63 loadConfig();
64 }
65
66
67
68 public void save() {
69 FileOutputStream fos = null;
70 try {
71 fos = new FileOutputStream(configFile);
72 xStream.toXML(globalSettings, fos);
73 } catch (FileNotFoundException e) {
74 Loggers.SERVER.error(WssUtils.logMsg(LOG_COMPONENT, "Failed to save config file " + configFile), e);
75 } finally {
76 FileUtil.close(fos);
77 }
78 }
79
80 public boolean isProxy() {
81 return globalSettings != null &&
82 globalSettings.getProxy() != null &&
83 !StringUtil.isEmptyOrSpaces(globalSettings.getProxy().getHost());
84 }
85
86 public String getHexEncodedPublicKey() {
87 return RSACipher.getHexEncodedPublicKey();
88 }
89
90
91
92 private void loadConfig() {
93 if (configFile.exists()) {
94 FileInputStream fis = null;
95 try {
96 fis = new FileInputStream(configFile);
97 globalSettings = (GlobalSettings) xStream.fromXML(fis);
98 } catch (IOException e) {
99 Loggers.SERVER.error(WssUtils.logMsg(LOG_COMPONENT, "Failed to load config file " + configFile), e);
100 } finally {
101 FileUtil.close(fis);
102 }
103 }
104 }
105
106
107
108 public GlobalSettings getGlobalSettings() {
109 return globalSettings;
110 }
111
112 public void setGlobalSettings(GlobalSettings globalSettings) {
113 this.globalSettings = globalSettings;
114 }
115 }