View Javadoc

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  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       * 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          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 }