Coverage Report - com.aragost.javahg.internals.ExtensionManager
 
Classes in this File Line Coverage Branch Coverage Complexity
ExtensionManager
89%
17/19
100%
4/4
4
ExtensionManager$1
46%
7/15
N/A
4
 
 1  
 /*
 2  
  * #%L
 3  
  * JavaHg parent POM
 4  
  * %%
 5  
  * Copyright (C) 2011 aragost Trifork ag
 6  
  * %%
 7  
  * Permission is hereby granted, free of charge, to any person obtaining a copy
 8  
  * of this software and associated documentation files (the "Software"), to deal
 9  
  * in the Software without restriction, including without limitation the rights
 10  
  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 11  
  * copies of the Software, and to permit persons to whom the Software is
 12  
  * furnished to do so, subject to the following conditions:
 13  
  * 
 14  
  * The above copyright notice and this permission notice shall be included in
 15  
  * all copies or substantial portions of the Software.
 16  
  * 
 17  
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 18  
  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 19  
  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 20  
  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 21  
  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 22  
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 23  
  * THE SOFTWARE.
 24  
  * #L%
 25  
  */
 26  
 package com.aragost.javahg.internals;
 27  
 
 28  
 import java.util.Collection;
 29  
 import java.util.List;
 30  
 import java.util.concurrent.ExecutionException;
 31  
 
 32  
 import com.aragost.javahg.MercurialExtension;
 33  
 import com.aragost.javahg.log.Logger;
 34  
 import com.aragost.javahg.log.LoggerFactory;
 35  
 import com.google.common.cache.CacheBuilder;
 36  
 import com.google.common.cache.CacheLoader;
 37  
 import com.google.common.cache.LoadingCache;
 38  
 import com.google.common.collect.Lists;
 39  
 
 40  
 /**
 41  
  * This is a singleton to manage Mercurial extensions.
 42  
  * <p>
 43  
  * To use a Mercurial extension with JavaHg there has to be an
 44  
  * implementation of {@link MercurialExtension}. Call the process()
 45  
  * method with this class to enable an extension.
 46  
  */
 47  1
 public class ExtensionManager {
 48  
 
 49  1
     private static final Logger LOG = LoggerFactory.getLogger(ExtensionManager.class);
 50  
 
 51  1
     private static final ExtensionManager INSTANCE = new ExtensionManager();
 52  
 
 53  1
     private CacheLoader<Class<? extends MercurialExtension>, MercurialExtension> createExtInstance = new CacheLoader<Class<? extends MercurialExtension>, MercurialExtension>() {
 54  
         public MercurialExtension load(Class<? extends MercurialExtension> klass) {
 55  3
             MercurialExtension instance = null;
 56  
             try {
 57  3
                 instance = klass.newInstance();
 58  0
             } catch (InstantiationException e) {
 59  0
                 throw Utils.asRuntime(e);
 60  0
             } catch (IllegalAccessException e) {
 61  0
                 throw Utils.asRuntime(e);
 62  3
             }
 63  
             try {
 64  3
                 instance.initialize();
 65  0
             } catch (Exception e) {
 66  0
                 LOG.error("Initialization of {} failed", klass);
 67  0
                 LOG.error("The extension will be used anyway");
 68  0
                 LOG.error("Exception: {}", e);
 69  3
             }
 70  3
             return instance;
 71  
         }
 72  
     };
 73  
 
 74  1
     private LoadingCache<Class<? extends MercurialExtension>, MercurialExtension> extInstances = CacheBuilder.newBuilder().build(
 75  
             createExtInstance);
 76  
 
 77  
     public static ExtensionManager getInstance() {
 78  433
         return INSTANCE;
 79  
     }
 80  
 
 81  
     /**
 82  
      * Enable the Mercurial extension for the classes.
 83  
      * 
 84  
      * @param classes
 85  
      * @return list of command line flags needed to enable the
 86  
      *         extensions
 87  
      */
 88  
     public List<String> process(Collection<Class<? extends MercurialExtension>> classes) {
 89  433
         List<String> result = Lists.newArrayList();
 90  433
         for (Class<? extends MercurialExtension> k : classes) {
 91  
             MercurialExtension ext;
 92  
             try {
 93  406
                 ext = this.extInstances.get(k);
 94  0
             } catch (ExecutionException e) {
 95  0
                 throw Utils.asRuntime(e);
 96  406
             }
 97  406
             String path = ext.getPath();
 98  406
             if (path == null) {
 99  2
                 path = "";
 100  
             }
 101  406
             result.add("--config");
 102  406
             result.add("extensions." + ext.getName() + "=" + path);
 103  406
         }
 104  433
         return result;
 105  
     }
 106  
 
 107  
 }