View Javadoc
1   /*
2    * REPOWEB, repository manager.
3    *
4    * Terms of license - http://opensource.org/licenses/apachepl.php
5    */
6   package org.repoweb.model;
7   import java.lang.reflect.Constructor;
8   /***
9    * Manager for Repository.
10   */
11  public final class Manager {
12      public static final ManagerConfig NULL_CONFIG = newNullConfig();
13      private static Repository _repo = new FailSafeRepository();
14      private static Repository _defaultPomRepository = new FailSafeRepository();
15      private static ManagerConfig _config = newNullConfig();
16  
17      private Manager() {}
18  
19      public static synchronized void setConfig(ManagerConfig config)
20          throws BadConfigException {
21          if (isNullConfig(config)) {
22              _repo = new FailSafeRepository();
23              _defaultPomRepository = new FailSafeRepository();
24              _config = newNullConfig();
25              return;
26          }
27  
28          _repo = newRepository(config.getMainRepository());
29          _defaultPomRepository = newRepository(config.getDefaultPomRepository());
30          _config = config;
31      }
32  
33  
34      private static Repository newRepository(
35          ManagerConfig.RepositoryConfig repositoryConfig)
36          throws BadConfigException {
37          try {
38              Class repoClass = Class.forName(repositoryConfig.getType());
39              final Constructor constructor =
40                  repoClass.getConstructor(new Class[] {
41                          String.class
42                      });
43              return (Repository)constructor.newInstance(new Object[] {
44                      repositoryConfig.getLocation()
45                  });
46          }
47          catch (Exception e) {
48              throw new BadConfigException(e);
49          }
50      }
51  
52  
53      public static synchronized ManagerConfig getConfig() {
54          return _config;
55      }
56  
57  
58      public static synchronized Repository getDefaultPomRepository() {
59          return _defaultPomRepository;
60      }
61  
62  
63      public static synchronized Repository getRepository() {
64          return _repo;
65      }
66  
67  
68      private static final ManagerConfig newNullConfig() {
69          ManagerConfig nullConfig = new ManagerConfig();
70          nullConfig.setMainRepository(new ManagerConfig.RepositoryConfig("N/A", "N/A"));
71          return nullConfig;
72      }
73  
74  
75      private static final boolean isNullConfig(final ManagerConfig config) {
76          return config == null || NULL_CONFIG.equals(config);
77      }
78  }