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  public class ExtensionManager {
48  
49      private static final Logger LOG = LoggerFactory.getLogger(ExtensionManager.class);
50  
51      private static final ExtensionManager INSTANCE = new ExtensionManager();
52  
53      private CacheLoader<Class<? extends MercurialExtension>, MercurialExtension> createExtInstance = new CacheLoader<Class<? extends MercurialExtension>, MercurialExtension>() {
54          public MercurialExtension load(Class<? extends MercurialExtension> klass) {
55              MercurialExtension instance = null;
56              try {
57                  instance = klass.newInstance();
58              } catch (InstantiationException e) {
59                  throw Utils.asRuntime(e);
60              } catch (IllegalAccessException e) {
61                  throw Utils.asRuntime(e);
62              }
63              try {
64                  instance.initialize();
65              } catch (Exception e) {
66                  LOG.error("Initialization of {} failed", klass);
67                  LOG.error("The extension will be used anyway");
68                  LOG.error("Exception: {}", e);
69              }
70              return instance;
71          }
72      };
73  
74      private LoadingCache<Class<? extends MercurialExtension>, MercurialExtension> extInstances = CacheBuilder.newBuilder().build(
75              createExtInstance);
76  
77      public static ExtensionManager getInstance() {
78          return INSTANCE;
79      }
80  
81      
82  
83  
84  
85  
86  
87  
88      public List<String> process(Collection<Class<? extends MercurialExtension>> classes) {
89          List<String> result = Lists.newArrayList();
90          for (Class<? extends MercurialExtension> k : classes) {
91              MercurialExtension ext;
92              try {
93                  ext = this.extInstances.get(k);
94              } catch (ExecutionException e) {
95                  throw Utils.asRuntime(e);
96              }
97              String path = ext.getPath();
98              if (path == null) {
99                  path = "";
100             }
101             result.add("--config");
102             result.add("extensions." + ext.getName() + "=" + path);
103         }
104         return result;
105     }
106 
107 }