View Javadoc

1   package de.matthias_burbach.deputy.core.repository;
2   
3   import java.util.HashMap;
4   import java.util.Map;
5   
6   import org.jdom.Document;
7   import org.jdom.input.SAXBuilder;
8   
9   /***
10   * Allows centralization of POM document loading and building in order to cache
11   * most effectively because loading and building many POMs is performance
12   * critical.
13   *
14   * @author Matthias Burbach
15   */
16  public final class PomLoader {
17      /***
18       * The builder used to load POMs.
19       */
20      private SAXBuilder builder = new SAXBuilder();
21  
22      /***
23       * The cache for already loaded documents. Maps the absolute path of the POM
24       * to the document of type {@link Document}.
25       */
26      private Map pomDocumentCache = new HashMap();
27  
28      /***
29       * The singleton instance of this class.
30       */
31      private static PomLoader instance = new PomLoader();
32  
33      /***
34       * Private default constructor to enforce usage of {@link #getInstance()}.
35       *
36       */
37      private PomLoader() {
38          //empty
39      }
40  
41      /***
42       * @return An instance of this class.
43       */
44      public static PomLoader getInstance() {
45          return instance;
46      }
47  
48      /***
49       * Clears the internal cache of already loaded POM documents.
50       */
51      public void clearCache() {
52          pomDocumentCache = new HashMap();
53      }
54  
55      /***
56       * @param path The absolute path of the Maven project object model (POM)
57       *             to load.
58       * @return The document loaded.
59       */
60      public Document loadPom(final String path) {
61          Document result = (Document) pomDocumentCache.get(path);
62          if (result == null) {
63              try {
64                  result = builder.build(path);
65                  pomDocumentCache.put(path, result);
66              } catch (Exception e) {
67                  e.printStackTrace();
68              }
69          }
70          return result;
71      }
72  }