1 package com.atlassian.activeobjects.backup;
2
3 import com.atlassian.activeobjects.admin.PluginInfo;
4 import com.atlassian.activeobjects.admin.PluginToTablesMapping;
5 import com.atlassian.activeobjects.plugin.ActiveObjectModuleDescriptor;
6 import com.atlassian.activeobjects.spi.PluginInformation;
7 import com.atlassian.plugin.ModuleDescriptor;
8 import com.atlassian.plugin.Plugin;
9 import com.atlassian.plugin.PluginAccessor;
10 import com.atlassian.plugin.predicate.ModuleDescriptorPredicate;
11
12 import java.util.Collection;
13
14 import static com.google.common.base.Preconditions.checkNotNull;
15
16 public final class PluginInformationFactory {
17 private final PluginToTablesMapping pluginToTablesMapping;
18 private final PluginAccessor pluginAccessor;
19
20 public PluginInformationFactory(PluginToTablesMapping pluginToTablesMapping, PluginAccessor pluginAccessor) {
21 this.pluginToTablesMapping = checkNotNull(pluginToTablesMapping);
22 this.pluginAccessor = checkNotNull(pluginAccessor);
23 }
24
25
26
27
28
29
30
31 public PluginInformation getPluginInformation(final String tableName) {
32 if (tableName == null) {
33 return new NotAvailablePluginInformation();
34 }
35
36 final PluginInfo pluginInfo = pluginToTablesMapping.get(tableName);
37 if (pluginInfo != null) {
38 return new AvailablePluginInformation(pluginInfo);
39 }
40
41 final ActiveObjectModuleDescriptor aomd = getModuleDescriptor(tableName);
42 if (aomd != null) {
43 return new AvailablePluginInformation(aomd.getPlugin());
44 }
45
46 return new NotAvailablePluginInformation();
47 }
48
49 private ActiveObjectModuleDescriptor getModuleDescriptor(String tableName) {
50 final Collection<ModuleDescriptor<Object>> moduleDescriptors = findModuleDescriptors(tableName);
51 return moduleDescriptors.isEmpty() ? null : (ActiveObjectModuleDescriptor) moduleDescriptors.iterator().next();
52 }
53
54 private Collection<ModuleDescriptor<Object>> findModuleDescriptors(final String tableName) {
55 return pluginAccessor.getModuleDescriptors(new ModuleDescriptorPredicate<Object>() {
56 @Override
57 public boolean matches(ModuleDescriptor<? extends Object> moduleDescriptor) {
58 return moduleDescriptor instanceof ActiveObjectModuleDescriptor
59 && ((ActiveObjectModuleDescriptor) moduleDescriptor).getConfiguration().getTableNamePrefix().isStarting(tableName, false);
60 }
61 });
62 }
63
64 private static final class NotAvailablePluginInformation implements PluginInformation {
65 @Override
66 public boolean isAvailable() {
67 return false;
68 }
69
70 @Override
71 public String getPluginName() {
72 return null;
73 }
74
75 @Override
76 public String getPluginKey() {
77 return null;
78 }
79
80 @Override
81 public String getPluginVersion() {
82 return null;
83 }
84
85 @Override
86 public String getVendorName() {
87 return null;
88 }
89
90 @Override
91 public String getVendorUrl() {
92 return null;
93 }
94
95 @Override
96 public boolean equals(Object o) {
97 return o != null && o instanceof NotAvailablePluginInformation;
98 }
99
100 @Override
101 public int hashCode() {
102 return 0;
103 }
104
105 @Override
106 public String toString() {
107 return "<unknown plugin>";
108 }
109 }
110
111 private static final class AvailablePluginInformation implements PluginInformation {
112 private final String name;
113 private final String key;
114 private final String version;
115 private final String vendorName;
116 private final String vendorUrl;
117
118 public AvailablePluginInformation(Plugin plugin) {
119 this(
120 checkNotNull(plugin).getName(),
121 plugin.getKey(),
122 plugin.getPluginInformation().getVersion(),
123 plugin.getPluginInformation().getVendorName(),
124 plugin.getPluginInformation().getVendorUrl()
125 );
126 }
127
128 public AvailablePluginInformation(PluginInfo pluginInfo) {
129 this(checkNotNull(pluginInfo).name, pluginInfo.key, pluginInfo.version, pluginInfo.vendorName, pluginInfo.vendorUrl);
130 }
131
132 private AvailablePluginInformation(String name, String key, String version, String vendorName, String vendorUrl) {
133 this.name = name;
134 this.key = key;
135 this.version = version;
136 this.vendorName = vendorName;
137 this.vendorUrl = vendorUrl;
138 }
139
140 @Override
141 public boolean isAvailable() {
142 return true;
143 }
144
145 @Override
146 public String getPluginName() {
147 return name;
148 }
149
150 @Override
151 public String getPluginKey() {
152 return key;
153 }
154
155 @Override
156 public String getPluginVersion() {
157 return version;
158 }
159
160 @Override
161 public String getVendorName() {
162 return vendorName;
163 }
164
165 @Override
166 public String getVendorUrl() {
167 return vendorUrl;
168 }
169
170 @Override
171 public boolean equals(Object o) {
172 if (this == o) {
173 return true;
174 }
175 if (o == null || getClass() != o.getClass()) {
176 return false;
177 }
178
179 final AvailablePluginInformation that = (AvailablePluginInformation) o;
180
181 if (key != null ? !key.equals(that.key) : that.key != null) {
182 return false;
183 }
184 if (name != null ? !name.equals(that.name) : that.name != null) {
185 return false;
186 }
187 if (vendorName != null ? !vendorName.equals(that.vendorName) : that.vendorName != null) {
188 return false;
189 }
190 if (vendorUrl != null ? !vendorUrl.equals(that.vendorUrl) : that.vendorUrl != null) {
191 return false;
192 }
193 if (version != null ? !version.equals(that.version) : that.version != null) {
194 return false;
195 }
196
197 return true;
198 }
199
200 @Override
201 public int hashCode() {
202 int result = name != null ? name.hashCode() : 1;
203 result = 31 * result + (key != null ? key.hashCode() : 1);
204 result = 31 * result + (version != null ? version.hashCode() : 1);
205 result = 31 * result + (vendorName != null ? vendorName.hashCode() : 1);
206 result = 31 * result + (vendorUrl != null ? vendorUrl.hashCode() : 1);
207 return result;
208 }
209
210 @Override
211 public String toString() {
212 return "plugin " + getPluginName() + "(" + getPluginKey() + ") #" + getPluginVersion();
213 }
214 }
215 }