org.osgi.util.promise.Deferred Java Examples

The following examples show how to use org.osgi.util.promise.Deferred. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TestAsyncResource.java    From aries-jax-rs-whiteboard with Apache License 2.0 6 votes vote down vote up
@GET
@Path("promise/{name}")
@Produces(MediaType.TEXT_PLAIN)
public Promise<String> echoPromise(@PathParam("name") String value) {
    
    Deferred<String> d = new Deferred<>();
    
    new Thread(() -> {
        try {
            try {
                Thread.sleep(1000);
            } catch (Exception e) {
                preResume.run();
                d.fail(e);
                return;
            }
            preResume.run();
            d.resolve(value);
        } finally {
            postResume.run();
        }
    }).start();
    
    return d.getPromise();
}
 
Example #2
Source File: Component.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Disable component. Dispose of all ComponentConfigurations and
 * stop listening for constraint changes.
 */
void disable(int reason, Deferred<Void> d) {
  Activator.logInfo(bc, "Disable " + toString());
  synchronized (lock) {
     final boolean dispose =  reason == ComponentConstants.DEACTIVATION_REASON_DISPOSED ||
       reason == ComponentConstants.DEACTIVATION_REASON_BUNDLE_STOPPED;
    if (d != null || isEnabled()) {
      state = dispose ? STATE_DISPOSING : STATE_DISABLING;
      disposeComponentConfigs(reason);
      untrackConstraints();
      refs = null;
      id = Long.valueOf(-1);
      state = dispose ? STATE_DISPOSED : STATE_DISABLED;
    } else if (dispose && state == STATE_DISABLED) {
      state = STATE_DISPOSED;
    }
  }
  if (d != null) {
    d.resolve(null);
  }
}
 
Example #3
Source File: PromiseRxInvokerImpl.java    From aries-jax-rs-whiteboard with Apache License 2.0 5 votes vote down vote up
@Override
public <R> Promise<R> method(
    String s, Entity<?> entity, GenericType<R> genericType) {

        Deferred<R> deferred = _promiseFactory.deferred();
    
    _webClient.doInvokeAsync(s, entity, null, null, genericType.getRawType(), genericType.getType(), 
            new DeferredHandler<R>(deferred));

    return deferred.getPromise();
}
 
Example #4
Source File: PromiseRxInvokerImpl.java    From aries-jax-rs-whiteboard with Apache License 2.0 5 votes vote down vote up
@Override
public <R> Promise<R> method(String s, GenericType<R> genericType) {
    Deferred<R> deferred = _promiseFactory.deferred();
    
    _webClient.doInvokeAsync(s, null, null, null, genericType.getRawType(), genericType.getType(), 
            new DeferredHandler<R>(deferred));

    return deferred.getPromise();
}
 
Example #5
Source File: RepositoryImpl.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public Promise<Collection<Resource>> findProviders(RequirementExpression expression) {
  Deferred<Collection<Resource>> d = new Deferred<Collection<Resource>>();
  try {
    new ExpressionResolver(this, expression, d).start();
  } catch (Exception e) {
    d.fail(e);
  }
  return d.getPromise();
}
 
Example #6
Source File: ExpressionResolver.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public ExpressionResolver(Repository repository,
                          RequirementExpression expression,
                          Deferred<Collection<Resource>> deferred)
{
  this.repository = repository;
  this.expression = expression;
  this.deferred = deferred;
}
 
Example #7
Source File: ServiceComponentRuntimeImpl.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 *  {@inheritDoc}
 */
public Promise<Void> enableComponent(ComponentDescriptionDTO description) {
  Component c = getComponent(description);
  Deferred<Void> d = new Deferred<Void>();
  if (c != null) {
    c.enable(d);
  } else {
    d.fail(new IllegalArgumentException("Unknown component"));
  }
  return d.getPromise();
}
 
Example #8
Source File: ServiceComponentRuntimeImpl.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 *  {@inheritDoc}
 */
public Promise<Void> disableComponent(ComponentDescriptionDTO description) {
  Component c = getComponent(description);
  Deferred<Void> d = new Deferred<Void>();
  if (c != null) {
    c.disable(d);
  } else {
    d.fail(new IllegalArgumentException("Unknown component"));
  }
  return d.getPromise();
}
 
Example #9
Source File: EmulatorFactory.java    From osgi.iot.contest.sdk with Apache License 2.0 5 votes vote down vote up
private synchronized void trigger(String rfid){
	// first keep the old deferred we have to resolve
	Deferred<String> toResolve = nextRFID;

	// create new deferred for next, since we will call 
	// a new nextRFID() when the previous is resolved
	nextRFID = new Deferred<String>();
	
	// resolve previous
	toResolve.resolve(rfid);
	// set lastRFID
	this.lastRFID = rfid;
}
 
