View Javadoc

    1   /**
    2    * Copyright (c) 2002-2011 "Neo Technology,"
    3    * Network Engine for Objects in Lund AB [http://neotechnology.com]
    4    *
    5    * This file is part of Neo4j.
    6    *
    7    * Neo4j is free software: you can redistribute it and/or modify
    8    * it under the terms of the GNU General Public License as published by
    9    * the Free Software Foundation, either version 3 of the License, or
    10   * (at your option) any later version.
    11   *
    12   * This program is distributed in the hope that it will be useful,
    13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    15   * GNU General Public License for more details.
    16   *
    17   * You should have received a copy of the GNU General Public License
    18   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    19   */
    20  package org.neo4j.server.configuration;
    21
    22  import java.io.File;
    23  import java.io.FilenameFilter;
    24  import java.net.URI;
    25  import java.net.URISyntaxException;
    26  import java.util.HashMap;
    27  import java.util.HashSet;
    28  import java.util.Map;
    29  import java.util.Properties;
    30  import java.util.Set;
    31
    32  import org.apache.commons.configuration.CompositeConfiguration;
    33  import org.apache.commons.configuration.Configuration;
    34  import org.apache.commons.configuration.ConfigurationException;
    35  import org.apache.commons.configuration.PropertiesConfiguration;
    36  import org.apache.commons.configuration.SystemConfiguration;
    37  import org.neo4j.kernel.EmbeddedGraphDatabase;
    38  import org.neo4j.server.configuration.validation.Validator;
    39  import org.neo4j.server.logging.Logger;
    40
    41  public class PropertyFileConfigurator implements Configurator {
    42
    43      private static final String NEO4J_PROPERTIES_FILENAME = "neo4j.properties";
    44
    45      public static Logger log = Logger.getLogger(PropertyFileConfigurator.class);
    46
    47      private CompositeConfiguration serverConfiguration = new CompositeConfiguration();
    48      private File propertyFileDirectory;
    49
    50      private Validator validator = new Validator();
    51      private Map<String, String> databaseTuningProperties = null;
    52      private HashSet<ThirdPartyJaxRsPackage> thirdPartyPackages;
    53
    54      public PropertyFileConfigurator(File propertiesFile) {
    55          this(null, propertiesFile);
    56      }
    57
    58      public PropertyFileConfigurator(Validator v) {
    59          this(v, null);
    60      }
    61
    62      public PropertyFileConfigurator(Validator v, File propertiesFile) {
    63          if (propertiesFile == null) {
    64              propertiesFile = new File(System.getProperty(Configurator.NEO_SERVER_CONFIG_FILE_KEY));
    65          }
    66
    67          try {
    68              propertyFileDirectory = propertiesFile.getParentFile();
    69              loadPropertiesConfig(propertiesFile);
    70              loadDatabaseTuningProperties(propertiesFile);
    71              normalizeUris();
    72              if (v != null) {
    73                  v.validate(this.configuration());
    74              }
    75          } catch (ConfigurationException ce) {
    76              log.warn(ce);
    77          }
    78
    79      }
    80
    81      @Override
    82      public Configuration configuration() {
    83          return serverConfiguration == null ? new SystemConfiguration() : serverConfiguration;
    84      }
    85
    86      private void loadDatabaseTuningProperties(File configFile) throws ConfigurationException {
    87          String databaseTuningPropertyFileLocation = serverConfiguration.getString(DB_TUNING_PROPERTY_FILE_KEY);
    88
    89          if (databaseTuningPropertyFileLocation == null) {
    90              if(propertyFileDirectoryContainsDBTuningFile()) {
    91                  databaseTuningPropertyFileLocation = new File (propertyFileDirectory, NEO4J_PROPERTIES_FILENAME).getAbsolutePath();
    92                  log.info("No database tuning file explicitly set, defaulting to [%s]", databaseTuningPropertyFileLocation);
    93              } else {
    94                  log.info("No database tuning properties (org.neo4j.server.db.tuning.properties) found in [%s], using defaults.", databaseTuningPropertyFileLocation);
    95                  return;
    96              }
    97          }
    98
    99          File databaseTuningPropertyFile = new File(databaseTuningPropertyFileLocation);
    100
    101         if (!databaseTuningPropertyFile.exists()) {
    102             log.warn("The specified file for database performance tuning properties [%s] does not exist.", databaseTuningPropertyFileLocation);
    103             return;
    104         }
    105
    106         databaseTuningProperties = EmbeddedGraphDatabase.loadConfigurations(databaseTuningPropertyFileLocation);
    107
    108     }
    109
    110
    111
    112
    113     private void loadPropertiesConfig(File configFile) throws ConfigurationException {
    114         PropertiesConfiguration propertiesConfig = new PropertiesConfiguration(configFile);
    115         if (validator.validate(propertiesConfig)) {
    116             serverConfiguration.addConfiguration(propertiesConfig);
    117         } else {
    118             String failed = String.format("Error processing [%s], configuration file has failed validation.", configFile.getAbsolutePath());
    119             log.fatal(failed);
    120             throw new InvalidServerConfigurationException(failed);
    121         }
    122     }
    123
    124     private void normalizeUris() {
    125         try {
    126             for (String key : new String[] { MANAGEMENT_PATH_PROPERTY_KEY, DATA_API_PATH_PROPERTY_KEY }) {
    127                 if (configuration().containsKey(key)) {
    128                     URI normalizedUri = new URI((String) configuration().getProperty(key)).normalize();
    129                     configuration().clearProperty(key);
    130                     configuration().addProperty(key, normalizedUri.toString());
    131                 }
    132             }
    133
    134         } catch (URISyntaxException e) {
    135             throw new RuntimeException(e);
    136         }
    137
    138     }
    139
    140     private boolean propertyFileDirectoryContainsDBTuningFile() {
    141         File[] neo4jPropertyFiles = propertyFileDirectory.listFiles(new FilenameFilter() {
    142
    143             @Override
    144             public boolean accept(File dir, String name) {
    145                 return name.toLowerCase().equals(NEO4J_PROPERTIES_FILENAME);
    146             }
    147         });
    148         return neo4jPropertyFiles != null && neo4jPropertyFiles.length == 1;
    149     }
    150
    151     @Override
    152     public Map<String, String> getDatabaseTuningProperties() {
    153         return databaseTuningProperties == null ? new HashMap<String, String>() : databaseTuningProperties;
    154     }
    155
    156     @Override
    157     public Set<ThirdPartyJaxRsPackage> getThirdpartyJaxRsClasses() {
    158         thirdPartyPackages = new HashSet<ThirdPartyJaxRsPackage>();
    159         Properties properties = this.configuration().getProperties(THIRD_PARTY_PACKAGES_KEY);
    160         for (Object key : properties.keySet()) {
    161             thirdPartyPackages.add(new ThirdPartyJaxRsPackage(key.toString(), properties.getProperty(key.toString())));
    162         }
    163         return thirdPartyPackages;
    164     }
    165 }