2016-11-30 6 views
0

PropertiesConfiguration.java hat keine close() Methode. Gibt es noch etwas, das getan werden muss, um die Datei freizugeben? Ich möchte sicher sein, bevor ich das in der Produktion verwende. Ich habe den Code durchgesehen und sehe nichts. Ich bin mir nicht ganz sicher, wie PropertiesConfiguration.setProperty() funktioniert, ohne eine Verbindung zu der Datei zu öffnen, die dann geschlossen werden müsste.Apache Commons Konfiguration - EigenschaftenKonfiguration geschlossen

+3

Wovon redest du? – shmosel

+0

In der JRE gibt es keine Klasse 'PropertiesConfiguration'. Wenn Sie es von woanders bekommen, müssen Sie es erklären. – chrylis

+0

Ich würde annehmen, dass er über die 'PropertiesConfiguration' Klasse von Apache Commons Configuration spricht. Diese Frage kann nicht definitiv beantwortet werden, ohne zu wissen, wie Sie die 'load'-Methode verwendet haben. Als allgemeine Faustregel gilt jedoch: Wenn Sie eine Ressource öffnen, müssen Sie _you_ schließen. Wenn eine Bibliothek von buchstäblich Tausenden geöffnet wird, können Sie sicher sein, dass es richtig aufgeräumt wird. – rmlan

Antwort

1

In org.apache.commons.configuration.PropertiesConfiguration ist die Eingabe (Strom, Pfad, URL, etc ...) natürlich geschlossen, wenn die Eigenschaften in der PropertiesConfiguration Instanz geladen wurden.

Sie könnten die Bestätigung in der void load(URL url) Methode von org.apache.commons.configuration.AbstractFileConfiguration haben.

Hier ist, wie diese Methode aufgerufen wird:

1) PropertiesConfiguration Konstruktor aufgerufen wird:

public PropertiesConfiguration(File file) throws ConfigurationException 

2), die ihre Super Konstruktor aufruft:

public AbstractFileConfiguration(File file) throws ConfigurationException 
{ 
    this(); 

    // set the file and update the url, the base path and the file name 
    setFile(file); 

    // load the file 
    if (file.exists()) 
    { 
     load(); // method which interest you 
    } 
} 

3), die load() ruft :

public void load() throws ConfigurationException 
{ 
    if (sourceURL != null) 
    { 
     load(sourceURL); 
    } 
    else 
    { 
     load(getFileName()); 
    } 
} 

4), die load(String fileName) ruft:

public void load(String fileName) throws ConfigurationException 
{ 
    try 
    { 
     URL url = ConfigurationUtils.locate(this.fileSystem, basePath, fileName); 

     if (url == null) 
     { 
      throw new ConfigurationException("Cannot locate configuration source " + fileName); 
     } 
     load(url); 
    } 
    catch (ConfigurationException e) 
    { 
     throw e; 
    } 
    catch (Exception e) 
    { 
     throw new ConfigurationException("Unable to load the configuration file " + fileName, e); 
    } 
} 

5), die load(URL url)

public void load(URL url) throws ConfigurationException 
{ 
    if (sourceURL == null) 
    { 
     if (StringUtils.isEmpty(getBasePath())) 
     { 
      // ensure that we have a valid base path 
      setBasePath(url.toString()); 
     } 
     sourceURL = url; 
    } 

    InputStream in = null; 

    try 
    { 
     in = fileSystem.getInputStream(url); 
     load(in); 
    } 
    catch (ConfigurationException e) 
    { 
     throw e; 
    } 
    catch (Exception e) 
    { 
     throw new ConfigurationException("Unable to load the configuration from the URL " + url, e); 
    } 
    finally 
    { 
     // close the input stream 
     try 
     { 
      if (in != null) 
      { 
       in.close(); 
      } 
     } 
     catch (IOException e) 
     { 
      getLogger().warn("Could not close input stream", e); 
     } 
    } 
} 

Und in der finally Anweisung ruft, können Sie sehen, dass der Eingabestrom in jedem Fall geschlossen ist.

+0

Danke @davidxxx. Ich glaube, ich habe nicht darauf vertraut, was ich sah. – pyetti