Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 1 | package aQute.bnd.build; |
| 2 | |
| 3 | import java.io.*; |
| 4 | import java.util.*; |
| 5 | import java.util.jar.*; |
| 6 | |
Stuart McCulloch | 39cc9ac | 2012-07-16 13:43:38 +0000 | [diff] [blame] | 7 | import aQute.bnd.osgi.*; |
Stuart McCulloch | b215bfd | 2012-09-06 18:28:06 +0000 | [diff] [blame] | 8 | import aQute.bnd.service.*; |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 9 | |
| 10 | public class Container { |
| 11 | public enum TYPE { |
Stuart McCulloch | 151384c | 2012-06-18 11:15:15 +0000 | [diff] [blame] | 12 | REPO, PROJECT, EXTERNAL, LIBRARY, ERROR |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 13 | } |
| 14 | |
Stuart McCulloch | b215bfd | 2012-09-06 18:28:06 +0000 | [diff] [blame] | 15 | private final File file; |
| 16 | private final String path; |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 17 | final TYPE type; |
| 18 | final String bsn; |
| 19 | final String version; |
| 20 | final String error; |
| 21 | final Project project; |
Stuart McCulloch | b215bfd | 2012-09-06 18:28:06 +0000 | [diff] [blame] | 22 | final DownloadBlocker db; |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 23 | volatile Map<String,String> attributes; |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 24 | private long manifestTime; |
| 25 | private Manifest manifest; |
| 26 | |
| 27 | Container(Project project, String bsn, String version, TYPE type, File source, String error, |
Stuart McCulloch | b215bfd | 2012-09-06 18:28:06 +0000 | [diff] [blame] | 28 | Map<String,String> attributes, DownloadBlocker db) { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 29 | this.bsn = bsn; |
| 30 | this.version = version; |
| 31 | this.type = type; |
| 32 | this.file = source != null ? source : new File("/" + bsn + ":" + version + ":" + type); |
Stuart McCulloch | b215bfd | 2012-09-06 18:28:06 +0000 | [diff] [blame] | 33 | this.path = file.getAbsolutePath(); |
| 34 | |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 35 | this.project = project; |
| 36 | this.error = error; |
| 37 | if (attributes == null || attributes.isEmpty()) |
| 38 | this.attributes = Collections.emptyMap(); |
| 39 | else |
| 40 | this.attributes = attributes; |
Stuart McCulloch | b215bfd | 2012-09-06 18:28:06 +0000 | [diff] [blame] | 41 | this.db = db; |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | public Container(Project project, File file) { |
Stuart McCulloch | b215bfd | 2012-09-06 18:28:06 +0000 | [diff] [blame] | 45 | this(project, file.getName(), "project", TYPE.PROJECT, file, null, null, null); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Stuart McCulloch | b215bfd | 2012-09-06 18:28:06 +0000 | [diff] [blame] | 48 | public Container(File file, DownloadBlocker db) { |
| 49 | this(null, file.getName(), "project", TYPE.EXTERNAL, file, null, null, db); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | public File getFile() { |
Stuart McCulloch | b215bfd | 2012-09-06 18:28:06 +0000 | [diff] [blame] | 53 | if (db != null && db.getReason() != null) { |
| 54 | return new File(db.getReason() + ": " + file); |
| 55 | } |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 56 | return file; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Iterate over the containers and get the files they represent |
| 61 | * |
| 62 | * @param files |
| 63 | * @return |
| 64 | * @throws Exception |
| 65 | */ |
| 66 | public boolean contributeFiles(List<File> files, Processor reporter) throws Exception { |
| 67 | switch (type) { |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 68 | case EXTERNAL : |
| 69 | case REPO : |
Stuart McCulloch | b215bfd | 2012-09-06 18:28:06 +0000 | [diff] [blame] | 70 | files.add(getFile()); |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 71 | return true; |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 72 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 73 | case PROJECT : |
| 74 | File[] fs = project.build(); |
| 75 | reporter.getInfo(project); |
| 76 | if (fs == null) |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 77 | return false; |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 78 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 79 | for (File f : fs) |
| 80 | files.add(f); |
| 81 | return true; |
| 82 | |
| 83 | case LIBRARY : |
| 84 | List<Container> containers = getMembers(); |
| 85 | for (Container container : containers) { |
| 86 | if (!container.contributeFiles(files, reporter)) |
| 87 | return false; |
| 88 | } |
| 89 | return true; |
| 90 | |
| 91 | case ERROR : |
| 92 | reporter.error(error); |
| 93 | return false; |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 94 | } |
| 95 | return false; |
| 96 | } |
| 97 | |
| 98 | public String getBundleSymbolicName() { |
| 99 | return bsn; |
| 100 | } |
| 101 | |
| 102 | public String getVersion() { |
| 103 | return version; |
| 104 | } |
| 105 | |
| 106 | public TYPE getType() { |
| 107 | return type; |
| 108 | } |
| 109 | |
| 110 | public String getError() { |
| 111 | return error; |
| 112 | } |
| 113 | |
Stuart McCulloch | 55d4dfe | 2012-08-07 10:57:21 +0000 | [diff] [blame] | 114 | @Override |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 115 | public boolean equals(Object other) { |
| 116 | if (other instanceof Container) |
Stuart McCulloch | b215bfd | 2012-09-06 18:28:06 +0000 | [diff] [blame] | 117 | return path.equals(((Container) other).path); |
Stuart McCulloch | d482610 | 2012-06-26 16:34:24 +0000 | [diff] [blame] | 118 | return false; |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Stuart McCulloch | 55d4dfe | 2012-08-07 10:57:21 +0000 | [diff] [blame] | 121 | @Override |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 122 | public int hashCode() { |
Stuart McCulloch | b215bfd | 2012-09-06 18:28:06 +0000 | [diff] [blame] | 123 | return path.hashCode(); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | public Project getProject() { |
| 127 | return project; |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Must show the file name or the error formatted as a file name |
| 132 | * |
| 133 | * @return |
| 134 | */ |
Stuart McCulloch | 55d4dfe | 2012-08-07 10:57:21 +0000 | [diff] [blame] | 135 | @Override |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 136 | public String toString() { |
| 137 | if (getError() != null) |
| 138 | return "/error/" + getError(); |
Stuart McCulloch | d482610 | 2012-06-26 16:34:24 +0000 | [diff] [blame] | 139 | return getFile().getAbsolutePath(); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 140 | } |
| 141 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 142 | public Map<String,String> getAttributes() { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 143 | return attributes; |
| 144 | } |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 145 | |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 146 | public void putAttribute(String name, String value) { |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 147 | if (attributes == Collections.<String, String> emptyMap()) |
| 148 | attributes = new HashMap<String,String>(1); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 149 | attributes.put(name, value); |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Return the this if this is anything else but a library. If it is a |
| 154 | * library, return the members. This could work recursively, e.g., libraries |
| 155 | * can point to libraries. |
| 156 | * |
| 157 | * @return |
| 158 | * @throws Exception |
| 159 | */ |
| 160 | public List<Container> getMembers() throws Exception { |
| 161 | List<Container> result = project.newList(); |
| 162 | |
| 163 | // Are ww a library? If no, we are the result |
| 164 | if (getType() == TYPE.LIBRARY) { |
| 165 | // We are a library, parse the file. This is |
| 166 | // basically a specification clause per line. |
| 167 | // I.e. you can do bsn; version, bsn2; version. But also |
| 168 | // spread it out over lines. |
| 169 | InputStream in = null; |
| 170 | BufferedReader rd = null; |
| 171 | String line; |
| 172 | try { |
Stuart McCulloch | b215bfd | 2012-09-06 18:28:06 +0000 | [diff] [blame] | 173 | in = new FileInputStream(getFile()); |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 174 | rd = new BufferedReader(new InputStreamReader(in, Constants.DEFAULT_CHARSET)); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 175 | while ((line = rd.readLine()) != null) { |
| 176 | line = line.trim(); |
| 177 | if (!line.startsWith("#") && line.length() > 0) { |
| 178 | List<Container> list = project.getBundles(Strategy.HIGHEST, line, null); |
| 179 | result.addAll(list); |
| 180 | } |
| 181 | } |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 182 | } |
| 183 | finally { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 184 | if (rd != null) { |
| 185 | rd.close(); |
| 186 | } |
| 187 | if (in != null) { |
| 188 | in.close(); |
| 189 | } |
| 190 | } |
| 191 | } else |
| 192 | result.add(this); |
| 193 | |
| 194 | return result; |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Answer the manifest for this container (if possible). Manifest is cached |
| 199 | * until the file is renewed. |
| 200 | */ |
| 201 | |
| 202 | public Manifest getManifest() throws Exception { |
| 203 | if (getError() != null || getFile() == null) |
| 204 | return null; |
| 205 | |
| 206 | if (manifestTime < getFile().lastModified()) { |
| 207 | InputStream in = new FileInputStream(getFile()); |
| 208 | try { |
| 209 | JarInputStream jin = new JarInputStream(in); |
| 210 | manifest = jin.getManifest(); |
| 211 | jin.close(); |
| 212 | manifestTime = getFile().lastModified(); |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 213 | } |
| 214 | finally { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 215 | in.close(); |
| 216 | } |
| 217 | } |
| 218 | return manifest; |
| 219 | } |
| 220 | } |