Skip to content

Commit

Permalink
Fix #106: ISM Plugin is not functional (#133)
Browse files Browse the repository at this point in the history
* Warn but don't fail on attempt to install duplicate bundle

* Address style issue
  • Loading branch information
neilxb authored Apr 11, 2017
1 parent a787701 commit 6b4d2fb
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
public interface Artifact {

String getName();

String getVersion();

/**
* Returns the hash for the artifact content if available, otherwise
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@
class ArtifactImpl implements Artifact {

private final String name;
private final String version;
private final String location;
private final Hash hash;

ArtifactImpl(String name, String location, Hash hash) {
if (location == null) {
ArtifactImpl(String name, String version, String location, Hash hash) {
this.version = version;
if (location == null) {
throw new IllegalArgumentException("Artifact location may not be null");
}
this.name = name;
Expand All @@ -38,6 +40,11 @@ class ArtifactImpl implements Artifact {
public String getName() {
return this.name;
}

@Override
public String getVersion() {
return version;
}

@Override
public Hash getHash() {
Expand All @@ -51,7 +58,12 @@ public String getLocation() {

@Override
public String toString() {
return new StringBuilder().append(this.name).append(":").append(this.hash != null ? this.hash : "<no-hash>").append("@").append(this.location).toString();
return new StringBuilder()
.append(this.name)
.append('/').append(this.version)
.append(":").append(this.hash != null ? this.hash : "<no-hash>")
.append("@").append(this.location)
.toString();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
import org.osgi.framework.BundleListener;
import org.osgi.framework.FrameworkEvent;
import org.osgi.framework.FrameworkListener;
import org.osgi.framework.Version;
import org.osgi.framework.namespace.IdentityNamespace;
import org.osgi.framework.wiring.BundleRevision;
import org.osgi.resource.Capability;
Expand Down Expand Up @@ -278,6 +279,9 @@ private List<Bundle> installArtifacts(InstallableUnitImpl unit) {
oldState = unit.getState();
unit.setState(State.ERROR);
unit.setErrorMessage(e.getMessage());
if (log != null) {
log.log(LogService.LOG_ERROR, "Error installing artifact(s)", e);
}
notifyListeners(Collections.singleton(new InstallableUnitEvent(oldState, State.ERROR, unit)));
return Collections.emptyList();
}
Expand Down Expand Up @@ -347,7 +351,8 @@ private InstallableUnitImpl performResolve(File file) {

List<Artifact> artifacts = new ArrayList<>(result.getResources().size());
for (Entry<Resource, String> resourceEntry : result.getResources().entrySet()) {
ArtifactImpl artifact = new ArtifactImpl(getIdentity(resourceEntry.getKey()), resourceEntry.getValue(), getContentHash(resourceEntry.getKey()));
Capability idCap = getIdentityCapability(resourceEntry.getKey());
ArtifactImpl artifact = new ArtifactImpl(getIdentity(idCap), getVersion(idCap), resourceEntry.getValue(), getContentHash(resourceEntry.getKey()));
artifacts.add(artifact);
}
debug("Sucessful resolve for file %s: Deployment-Name=%s, Deployment-SymbolicName=%s, Deployment-Version= %s, Deployment-Type=%s", file, request.getName(), request.getSymbolicName(), request.getVersion(), request.getType());
Expand Down Expand Up @@ -472,19 +477,33 @@ public void install(File file) {
log(LogService.LOG_INFO, null, "Installing bundle archive: %s", file.getAbsolutePath());
putResolveJob(file);
}

private static String getIdentity(Capability identityCap) {
Object idObj = identityCap.getAttributes().get(IdentityNamespace.IDENTITY_NAMESPACE);
if (!(idObj instanceof String)) {
throw new IllegalArgumentException("Missing identity capability on resource, or incorrect type");
}

return (String) idObj;
}

private static String getVersion(Capability identityCap) {
Object versionObj = identityCap.getAttributes().get(IdentityNamespace.CAPABILITY_VERSION_ATTRIBUTE);
if (versionObj == null)
return Version.emptyVersion.toString();
if (versionObj instanceof Version)
return ((Version) versionObj).toString();
if (versionObj instanceof String)
return Version.parseVersion((String) versionObj).toString();
throw new IllegalArgumentException("Incorrect type on identity version");
}

private static String getIdentity(Resource resource) {
private static Capability getIdentityCapability(Resource resource) {
List<Capability> caps = resource.getCapabilities(IdentityNamespace.IDENTITY_NAMESPACE);
if (caps == null || caps.isEmpty()) {
throw new IllegalArgumentException("Missing identity capability on resource");
}

Object idObj = caps.get(0).getAttributes().get(IdentityNamespace.IDENTITY_NAMESPACE);
if (!(idObj instanceof String)) {
throw new IllegalArgumentException("Missing identity capability on resource, or incorrect type");
}

return (String) idObj;
return caps.get(0);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ public synchronized List<Bundle> addLocations(Object sponsor, List<String> bundl
Set<Object> sponsors = new HashSet<>();
sponsors.add(sponsor);
this.bundleSponsors.put(bundle.getBundleId(), sponsors);
} catch (BundleException e) {
if (e.getType() == BundleException.DUPLICATE_BUNDLE_ERROR)
log.log(LogService.LOG_WARNING, "Duplicate bundle symbolic-name/version in install for location " + location, e);
else
throw e;
}
} catch (URISyntaxException e) {
throw new IOException("Invalid bundle location URI: " + location, e);
Expand Down

0 comments on commit 6b4d2fb

Please sign in to comment.