Coverage Report - org.apache.onami.autobind.integrations.commons.configuration.CommonsConfigurationFeature
 
Classes in this File Line Coverage Branch Coverage Complexity
CommonsConfigurationFeature
0%
0/41
0%
0/14
9
CommonsConfigurationFeature$1
0%
0/1
N/A
9
 
 1  
 /**
 2  
  * Copyright (C) 2010 Daniel Manzke <daniel.manzke@googlemail.com>
 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  
 /**
 17  
  * 
 18  
  */
 19  
 package org.apache.onami.autobind.integrations.commons.configuration;
 20  
 
 21  
 import java.io.File;
 22  
 import java.lang.annotation.Annotation;
 23  
 import java.net.MalformedURLException;
 24  
 import java.net.URL;
 25  
 import java.util.Map;
 26  
 import java.util.logging.Level;
 27  
 import java.util.logging.Logger;
 28  
 
 29  
 import javax.inject.Named;
 30  
 import javax.inject.Singleton;
 31  
 
 32  
 import org.apache.commons.configuration.ConfigurationException;
 33  
 import org.apache.commons.configuration.FileConfiguration;
 34  
 import org.apache.onami.autobind.configuration.Configuration;
 35  
 import org.apache.onami.autobind.install.BindingStage;
 36  
 import org.apache.onami.autobind.scanner.features.BindingScannerFeature;
 37  
 
 38  
 
 39  
 /**
 40  
  * This Class will be called for each Class, which is annotated with
 41  
  * {@link Configuration} and which needs an Apache Commons-based Configuration.
 42  
  * 
 43  
  * @author Daniel Manzke
 44  
  * 
 45  
  */
 46  
 @Singleton
 47  0
 public class CommonsConfigurationFeature extends BindingScannerFeature {
 48  0
         private Logger _logger = Logger.getLogger(CommonsConfigurationFeature.class.getName());
 49  
 
 50  
         @Override
 51  
         public BindingStage accept(Class<Object> annotatedClass, Map<String, Annotation> annotations) {
 52  0
                 if (annotations.containsKey(Configuration.class.getName())) {
 53  0
                         Configuration config = (Configuration) annotations.get(Configuration.class.getName());
 54  0
                         if (FileConfiguration.class.isAssignableFrom(config.to())) {
 55  0
                                 return BindingStage.BOOT_BEFORE;
 56  
                         }
 57  
                 }
 58  0
                 return BindingStage.IGNORE;
 59  
         }
 60  
 
 61  
         @SuppressWarnings("unchecked")
 62  
         @Override
 63  
         public void process(Class<Object> annotatedClass, Map<String, Annotation> annotations) {
 64  0
                 Configuration config = (Configuration) annotations.get(Configuration.class.getName());
 65  0
                 Named name = config.name();
 66  
 
 67  
                 // TODO Implement Location overriding
 68  
                 URL url;
 69  0
                 switch (config.location().type()) {
 70  
                 case FILE:
 71  0
                         File file = new File(config.location().value());
 72  0
                         if (!file.exists()) {
 73  0
                                 _logger.log(Level.WARNING, "Ignoring Configuration " + name + " in "
 74  
                                                 + config.location() + ". In the Path " + file.getAbsolutePath()
 75  
                                                 + " no Configuration was found.");
 76  0
                                 return;
 77  
                         }
 78  
                         try {
 79  0
                                 url = file.toURI().toURL();
 80  0
                         } catch (MalformedURLException e) {
 81  0
                                 _logger.log(Level.WARNING, "Ignoring Configuration " + name + " in "
 82  
                                                 + config.location() + ". It has an illegal URL-Format.", e);
 83  0
                                 return;
 84  0
                         }
 85  
                         break;
 86  
                 case URL:
 87  
                         try {
 88  0
                                 url = new URL(config.location().value());
 89  0
                         } catch (MalformedURLException e) {
 90  0
                                 _logger.log(Level.WARNING, "Ignoring Configuration " + name + " in "
 91  
                                                 + config.location() + ". It has an illegal URL-Format.", e);
 92  0
                                 return;
 93  0
                         }
 94  
                         break;
 95  
                 case CLASSPATH:
 96  
                 default:
 97  0
                         url = this.getClass().getResource(config.location().value());
 98  
                         break;
 99  
                 }
 100  
 
 101  0
                 if (url == null) {
 102  0
                         _logger.log(Level.WARNING, "Ignoring Configuration " + name + " in "
 103  
                                         + config.location() + ", because is couldn't be found in the Classpath.");
 104  
                         // TODO Throw an exception if config doesn't exist?
 105  0
                         return;
 106  
                 }
 107  
 
 108  0
                 Named named = null;
 109  0
                 if (name.value().length() > 0) {
 110  0
                         named = name;
 111  
                 }
 112  
 
 113  
                 FileConfiguration configuration;
 114  
                 try {
 115  
                         // Class<? extends FileConfiguration> interf =
 116  
                         // config.to().asSubclass(FileConfiguration.class);
 117  0
                         Class<FileConfiguration> interf = (Class<FileConfiguration>) config.to();
 118  0
                         configuration = (FileConfiguration) injector.getInstance(interf);
 119  0
                         configuration.load(url);
 120  
 
 121  0
                         bindInstance(configuration, interf, named, null);
 122  0
                         bindInstance(configuration, FileConfiguration.class, named, null);
 123  0
                         bindInstance(configuration, org.apache.commons.configuration.Configuration.class,
 124  
                                 named, null);
 125  0
                 } catch (ConfigurationException e) {
 126  0
                         _logger.log(Level.WARNING, "Configuration " + name + " couldn't be loaded/bound: "
 127  
                                         + e.getMessage(), e);
 128  0
                 }
 129  0
         }
 130  
 }