Skip to content

Commit

Permalink
Pull down the replace method from ShadowList to Candidates
Browse files Browse the repository at this point in the history
The replace method is currently used on one place and the reason we
construct a new ShadowList, to remove references to this class the
method can simply pulls down to Candidates as it only operates on the
internal unmodifiable list.

This also adds a check that if the Capability to replace does not exits
it is a noop to call this method instead of running into
IndexOutOfBoundsException.
  • Loading branch information
laeubi committed Mar 8, 2024
1 parent f9b6e09 commit 22fe7be
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,38 @@
*/
package org.apache.felix.resolver;

import java.util.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;
import java.util.concurrent.atomic.AtomicBoolean;

import org.apache.felix.resolver.ResolverImpl.PermutationType;
import org.apache.felix.resolver.ResolverImpl.ResolveSession;
import org.apache.felix.resolver.reason.ReasonException;
import org.apache.felix.resolver.util.*;
import org.apache.felix.resolver.util.CandidateSelector;
import org.apache.felix.resolver.util.CopyOnWriteSet;
import org.apache.felix.resolver.util.OpenHashMap;
import org.apache.felix.resolver.util.OpenHashMapList;
import org.apache.felix.resolver.util.OpenHashMapSet;
import org.apache.felix.resolver.util.ShadowList;
import org.osgi.framework.Version;
import org.osgi.framework.namespace.*;
import org.osgi.resource.*;
import org.osgi.framework.namespace.HostNamespace;
import org.osgi.framework.namespace.IdentityNamespace;
import org.osgi.framework.namespace.PackageNamespace;
import org.osgi.resource.Capability;
import org.osgi.resource.Requirement;
import org.osgi.resource.Resource;
import org.osgi.resource.Wire;
import org.osgi.resource.Wiring;
import org.osgi.service.resolver.HostedCapability;
import org.osgi.service.resolver.ResolutionException;
import org.osgi.service.resolver.ResolveContext;
Expand Down Expand Up @@ -868,7 +889,8 @@ public ResolutionError prepare()
// really be attached to the original host, not the wrapper.
if (!c.getNamespace().equals(HostNamespace.HOST_NAMESPACE))
{
Capability origCap = ((HostedCapability) c).getDeclaredCapability();
HostedCapability hostedCapability = (HostedCapability) c;
Capability origCap = hostedCapability.getDeclaredCapability();
// Note that you might think we could remove the original cap
// from the dependent map, but you can't since it may come from
// a fragment that is attached to multiple hosts, so each host
Expand Down Expand Up @@ -912,7 +934,7 @@ public ResolutionError prepare()
{
// If the original capability is from the host, then
// we just need to replace it in the shadow list.
getShadowList(r).replace(origCap, c);
m_candidateMap.get(r).replaceHostedCapability(hostedCapability);
}
else
{
Expand All @@ -921,7 +943,7 @@ public ResolutionError prepare()
// shadow copy of the list accordingly.
getShadowList(r).insertHostedCapability(
m_session.getContext(),
(HostedCapability) c,
hostedCapability,
new SimpleHostedCapability(
hostResource.getDeclaredResource(),
origCap));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
import java.util.Collections;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

import org.osgi.resource.Capability;
import org.osgi.service.resolver.HostedCapability;

public class CandidateSelector {
protected final AtomicBoolean isUnmodifiable;
Expand Down Expand Up @@ -82,9 +82,22 @@ public int remove(Capability cap) {
return index;
}

public void replaceHostedCapability(HostedCapability c) {
checkModifiable();
Capability origCap = c.getDeclaredCapability();
int idx = unmodifiable.indexOf(origCap);
if (idx < 0) {
return;
}
unmodifiable.set(idx, new ShadowedCapability(c, origCap));
}

protected void checkModifiable() {
if (isUnmodifiable.get()) {
throw new IllegalStateException("Trying to mutate after candidates have been prepared.");
}
if (currentIndex > 0) {
throw new IllegalStateException("Trying to mutate after candidates have been removed already.");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.apache.felix.resolver.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import org.osgi.resource.Capability;
Expand Down Expand Up @@ -64,13 +65,18 @@ public void insertHostedCapability(ResolveContext context, HostedCapability wrap
m_original.remove(removeIdx);
unmodifiable.remove(removeIdx);
}
int insertIdx = context.insertHostedCapability(m_original, toInsertCapability);
Capability[] copy = m_original.toArray(new Capability[0]); // make a copy so we can modify it and the caller can
// not modify ours
for (int i = 0; i < copy.length; i++) {
Capability capability = copy[i];
if (capability instanceof ShadowedCapability) {
// we unwrap the ShadowedCapability here as we always must pass only what we
// have got from the ResolveContext#findProviders.
copy[i] = ((ShadowedCapability) capability).getShadowed();
}
}
int insertIdx = context.insertHostedCapability(Arrays.asList(copy), toInsertCapability);
unmodifiable.add(insertIdx, wrappedCapability);
}

public void replace(Capability origCap, Capability c) {
checkModifiable();
int idx = unmodifiable.indexOf(origCap);
unmodifiable.set(idx, c);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.felix.resolver.util;

import java.util.Map;
import java.util.Objects;
import org.osgi.resource.Capability;
import org.osgi.resource.Resource;
import org.osgi.service.resolver.HostedCapability;

/**
* A {@link Capability} that is shadowed by a {@link HostedCapability}, this is
* done when we merge the fragments capabilities with its host, but to insert
* {@link HostedCapability} we need to make sure we always pass the original so
* this class keeps track of the fact that we have replaced it with something
* else.
*/
class ShadowedCapability implements HostedCapability {

private HostedCapability hosted;
private int hashCode;
private Capability shadowed;

public ShadowedCapability(HostedCapability hosted, Capability shadowed) {
this.hosted = hosted;
this.shadowed = shadowed;
}

@Override
public String getNamespace() {
return hosted.getNamespace();
}

@Override
public Map<String, String> getDirectives() {
return null;
}

@Override
public Map<String, Object> getAttributes() {
return hosted.getAttributes();
}

@Override
public Resource getResource() {
return hosted.getResource();
}

@Override
public Capability getDeclaredCapability() {
return hosted.getDeclaredCapability();
}

public Capability getShadowed() {
return shadowed;
}

@Override
public int hashCode() {
if (hashCode != 0) {
return hashCode;
}
return hashCode = Objects.hash(getNamespace(), getDirectives(), getAttributes(), getResource());
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof Capability) {
Capability other = (Capability) obj;
return Objects.equals(getNamespace(), other.getNamespace())
&& Objects.equals(getDirectives(), other.getDirectives())
&& Objects.equals(getAttributes(), other.getAttributes())
&& Objects.equals(getResource(), other.getResource());
}
return false;
}

}

0 comments on commit 22fe7be

Please sign in to comment.