/
AddonRepositoryImpl.java
455 lines (406 loc) · 13.8 KB
/
AddonRepositoryImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
/*
* Copyright 2012 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Eclipse Public License version 1.0, available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.jboss.forge.container.impl;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.enterprise.inject.Typed;
import org.jboss.forge.container.AddonDependency;
import org.jboss.forge.container.AddonEntry;
import org.jboss.forge.container.AddonRepository;
import org.jboss.forge.container.util.Assert;
import org.jboss.forge.container.util.Files;
import org.jboss.forge.container.util.OSUtils;
import org.jboss.forge.container.util.Streams;
import org.jboss.forge.container.util.Strings;
import org.jboss.forge.parser.xml.Node;
import org.jboss.forge.parser.xml.XMLParser;
import org.jboss.forge.parser.xml.XMLParserException;
/**
* Used to perform Addon installation/registration operations.
*
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
* @author <a href="mailto:koen.aers@gmail.com">Koen Aers</a>
* @author <a href="mailto:ggastald@redhat.com">George Gastaldi</a>
*/
@Typed()
public final class AddonRepositoryImpl implements AddonRepository
{
public static String getRuntimeAPIVersion()
{
String version = AddonRepository.class.getPackage()
.getImplementationVersion();
return version;
}
public static boolean hasRuntimeAPIVersion()
{
return getRuntimeAPIVersion() != null;
}
public static boolean isApiCompatible(CharSequence runtimeVersion, AddonEntry entry)
{
Assert.notNull(runtimeVersion, "Runtime API version must not be null.");
Assert.notNull(entry, "Addon entry must not be null.");
String addonApiVersion = entry.getApiVersion();
Assert.notNull(addonApiVersion, "Addon entry.getApiVersion() must not be null.");
return isApiCompatible(runtimeVersion, addonApiVersion);
}
/**
* This method only returns true if:
*
* - The major version of pluginApiVersion is equal to the major version of runtimeVersion AND
*
* - The minor version of pluginApiVersion is less or equal to the minor version of runtimeVersion
*
* @param runtimeVersion a version in the format x.x.x
* @param addonApiVersion a version in the format x.x.x
* @return
*/
public static boolean isApiCompatible(CharSequence runtimeVersion, CharSequence addonApiVersion)
{
Matcher runtimeMatcher = VERSION_PATTERN.matcher(runtimeVersion);
if (runtimeMatcher.matches())
{
int runtimeMajorVersion = Integer.parseInt(runtimeMatcher.group(1));
int runtimeMinorVersion = Integer.parseInt(runtimeMatcher.group(2));
Matcher addonApiMatcher = VERSION_PATTERN.matcher(addonApiVersion);
if (addonApiMatcher.matches())
{
int addonApiMajorVersion = Integer.parseInt(addonApiMatcher.group(1));
int addonApiMinorVersion = Integer.parseInt(addonApiMatcher.group(2));
if (addonApiMajorVersion == runtimeMajorVersion && addonApiMinorVersion <= runtimeMinorVersion)
{
return true;
}
}
}
return false;
}
private static final String ATTR_SLOT = "slot";
private static final String ATTR_API_VERSION = "api-version";
private static final String ATTR_NAME = "name";
private static final String ADDON_DIR_DEFAULT = ".forge/addons";
private static final String REGISTRY_FILE_NAME = "installed.xml";
private static final Pattern VERSION_PATTERN = Pattern.compile("(\\d+)\\.(\\d+)\\.(\\d+)(\\.|-)(.*)");
private static final String FAR_DESCRIPTOR_FILENAME = "forge.xml";
private File addonDir;
private AddonRepositoryImpl(File dir)
{
Assert.notNull(dir, "Addon directory must not be null");
this.addonDir = dir;
}
public static AddonRepository forAddonDir(File dir)
{
return new AddonRepositoryImpl(dir);
}
public static AddonRepository forDefaultAddonDir()
{
return new AddonRepositoryImpl(new File(OSUtils.getUserHomePath() + ADDON_DIR_DEFAULT));
}
public File getRepositoryDirectory()
{
if (!addonDir.exists() || !addonDir.isDirectory())
{
addonDir.delete();
System.gc();
if (!addonDir.mkdirs())
{
throw new RuntimeException("Could not create Addon Directory [" + addonDir + "]");
}
}
return addonDir;
}
public synchronized File getRegistryFile()
{
File registryFile = new File(getRepositoryDirectory(), REGISTRY_FILE_NAME);
try
{
if (!registryFile.exists())
{
registryFile.createNewFile();
FileOutputStream out = new FileOutputStream(registryFile);
try
{
Streams.write(XMLParser.toXMLInputStream(XMLParser.parse("<installed></installed>")), out);
}
finally
{
out.close();
}
}
return registryFile;
}
catch (Exception e)
{
throw new RuntimeException("Error initializing addon registry file [" + registryFile + "]", e);
}
}
public synchronized List<AddonEntry> listByAPICompatibleVersion(final String version)
{
List<AddonEntry> list = listInstalled();
List<AddonEntry> result = list;
if (version != null)
{
result = new ArrayList<AddonEntry>();
for (AddonEntry entry : list)
{
if (isApiCompatible(version, entry))
{
result.add(entry);
}
}
}
return result;
}
public synchronized List<AddonEntry> listInstalled()
{
List<AddonEntry> result = new ArrayList<AddonEntry>();
File registryFile = getRegistryFile();
try
{
Node installed = XMLParser.parse(registryFile);
if (installed == null)
{
return Collections.emptyList();
}
List<Node> list = installed.get("addon");
for (Node addon : list)
{
AddonEntry entry = AddonEntry.from(addon.getAttribute(ATTR_NAME),
addon.getAttribute(ATTR_API_VERSION),
addon.getAttribute(ATTR_SLOT));
result.add(entry);
}
}
catch (XMLParserException e)
{
throw new RuntimeException("Invalid syntax in [" + registryFile.getAbsolutePath()
+ "] - Please delete this file and restart Forge", e);
}
catch (FileNotFoundException e)
{
// this is OK, no addons installed
}
return result;
}
public synchronized boolean enable(AddonEntry addon)
{
if (addon == null)
{
throw new RuntimeException("Addon must not be null");
}
if (Strings.isNullOrEmpty(addon.getName()))
{
throw new RuntimeException("Addon name must not be null");
}
if (Strings.isNullOrEmpty(addon.getApiVersion()))
{
throw new RuntimeException("Addon API version must not be null");
}
if (Strings.isNullOrEmpty(addon.getSlot()))
{
addon = AddonEntry.from(addon.getName(), addon.getApiVersion(), addon.getSlot());
}
List<AddonEntry> installedAddons = listInstalled();
for (AddonEntry e : installedAddons)
{
if (addon.getName().equals(e.getName()))
{
disable(e);
}
}
File registryFile = getRegistryFile();
try
{
Node installed = XMLParser.parse(registryFile);
installed.getOrCreate("addon@" + ATTR_NAME + "=" + addon.getName() +
"&" + ATTR_API_VERSION + "=" + addon.getApiVersion())
.attribute(ATTR_SLOT, addon.getSlot());
Streams.write(XMLParser.toXMLInputStream(installed), new FileOutputStream(registryFile));
return true;
}
catch (FileNotFoundException e)
{
throw new RuntimeException("Could not read [" + registryFile.getAbsolutePath() + "] - ", e);
}
}
public synchronized boolean disable(final AddonEntry addon)
{
if (addon == null)
{
throw new RuntimeException("Addon must not be null");
}
File registryFile = getRegistryFile();
if (registryFile.exists())
{
try
{
Node installed = XMLParser.parse(registryFile);
Node child = installed.getSingle("addon@" + ATTR_NAME + "=" + addon.getName() + "&"
+ ATTR_API_VERSION
+ "=" + addon.getApiVersion());
installed.removeChild(child);
Streams.write(XMLParser.toXMLInputStream(installed), new FileOutputStream(registryFile));
return true;
}
catch (FileNotFoundException e)
{
throw new RuntimeException("Could not read [" + registryFile.getAbsolutePath() + "] - ", e);
}
}
return false;
}
public synchronized AddonEntry get(final AddonEntry addon)
{
if (addon == null)
{
throw new RuntimeException("Addon must not be null");
}
File registryFile = getRegistryFile();
try
{
Node installed = XMLParser.parse(registryFile);
List<Node> children = installed.get("addon@" + ATTR_NAME + "=" + addon.getName());
for (Node child : children)
{
if (child != null)
{
if ((addon.getApiVersion() == null)
|| addon.getApiVersion().equals(child.getAttribute(ATTR_API_VERSION)))
{
if ((addon.getSlot() == null)
|| addon.getSlot().equals(child.getAttribute(ATTR_SLOT)))
{
return AddonEntry.from(child.getAttribute(ATTR_NAME),
child.getAttribute(ATTR_API_VERSION),
child.getAttribute(ATTR_SLOT));
}
}
}
}
}
catch (FileNotFoundException e)
{
// already removed
}
return null;
}
public synchronized File getAddonResourceDir(AddonEntry found)
{
Assert.notNull(found.getSlot(), "Addon slot must be specified.");
Assert.notNull(found.getName(), "Addon name must be specified.");
String path = found.getName().replaceAll("\\.", "/");
File addonDir = new File(getRepositoryDirectory(), path + "/" + found.getSlot());
return addonDir;
}
public synchronized File getAddonBaseDir(AddonEntry found)
{
Assert.notNull(found.getSlot(), "Addon slot must be specified.");
Assert.notNull(found.getName(), "Addon name must be specified.");
String path = found.getName().split("\\.")[0];
File addonDir = new File(getRepositoryDirectory(), path);
return addonDir;
}
public synchronized boolean has(final AddonEntry addon)
{
return get(addon) != null;
}
public List<File> getAddonResources(AddonEntry found)
{
File dir = getAddonResourceDir(found);
if (dir.exists())
{
return Arrays.asList(dir.listFiles(new FilenameFilter()
{
@Override
public boolean accept(File file, String name)
{
return name.endsWith(".jar");
}
}));
}
return new ArrayList<File>();
}
public File getAddonSlotDir(AddonEntry addon)
{
return new File(getAddonBaseDir(addon).getAbsolutePath(), addon.getSlot());
}
public synchronized List<AddonDependency> getAddonDependencies(AddonEntry addon)
{
List<AddonDependency> result = new ArrayList<AddonDependency>();
File descriptor = getAddonDescriptor(addon);
try
{
Node installed = XMLParser.parse(descriptor);
List<Node> children = installed.get("dependency");
for (Node child : children)
{
if (child != null)
{
result.add(new AddonDependency(
child.getAttribute(ATTR_NAME),
child.getAttribute("min-version"),
child.getAttribute("max-version"),
Boolean.valueOf(child.getAttribute("optional"))));
}
}
}
catch (FileNotFoundException e)
{
// already removed
}
return result;
}
public synchronized File getAddonDescriptor(AddonEntry addon)
{
File descriptorFile = new File(getAddonResourceDir(addon), FAR_DESCRIPTOR_FILENAME);
try
{
if (!descriptorFile.exists())
{
descriptorFile.mkdirs();
descriptorFile.delete();
descriptorFile.createNewFile();
FileOutputStream stream = new FileOutputStream(descriptorFile);
Streams.write(XMLParser.toXMLInputStream(XMLParser.parse("<addon/>")), stream);
stream.close();
}
return descriptorFile;
}
catch (Exception e)
{
throw new RuntimeException("Error initializing addon descriptor file.", e);
}
}
@Override
public synchronized boolean deploy(AddonEntry entry, File farFile, File... dependencies)
{
File addonSlotDir = getAddonSlotDir(entry);
try
{
Files.copyFileToDirectory(farFile, addonSlotDir);
for (File dependency : dependencies)
{
Files.copyFileToDirectory(dependency, addonSlotDir);
}
return true;
}
catch (IOException io)
{
// TODO throw exception instead?
io.printStackTrace();
return false;
}
}
}