Stuart McCulloch | de5fbec | 2008-12-03 12:03:12 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * $Id: ResourceImpl.java 44 2007-07-13 20:49:41Z hargrave@us.ibm.com $ |
| 3 | * |
| 4 | * Copyright (c) OSGi Alliance (2002, 2006, 2007). All Rights Reserved. |
| 5 | * |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | */ |
| 18 | package org.osgi.impl.bundle.obr.resource; |
| 19 | |
| 20 | import java.io.File; |
| 21 | import java.net.URL; |
| 22 | import java.util.*; |
| 23 | |
| 24 | import org.osgi.framework.Version; |
| 25 | import org.osgi.service.obr.*; |
| 26 | import org.xmlpull.v1.XmlPullParser; |
| 27 | |
| 28 | public class ResourceImpl implements Resource { |
| 29 | List capabilities = new ArrayList(); |
| 30 | List requirements = new ArrayList(); |
| 31 | URL url; |
| 32 | String symbolicName; |
| 33 | VersionRange version; |
| 34 | List categories = new ArrayList(); |
| 35 | long size = -1; |
| 36 | String id; |
| 37 | static int ID = 1; |
| 38 | Map map = new HashMap(); |
| 39 | RepositoryImpl repository; |
| 40 | String presentationName; |
| 41 | File file; |
| 42 | |
| 43 | |
| 44 | public ResourceImpl(RepositoryImpl repository, String name, |
| 45 | VersionRange version) { |
| 46 | this.version = version; |
| 47 | if ( version == null) |
| 48 | this.version = new VersionRange("0"); |
| 49 | this.symbolicName = name; |
| 50 | this.repository = repository; |
| 51 | } |
| 52 | |
| 53 | public ResourceImpl(RepositoryImpl repository, XmlPullParser parser) |
| 54 | throws Exception { |
| 55 | this.repository = repository; |
| 56 | parser.require(XmlPullParser.START_TAG, null, "resource"); |
| 57 | symbolicName = parser.getAttributeValue(null, "symbolicname"); |
| 58 | if (symbolicName == null) |
| 59 | System.err.println("Hey, no symb name! " |
| 60 | + parser.getAttributeValue(null, "uri")); |
| 61 | |
| 62 | map.put(SYMBOLIC_NAME, symbolicName); |
| 63 | presentationName = parser.getAttributeValue(null, PRESENTATION_NAME); |
| 64 | if (presentationName != null) |
| 65 | map.put(PRESENTATION_NAME, presentationName); |
| 66 | String v = parser.getAttributeValue(null, "version"); |
| 67 | if (v == null) |
| 68 | setVersion(new VersionRange("0")); |
| 69 | else |
| 70 | setVersion(new VersionRange(v)); |
| 71 | |
| 72 | setURL(toURL(parser.getAttributeValue(null, "uri"))); |
| 73 | |
| 74 | while (parser.nextTag() == XmlPullParser.START_TAG) { |
| 75 | if (parser.getName().equals("category")) { |
| 76 | categories.add(parser.getAttributeValue(null, "id").trim()); |
| 77 | } |
| 78 | else if (parser.getName().equals("require")) |
| 79 | addRequirement(new RequirementImpl(parser)); |
| 80 | else if (parser.getName().equals("capability")) |
| 81 | addCapability(new CapabilityImpl(parser)); |
| 82 | else { |
| 83 | String text = parser.nextText(); |
| 84 | if (text != null) |
| 85 | map.put(parser.getName(), text.trim()); |
| 86 | } |
| 87 | parser.next(); |
| 88 | } |
| 89 | parser.require(XmlPullParser.END_TAG, null, "resource"); |
| 90 | } |
| 91 | |
| 92 | public ResourceImpl(RepositoryImpl impl) { |
| 93 | this.repository = impl; |
| 94 | } |
| 95 | |
| 96 | private URL toURL(String attributeValue) throws Exception { |
| 97 | if (attributeValue == null) |
| 98 | return null; |
| 99 | |
| 100 | return new URL(repository.getURL(), attributeValue); |
| 101 | } |
| 102 | |
| 103 | public void addCategory(String category) { |
| 104 | categories.add(category); |
| 105 | } |
| 106 | |
| 107 | public void addCapability(CapabilityImpl capability) { |
| 108 | if (capability != null) |
| 109 | capabilities.add(capability); |
| 110 | } |
| 111 | |
| 112 | public void addRequirement(RequirementImpl requirement) { |
| 113 | if (requirement != null) |
| 114 | requirements.add(requirement); |
| 115 | } |
| 116 | |
| 117 | public void setLicense(URL license) { |
| 118 | if (license != null) |
| 119 | map.put(LICENSE_URL, license); |
| 120 | } |
| 121 | |
| 122 | public String getDescription() { |
| 123 | return (String) map.get(DESCRIPTION); |
| 124 | } |
| 125 | |
| 126 | public void setDescription(String description) { |
| 127 | if (description != null) |
| 128 | map.put(DESCRIPTION, description); |
| 129 | } |
| 130 | |
| 131 | public Capability[] getCapabilities() { |
| 132 | return (Capability[]) capabilities.toArray(new Capability[capabilities |
| 133 | .size()]); |
| 134 | } |
| 135 | |
| 136 | public URL getLicense() { |
| 137 | return (URL) map.get(LICENSE_URL); |
| 138 | } |
| 139 | |
| 140 | public String getSymbolicName() { |
| 141 | return symbolicName; |
| 142 | } |
| 143 | |
| 144 | public Requirement[] getRequirements() { |
| 145 | return (Requirement[]) requirements |
| 146 | .toArray(new Requirement[requirements.size()]); |
| 147 | } |
| 148 | |
| 149 | public Tag toXML() { |
| 150 | return toXML(this ); |
| 151 | } |
| 152 | |
| 153 | public static Tag toXML(Resource resource) { |
| 154 | return toXML(resource,true); |
| 155 | } |
| 156 | |
| 157 | public static Tag toXML(Resource resource, boolean relative ) { |
| 158 | Tag meta = new Tag("resource"); |
| 159 | URL url = resource.getURL(); |
| 160 | String urlString = url.toExternalForm(); |
| 161 | |
| 162 | if ( relative ) |
| 163 | urlString = makeRelative(resource.getRepository().getURL(), url); |
| 164 | |
| 165 | meta.addAttribute("uri", urlString ); |
| 166 | meta.addAttribute(SYMBOLIC_NAME, resource.getSymbolicName()); |
| 167 | if (resource.getPresentationName() != null) |
| 168 | meta |
| 169 | .addAttribute(PRESENTATION_NAME, resource |
| 170 | .getPresentationName()); |
| 171 | meta.addAttribute(VERSION, resource.getVersion().toString()); |
| 172 | meta.addAttribute("id", resource.getId()); |
| 173 | Map map = new TreeMap(resource.getProperties()); |
| 174 | for (int i = 0; i < Resource.KEYS.length; i++) { |
| 175 | String key = KEYS[i]; |
| 176 | if (!(key.equals(URL) || key.equals(SYMBOLIC_NAME) || key |
| 177 | .equals(VERSION) || key.equals(PRESENTATION_NAME))) { |
| 178 | Object value = map.get(KEYS[i]); |
| 179 | if (value != null) { |
| 180 | if (value instanceof URL) |
| 181 | value = makeRelative(resource.getRepository().getURL(),(URL) value); |
| 182 | meta.addContent(new Tag(key, value.toString())); |
| 183 | } |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | String[] categories = resource.getCategories(); |
| 188 | for (int i = 0; i < categories.length; i++) { |
| 189 | String category = categories[i]; |
| 190 | meta.addContent(new Tag("category", new String[] {"id", |
| 191 | category.toLowerCase()})); |
| 192 | } |
| 193 | |
| 194 | Capability[] capabilities = resource.getCapabilities(); |
| 195 | for (int i = 0; i < capabilities.length; i++) { |
| 196 | meta.addContent(CapabilityImpl.toXML(capabilities[i])); |
| 197 | } |
| 198 | |
| 199 | Requirement[] requirements = resource.getRequirements(); |
| 200 | for (int i = 0; i < requirements.length; i++) { |
| 201 | meta.addContent(RequirementImpl.toXML(requirements[i])); |
| 202 | } |
| 203 | return meta; |
| 204 | } |
| 205 | |
| 206 | public URL getURL() { |
| 207 | return url; |
| 208 | } |
| 209 | |
| 210 | static String makeRelative(URL repository, URL url) { |
| 211 | try { |
| 212 | if (repository != null) { |
| 213 | String a = url.toExternalForm(); |
| 214 | String b = repository.toExternalForm(); |
| 215 | int index = b.lastIndexOf('/'); |
| 216 | if ( index > 0 ) |
| 217 | b = b.substring(0,index+1); |
| 218 | if (a.startsWith(b)) |
| 219 | return a.substring(b.length()); |
| 220 | } |
| 221 | } |
| 222 | catch (Exception e) { |
| 223 | // Ignore |
| 224 | } |
| 225 | return url.toExternalForm(); |
| 226 | } |
| 227 | |
| 228 | public void setURL(URL url) { |
| 229 | this.url = url; |
| 230 | if (url != null) |
| 231 | map.put(URL, url); |
| 232 | } |
| 233 | |
| 234 | public String getCopyright() { |
| 235 | return (String) map.get(COPYRIGHT); |
| 236 | } |
| 237 | |
| 238 | public Version getVersion() { |
| 239 | if (version == null) |
| 240 | version = new VersionRange("0"); |
| 241 | return version.low; |
| 242 | } |
| 243 | |
| 244 | void setVersion(VersionRange version) { |
| 245 | if (version == null) |
| 246 | this.version = new VersionRange("0"); |
| 247 | else |
| 248 | this.version = version; |
| 249 | } |
| 250 | |
| 251 | public void setCopyright(String copyright) { |
| 252 | if (copyright != null) |
| 253 | map.put(COPYRIGHT, copyright); |
| 254 | } |
| 255 | |
| 256 | public URL getDocumentation() { |
| 257 | return (URL) map.get(DOCUMENTATION_URL); |
| 258 | } |
| 259 | |
| 260 | public void setDocumentation(URL documentation) { |
| 261 | if (documentation != null) |
| 262 | map.put(DOCUMENTATION_URL, documentation); |
| 263 | } |
| 264 | |
| 265 | public URL getSource() { |
| 266 | return (URL) map.get(SOURCE_URL); |
| 267 | } |
| 268 | |
| 269 | public void setSource(URL source) { |
| 270 | if (source != null) |
| 271 | map.put(SOURCE_URL, source); |
| 272 | } |
| 273 | |
| 274 | public boolean satisfies(RequirementImpl requirement) { |
| 275 | for (Iterator i = capabilities.iterator(); i.hasNext();) { |
| 276 | CapabilityImpl capability = (CapabilityImpl) i.next(); |
| 277 | if (requirement.isSatisfied(capability)) |
| 278 | return true; |
| 279 | } |
| 280 | return false; |
| 281 | } |
| 282 | |
| 283 | public String toString() { |
| 284 | return symbolicName + "-" + version; |
| 285 | } |
| 286 | |
| 287 | public long getSize() { |
| 288 | return size; |
| 289 | } |
| 290 | |
| 291 | public void setSize(long size) { |
| 292 | this.size = size; |
| 293 | map.put(SIZE, new Long(size)); |
| 294 | } |
| 295 | |
| 296 | public Collection getRequirementList() { |
| 297 | return requirements; |
| 298 | } |
| 299 | |
| 300 | public Collection getCapabilityList() { |
| 301 | return capabilities; |
| 302 | } |
| 303 | |
| 304 | public int hashCode() { |
| 305 | return symbolicName.hashCode() ^ version.hashCode(); |
| 306 | } |
| 307 | |
| 308 | public boolean equals(Object o) { |
| 309 | try { |
| 310 | ResourceImpl other = (ResourceImpl) o; |
| 311 | return symbolicName.equals(other.symbolicName) |
| 312 | && version.equals(other.version); |
| 313 | } |
| 314 | catch (ClassCastException e) { |
| 315 | return false; |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | public String[] getCategories() { |
| 320 | return (String[]) categories.toArray(new String[categories.size()]); |
| 321 | } |
| 322 | |
| 323 | public Map getProperties() { |
| 324 | return Collections.unmodifiableMap(map); |
| 325 | } |
| 326 | |
| 327 | public synchronized String getId() { |
| 328 | if ( id == null ) |
| 329 | id = symbolicName + "/" + version; |
| 330 | return id; |
| 331 | } |
| 332 | |
| 333 | public Repository getRepository() { |
| 334 | return repository; |
| 335 | } |
| 336 | |
| 337 | void setName(String value) { |
| 338 | this.symbolicName = value; |
| 339 | } |
| 340 | |
| 341 | void put(String name, Object value) { |
| 342 | map.put(name, value); |
| 343 | } |
| 344 | |
| 345 | public void setPresentationName(String name) { |
| 346 | presentationName = name; |
| 347 | if (name != null) |
| 348 | map.put(PRESENTATION_NAME, name); |
| 349 | } |
| 350 | |
| 351 | public String getPresentationName() { |
| 352 | return presentationName; |
| 353 | } |
| 354 | |
| 355 | public void setFile(File zipFile) { |
| 356 | file = zipFile; |
| 357 | } |
| 358 | |
| 359 | public Set getExtendList() { |
| 360 | Set set = new HashSet(); |
| 361 | for (Iterator i = requirements.iterator(); i.hasNext();) { |
| 362 | RequirementImpl impl = (RequirementImpl) i.next(); |
| 363 | if ( impl.isExtend()) |
| 364 | set.add(impl); |
| 365 | } |
| 366 | return set; |
| 367 | } |
| 368 | |
| 369 | } |