View Javadoc

1   package com.atlassian.seraph.config;
2   
3   /**
4    * Factory for {@link SecurityConfig} instances.
5    */
6   public class SecurityConfigFactory
7   {
8       private static volatile SecurityConfig instance;
9   
10      /**
11       * Get a SecurityConfig instance.
12       * 
13       * @return A default implementation of SecurityConfig.
14       * @throws RuntimeException
15       *             If loading the configuration failed.
16       */
17      public static SecurityConfig getInstance()
18      {
19          if (instance == null)
20          {
21              loadInstance(SecurityConfigImpl.DEFAULT_CONFIG_LOCATION);
22          }
23          return instance;
24      }
25  
26      /**
27       * Get a SecurityConfig instance.
28       * 
29       * @param configFileLocation
30       *            Path to config file resource (usu. 'seraph-config.xml')
31       * @return
32       * @throws RuntimeException
33       *             If loading the configuration failed.
34       */
35      public static SecurityConfig getInstance(final String configFileLocation)
36      {
37          if (instance == null)
38          {
39              loadInstance(configFileLocation);
40          }
41          return instance;
42      }
43  
44      /** Set the SecurityConfig instance to return. Useful for unit tests. */
45      public static void setSecurityConfig(final SecurityConfig securityConfig)
46      {
47          instance = securityConfig;
48      }
49  
50      private synchronized static void loadInstance(final String configFileLocation)
51      {
52          if (instance == null)
53          {
54              try
55              {
56                  instance = new SecurityConfigImpl(configFileLocation);
57              }
58              catch (final ConfigurationException e)
59              {
60                  throw new RuntimeException("Could not load security config '" + configFileLocation + "': " + e.getMessage());
61              }
62          }
63      }
64  
65      // Factories should never be instantiated
66      private SecurityConfigFactory()
67      {}
68  }