Coverage Report - org.apache.onami.configuration.OnamiVariablesExpander
 
Classes in this File Line Coverage Branch Coverage Complexity
OnamiVariablesExpander
93%
15/16
100%
4/4
2
OnamiVariablesExpander$1
100%
1/1
N/A
2
OnamiVariablesExpander$2
88%
8/9
40%
4/10
2
 
 1  
 package org.apache.onami.configuration;
 2  
 
 3  
 /*
 4  
  * Licensed to the Apache Software Foundation (ASF) under one
 5  
  * or more contributor license agreements.  See the NOTICE file
 6  
  * distributed with this work for additional information
 7  
  * regarding copyright ownership.  The ASF licenses this file
 8  
  * to you under the Apache License, Version 2.0 (the
 9  
  * "License"); you may not use this file except in compliance
 10  
  * with the License.  You may obtain a copy of the License at
 11  
  *
 12  
  *   http://www.apache.org/licenses/LICENSE-2.0
 13  
  *
 14  
  * Unless required by applicable law or agreed to in writing,
 15  
  * software distributed under the License is distributed on an
 16  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 17  
  * KIND, either express or implied.  See the License for the
 18  
  * specific language governing permissions and limitations
 19  
  * under the License.
 20  
  */
 21  
 
 22  
 import static com.google.inject.name.Names.named;
 23  
 import static com.google.inject.spi.Elements.getElements;
 24  
 import static com.google.inject.util.Modules.override;
 25  
 import static java.util.Arrays.asList;
 26  
 
 27  
 import java.util.List;
 28  
 import java.util.Map.Entry;
 29  
 
 30  
 import org.apache.onami.configuration.variables.AntStyleParser;
 31  
 import org.apache.onami.configuration.variables.Parser;
 32  
 import org.apache.onami.configuration.variables.VariablesMap;
 33  
 
 34  
 import com.google.inject.AbstractModule;
 35  
 import com.google.inject.Binding;
 36  
 import com.google.inject.Key;
 37  
 import com.google.inject.Module;
 38  
 import com.google.inject.TypeLiteral;
 39  
 import com.google.inject.name.Named;
 40  
 import com.google.inject.spi.DefaultElementVisitor;
 41  
 import com.google.inject.spi.Element;
 42  
 
 43  
 /**
 44  
  * @since 6.0
 45  
  */
 46  240
 public final class OnamiVariablesExpander
 47  
     extends AbstractModule
 48  
 {
 49  
 
 50  
     public static Module expandVariables( Module...baseModules )
 51  
     {
 52  2
         return expandVariables( asList( baseModules ) );
 53  
     }
 54  
 
 55  
     /**
 56  
      *
 57  
      * @param parser
 58  
      * @param baseModules
 59  
      * @return
 60  
      * @since 6.3.0
 61  
      */
 62  
     public static Module expandVariables( Parser parser, Module...baseModules )
 63  
     {
 64  0
         return expandVariables( parser, asList( baseModules ) );
 65  
     }
 66  
 
 67  
     public static Module expandVariables( Iterable<? extends Module> baseModules )
 68  
     {
 69  2
         return expandVariables( new AntStyleParser(), baseModules );
 70  
     }
 71  
 
 72  
     /**
 73  
      *
 74  
      * @param parser
 75  
      * @param baseModules
 76  
      * @return
 77  
      * @since 6.3.0
 78  
      */
 79  
     public static Module expandVariables( Parser parser, Iterable<? extends Module> baseModules )
 80  
     {
 81  2
         return override( baseModules ).with( new OnamiVariablesExpander( parser, getElements( baseModules ) ) );
 82  
     }
 83  
 
 84  2
     private final TypeLiteral<String> stringLiteral = new TypeLiteral<String>(){};
 85  
 
 86  
     private final Parser parser;
 87  
 
 88  
     private final List<Element> elements;
 89  
 
 90  
     /**
 91  
      * Do nothing, this class cannot be instantiated
 92  
      */
 93  
     private OnamiVariablesExpander( Parser parser, List<Element> elements )
 94  2
     {
 95  2
         this.parser = parser;
 96  2
         this.elements = elements;
 97  2
     }
 98  
 
 99  
     @Override
 100  
     protected void configure()
 101  
     {
 102  2
         final VariablesMap variablesMap = new VariablesMap( parser );
 103  
 
 104  2
         for ( final Element element : elements )
 105  
         {
 106  240
             element.acceptVisitor( new DefaultElementVisitor<Void>()
 107  480
             {
 108  
 
 109  
                 @Override
 110  
                 public <T> Void visit( Binding<T> binding )
 111  
                 {
 112  240
                     Key<?> bindingKey = binding.getKey();
 113  
 
 114  240
                     if ( stringLiteral.equals( bindingKey.getTypeLiteral() )
 115  
                             && bindingKey.getAnnotation() != null
 116  
                             && ( Named.class.isAssignableFrom( bindingKey.getAnnotationType() )
 117  
                                             || javax.inject.Named.class.isAssignableFrom( bindingKey.getAnnotationType() ) ) )
 118  
                     {
 119  
                         String propertyKey;
 120  
 
 121  240
                         if ( Named.class.isAssignableFrom( bindingKey.getAnnotationType() ) )
 122  
                         {
 123  240
                             propertyKey = ( (Named) bindingKey.getAnnotation() ).value();
 124  
                         }
 125  
                         else
 126  
                         {
 127  0
                             propertyKey = ( (javax.inject.Named) bindingKey.getAnnotation() ).value();
 128  
                         }
 129  
 
 130  240
                         String propertyValue = (String) binding.getProvider().get();
 131  
 
 132  240
                         variablesMap.put( propertyKey, propertyValue );
 133  
                     }
 134  
 
 135  240
                     return super.visit( binding );
 136  
                 }
 137  
 
 138  
             });
 139  
         }
 140  
 
 141  2
         for ( Entry<String, String> variable : variablesMap.entrySet() )
 142  
         {
 143  240
             bindConstant().annotatedWith( named( variable.getKey() ) ).to( variable.getValue() );
 144  
         }
 145  2
     }
 146  
 
 147  
 }