View Javadoc

1   /**
2    * Copyright (C) 2012 White Source Ltd.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Global configuration for the plugin.
31   *
32   * @author Edo.Shor
33   */
34  public class GlobalSettingsManager {
35  
36      /* --- Static members --- */
37  
38      private static final String LOG_COMPONENT = "GlobalConfig";
39  
40      private final static String CONFIG_FILE_NAME = "whitesource-config.xml";
41  
42      /* --- Members --- */
43  
44      private File configFile;
45  
46      private XStream xStream;
47  
48      private GlobalSettings globalSettings;
49  
50      /* --- Constructors --- */
51  
52      /**
53       * Constructor
54       *
55       * @param serverPaths
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      /* --- Public methods --- */
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      /* --- Private methods --- */
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     /* --- Getters / Setters --- */
107 
108     public GlobalSettings getGlobalSettings() {
109         return globalSettings;
110     }
111 
112     public void setGlobalSettings(GlobalSettings globalSettings) {
113         this.globalSettings = globalSettings;
114     }
115 }