Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2014 dc-square GmbH
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *       http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package com.dcsquare.hivemq.spi.config;
 
 
 import java.io.File;
 import java.util.Map;
 
 import static com.google.common.io.Closeables.closeQuietly;
 import static com.netflix.config.PollResult.createFull;

This is used in Configurations to provide an easy to use property-based configuration.

Author(s):
Dominik Obermaier
Since:
1.4
 
 public class PropertiesFileDatasource implements PolledConfigurationSource {
 
     private final static Logger log = LoggerFactory.getLogger(PropertiesFileDatasource.class);
 
     private final Iterable<StringpropertiesFileNames;
     private boolean optional;
     private final File baseFolder;
 
     public PropertiesFileDatasource(final File baseFolderfinal String... propertiesFileNames) {
 
         this(baseFolder, Arrays.asList(propertiesFileNames));
     }
 
     public PropertiesFileDatasource(final File baseFolderfinal Iterable<StringpropertiesFileNames) {
         this(baseFolderpropertiesFileNamesfalse);
     }
 
     public PropertiesFileDatasource(final File baseFolderfinal Iterable<StringpropertiesFileNamesfinal boolean optional) {
         this. = propertiesFileNames;
         this. = optional;
         this. = baseFolder;
     }
 
     @Override
     public PollResult poll(final boolean initialfinal Object checkPointthrows Exception {
 
         final Map<StringObjectpropertiesMap = new HashMap<StringObject>();
         for (final String fileName : ) {
 
             final File configFromFolder = new File(fileName);
 
             if (configFromFolder.canRead() && configFromFolder.isFile()) {
 
                 final Properties properties = loadProperties(configFromFolder);
                 for (final Map.Entry<ObjectObjectproperty : properties.entrySet()) {
                     propertiesMap.put((Stringproperty.getKey(), property.getValue());
                 }
             } else {
                 if (!) {
                     .warn("Properties File {} does not exist in {} "fileName);
                 } else {
                     .trace("Properties File {} does not exist in {} "fileName);
                 }
             }
         }
         return createFull(propertiesMap);
     }
 
 
     private Properties loadProperties(final File filethrows Exception {
         final Properties properties = new Properties();
 
         FileInputStream fis = null;
         try {
             fis = new FileInputStream(file);
             properties.load(fis);
         } finally {
             closeQuietly(fis);
         }
         return properties;
    }
New to GrepCode? Check out our FAQ X