Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
---
 Signed-off-by: Peter Kriens <[email protected]>

Signed-off-by: Peter Kriens <[email protected]>
  • Loading branch information
pkriens committed Nov 27, 2023
1 parent 3f367c2 commit 499dd74
Show file tree
Hide file tree
Showing 34 changed files with 1,955 additions and 218 deletions.
30 changes: 2 additions & 28 deletions biz.aQute.bnd.api/src/biz/aQute/bnd/facade/api/Binder.java
Original file line number Diff line number Diff line change
Expand Up @@ -271,34 +271,8 @@ private static void close(AutoCloseable c) {
}
}

/*
* This adapter is only for test purposes
*/
public interface TestAdapterNotApiWillChange {

default WeakReference<?> facade(Binder<?> binder) {
return binder.facade;
}

default AutoCloseable reg(Binder<?> binder) {
return binder.registration.get();
}

default boolean isClosed(Binder<?> binder) {
return binder.closed;
}

default List<Binder<?>> binders() {
return binders;
}

default int cycles(Binder<?> binder) {
return binder.cycles;
}

default FacadeManager facadeManager() {
return facadeManager;
}
public WeakReference<?> getFacade() {
return facade;
}

}
5 changes: 4 additions & 1 deletion biz.aQute.bnd.api/src/biz/aQute/bnd/facade/api/Delegate.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package biz.aQute.bnd.facade.api;

import java.lang.ref.WeakReference;

import org.eclipse.jdt.annotation.Nullable;
import org.osgi.annotation.versioning.ConsumerType;

