blob: ddf5fcc17406a65511ad757a7014de624c38f8fc [file] [log] [blame]
Stuart McCulloch26e7a5a2011-10-17 10:31:43 +00001package aQute.lib.deployer.obr;
2
3import java.io.File;
4import java.net.MalformedURLException;
5import java.net.URL;
6import java.util.Arrays;
7import java.util.Collections;
8import java.util.List;
9import java.util.Map;
10
11import aQute.lib.deployer.FileRepo;
12
13/**
14 * A simple read-only OBR-based repository that uses a list of index locations
15 * and a basic local cache.
16 *
17 * <p>
18 * <h2>Properties</h2>
19 * <ul>
20 * <li><b>locations:</b> comma-separated list of index URLs. <b>NB:</b> surround with single quotes!</li>
21 * <li><b>name:</b> repository name; defaults to the index URLs.
22 * <li><b>cache:</b> local cache directory. May be omitted, in which case the repository will only be
23 * able to serve resources with {@code file:} URLs.</li>
24 * <li><b>location:</b> (deprecated) alias for "locations".
25 * </ul>
26 *
27 * <p>
28 * <h2>Example</h2>
29 *
30 * <pre>
31 * -plugin: aQute.lib.deployer.obr.OBR;locations='http://www.example.com/repository.xml';cache=${workspace}/.cache
32 * </pre>
33 *
34 * @author Neil Bartlett
35 *
36 */
37public class OBR extends AbstractBaseOBR {
38
39 public static final String PROP_LOCATIONS = "locations";
40 @Deprecated
41 public static final String PROP_LOCATION = "location";
42 public static final String PROP_CACHE = "cache";
43
44 protected List<URL> locations;
45 protected File cacheDir;
46
47 public void setProperties(Map<String, String> map) {
48 super.setProperties(map);
49
50 String locationsStr = map.get(PROP_LOCATIONS);
51 // backwards compatibility
52 if (locationsStr == null) locationsStr = map.get(PROP_LOCATION);
53
54 try {
55 if (locationsStr != null)
56 locations = parseLocations(locationsStr);
57 else
58 locations = Collections.emptyList();
59 } catch (MalformedURLException e) {
60 throw new IllegalArgumentException(String.format("Invalid location, unable to parse as URL list: %s", locationsStr), e);
61 }
62
63 String cacheDirStr = map.get(PROP_CACHE);
64 if (cacheDirStr != null)
65 cacheDir = new File(cacheDirStr);
66 }
67
68 private FileRepo lookupCachedFileRepo() {
69 if (registry != null) {
70 List<FileRepo> repos = registry.getPlugins(FileRepo.class);
71 for (FileRepo repo : repos) {
72 if ("cache".equals(repo.getName()))
73 return repo;
74 }
75 }
76 return null;
77 }
78
79 public List<URL> getOBRIndexes() {
80 return locations;
81 }
82
83 @Override
84 public synchronized File getCacheDirectory() {
85 if (cacheDir == null) {
86 FileRepo cacheRepo = lookupCachedFileRepo();
87 if (cacheRepo != null) {
88 File temp = new File(cacheRepo.getRoot(), ".obr");
89 temp.mkdirs();
90 if (temp.exists())
91 cacheDir = temp;
92 }
93 }
94 return cacheDir;
95 }
96
97 public void setCacheDirectory(File cacheDir) {
98 this.cacheDir = cacheDir;
99 }
100
101 @Override
102 public String getName() {
103 if (name != null && name != this.getClass().getName())
104 return name;
105
106 StringBuilder builder = new StringBuilder();
107
108 int count = 0;
109 for (URL location : locations) {
110 if (count++ > 0 ) builder.append(',');
111 builder.append(location);
112 }
113 return builder.toString();
114 }
115
116 public void setLocations(URL[] urls) {
117 this.locations = Arrays.asList(urls);
118 }
119
120}