2017-10-17 135 views
-1

我用eclipse製作了一個web項目來在Tomcat v7上運行我的webservice。我有一個配置文件,必須定義一些變量,這些變量應該在Web服務器啓動時更改,而無需重新啓動它。問題是:我可以把這個文件放在哪裏? 目前我已經把它放到「./」目錄中,但似乎當我導出WAR文件並將其安裝在網絡服務器上時,它不起作用。有沒有辦法在WAR中創建這個文件並修改它的運行時?在Java導出的運行時創建配置文件WAR

這裏是.class文件的訪問配置文件

public class ToolConfigurations { 
    private static final Logger log = LogManager.getLogger(ToolConfigurations.class); //Oggetto logging 

    private static ToolConfigurations instance = null; //verificarne la necessità 

    private static String defaultServerName = null; 
    private static String defaultDbName = "postgres"; 
    private static String defaultDbUser = "postgres"; 
    private static String defaultDbPass = "password"; 
    private static int defaultDbMaxConnNum = 10; 

    private static String configFilePath = ".\\"; 
    private static String configFileName = "tool.properties"; 
    private String serverName = null; 
    private String dbName = null; 
    private String dbUser = null; 
    private String d`enter code here`bPass = null; 
    private int dbMaxConnNum = -1; 

    private ToolConfigurations() throws Exception { 
     File file = new File(configFilePath + configFileName); 
     if(file.exists()) { 
      //file configurazione esiste 
      FileReader in = new FileReader(file); 
      BufferedReader br = new BufferedReader(in); 
      String line = null; 

      while((line = br.readLine()) != null) { //Leggo riga per riga il file 
       String[] values = line.split(" "); 
       switch (values[0]) { 
        case "serverName": 
         serverName = values[1]; 
         break; 
        case "dbName": 
         dbName = values[1]; 
         break; 
        case "dbUser": 
         dbUser = values[1]; 
         break; 
        case "dbPass": 
         dbPass = values[1]; 
         break; 
        case "dbMaxConnNum": 
         dbMaxConnNum = Integer.parseInt(values[1]); 
         break; 
        default: 
         log.warn("Elemento inaspettato nel file di configurazione: " + values[0]); 
         break; 
       } 
      } 

      br.close(); 
      in.close(); 
     }else { 
      if(file.createNewFile() == false) { 
       //Errore creazione file 
       log.error("Errore creazione file di configurazione"); 
       throw new Exception("Errore creazione file configurazione"); 
      } 
      //CREO FILE CONFIGURAZIONE CON IMPOSTAZIONI DI DEFAULT 
      FileWriter fw = new FileWriter(file); 
      fw.write("dbName " + defaultDbName + "\r\n"); 
      fw.write("dbUser " + defaultDbUser + "\r\n"); 
      fw.write("dbPass " + defaultDbPass + "\r\n"); 
      fw.write("dbMaxConnNum " + defaultDbMaxConnNum + "\r\n"); 
      fw.flush(); 
      fw.close(); 
      log.warn("File di configurazione non trovato. Path: " + file.getAbsolutePath() + ". Creato nuovo file con configurazioni di default."); 
      //CARICO IMPOSTAZIONI DI DEFAULT 
      dbName = defaultDbName; 
      dbUser = defaultDbUser; 
      dbPass = defaultDbPass; 
      dbMaxConnNum = defaultDbMaxConnNum; 
     } 
    } 

    public static synchronized ToolConfigurations getInstance() throws Exception { 
     if(instance == null) { 
      instance = new ToolConfigurations(); 
     } 
     return instance; 
    } 
    ... 
+0

WAR的並不意在運行時進行修改。也就是說,你可以操縱爆炸(解壓縮)的戰爭。我會建議反對它。您可能會發現將您的配置放在JNDI中是一個更好的主意。 –

+0

我需要一個地方放置並從我放置的環境中獨立地訪問我的配置文件。 –

回答

0

我發現的最佳解決方案是使用ClassLoader來搜索我以前在主目錄中創建的配置文件。 使用的例子是:

ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); 
InputStream fileInput = classLoader.getResourceAsStream(configFileName); 
if(fileInput == null){ 
    //File not found 
}else{ 
    //File found 
    InputStreamReader sr = new InputStreamReader(fileInput); 
    BufferedReader br = new BufferedReader(sr); 
    try { 
     while((line = br.readLine()) != null) { 
      //Read file line by line 
     } 
    }catch (IOException e) { 
     throw new IOException("Error parsing file: " + e.getMessage()); 
    } 
} 

0

在Web服務器的部分代碼,文件以外的.class直接由服務器所掌管。您不需要重新加載服務器。

但嘗試使用.properties文件並檢查部署在服務器上時WAR的結構。

+0

是的,我正在使用.properties文件,但問題是:如何鏈接相對它? –

+0

請特別注意你的代碼的一部分,如何訪問你的文件。 – Skykaza

+0

您可能會發現https://stackoverflow.com/q/1188400/53897有趣。 –