| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 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 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 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 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 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 | |
} |