Expand Down Expand Up @@ -35,5 +37,6 @@ public interface Delegate<D> {
* the current state or null
* @return an instance
*/
Instance<D> create(String description, @Nullable Object state);
Instance<D> create(String description, WeakReference<?> facade, @Nullable
Object state);
}
6 changes: 4 additions & 2 deletions biz.aQute.bnd.api/src/biz/aQute/bnd/facade/api/Instance.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* the type of the domain
*/
@ConsumerType
public interface Instance<D> {
public interface Instance<D> extends AutoCloseable {

/**
* Get the backing object. This is never null.
Expand All @@ -20,7 +20,8 @@ public interface Instance<D> {

/**
* Get the current state of the backing object. This state is passed to the
* {@link Delegate#create(String, Object)} when it needs to be recreated.
* {@link Delegate#create(String, java.lang.ref.WeakReference, Object)} when
* it needs to be recreated.
*/
@Nullable
Object getState();
Expand All @@ -29,5 +30,6 @@ public interface Instance<D> {
* Close the instance. This will free the backing object. This method can be
* called multiple times but will only work the first time.
*/
@Override
void close();
}
11 changes: 7 additions & 4 deletions biz.aQute.bnd.api/src/biz/aQute/bnd/facade/api/Memento.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package biz.aQute.bnd.facade.api;

import java.lang.ref.WeakReference;

import org.osgi.annotation.versioning.ConsumerType;

/**
Expand All @@ -9,14 +11,15 @@
* <p>
* The return object must take care not to use domain classes. It should be
* possible to recycle the bundle of the implementation between
* {@link #getState()} and {@link #setState(Object)}. If the state object refers
* to classes from this bundle, it is bound to cause class cast exceptions.
* {@link #getState()} and {@link #setState(Object,WeakReference)}. If the state
* object refers to classes from this bundle, it is bound to cause class cast
* exceptions.
*/
@ConsumerType
public interface Memento {
/**
* Get the state of the object so that the state of a new object can be
* resurrected with the {@link #setState(Object)}.
* resurrected with the {@link #setState(Object,WeakReference)}.
*/
Object getState();

Expand All @@ -26,5 +29,5 @@ public interface Memento {
* @param state the given state, retrieved from {@link #getState()} of a
* previous version
*/
void setState(Object state);
void setState(Object state, WeakReference<?> facade);
}
3 changes: 1 addition & 2 deletions biz.aQute.bnd.facade.provider/launch.bndrun
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#-runfw: org.apache.felix.framework;version=5
#-runee: JavaSE-1.8
# Used in launchpad FacadeManagerProvider.java

-runprovidedcapabilities: ${native_capability}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package biz.aQute.bnd.facade.provider;

import java.lang.ref.WeakReference;
import java.util.IdentityHashMap;
import java.util.Map;
import java.util.Map.Entry;
Expand Down Expand Up @@ -125,7 +126,8 @@ private Instance bind(Delegate delegate, Binder binder) {
assert binder.getId().equals(delegate.getId());

Object state = binder.getState();
Instance instance = delegate.create(binder.getDescription(), state);
WeakReference ref = binder.getFacade();
Instance instance = delegate.create(binder.getDescription(), binder.getFacade(), state);
binder.setState(null);
binder.accept(instance.get());
return instance;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package biz.aQute.bnd.facade.provider;

import java.lang.ref.WeakReference;
import java.util.concurrent.atomic.AtomicBoolean;

import org.osgi.framework.ServiceObjects;
Expand All @@ -17,10 +18,10 @@ class InstanceImpl implements Instance<D> {
final D service = factory.getService();
final AtomicBoolean closed = new AtomicBoolean(false);

InstanceImpl(String description, Object state) {
InstanceImpl(String description, WeakReference<?> facade, Object state) {
assert service != null;
if (service instanceof Memento m) {
m.setState(state);
m.setState(state, facade);
}
}

Expand Down Expand Up @@ -63,7 +64,7 @@ public String getId() {
}

@Override
public Instance<D> create(String description, Object state) {
return new InstanceImpl(description, state);
public Instance<D> create(String description, WeakReference<?> facade, Object state) {
return new InstanceImpl(description, facade, state);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package biz.aQute.bnd.facade.api;

import java.lang.ref.WeakReference;
import java.util.List;

public class JTAGBinder {

public WeakReference<?> facade(Binder<?> binder) {
return binder.facade;
}

public AutoCloseable reg(Binder<?> binder) {
return binder.registration.get();
}

public boolean isClosed(Binder<?> binder) {
return binder.closed;
}

public List<Binder<?>> binders() {
return Binder.binders;
}

public int cycles(Binder<?> binder) {
return binder.cycles;
}

public FacadeManager facadeManager() {
return Binder.facadeManager;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;

import java.lang.ref.WeakReference;
import java.util.concurrent.atomic.AtomicInteger;

import org.awaitility.Awaitility;
Expand All @@ -16,13 +17,12 @@
import aQute.launchpad.Launchpad;
import aQute.launchpad.LaunchpadBuilder;
import biz.aQute.bnd.facade.api.Binder;
import biz.aQute.bnd.facade.api.Binder.TestAdapterNotApiWillChange;
import biz.aQute.bnd.facade.api.FacadeManager;
import biz.aQute.bnd.facade.api.JTAGBinder;
import biz.aQute.bnd.facade.api.Memento;

class FacadeManagerProviderTest {
static TestAdapterNotApiWillChange testAdapter = new Binder.TestAdapterNotApiWillChange() {
};
static JTAGBinder testAdapter = new JTAGBinder();
final static String ID = "biz.aQute.bnd.facade.provider.FacadeManagerProviderTest.DomainImpl";

interface Domain {
Expand Down Expand Up @@ -54,7 +54,7 @@ public String get() {
}

@Override
public void setState(Object state) {
public void setState(Object state, WeakReference<?> facade) {
this.state = (String) state;
}

Expand Down Expand Up @@ -89,8 +89,6 @@ public String get() {
@Test
void testPrototypeScoped() throws Exception {
try (Launchpad lp = builder.create()) {
TestAdapterNotApiWillChange testAdapter =new Binder.TestAdapterNotApiWillChange() {
};

DomainFacade facade_1 = new DomainFacade();
Binder<Domain> binder_1 = facade_1.binder;
Expand Down
5 changes: 4 additions & 1 deletion biz.aQute.bnd.javagen/bnd.bnd
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@
biz.aQute.bndlib;version=latest

-testpath: \
${junit}
${junit}, \
slf4j.api, \
slf4j.simple

-conditionalpackage: \
aQute.lib.*, \
aQute.libg.*

# Tested in biz.aQute.bndall.tests/test/biz/aQute/bnd/project/ProjectGenerateTest.java

Loading

0 comments on commit 499dd74

Please sign in to comment.