View Javadoc

1   package com.atlassian.config;
2   
3   import com.atlassian.core.util.ClassHelper;
4   
5   import java.util.HashMap;
6   import java.util.Iterator;
7   import java.util.Map;
8   
9   public abstract class AbstractConfigurationPersister implements ConfigurationPersister
10  {
11      private Map configMappings = new HashMap();
12  
13      public void addConfigMapping(Class propertyType, Class configType)
14      {
15          configMappings.put(propertyType, configType);
16      }
17  
18      public abstract Object getRootContext();
19  
20      public void addConfigElement(Object item, String propertyName) throws ConfigurationException
21      {
22          addConfigElement(item, propertyName, getRootContext());
23      }
24  
25      public Object getConfigElement(Class propertyType, String propertyName) throws ConfigurationException
26      {
27          return getConfigElement(propertyType, propertyName, getRootContext());
28      }
29  
30      public String getStringConfigElement(String propertyName) throws ConfigurationException
31      {
32          return (String) getConfigElement(String.class, propertyName);
33      }
34  
35      public void addConfigElement(Object item, String propertyName, Object context) throws ConfigurationException
36      {
37          Class clazz = null;
38          Map.Entry entry;
39  
40          if (item == null)
41              return;
42  
43          for (Iterator iterator = configMappings.entrySet().iterator(); iterator.hasNext();)
44          {
45              entry = (Map.Entry) iterator.next();
46              Class c = (Class) entry.getKey();
47              if (c.isAssignableFrom(item.getClass()))
48              {
49                  clazz = (Class) entry.getValue();
50                  break;
51              }
52          }
53          if (clazz != null)
54          {
55              try
56              {
57                  ConfigElement config = (ConfigElement) ClassHelper.instantiateClass(clazz, new Object[]{propertyName, context, this});
58                  config.save(item);
59              }
60              catch (Exception e)
61              {
62                  throw new ConfigurationException("Failed to create config element: " + clazz.getName(), e);
63              }
64          }
65          else
66          {
67              throw new ConfigurationException("Failed to find config element for " + item.getClass().getName());
68          }
69      }
70  
71      public Object getConfigElement(Class propertyType, String propertyName, Object context) throws ConfigurationException
72      {
73          Class clazz = (Class) configMappings.get(propertyType);
74          if (clazz != null)
75          {
76              try
77              {
78                  ConfigElement config = (ConfigElement) ClassHelper.instantiateClass(clazz, new Object[]{propertyName, context, this});
79                  return config.load();
80              }
81              catch (Exception e)
82              {
83                  throw new ConfigurationException("Failed to create config element: " + clazz.getName(), e);
84              }
85          }
86          else
87          {
88              throw new ConfigurationException("Failed to find config element for " + propertyType.getName());
89          }
90      }
91  }