Example #10
Source File: MicroSwitchSegmentImpl.java    From osgi.iot.contest.sdk with Apache License 2.0 4 votes vote down vote up
private synchronized void trigger(String rfid) {
	Deferred<String> toResolve = nextRFID;
	nextRFID = new Deferred<String>();
	toResolve.resolve(rfid);
	this.lastRFID = rfid;
}
 
Example #11
Source File: PromiseRxInvokerImpl.java    From aries-jax-rs-whiteboard with Apache License 2.0 4 votes vote down vote up
private DeferredHandler(Deferred<R> deferred) {
    this.deferred = deferred;
}
 
Example #12
Source File: RFIDSegmentImpl.java    From osgi.iot.contest.sdk with Apache License 2.0 4 votes vote down vote up
private synchronized void trigger(String rfid) {
	Deferred<String> toResolve = nextRFID;
	nextRFID = new Deferred<String>();
	toResolve.resolve(rfid);
	this.lastRFID = rfid;
}
 
Example #13
Source File: TrainControllerImpl.java    From osgi.iot.contest.sdk with Apache License 2.0 4 votes vote down vote up
private synchronized void trigger(String trainId, String segment) {
    Deferred<String> currentLocation = nextLocation;
    nextLocation = new Deferred<String>();
    currentLocation.resolve(trainId + ":" + segment);
}
 
Example #14
Source File: TrainLocationClient.java    From osgi.iot.contest.sdk with Apache License 2.0 4 votes vote down vote up
private synchronized void trigger(String trainId, String segment) {
    // info("trigger: trainId={} segment={}", trainId, segment);
    Deferred<String> currentLocation = nextLocation;
    nextLocation = new Deferred<String>();
    currentLocation.resolve(trainId + ":" + segment);
}
 
Example #15
Source File: Component.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Start tracking services and CM config
 */
private void enableTrackConstraints(Deferred<Void> d) {
  // unresolvedConstraints set to DISABLED_OFFSET, so that we don't satisfy until
  // end of this method
  unresolvedConstraints = DISABLED_OFFSET;
  id = scr.getNextComponentId();
  if (!cmConfig.subscribe()) {
    // If we have no mandatory CM data, add constraint
    unresolvedConstraints++;
  }
  final ArrayList<ReferenceDescription> rds = compDesc.getReferences();
  if (rds != null) {
    unresolvedConstraints += rds.size();
    refs = new Reference[rds.size()];
    for (int i = 0; i < refs.length; i++) {
      final Reference r = new Reference(this, rds.get(i));
      refs[i] = r;
      if (r.isRefOptional()) {
        // Optional references does not need to be check
        // if they are available
        unresolvedConstraints--;
      }
      r.start();
    }
  }
  // Remove blocking constraint, to see if we are satisfied
  unresolvedConstraints -= DISABLED_OFFSET;
  if (unresolvedConstraints == 0) {
    satisfied(null);
  } else {
    state = STATE_ENABLED;
    Activator.logDebug("Component enabled: " + toString() +
                       ", unresolvedConstraints=" + unresolvedConstraints);
    if (compDesc.getServices() != null) {
      // No satisfied, check if we have circular problems.
      // Only applicable if component registers a service.
      Activator.logDebug("Check circular: " + toString());
      String res = scr.checkCircularReferences(this, new ArrayList<Component>());
      if (res != null) {
        Activator.logError(bc, res, null);
      }
    }
  }
  d.resolve(null);
}
 
Example #16
Source File: PromiseRxInvokerImpl.java    From aries-jax-rs-whiteboard with Apache License 2.0 3 votes vote down vote up
@Override
public <R> Promise<R> method(String s, Entity<?> entity, Class<R> responseType) {
    
    Deferred<R> deferred = _promiseFactory.deferred();

    _webClient.doInvokeAsync(s, entity, null, null, responseType, responseType, 
            new DeferredHandler<R>(deferred));

return deferred.getPromise();
}
 
Example #17
Source File: PromiseRxInvokerImpl.java    From aries-jax-rs-whiteboard with Apache License 2.0 3 votes vote down vote up
@Override
public <R> Promise<R> method(String s, Class<R> responseType) {
    
        Deferred<R> deferred = _promiseFactory.deferred();
    
        _webClient.doInvokeAsync(s, null, null, null, responseType, responseType, 
                new DeferredHandler<R>(deferred));
    
    return deferred.getPromise();
}