| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2007, 2019 IBM Corporation and others. |
| 3 |
* |
| 4 |
* This program and the accompanying materials |
| 5 |
* are made available under the terms of the Eclipse Public License 2.0 |
| 6 |
* which accompanies this distribution, and is available at |
| 7 |
* https://www.eclipse.org/legal/epl-2.0/ |
| 8 |
* |
| 9 |
* SPDX-License-Identifier: EPL-2.0 |
| 10 |
* |
| 11 |
* Contributors: |
| 12 |
* IBM Corporation - initial API and implementation |
| 13 |
*******************************************************************************/ |
| 14 |
package org.eclipse.equinox.frameworkadmin; |
| 15 |
|
| 16 |
import java.net.URI; |
| 17 |
import org.eclipse.core.runtime.URIUtil; |
| 18 |
import org.osgi.framework.Version; |
| 19 |
|
| 20 |
/** |
| 21 |
* This object represents information of a bundle. |
| 22 |
* |
| 23 |
* @since 2.0 |
| 24 |
*/ |
| 25 |
public class BundleInfo { |
| 26 |
public static final String EMPTY_VERSION = "0.0.0"; //$NON-NLS-1$ |
| 27 |
public static final int NO_LEVEL = -1; |
| 28 |
public static final int NO_BUNDLEID = -1; |
| 29 |
|
| 30 |
private String symbolicName = null; |
| 31 |
private String version = EMPTY_VERSION; |
| 32 |
private URI baseLocation = null; |
| 33 |
private URI location = null; |
| 34 |
private long bundleId = NO_BUNDLEID; |
| 35 |
|
| 36 |
private boolean markedAsStarted = false; |
| 37 |
private int startLevel = NO_LEVEL; |
| 38 |
private boolean resolved = false; |
| 39 |
|
| 40 |
private String manifest = null; |
| 41 |
private String fragmentHost = null; |
| 42 |
|
| 43 |
/** |
| 44 |
* Create a new BundleInfo object |
| 45 |
*/ |
| 46 |
public BundleInfo() { |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Create a new BundleInfo object |
| 51 |
* |
| 52 |
* @param location |
| 53 |
* - the location of the bundle |
| 54 |
*/ |
| 55 |
public BundleInfo(URI location) { |
| 56 |
this.location = location; |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Create a new BundleInfo object |
| 61 |
* |
| 62 |
* @param location |
| 63 |
* - the location of the bundle |
| 64 |
* @param startLevel |
| 65 |
* - the start to be used or {@link BundleInfo#NO_LEVEL} |
| 66 |
* @param started |
| 67 |
* - whether or not the bundle should be started |
| 68 |
*/ |
| 69 |
public BundleInfo(URI location, int startLevel, boolean started) { |
| 70 |
this.location = location; |
| 71 |
this.startLevel = startLevel < 0 ? NO_LEVEL : startLevel; |
| 72 |
this.markedAsStarted = started; |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Create a new BundleInfo object |
| 77 |
* |
| 78 |
* @param symbolic |
| 79 |
* The Bundle-SymbolicName name for this bundle |
| 80 |
* @param version |
| 81 |
* - The version for this bundle, this must be a valid |
| 82 |
* {@link Version} string, if null is passed {@link #EMPTY_VERSION} |
| 83 |
* will be used instead |
| 84 |
* @param location |
| 85 |
* - the location of the bundle |
| 86 |
* @param startLevel |
| 87 |
* - the start level of the bundle or {@link BundleInfo#NO_LEVEL} |
| 88 |
* @param started |
| 89 |
* - whether or not the bundle should be started |
| 90 |
*/ |
| 91 |
public BundleInfo(String symbolic, String version, URI location, int startLevel, boolean started) { |
| 92 |
this.symbolicName = symbolic; |
| 93 |
this.version = version != null ? version : EMPTY_VERSION; |
| 94 |
this.location = location; |
| 95 |
this.markedAsStarted = started; |
| 96 |
this.startLevel = startLevel < 0 ? NO_LEVEL : startLevel; |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Get the bundle id |
| 101 |
* |
| 102 |
* @return the bundle id or {@link #NO_BUNDLEID} |
| 103 |
*/ |
| 104 |
public long getBundleId() { |
| 105 |
return bundleId; |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* The base location An absolute URI which may be used to resolve relative |
| 110 |
* {@link #getLocation()} URIs |
| 111 |
* |
| 112 |
* @return absolute URI or null if not set |
| 113 |
*/ |
| 114 |
public URI getBaseLocation() { |
| 115 |
return baseLocation; |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* The location of this bundle. A location is required if this bundle will be |
| 120 |
* persisted into a configuration file |
| 121 |
* |
| 122 |
* @return URI location or null if not set |
| 123 |
*/ |
| 124 |
public URI getLocation() { |
| 125 |
return location; |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* The manifest for this bundle |
| 130 |
* |
| 131 |
* @return the manifest or null if not set |
| 132 |
*/ |
| 133 |
public String getManifest() { |
| 134 |
return manifest; |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* The start level for this bundle |
| 139 |
* |
| 140 |
* @return the start level or {@link #NO_LEVEL} |
| 141 |
*/ |
| 142 |
public int getStartLevel() { |
| 143 |
return startLevel; |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* The Bundle-SymbolicName for this bundle. A symbolic name is required if this |
| 148 |
* bundle will be persisted into a configuration file |
| 149 |
* |
| 150 |
* @return the symbolic name or null if not set |
| 151 |
*/ |
| 152 |
public String getSymbolicName() { |
| 153 |
return symbolicName; |
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* Return the version |
| 158 |
* |
| 159 |
* @return an {@link Version} string, or "0.0.0" if not set |
| 160 |
*/ |
| 161 |
public String getVersion() { |
| 162 |
if (version == null) |
| 163 |
return EMPTY_VERSION; |
| 164 |
return version; |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Return the host if this bundle is a fragment |
| 169 |
* |
| 170 |
* @return the host, or null if this is not a fragment |
| 171 |
*/ |
| 172 |
public String getFragmentHost() { |
| 173 |
return fragmentHost; |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* Whether or not this bundle is marked to be started Default is false |
| 178 |
* |
| 179 |
* @return boolean |
| 180 |
*/ |
| 181 |
public boolean isMarkedAsStarted() { |
| 182 |
return markedAsStarted; |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* Whether or not this bundle is resolved Default is false |
| 187 |
* |
| 188 |
* @return boolean |
| 189 |
*/ |
| 190 |
public boolean isResolved() { |
| 191 |
return resolved; |
| 192 |
} |
| 193 |
|
| 194 |
/** |
| 195 |
* Set the bundle id. |
| 196 |
* |
| 197 |
* @param bundleId |
| 198 |
* The bundle id. |
| 199 |
*/ |
| 200 |
public void setBundleId(long bundleId) { |
| 201 |
this.bundleId = bundleId; |
| 202 |
} |
| 203 |
|
| 204 |
/** |
| 205 |
* Set a base location against which relative {@link #getLocation()} URIs may be |
| 206 |
* resolved |
| 207 |
* |
| 208 |
* @param baseLocation |
| 209 |
* - an absolute URI |
| 210 |
*/ |
| 211 |
public void setBaseLocation(URI baseLocation) { |
| 212 |
this.baseLocation = baseLocation; |
| 213 |
} |
| 214 |
|
| 215 |
/** |
| 216 |
* Set the location for this bundle. |
| 217 |
* |
| 218 |
* @param location |
| 219 |
* The location of the bundle. |
| 220 |
*/ |
| 221 |
public void setLocation(URI location) { |
| 222 |
this.location = location; |
| 223 |
} |
| 224 |
|
| 225 |
/** |
| 226 |
* Set the manifest for this bundle |
| 227 |
* |
| 228 |
* @param manifest |
| 229 |
* The manifest. |
| 230 |
*/ |
| 231 |
public void setManifest(String manifest) { |
| 232 |
this.manifest = manifest; |
| 233 |
} |
| 234 |
|
| 235 |
/** |
| 236 |
* Set whether or not this bundle should be started |
| 237 |
* |
| 238 |
* @param markedAsStarted |
| 239 |
* Whether the bundle should be started. |
| 240 |
*/ |
| 241 |
public void setMarkedAsStarted(boolean markedAsStarted) { |
| 242 |
this.markedAsStarted = markedAsStarted; |
| 243 |
} |
| 244 |
|
| 245 |
/** |
| 246 |
* Set whether or not the bundle is resolved |
| 247 |
* |
| 248 |
* @param resolved |
| 249 |
* If the bundle is resolved or not. |
| 250 |
*/ |
| 251 |
public void setResolved(boolean resolved) { |
| 252 |
this.resolved = resolved; |
| 253 |
} |
| 254 |
|
| 255 |
/** |
| 256 |
* Set the start level. |
| 257 |
* |
| 258 |
* @param level |
| 259 |
* if a value < 0 is passed, the start level will be set to |
| 260 |
* {@link #NO_LEVEL} |
| 261 |
*/ |
| 262 |
public void setStartLevel(int level) { |
| 263 |
this.startLevel = level < 0 ? NO_LEVEL : level; |
| 264 |
} |
| 265 |
|
| 266 |
/** |
| 267 |
* Set the Bundle-SymbolicName |
| 268 |
* |
| 269 |
* @param symbolicName |
| 270 |
* The Bundle-SymbolicName. |
| 271 |
*/ |
| 272 |
public void setSymbolicName(String symbolicName) { |
| 273 |
this.symbolicName = symbolicName; |
| 274 |
} |
| 275 |
|
| 276 |
/** |
| 277 |
* Set the version, should be a valid {@link Version} string |
| 278 |
* |
| 279 |
* @param value |
| 280 |
* The version. A valid {@link Version} string. |
| 281 |
*/ |
| 282 |
public void setVersion(String value) { |
| 283 |
if (value == null) |
| 284 |
this.version = EMPTY_VERSION; |
| 285 |
else |
| 286 |
this.version = value; |
| 287 |
} |
| 288 |
|
| 289 |
/** |
| 290 |
* Set the host if this bundle is a fragment |
| 291 |
* |
| 292 |
* @param fragmentHost |
| 293 |
* The host for this fragment. |
| 294 |
*/ |
| 295 |
public void setFragmentHost(String fragmentHost) { |
| 296 |
this.fragmentHost = fragmentHost; |
| 297 |
} |
| 298 |
|
| 299 |
@Override |
| 300 |
public String toString() { |
| 301 |
StringBuilder buffer = new StringBuilder(); |
| 302 |
buffer.append("BundleInfo("); //$NON-NLS-1$ |
| 303 |
if (symbolicName != null) |
| 304 |
buffer.append(symbolicName); |
| 305 |
buffer.append(", "); //$NON-NLS-1$ |
| 306 |
buffer.append(version); |
| 307 |
|
| 308 |
if (fragmentHost != null) { |
| 309 |
buffer.append(", fragmentHost="); //$NON-NLS-1$ |
| 310 |
buffer.append(fragmentHost); |
| 311 |
} |
| 312 |
|
| 313 |
if (baseLocation != null) { |
| 314 |
buffer.append(", baseLocation="); //$NON-NLS-1$ |
| 315 |
buffer.append(baseLocation); |
| 316 |
} |
| 317 |
buffer.append(", location="); //$NON-NLS-1$ |
| 318 |
buffer.append(location); |
| 319 |
buffer.append(", startLevel="); //$NON-NLS-1$ |
| 320 |
buffer.append(startLevel); |
| 321 |
buffer.append(", toBeStarted="); //$NON-NLS-1$ |
| 322 |
buffer.append(markedAsStarted); |
| 323 |
buffer.append(", resolved="); //$NON-NLS-1$ |
| 324 |
buffer.append(resolved); |
| 325 |
buffer.append(", id="); //$NON-NLS-1$ |
| 326 |
buffer.append(this.bundleId);// buffer.append(',').append(manifest == null ? "no manifest" : "manifest |
| 327 |
// available"); |
| 328 |
buffer.append(',').append(manifest == null ? "no manifest" : "manifest available"); //$NON-NLS-1$ //$NON-NLS-2$ |
| 329 |
buffer.append(')'); |
| 330 |
return buffer.toString(); |
| 331 |
} |
| 332 |
|
| 333 |
@Override |
| 334 |
public int hashCode() { |
| 335 |
final int prime = 31; |
| 336 |
int result = 1; |
| 337 |
result = prime * result + ((symbolicName == null) ? 0 : symbolicName.hashCode()); |
| 338 |
result = prime * result + version.hashCode(); |
| 339 |
return result; |
| 340 |
} |
| 341 |
|
| 342 |
@Override |
| 343 |
public boolean equals(Object obj) { |
| 344 |
if (this == obj) |
| 345 |
return true; |
| 346 |
|
| 347 |
if (obj == null) |
| 348 |
return false; |
| 349 |
|
| 350 |
if (getClass() != obj.getClass()) |
| 351 |
return false; |
| 352 |
|
| 353 |
BundleInfo other = (BundleInfo) obj; |
| 354 |
if (symbolicName == null) { |
| 355 |
if (other.symbolicName != null) |
| 356 |
return false; |
| 357 |
} else if (!symbolicName.equals(other.symbolicName)) |
| 358 |
return false; |
| 359 |
|
| 360 |
if (!version.equals(other.getVersion())) |
| 361 |
return false; |
| 362 |
|
| 363 |
if (location == null || other.location == null) |
| 364 |
return true; |
| 365 |
|
| 366 |
// compare absolute location URIs |
| 367 |
URI absoluteLocation = null; |
| 368 |
if (location.isAbsolute() || baseLocation == null) |
| 369 |
absoluteLocation = location; |
| 370 |
else |
| 371 |
absoluteLocation = URIUtil.append(baseLocation, URIUtil.toUnencodedString(location)); |
| 372 |
|
| 373 |
URI otherAbsoluteLocation = null; |
| 374 |
if (other.location.isAbsolute() || other.baseLocation == null) |
| 375 |
otherAbsoluteLocation = other.location; |
| 376 |
else |
| 377 |
otherAbsoluteLocation = URIUtil.append(other.baseLocation, URIUtil.toUnencodedString(other.location)); |
| 378 |
return URIUtil.sameURI(absoluteLocation, otherAbsoluteLocation); |
| 379 |
} |
| 380 |
} |