PageRenderTime 18ms CodeModel.GetById 6ms app.highlight 11ms RepoModel.GetById 0ms app.codeStats 0ms

/activeobjects-plugin/src/main/java/com/atlassian/activeobjects/admin/ActiveObjectsPluginToTablesMapping.java

https://bitbucket.org/activeobjects/ao-plugin
Java | 98 lines | 84 code | 14 blank | 0 comment | 2 complexity | 37794479d0dac6982cbee57099a23180 MD5 | raw file
Possible License(s): Apache-2.0
 1package com.atlassian.activeobjects.admin;
 2
 3import com.atlassian.sal.api.pluginsettings.PluginSettings;
 4import com.atlassian.sal.api.pluginsettings.PluginSettingsFactory;
 5import com.google.common.collect.Maps;
 6import com.google.gson.Gson;
 7import com.google.gson.reflect.TypeToken;
 8
 9import java.lang.reflect.Type;
10import java.util.List;
11import java.util.Map;
12import java.util.concurrent.locks.ReadWriteLock;
13import java.util.concurrent.locks.ReentrantReadWriteLock;
14
15import static com.google.common.base.Preconditions.*;
16
17public final class ActiveObjectsPluginToTablesMapping implements PluginToTablesMapping
18{
19    private static final String KEY = ActiveObjectsPluginToTablesMapping.class.getName();
20
21    private static final Type MAPPINGS_TYPE = new TypeToken<Map<String, PluginInfo>>()
22    {
23    }.getType();
24
25    private final ReadWriteLock lock = new ReentrantReadWriteLock();
26
27    private final PluginSettings settings;
28    private Map<String, PluginInfo> mappings;
29
30    public ActiveObjectsPluginToTablesMapping(PluginSettingsFactory factory)
31    {
32        this.settings = checkNotNull(factory).createGlobalSettings();
33        this.mappings = getMappingFromSettings();
34    }
35
36    @Override
37    public void add(PluginInfo pluginInfo, List<String> tableNames)
38    {
39        lock.writeLock().lock();
40        try
41        {
42            doAdd(pluginInfo, tableNames);
43        }
44        finally
45        {
46            lock.writeLock().unlock();
47        }
48    }
49
50    @Override
51    public PluginInfo get(String tableName)
52    {
53        lock.readLock().lock();
54        try
55        {
56            return doGet(tableName);
57        }
58        finally
59        {
60            lock.readLock().unlock();
61        }
62    }
63
64    private void doAdd(PluginInfo pluginInfo, List<String> tableNames)
65    {
66        final Map<String, PluginInfo> newMappings = Maps.newHashMap(mappings);
67        for (String tableName : tableNames)
68        {
69            newMappings.put(tableName, pluginInfo);
70        }
71        putMapInSettings(newMappings);
72        this.mappings = newMappings;
73    }
74
75    private PluginInfo doGet(String tableName)
76    {
77        return mappings.get(tableName);
78    }
79
80    private void putMapInSettings(Map<String, PluginInfo> newMappings)
81    {
82        settings.put(KEY, new Gson().toJson(newMappings));
83    }
84
85    public Map<String, PluginInfo> getMappingFromSettings()
86    {
87        lock.readLock().lock();
88        try
89        {
90            final Map<String, PluginInfo> map = new Gson().fromJson((String) settings.get(KEY), MAPPINGS_TYPE);
91            return map != null ? map : Maps.<String, PluginInfo>newHashMap();
92        }
93        finally
94        {
95            lock.readLock().unlock();
96        }
97    }
98}