aries-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From timothyjw...@apache.org
Subject svn commit: r1678227 [1/2] - in /aries/trunk/async: ./ promise-api/ promise-api/src/ promise-api/src/main/ promise-api/src/main/java/ promise-api/src/main/java/org/ promise-api/src/main/java/org/apache/ promise-api/src/main/java/org/apache/aries/ promi...
Date Thu, 07 May 2015 15:00:54 GMT
Author: timothyjward
Date: Thu May  7 15:00:53 2015
New Revision: 1678227

URL: http://svn.apache.org/r1678227
Log:
[ARIES-1317] Commit OSGi Promises implementation contributed by derek.baum@paremus.com

Added:
    aries/trunk/async/
    aries/trunk/async/README
    aries/trunk/async/pom.xml
    aries/trunk/async/promise-api/   (with props)
    aries/trunk/async/promise-api/LICENSE
    aries/trunk/async/promise-api/NOTICE
    aries/trunk/async/promise-api/pom.xml
    aries/trunk/async/promise-api/src/
    aries/trunk/async/promise-api/src/main/
    aries/trunk/async/promise-api/src/main/java/
    aries/trunk/async/promise-api/src/main/java/org/
    aries/trunk/async/promise-api/src/main/java/org/apache/
    aries/trunk/async/promise-api/src/main/java/org/apache/aries/
    aries/trunk/async/promise-api/src/main/java/org/apache/aries/async/
    aries/trunk/async/promise-api/src/main/java/org/apache/aries/async/promise/
    aries/trunk/async/promise-api/src/main/java/org/apache/aries/async/promise/PromiseImpl.java
    aries/trunk/async/promise-api/src/main/java/org/osgi/
    aries/trunk/async/promise-api/src/main/java/org/osgi/util/
    aries/trunk/async/promise-api/src/main/java/org/osgi/util/function/
    aries/trunk/async/promise-api/src/main/java/org/osgi/util/function/Function.java
    aries/trunk/async/promise-api/src/main/java/org/osgi/util/function/Predicate.java
    aries/trunk/async/promise-api/src/main/java/org/osgi/util/function/packageinfo
    aries/trunk/async/promise-api/src/main/java/org/osgi/util/promise/
    aries/trunk/async/promise-api/src/main/java/org/osgi/util/promise/Deferred.java
    aries/trunk/async/promise-api/src/main/java/org/osgi/util/promise/FailedPromisesException.java
    aries/trunk/async/promise-api/src/main/java/org/osgi/util/promise/Failure.java
    aries/trunk/async/promise-api/src/main/java/org/osgi/util/promise/Promise.java
    aries/trunk/async/promise-api/src/main/java/org/osgi/util/promise/Promises.java
    aries/trunk/async/promise-api/src/main/java/org/osgi/util/promise/Success.java
    aries/trunk/async/promise-api/src/main/java/org/osgi/util/promise/packageinfo
    aries/trunk/async/promise-api/src/test/
    aries/trunk/async/promise-api/src/test/java/
    aries/trunk/async/promise-api/src/test/java/org/
    aries/trunk/async/promise-api/src/test/java/org/apache/
    aries/trunk/async/promise-api/src/test/java/org/apache/aries/
    aries/trunk/async/promise-api/src/test/java/org/apache/aries/async/
    aries/trunk/async/promise-api/src/test/java/org/apache/aries/async/promise/
    aries/trunk/async/promise-api/src/test/java/org/apache/aries/async/promise/test/
    aries/trunk/async/promise-api/src/test/java/org/apache/aries/async/promise/test/CallbackTest.java
    aries/trunk/async/promise-api/src/test/java/org/apache/aries/async/promise/test/ChainTest.java
    aries/trunk/async/promise-api/src/test/java/org/apache/aries/async/promise/test/DeferredTest.java
    aries/trunk/async/promise-api/src/test/java/org/apache/aries/async/promise/test/FunctionTest.java
    aries/trunk/async/promise-api/src/test/java/org/apache/aries/async/promise/test/PromisesTest.java

Added: aries/trunk/async/README
URL: http://svn.apache.org/viewvc/aries/trunk/async/README?rev=1678227&view=auto
==============================================================================
--- aries/trunk/async/README (added)
+++ aries/trunk/async/README Thu May  7 15:00:53 2015
@@ -0,0 +1,2 @@
+Sample OSGi Asynchronous Services implementation
+------------------------------------------------

Added: aries/trunk/async/pom.xml
URL: http://svn.apache.org/viewvc/aries/trunk/async/pom.xml?rev=1678227&view=auto
==============================================================================
--- aries/trunk/async/pom.xml (added)
+++ aries/trunk/async/pom.xml Thu May  7 15:00:53 2015
@@ -0,0 +1,45 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ 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.
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+
+    <modelVersion>4.0.0</modelVersion>
+
+    <parent>
+        <groupId>org.apache.aries</groupId>
+        <artifactId>parent</artifactId>
+        <version>2.0.0</version>
+        <relativePath>../parent/pom.xml</relativePath>
+    </parent>
+
+    <groupId>org.apache.aries.async</groupId>
+    <artifactId>async</artifactId>
+    <name>Apache Aries Async</name>
+    <packaging>pom</packaging>
+    <version>1.0.0-SNAPSHOT</version>
+    <description>
+       Async services, including Promises.
+    </description>
+
+    <modules>
+        <module>promise-api</module>
+    </modules>
+
+</project>
+

Propchange: aries/trunk/async/promise-api/
------------------------------------------------------------------------------
--- svn:ignore (added)
+++ svn:ignore Thu May  7 15:00:53 2015
@@ -0,0 +1 @@
+target

Added: aries/trunk/async/promise-api/LICENSE
URL: http://svn.apache.org/viewvc/aries/trunk/async/promise-api/LICENSE?rev=1678227&view=auto
==============================================================================
--- aries/trunk/async/promise-api/LICENSE (added)
+++ aries/trunk/async/promise-api/LICENSE Thu May  7 15:00:53 2015
@@ -0,0 +1,203 @@
+
+                                 Apache License
+                           Version 2.0, January 2004
+                        http://www.apache.org/licenses/
+
+   TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+   1. Definitions.
+
+      "License" shall mean the terms and conditions for use, reproduction,
+      and distribution as defined by Sections 1 through 9 of this document.
+
+      "Licensor" shall mean the copyright owner or entity authorized by
+      the copyright owner that is granting the License.
+
+      "Legal Entity" shall mean the union of the acting entity and all
+      other entities that control, are controlled by, or are under common
+      control with that entity. For the purposes of this definition,
+      "control" means (i) the power, direct or indirect, to cause the
+      direction or management of such entity, whether by contract or
+      otherwise, or (ii) ownership of fifty percent (50%) or more of the
+      outstanding shares, or (iii) beneficial ownership of such entity.
+
+      "You" (or "Your") shall mean an individual or Legal Entity
+      exercising permissions granted by this License.
+
+      "Source" form shall mean the preferred form for making modifications,
+      including but not limited to software source code, documentation
+      source, and configuration files.
+
+      "Object" form shall mean any form resulting from mechanical
+      transformation or translation of a Source form, including but
+      not limited to compiled object code, generated documentation,
+      and conversions to other media types.
+
+      "Work" shall mean the work of authorship, whether in Source or
+      Object form, made available under the License, as indicated by a
+      copyright notice that is included in or attached to the work
+      (an example is provided in the Appendix below).
+
+      "Derivative Works" shall mean any work, whether in Source or Object
+      form, that is based on (or derived from) the Work and for which the
+      editorial revisions, annotations, elaborations, or other modifications
+      represent, as a whole, an original work of authorship. For the purposes
+      of this License, Derivative Works shall not include works that remain
+      separable from, or merely link (or bind by name) to the interfaces of,
+      the Work and Derivative Works thereof.
+
+      "Contribution" shall mean any work of authorship, including
+      the original version of the Work and any modifications or additions
+      to that Work or Derivative Works thereof, that is intentionally
+      submitted to Licensor for inclusion in the Work by the copyright owner
+      or by an individual or Legal Entity authorized to submit on behalf of
+      the copyright owner. For the purposes of this definition, "submitted"
+      means any form of electronic, verbal, or written communication sent
+      to the Licensor or its representatives, including but not limited to
+      communication on electronic mailing lists, source code control systems,
+      and issue tracking systems that are managed by, or on behalf of, the
+      Licensor for the purpose of discussing and improving the Work, but
+      excluding communication that is conspicuously marked or otherwise
+      designated in writing by the copyright owner as "Not a Contribution."
+
+      "Contributor" shall mean Licensor and any individual or Legal Entity
+      on behalf of whom a Contribution has been received by Licensor and
+      subsequently incorporated within the Work.
+
+   2. Grant of Copyright License. Subject to the terms and conditions of
+      this License, each Contributor hereby grants to You a perpetual,
+      worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+      copyright license to reproduce, prepare Derivative Works of,
+      publicly display, publicly perform, sublicense, and distribute the
+      Work and such Derivative Works in Source or Object form.
+
+   3. Grant of Patent License. Subject to the terms and conditions of
+      this License, each Contributor hereby grants to You a perpetual,
+      worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+      (except as stated in this section) patent license to make, have made,
+      use, offer to sell, sell, import, and otherwise transfer the Work,
+      where such license applies only to those patent claims licensable
+      by such Contributor that are necessarily infringed by their
+      Contribution(s) alone or by combination of their Contribution(s)
+      with the Work to which such Contribution(s) was submitted. If You
+      institute patent litigation against any entity (including a
+      cross-claim or counterclaim in a lawsuit) alleging that the Work
+      or a Contribution incorporated within the Work constitutes direct
+      or contributory patent infringement, then any patent licenses
+      granted to You under this License for that Work shall terminate
+      as of the date such litigation is filed.
+
+   4. Redistribution. You may reproduce and distribute copies of the
+      Work or Derivative Works thereof in any medium, with or without
+      modifications, and in Source or Object form, provided that You
+      meet the following conditions:
+
+      (a) You must give any other recipients of the Work or
+          Derivative Works a copy of this License; and
+
+      (b) You must cause any modified files to carry prominent notices
+          stating that You changed the files; and
+
+      (c) You must retain, in the Source form of any Derivative Works
+          that You distribute, all copyright, patent, trademark, and
+          attribution notices from the Source form of the Work,
+          excluding those notices that do not pertain to any part of
+          the Derivative Works; and
+
+      (d) If the Work includes a "NOTICE" text file as part of its
+          distribution, then any Derivative Works that You distribute must
+          include a readable copy of the attribution notices contained
+          within such NOTICE file, excluding those notices that do not
+          pertain to any part of the Derivative Works, in at least one
+          of the following places: within a NOTICE text file distributed
+          as part of the Derivative Works; within the Source form or
+          documentation, if provided along with the Derivative Works; or,
+          within a display generated by the Derivative Works, if and
+          wherever such third-party notices normally appear. The contents
+          of the NOTICE file are for informational purposes only and
+          do not modify the License. You may add Your own attribution
+          notices within Derivative Works that You distribute, alongside
+          or as an addendum to the NOTICE text from the Work, provided
+          that such additional attribution notices cannot be construed
+          as modifying the License.
+
+      You may add Your own copyright statement to Your modifications and
+      may provide additional or different license terms and conditions
+      for use, reproduction, or distribution of Your modifications, or
+      for any such Derivative Works as a whole, provided Your use,
+      reproduction, and distribution of the Work otherwise complies with
+      the conditions stated in this License.
+
+   5. Submission of Contributions. Unless You explicitly state otherwise,
+      any Contribution intentionally submitted for inclusion in the Work
+      by You to the Licensor shall be under the terms and conditions of
+      this License, without any additional terms or conditions.
+      Notwithstanding the above, nothing herein shall supersede or modify
+      the terms of any separate license agreement you may have executed
+      with Licensor regarding such Contributions.
+
+   6. Trademarks. This License does not grant permission to use the trade
+      names, trademarks, service marks, or product names of the Licensor,
+      except as required for reasonable and customary use in describing the
+      origin of the Work and reproducing the content of the NOTICE file.
+
+   7. Disclaimer of Warranty. Unless required by applicable law or
+      agreed to in writing, Licensor provides the Work (and each
+      Contributor provides its Contributions) on an "AS IS" BASIS,
+      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+      implied, including, without limitation, any warranties or conditions
+      of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+      PARTICULAR PURPOSE. You are solely responsible for determining the
+      appropriateness of using or redistributing the Work and assume any
+      risks associated with Your exercise of permissions under this License.
+
+   8. Limitation of Liability. In no event and under no legal theory,
+      whether in tort (including negligence), contract, or otherwise,
+      unless required by applicable law (such as deliberate and grossly
+      negligent acts) or agreed to in writing, shall any Contributor be
+      liable to You for damages, including any direct, indirect, special,
+      incidental, or consequential damages of any character arising as a
+      result of this License or out of the use or inability to use the
+      Work (including but not limited to damages for loss of goodwill,
+      work stoppage, computer failure or malfunction, or any and all
+      other commercial damages or losses), even if such Contributor
+      has been advised of the possibility of such damages.
+
+   9. Accepting Warranty or Additional Liability. While redistributing
+      the Work or Derivative Works thereof, You may choose to offer,
+      and charge a fee for, acceptance of support, warranty, indemnity,
+      or other liability obligations and/or rights consistent with this
+      License. However, in accepting such obligations, You may act only
+      on Your own behalf and on Your sole responsibility, not on behalf
+      of any other Contributor, and only if You agree to indemnify,
+      defend, and hold each Contributor harmless for any liability
+      incurred by, or claims asserted against, such Contributor by reason
+      of your accepting any such warranty or additional liability.
+
+   END OF TERMS AND CONDITIONS
+
+   APPENDIX: How to apply the Apache License to your work.
+
+      To apply the Apache License to your work, attach the following
+      boilerplate notice, with the fields enclosed by brackets "[]"
+      replaced with your own identifying information. (Don't include
+      the brackets!)  The text should be enclosed in the appropriate
+      comment syntax for the file format. We also recommend that a
+      file or class name and description of purpose be included on the
+      same "printed page" as the copyright notice for easier
+      identification within third-party archives.
+
+   Copyright [yyyy] [name of copyright owner]
+
+   Licensed 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.
+

Added: aries/trunk/async/promise-api/NOTICE
URL: http://svn.apache.org/viewvc/aries/trunk/async/promise-api/NOTICE?rev=1678227&view=auto
==============================================================================
--- aries/trunk/async/promise-api/NOTICE (added)
+++ aries/trunk/async/promise-api/NOTICE Thu May  7 15:00:53 2015
@@ -0,0 +1,8 @@
+
+Apache Aries
+Copyright 2009-2011 The Apache Software Foundation
+
+This product includes software developed at
+The Apache Software Foundation (http://www.apache.org/).
+
+

Added: aries/trunk/async/promise-api/pom.xml
URL: http://svn.apache.org/viewvc/aries/trunk/async/promise-api/pom.xml?rev=1678227&view=auto
==============================================================================
--- aries/trunk/async/promise-api/pom.xml (added)
+++ aries/trunk/async/promise-api/pom.xml Thu May  7 15:00:53 2015
@@ -0,0 +1,93 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ 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.
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+
+    <modelVersion>4.0.0</modelVersion>
+
+    <parent>
+        <groupId>org.apache.aries</groupId>
+        <artifactId>parent</artifactId>
+        <version>2.0.0</version>
+        <relativePath>../../parent/pom.xml</relativePath>
+    </parent>
+
+    <groupId>org.apache.aries.async</groupId>
+    <artifactId>org.apache.aries.async.promise.api</artifactId>
+    <packaging>bundle</packaging>
+    <name>Apache Aries Async Promise API</name>
+    <version>0.0.1-SNAPSHOT</version>
+    <description>
+        This bundle contains the Apache Aries Async Promise service API.
+    </description>
+
+    <!--<scm>-->
+        <!--<connection>scm:svn:http://svn.apache.org/repos/asf/aries/trunk/async/promise-api</connection>-->
+        <!--<developerConnection>scm:svn:https://svn.apache.org/repos/asf/aries/trunk/async/promise-api</developerConnection>-->
+        <!--<url>http://svn.apache.org/viewvc/aries/trunk/async/promise-api</url>-->
+    <!--</scm>-->
+
+    <properties>
+        <!-- Export package versions are maintained in packageinfo files -->
+        <aries.osgi.export.pkg>
+            org.osgi.util.function,
+            org.osgi.util.promise;provide:=true,
+            org.apache.aries.async.promise
+        </aries.osgi.export.pkg>
+        <aries.osgi.private.pkg />
+        <aries.osgi.import.pkg>
+            org.osgi.util.function,
+            org.osgi.util.promise,
+            *
+        </aries.osgi.import.pkg>
+        <lastReleaseVersion>0.0.0</lastReleaseVersion>
+    </properties>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.apache.aries</groupId>
+            <artifactId>org.apache.aries.util</artifactId>
+            <version>1.0.0</version>
+        </dependency>
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+            <scope>test</scope>
+        </dependency>
+    </dependencies>
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.aries.versioning</groupId>
+                <artifactId>org.apache.aries.versioning.plugin</artifactId>
+                <executions>
+                    <execution>
+                        <id>default-verify</id>
+                        <phase>verify</phase>
+                        <goals>
+                            <goal>version-check</goal>
+                        </goals>
+                    </execution>
+                </executions>
+            </plugin>
+        </plugins>
+    </build>
+
+</project>

Added: aries/trunk/async/promise-api/src/main/java/org/apache/aries/async/promise/PromiseImpl.java
URL: http://svn.apache.org/viewvc/aries/trunk/async/promise-api/src/main/java/org/apache/aries/async/promise/PromiseImpl.java?rev=1678227&view=auto
==============================================================================
--- aries/trunk/async/promise-api/src/main/java/org/apache/aries/async/promise/PromiseImpl.java (added)
+++ aries/trunk/async/promise-api/src/main/java/org/apache/aries/async/promise/PromiseImpl.java Thu May  7 15:00:53 2015
@@ -0,0 +1,371 @@
+package org.apache.aries.async.promise;
+
+import org.osgi.util.function.Function;
+import org.osgi.util.function.Predicate;
+import org.osgi.util.promise.Failure;
+import org.osgi.util.promise.Promise;
+import org.osgi.util.promise.Success;
+
+import java.lang.reflect.InvocationTargetException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.NoSuchElementException;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+
+public class PromiseImpl<T> implements Promise<T> {
+
+    private final ExecutorService exec;
+    private final List<Runnable> tasks = new ArrayList<Runnable>();
+    private final CountDownLatch resolved = new CountDownLatch(1);
+
+    private List<PromiseImpl> chain;
+    private Success onSuccess;
+    private Failure onFailure;
+    private Throwable failure;
+    private T value;
+
+    public PromiseImpl() {
+        // Executor for onResolve() callbacks
+        // We could use an Executor that runs tasks in current thread
+        exec = Executors.newSingleThreadExecutor();
+    }
+
+    public void fail(Throwable failure) {
+        if (failure == null)
+            throw new NullPointerException();
+        complete(null, failure);
+    }
+
+    public void resolve(T value) {
+        complete(value, null);
+    }
+
+    public Promise<Void> resolveWith(final Promise<? extends T> with) {
+        if (with == null)
+            throw new NullPointerException();
+        final PromiseImpl<Void> result = new PromiseImpl<Void>();
+
+        with.then(new Success<T, T>() {
+            @Override
+            public Promise<T> call(Promise<T> resolved) throws Exception {
+                if (isDone()) {
+                    result.fail(new IllegalStateException("associated Promise already resolved"));
+                }
+                PromiseImpl.this.resolve(resolved.getValue());
+                result.resolve(null);
+                return null;
+            }
+        }, new Failure() {
+            @Override
+            public void fail(Promise<?> resolved) throws Exception {
+                if (isDone()) {
+                    result.fail(new IllegalStateException("associated Promise already resolved"));
+                }
+                PromiseImpl.this.fail(resolved.getFailure());
+                result.resolve(null);
+            }
+        });
+
+        return result;
+    }
+
+    private synchronized void complete(T value, Throwable failure) {
+        if (isDone()) {
+            throw new IllegalStateException("Promise is already resolved");
+        }
+
+        // mark this Promise as complete before invoking callbacks
+        if (failure != null) {
+            this.failure = failure;
+        } else {
+            this.value = value;
+        }
+        resolved.countDown();
+
+        if (chain != null) {
+            runChain();
+        }
+
+        // run onResolve() callbacks
+        for (Runnable task : tasks) {
+            exec.submit(task);
+        }
+    }
+
+    // run chained success/failure callbacks
+    @SuppressWarnings("unchecked")
+    private void runChain() {
+        while (!chain.isEmpty()) {
+            PromiseImpl next = chain.remove(0);
+            if (failure != null) {
+                try {
+                    if (next.onFailure != null) {
+                        // "This method is called if the Promise with which it is registered resolves with a failure."
+                        next.onFailure.fail(this);
+                    }
+                    // "If this method completes normally, the chained Promise will be failed
+                    // with the same exception which failed the resolved Promise."
+                    next.fail(failure);
+                } catch (Exception e) {
+                    // "If this method throws an exception, the chained Promise will be failed with the thrown exception."
+                    next.fail(e);
+                }
+            } else {
+                try {
+                    // "This method is called if the Promise with which it is registered resolves successfully."
+                    Promise<T> p = null;
+                    if (next.onSuccess != null) {
+                        p = next.onSuccess.call(this);
+                    }
+                    if (p == null) {
+                        // "If the returned Promise is null then the chained Promise will resolve immediately with a successful value of null."
+                        next.resolve(null);
+                    } else {
+                        // "If the returned Promise is not null then the chained Promise will be resolved when the returned Promise is resolved"
+                        next.resolveWith(p);
+                    }
+                } catch (InvocationTargetException e) {
+                    next.fail(e.getCause());
+                } catch (Exception e) {
+                    next.fail(e);
+                }
+            }
+        }
+    }
+
+    // Promise API methods
+
+    @Override
+    public boolean isDone() {
+        return resolved.getCount() == 0;
+    }
+
+    @Override
+    public T getValue() throws InvocationTargetException, InterruptedException {
+        resolved.await();
+        if (failure != null) {
+            throw new InvocationTargetException(failure);
+        }
+        return value;
+    }
+
+    @Override
+    public Throwable getFailure() throws InterruptedException {
+        resolved.await();
+        return failure;
+    }
+
+    @Override
+    public synchronized Promise<T> onResolve(Runnable callback) {
+        if (callback == null)
+            throw new NullPointerException();
+
+        if (isDone()) {
+            exec.submit(callback);
+        } else {
+            tasks.add(callback);
+        }
+        return this;
+    }
+
+    @Override
+    public <R> Promise<R> then(Success<? super T, ? extends R> success, Failure failure) {
+        PromiseImpl<R> result = new PromiseImpl<R>();
+        result.onSuccess = success;
+        result.onFailure = failure;
+        synchronized (this) {
+            if (chain == null) {
+                chain = new ArrayList<PromiseImpl>();
+            }
+            chain.add(result);
+            if (isDone()) {
+                runChain();
+            }
+        }
+        return result;
+    }
+
+    @Override
+    public <R> Promise<R> then(Success<? super T, ? extends R> success) {
+        return then(success, null);
+    }
+
+    @Override
+    public Promise<T> filter(final Predicate<? super T> predicate) {
+        if (predicate == null)
+            throw new NullPointerException();
+        final PromiseImpl<T> result = new PromiseImpl<T>();
+
+        then(new Success<T, T>() {
+            @Override
+            public Promise<T> call(Promise<T> resolved) throws Exception {
+                try {
+                    if (predicate.test(resolved.getValue())) {
+                        result.resolve(resolved.getValue());
+                    } else {
+                        result.fail(new NoSuchElementException("predicate does not accept value"));
+                    }
+                } catch (Throwable t) {
+                    result.fail(t);
+                }
+                return null;
+            }
+        }, new Failure() {
+            @Override
+            public void fail(Promise<?> resolved) throws Exception {
+                result.fail(resolved.getFailure());
+            }
+        });
+
+        return result;
+    }
+
+    @Override
+    public <R> Promise<R> map(final Function<? super T, ? extends R> mapper) {
+        if (mapper == null)
+            throw new NullPointerException();
+        final PromiseImpl<R> result = new PromiseImpl<R>();
+
+        then(new Success<T, T>() {
+            @Override
+            public Promise<T> call(Promise<T> resolved) throws Exception {
+                try {
+                    R val = mapper.apply(resolved.getValue());
+                    result.resolve(val);
+                } catch (Throwable t) {
+                    result.fail(t);
+                }
+                return null;
+            }
+        }, new Failure() {
+            @Override
+            public void fail(Promise<?> resolved) throws Exception {
+                result.fail(resolved.getFailure());
+            }
+        });
+
+        return result;
+    }
+
+    @Override
+    public <R> Promise<R> flatMap(final Function<? super T, Promise<? extends R>> mapper) {
+        if (mapper == null)
+            throw new NullPointerException();
+        final PromiseImpl<R> result = new PromiseImpl<R>();
+
+        then(new Success<T, T>() {
+            @Override
+            public Promise<T> call(Promise<T> resolved) throws Exception {
+                try {
+                    Promise<? extends R> p = mapper.apply(resolved.getValue());
+                    result.resolveWith(p);
+                } catch (Throwable t) {
+                    result.fail(t);
+                }
+                return null;
+            }
+        }, new Failure() {
+            @Override
+            public void fail(Promise<?> resolved) throws Exception {
+                result.fail(resolved.getFailure());
+            }
+        });
+
+        return result;
+    }
+
+    @Override
+    public Promise<T> recover(final Function<Promise<?>, ? extends T> recovery) {
+        if (recovery == null)
+            throw new NullPointerException();
+
+        final PromiseImpl<T> result = new PromiseImpl<T>();
+
+        then(new Success<T, T>() {
+            @Override
+            public Promise<T> call(Promise<T> resolved) throws Exception {
+                result.resolve(resolved.getValue());
+                return null;
+            }
+        }, new Failure() {
+            @Override
+            public void fail(Promise<?> resolved) throws Exception {
+                try {
+                    T recover = recovery.apply(resolved);
+                    if (recover != null) {
+                        result.resolve(recover);
+                    } else {
+                        result.fail(resolved.getFailure());
+                    }
+                } catch (Throwable t) {
+                    result.fail(t);
+                }
+            }
+        });
+
+        return result;
+    }
+
+    @Override
+    public Promise<T> recoverWith(final Function<Promise<?>, Promise<? extends T>> recovery) {
+        if (recovery == null)
+            throw new NullPointerException();
+
+        final PromiseImpl<T> result = new PromiseImpl<T>();
+
+        then(new Success<T, T>() {
+            @Override
+            public Promise<T> call(Promise<T> resolved) throws Exception {
+                result.resolve(resolved.getValue());
+                return null;
+            }
+        }, new Failure() {
+            @Override
+            public void fail(Promise<?> resolved) throws Exception {
+                try {
+                    Promise<? extends T> recover = recovery.apply(resolved);
+                    if (recover != null) {
+                        result.resolveWith(recover);
+                    } else {
+                        result.fail(resolved.getFailure());
+                    }
+                } catch (Throwable t) {
+                    result.fail(t);
+                }
+            }
+        });
+
+        return result;
+    }
+
+    @Override
+    public Promise<T> fallbackTo(final Promise<? extends T> fallback) {
+        if (fallback == null)
+            throw new NullPointerException();
+
+        final PromiseImpl<T> result = new PromiseImpl<T>();
+
+        then(new Success<T, T>() {
+            @Override
+            public Promise<T> call(Promise<T> resolved) throws Exception {
+                result.resolve(resolved.getValue());
+                return null;
+            }
+        }, new Failure() {
+            @Override
+            public void fail(Promise<?> resolved) throws Exception {
+                @SuppressWarnings({"not thrown", "all"})
+                Throwable fail = fallback.getFailure();
+                if (fail != null) {
+                    result.fail(resolved.getFailure());
+                } else {
+                    result.resolve(fallback.getValue());
+                }
+            }
+        });
+
+        return result;
+    }
+}

Added: aries/trunk/async/promise-api/src/main/java/org/osgi/util/function/Function.java
URL: http://svn.apache.org/viewvc/aries/trunk/async/promise-api/src/main/java/org/osgi/util/function/Function.java?rev=1678227&view=auto
==============================================================================
--- aries/trunk/async/promise-api/src/main/java/org/osgi/util/function/Function.java (added)
+++ aries/trunk/async/promise-api/src/main/java/org/osgi/util/function/Function.java Thu May  7 15:00:53 2015
@@ -0,0 +1,20 @@
+package org.osgi.util.function;
+
+/**
+ * A function that accepts a single argument and produces a result.
+ * <p>
+ * This is a functional interface and can be used as the assignment target for a lambda expression or method reference.
+ *
+ * @param <T> The type of the function input.
+ * @param <R> The type of the function output.
+ */
+//@org.osgi.annotation.versioning.ConsumerType
+public interface Function<T, R> {
+
+    /**
+     * Applies this function to the specified argument.
+     * @param t The input to this function.
+     * @return The output of this function.
+     */
+    R apply(T t);
+}

Added: aries/trunk/async/promise-api/src/main/java/org/osgi/util/function/Predicate.java
URL: http://svn.apache.org/viewvc/aries/trunk/async/promise-api/src/main/java/org/osgi/util/function/Predicate.java?rev=1678227&view=auto
==============================================================================
--- aries/trunk/async/promise-api/src/main/java/org/osgi/util/function/Predicate.java (added)
+++ aries/trunk/async/promise-api/src/main/java/org/osgi/util/function/Predicate.java Thu May  7 15:00:53 2015
@@ -0,0 +1,19 @@
+package org.osgi.util.function;
+
+/**
+ * A predicate that accepts a single argument and produces a boolean result.
+ * <p>
+ * This is a functional interface and can be used as the assignment target for a lambda expression or method reference.
+ *
+ * @param <T> The type of the predicate input.
+ */
+//@org.osgi.annotation.versioning.ConsumerType
+public interface Predicate<T> {
+    /**
+     * Evaluates this predicate on the specified argument.
+     *
+     * @param t The input to this predicate.
+     * @return true if the specified argument is accepted by this predicate; false otherwise.
+     */
+    boolean test(T t);
+}

Added: aries/trunk/async/promise-api/src/main/java/org/osgi/util/function/packageinfo
URL: http://svn.apache.org/viewvc/aries/trunk/async/promise-api/src/main/java/org/osgi/util/function/packageinfo?rev=1678227&view=auto
==============================================================================
--- aries/trunk/async/promise-api/src/main/java/org/osgi/util/function/packageinfo (added)
+++ aries/trunk/async/promise-api/src/main/java/org/osgi/util/function/packageinfo Thu May  7 15:00:53 2015
@@ -0,0 +1 @@
+version 1.0.0

Added: aries/trunk/async/promise-api/src/main/java/org/osgi/util/promise/Deferred.java
URL: http://svn.apache.org/viewvc/aries/trunk/async/promise-api/src/main/java/org/osgi/util/promise/Deferred.java?rev=1678227&view=auto
==============================================================================
--- aries/trunk/async/promise-api/src/main/java/org/osgi/util/promise/Deferred.java (added)
+++ aries/trunk/async/promise-api/src/main/java/org/osgi/util/promise/Deferred.java Thu May  7 15:00:53 2015
@@ -0,0 +1,92 @@
+package org.osgi.util.promise;
+
+import org.apache.aries.async.promise.PromiseImpl;
+
+/**
+ * A Deferred Promise resolution.
+ * <p/>
+ * Instances of this class can be used to create a Promise that can be resolved in the future. The associated Promise
+ * can be successfully resolved with resolve(Object) or resolved with a failure with fail(Throwable).
+ * <p/>
+ * It can also be resolved with the resolution of another promise using resolveWith(Promise).
+ * <p/>
+ * The associated Promise can be provided to anyone, but the Deferred object should be made available only to the party
+ * that will responsible for resolving the Promise.
+ *
+ * @param <T> The value type associated with the created Promise.
+ */
+public class Deferred<T> {
+    private final PromiseImpl<T> promise;
+
+    /**
+     * Create a new Deferred with an associated Promise.
+     */
+    public Deferred() {
+        promise = new PromiseImpl<T>();
+    }
+
+    /**
+     * Returns the Promise associated with this Deferred.
+     *
+     * @return The Promise associated with this Deferred.
+     */
+    public Promise<T> getPromise() {
+        return promise;
+    }
+
+    /**
+     * Successfully resolve the Promise associated with this Deferred.
+     * <p/>
+     * After the associated Promise is resolved with the specified value, all registered callbacks are called and any
+     * chained Promises are resolved.
+     * <p/>
+     * Resolving the associated Promise happens-before any registered callback is called. That is, in a registered
+     * callback, Promise.isDone() must return true and Promise.getValue() and Promise.getFailure() must not block.
+     *
+     * @param value The value of the resolved Promise.
+     * @throws IllegalStateException If the associated Promise was already resolved.
+     */
+    public void resolve(T value) {
+        promise.resolve(value);
+    }
+
+    /**
+     * Fail the Promise associated with this Deferred.
+     * <p/>
+     * After the associated Promise is resolved with the specified failure, all registered callbacks are called and any
+     * chained Promises are resolved.
+     * <p/>
+     * Resolving the associated Promise happens-before any registered callback is called. That is, in a registered
+     * callback, Promise.isDone() must return true and Promise.getValue() and Promise.getFailure() must not block.
+     *
+     * @param failure The failure of the resolved Promise. Must not be null.
+     * @throws IllegalStateException If the associated Promise was already resolved.
+     */
+    public void fail(Throwable failure) {
+        promise.fail(failure);
+    }
+
+    /**
+     * Resolve the Promise associated with this Deferred with the specified Promise.
+     * <p/>
+     * If the specified Promise is successfully resolved, the associated Promise is resolved with the value of the
+     * specified Promise. If the specified Promise is resolved with a failure, the associated Promise is resolved with
+     * the failure of the specified Promise.
+     * <p/>
+     * After the associated Promise is resolved with the specified Promise, all registered callbacks are called and any
+     * chained Promises are resolved.
+     * <p/>
+     * Resolving the associated Promise happens-before any registered callback is called. That is, in a registered
+     * callback, Promise.isDone() must return true and Promise.getValue() and Promise.getFailure() must not block
+     *
+     * @param with A Promise whose value or failure will be used to resolve the associated Promise. Must not be null.
+     * @return A Promise that is resolved only when the associated Promise is resolved by the specified Promise. The
+     * returned Promise will be successfully resolved, with the value null, if the associated Promise was resolved by
+     * the specified Promise. The returned Promise will be resolved with a failure of IllegalStateException if the
+     * associated Promise was already resolved when the specified Promise was resolved.
+     */
+    public Promise<Void> resolveWith(Promise<? extends T> with) {
+        return promise.resolveWith(with);
+    }
+
+}

Added: aries/trunk/async/promise-api/src/main/java/org/osgi/util/promise/FailedPromisesException.java
URL: http://svn.apache.org/viewvc/aries/trunk/async/promise-api/src/main/java/org/osgi/util/promise/FailedPromisesException.java?rev=1678227&view=auto
==============================================================================
--- aries/trunk/async/promise-api/src/main/java/org/osgi/util/promise/FailedPromisesException.java (added)
+++ aries/trunk/async/promise-api/src/main/java/org/osgi/util/promise/FailedPromisesException.java Thu May  7 15:00:53 2015
@@ -0,0 +1,44 @@
+package org.osgi.util.promise;
+
+import java.util.Collection;
+import java.util.Collections;
+
+/**
+ * Promise failure exception for a collection of failed Promises.
+ */
+public class FailedPromisesException extends RuntimeException {
+
+    private final Collection<Promise<?>> failed;
+
+    /**
+     * Create a new FailedPromisesException with the specified Promises.
+     *
+     * @param failed A collection of Promises that have been resolved with a failure. Must not be null.
+     */
+    public FailedPromisesException(Collection<Promise<?>> failed) {
+        this(failed, null);
+    }
+
+    /**
+     * Create a new FailedPromisesException with the specified Promises.
+     *
+     * @param failed A collection of Promises that have been resolved with a failure. Must not be null.
+     * @param cause  the cause (which is saved for later retrieval by the {@link #getCause()} method).  (A <tt>null</tt>
+     *               value is permitted, and indicates that the cause is nonexistent or unknown.)
+     */
+    public FailedPromisesException(Collection<Promise<?>> failed, Throwable cause) {
+        super(cause);
+        assert failed != null;
+        this.failed = failed;
+    }
+
+    /**
+     * Returns the collection of Promises that have been resolved with a failure.
+     *
+     * @return The collection of Promises that have been resolved with a failure. The returned collection is
+     * unmodifiable.
+     */
+    public Collection<Promise<?>> getFailedPromises() {
+        return Collections.unmodifiableCollection(failed);
+    }
+}

Added: aries/trunk/async/promise-api/src/main/java/org/osgi/util/promise/Failure.java
URL: http://svn.apache.org/viewvc/aries/trunk/async/promise-api/src/main/java/org/osgi/util/promise/Failure.java?rev=1678227&view=auto
==============================================================================
--- aries/trunk/async/promise-api/src/main/java/org/osgi/util/promise/Failure.java (added)
+++ aries/trunk/async/promise-api/src/main/java/org/osgi/util/promise/Failure.java Thu May  7 15:00:53 2015
@@ -0,0 +1,25 @@
+package org.osgi.util.promise;
+
+/**
+ * Failure callback for a Promise.
+ * <p>
+ * A Failure callback is registered with a Promise using the Promise.then(Success, Failure) method and is called if the Promise is resolved with a failure.
+ * <p>
+ * This is a functional interface and can be used as the assignment target for a lambda expression or method reference.
+ */
+//@org.osgi.annotation.versioning.ConsumerType
+public interface Failure {
+    /**
+     * Failure callback for a Promise.
+     * <p>
+     * This method is called if the Promise with which it is registered resolves with a failure.
+     * <p>
+     * In the remainder of this description we will refer to the Promise returned by Promise.then(Success, Failure) when this Failure callback was registered as the chained Promise.
+     * <p>
+     * If this method completes normally, the chained Promise will be failed with the same exception which failed the resolved Promise. If this method throws an exception, the chained Promise will be failed with the thrown exception.
+     *
+     * @param resolved The failed resolved Promise.
+     * @throws Exception The chained Promise will be failed with the thrown exception.
+     */
+    void fail(Promise<?> resolved) throws Exception;
+}

Added: aries/trunk/async/promise-api/src/main/java/org/osgi/util/promise/Promise.java
URL: http://svn.apache.org/viewvc/aries/trunk/async/promise-api/src/main/java/org/osgi/util/promise/Promise.java?rev=1678227&view=auto
==============================================================================
--- aries/trunk/async/promise-api/src/main/java/org/osgi/util/promise/Promise.java (added)
+++ aries/trunk/async/promise-api/src/main/java/org/osgi/util/promise/Promise.java Thu May  7 15:00:53 2015
@@ -0,0 +1,279 @@
+package org.osgi.util.promise;
+
+import org.osgi.util.function.Function;
+import org.osgi.util.function.Predicate;
+
+import java.lang.reflect.InvocationTargetException;
+
+/**
+ * A Promise of a value.
+ * <p/>
+ * A Promise represents a future value. It handles the interactions to for asynchronous processing. A Deferred object
+ * can be used to create a Promise and later resolve the Promise. A Promise is used by the caller of an asynchronous
+ * function to get the result or handle the error. The caller can either get a callback when the Promise is resolved
+ * with a value or an error, or the Promise can be used in chaining. In chaining, callbacks are provided that receive
+ * the resolved Promise, and a new Promise is generated that resolves based upon the result of a callback.
+ * <p/>
+ * Both callbacks and chaining can be repeated any number of times, even after the Promise has been resolved.
+ * <p/>
+ * Example callback usage:
+ * <pre>
+ * final Promise<String> foo= foo();
+ * foo.onResolve(new Runnable() {
+ * public void run() {
+ * System.out.println(foo.getValue());
+ * }
+ * });
+ * </pre>
+ * <p/>
+ * Example chaining usage;
+ * <pre>
+ * Success<String,String> doubler = new Success<String,String>() {
+ * public Promise<String> call(Promise<String> p) throws Exception {
+ * return Promises.resolved(p.getValue()+p.getValue());
+ * }
+ * };
+ * final Promise<String> foo = foo().then(doubler).then(doubler);
+ * foo.onResolve(new Runnable() {
+ * public void run() {
+ * System.out.println(foo.getValue());
+ * }
+ * });
+ * </pre>
+ *
+ * @param <T> The value type associated with this Promise.
+ */
+//@org.osgi.annotation.versioning.ProviderType
+public interface Promise<T> {
+    /**
+     * Returns whether this Promise has been resolved.
+     * <p/>
+     * This Promise may be successfully resolved or resolved with a failure.
+     *
+     * @return true if this Promise was resolved either successfully or with a failure; false if this Promise is
+     * unresolved.
+     */
+    boolean isDone();
+
+    /**
+     * Returns the value of this Promise.
+     * <p/>
+     * If this Promise is not resolved, this method must block and wait for this Promise to be resolved before
+     * completing.
+     * <p/>
+     * If this Promise was successfully resolved, this method returns with the value of this Promise. If this Promise
+     * was resolved with a failure, this method must throw an InvocationTargetException with the failure exception as
+     * the cause.
+     *
+     * @return The value of this resolved Promise.
+     * @throws InvocationTargetException If this Promise was resolved with a failure. The cause of the
+     *                                   InvocationTargetException is the failure exception.
+     * @throws InterruptedException      If the current thread was interrupted while waiting.
+     */
+    T getValue() throws InvocationTargetException, InterruptedException;
+
+    /**
+     * Returns the failure of this Promise.
+     * <p/>
+     * If this Promise is not resolved, this method must block and wait for this Promise to be resolved before
+     * completing.
+     * <p/>
+     * If this Promise was resolved with a failure, this method returns with the failure of this Promise. If this
+     * Promise was successfully resolved, this method must return null.
+     *
+     * @return The failure of this resolved Promise or null if this Promise was successfully resolved.
+     * @throws InterruptedException If the current thread was interrupted while waiting.
+     */
+    Throwable getFailure() throws InterruptedException;
+
+    /**
+     * Register a callback to be called when this Promise is resolved.
+     * <p/>
+     * The specified callback is called when this Promise is resolved either successfully or with a failure.
+     * <p/>
+     * This method may be called at any time including before and after this Promise has been resolved.
+     * <p/>
+     * Resolving this Promise happens-before any registered callback is called. That is, in a registered callback,
+     * isDone() must return true and getValue() and getFailure() must not block.
+     * <p/>
+     * A callback may be called on a different thread than the thread which registered the callback. So the callback
+     * must be thread safe but can rely upon that the registration of the callback happens-before the registered
+     * callback is called.
+     *
+     * @param callback A callback to be called when this Promise is resolved. Must not be null.
+     * @return This Promise.
+     */
+    Promise<T> onResolve(Runnable callback);
+
+
+    /**
+     * Chain a new Promise to this Promise with Success and Failure callbacks.
+     * <p/>
+     * The specified Success callback is called when this Promise is successfully resolved and the specified Failure
+     * callback is called when this Promise is resolved with a failure.
+     * <p/>
+     * This method returns a new Promise which is chained to this Promise. The returned Promise must be resolved when
+     * this Promise is resolved after the specified Success or Failure callback is executed. The result of the executed
+     * callback must be used to resolve the returned Promise. Multiple calls to this method can be used to create a
+     * chain of promises which are resolved in sequence.
+     * <p/>
+     * If this Promise is successfully resolved, the Success callback is executed and the result Promise, if any, or
+     * thrown exception is used to resolve the returned Promise from this method. If this Promise is resolved with a
+     * failure, the Failure callback is executed and the returned Promise from this method is failed.
+     * <p/>
+     * This method may be called at any time including before and after this Promise has been resolved.
+     * <p/>
+     * Resolving this Promise happens-before any registered callback is called. That is, in a registered callback,
+     * isDone() must return true and getValue() and getFailure() must not block.
+     * <p/>
+     * A callback may be called on a different thread than the thread which registered the callback. So the callback
+     * must be thread safe but can rely upon that the registration of the callback happens-before the registered
+     * callback is called.
+     *
+     * @param <R>     The value type associated with the returned Promise.
+     * @param success A Success callback to be called when this Promise is successfully resolved. May be null if no
+     *                Success callback is required. In thi
+     * @param failure A Failure callback to be called when this Promise is resolved with a failure. May be null if no
+     *                Failure callback is required.
+     * @return A new Promise which is chained to this Promise. The returned Promise must be resolved when this Promise
+     * is resolved after the specified Success or Failure callback, if any, is executed
+     */
+    <R> Promise<R> then(Success<? super T, ? extends R> success, Failure failure);
+
+
+    /**
+     * Chain a new Promise to this Promise with a Success callback.
+     * <p/>
+     * This method performs the same function as calling then(Success, Failure) with the specified Success callback and
+     * null for the Failure callback
+     *
+     * @param success A Success callback to be called when this Promise is successfully resolved. May be null if no
+     *                Success callback is required. In this case, the returned Promise must be resolved with the value
+     *                null when this Promise is successfully resolved.
+     * @param <R>     The value type associated with the returned Promise.
+     * @return A new Promise which is chained to this Promise. The returned Promise must be resolved when this Promise
+     * is resolved after the specified Success, if any, is executed.
+     * @see #then(Success, Failure)
+     */
+    <R> Promise<R> then(Success<? super T, ? extends R> success);
+
+
+    /**
+     * Filter the value of this Promise.
+     * <p/>
+     * If this Promise is successfully resolved, the returned Promise will either be resolved with the value of this
+     * Promise if the specified Predicate accepts that value or failed with a NoSuchElementException if the specified
+     * Predicate does not accept that value. If the specified Predicate throws an exception, the returned Promise will
+     * be failed with the exception.
+     * <p/>
+     * If this Promise is resolved with a failure, the returned Promise will be failed with that failure.
+     * <p/>
+     * This method may be called at any time including before and after this Promise has been resolved.
+     *
+     * @param predicate The Predicate to evaluate the value of this Promise. Must not be null.
+     * @return A Promise that filters the value of this Promise.
+     */
+    Promise<T> filter(Predicate<? super T> predicate);
+
+    /**
+     * Map the value of this Promise.
+     * <p/>
+     * If this Promise is successfully resolved, the returned Promise will be resolved with the value of specified
+     * Function as applied to the value of this Promise. If the specified Function throws an exception, the returned
+     * Promise will be failed with the exception.
+     * <p/>
+     * If this Promise is resolved with a failure, the returned Promise will be failed with that failure.
+     * <p/>
+     * This method may be called at any time including before and after this Promise has been resolved.
+     *
+     * @param mapper The Function that will map the value of this Promise to the value that will be used to resolve the
+     *               returned Promise. Must not be null.
+     * @param <R>    The value type associated with the returned Promise.
+     * @return A Promise that returns the value of this Promise as mapped by the specified Function.
+     */
+    <R> Promise<R> map(Function<? super T, ? extends R> mapper);
+
+    /**
+     * FlatMap the value of this Promise.
+     * <p/>
+     * If this Promise is successfully resolved, the returned Promise will be resolved with the Promise from the
+     * specified Function as applied to the value of this Promise. If the specified Function throws an exception, the
+     * returned Promise will be failed with the exception.
+     * <p/>
+     * If this Promise is resolved with a failure, the returned Promise will be failed with that failure.
+     * <p/>
+     * This method may be called at any time including before and after this Promise has been resolved.
+     *
+     * @param mapper The Function that will flatMap the value of this Promise to a Promise that will be used to resolve
+     *               the returned Promise. Must not be null.
+     * @param <R>    The value type associated with the returned Promise.
+     * @return A Promise that returns the value of this Promise as mapped by the specified Function.
+     */
+    <R> Promise<R> flatMap(Function<? super T, Promise<? extends R>> mapper);
+
+    /**
+     * Recover from a failure of this Promise with a recovery value.
+     * <p/>
+     * If this Promise is successfully resolved, the returned Promise will be resolved with the value of this Promise.
+     * <p/>
+     * If this Promise is resolved with a failure, the specified Function is applied to this Promise to produce a
+     * recovery value.
+     * <p/>
+     * If the recovery value is not null, the returned Promise will be resolved with the recovery value.
+     * <p/>
+     * If the recovery value is null, the returned Promise will be failed with the failure of this Promise.
+     * <p/>
+     * If the specified Function throws an exception, the returned Promise will be failed with that exception.
+     * <p/>
+     * To recover from a failure of this Promise with a recovery value of null, the recoverWith(Function) method must be
+     * used. The specified Function for recoverWith(Function) can return Promises.resolved(null) to supply the desired
+     * null value.
+     * <p/>
+     * This method may be called at any time including before and after this Promise has been resolved.
+     *
+     * @param recovery If this Promise resolves with a failure, the specified Function is called to produce a recovery
+     *                 value to be used to resolve the returned Promise. Must not be null.
+     * @return A Promise that resolves with the value of this Promise or recovers from the failure of this Promise.
+     */
+    Promise<T> recover(Function<Promise<?>, ? extends T> recovery);
+
+    /**
+     * Recover from a failure of this Promise with a recovery Promise.
+     * <p/>
+     * If this Promise is successfully resolved, the returned Promise will be resolved with the value of this Promise.
+     * <p/>
+     * If this Promise is resolved with a failure, the specified Function is applied to this Promise to produce a
+     * recovery Promise.
+     * <p/>
+     * If the recovery Promise is not null, the returned Promise will be resolved with the recovery Promise.
+     * <p/>
+     * If the recovery Promise is null, the returned Promise will be failed with the failure of this Promise.
+     * <p/>
+     * If the specified Function throws an exception, the returned Promise will be failed with that exception.
+     * <p/>
+     * This method may be called at any time including before and after this Promise has been resolved.
+     *
+     * @param recovery If this Promise resolves with a failure, the specified Function is called to produce a recovery
+     *                 Promise to be used to resolve the returned Promise. Must not be null.
+     * @return A Promise that resolves with the value of this Promise or recovers from the failure of this Promise.
+     */
+    Promise<T> recoverWith(Function<Promise<?>, Promise<? extends T>> recovery);
+
+    /**
+     * Fall back to the value of the specified Promise if this Promise fails.
+     * <p/>
+     * If this Promise is successfully resolved, the returned Promise will be resolved with the value of this Promise.
+     * <p/>
+     * If this Promise is resolved with a failure, the successful result of the specified Promise is used to resolve the
+     * returned Promise. If the specified Promise is resolved with a failure, the returned Promise will be failed with
+     * the failure of this Promise rather than the failure of the specified Promise.
+     * <p/>
+     * This method may be called at any time including before and after this Promise has been resolved.
+     *
+     * @param fallback The Promise whose value will be used to resolve the returned Promise if this Promise resolves
+     *                 with a failure. Must not be null.
+     * @return A Promise that returns the value of this Promise or falls back to the value of the specified Promise.
+     */
+    Promise<T> fallbackTo(Promise<? extends T> fallback);
+
+}

Added: aries/trunk/async/promise-api/src/main/java/org/osgi/util/promise/Promises.java
URL: http://svn.apache.org/viewvc/aries/trunk/async/promise-api/src/main/java/org/osgi/util/promise/Promises.java?rev=1678227&view=auto
==============================================================================
--- aries/trunk/async/promise-api/src/main/java/org/osgi/util/promise/Promises.java (added)
+++ aries/trunk/async/promise-api/src/main/java/org/osgi/util/promise/Promises.java Thu May  7 15:00:53 2015
@@ -0,0 +1,122 @@
+package org.osgi.util.promise;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+
+/**
+ * Static helper methods for Promises.
+ */
+public class Promises {
+    /**
+     * Create a new Promise that has been resolved with the specified value.
+     *
+     * @param value The value of the resolved Promise.
+     * @param <T>   The value type associated with the returned Promise.
+     * @return A new Promise that has been resolved with the specified value.
+     */
+    public static <T> Promise<T> resolved(T value) {
+        Deferred<T> def = new Deferred<T>();
+        def.resolve(value);
+        return def.getPromise();
+    }
+
+    /**
+     * Create a new Promise that has been resolved with the specified failure.
+     *
+     * @param failure The failure of the resolved Promise. Must not be null.
+     * @param <T>     The value type associated with the returned Promise.
+     * @return A new Promise that has been resolved with the specified failure.
+     */
+    public static <T> Promise<T> failed(Throwable failure) {
+        if (failure == null)
+            throw new NullPointerException();
+        Deferred<T> def = new Deferred<T>();
+        def.fail(failure);
+        return def.getPromise();
+    }
+
+    /**
+     * Create a new Promise that is a latch on the resolution of the specified Promises.
+     * <p/>
+     * The new Promise acts as a gate and must be resolved after all of the specified Promises are resolved.
+     *
+     * @param promises The Promises which must be resolved before the returned Promise must be resolved. Must not be
+     *                 null.
+     * @param <T>      The value type of the List value associated with the returned Promise.
+     * @param <S>      A subtype of the value type of the List value associated with the returned Promise.
+     * @return A Promise that is resolved only when all the specified Promises are resolved. The returned Promise will
+     * be successfully resolved, with a List of the values in the order of the specified Promises, if all the specified
+     * Promises are successfully resolved. The List in the returned Promise is the property of the caller and is
+     * modifiable. The returned Promise will be resolved with a failure of FailedPromisesException if any of the
+     * specified Promises are resolved with a failure. The failure FailedPromisesException must contain all of the
+     * specified Promises which resolved with a failure.
+     */
+    public static <T, S> Promise<List<T>> all(final Collection<Promise<S>> promises) {
+        if (promises == null)
+            throw new NullPointerException();
+        final Deferred<List<T>> result = new Deferred<List<T>>();
+        final Collection<Promise<?>> failedPromises = new ArrayList<Promise<?>>();
+        final List<T> resolvedValues = new ArrayList<T>();
+
+        if (promises.size() == 0) {
+            result.resolve(resolvedValues);
+        }
+        for (final Promise<S> promise : promises) {
+            promise.then(new Success<S, T>() {
+                @Override
+                public Promise<T> call(Promise<S> resolved) throws Exception {
+                    // "S is subtype of the value type of the List"
+                    @SuppressWarnings("unchecked")
+                    T value = (T) resolved.getValue();
+                    resolvedValues.add(value);
+
+                    if (resolvedValues.size() == promises.size()) {
+                        result.resolve(resolvedValues);
+                    } else if (failedPromises.size() + resolvedValues.size() == promises.size()) {
+                        result.fail(new FailedPromisesException(failedPromises));
+                    }
+                    return null;
+                }
+            }, new Failure() {
+                @Override
+                public void fail(Promise<?> resolved) throws Exception {
+                    failedPromises.add(resolved);
+                    if (failedPromises.size() + resolvedValues.size() == promises.size()) {
+                        result.fail(new FailedPromisesException(failedPromises));
+                    }
+                }
+            });
+
+        }
+
+        return result.getPromise();
+    }
+
+    /**
+     * Create a new Promise that is a latch on the resolution of the specified Promises.
+     * <p/>
+     * The new Promise acts as a gate and must be resolved after all of the specified Promises are resolved.
+     *
+     * @param promises The Promises which must be resolved before the returned Promise must be resolved. Must not be
+     *                 null.
+     * @param <T>      The value type associated with the specified Promises.
+     * @return A Promise that is resolved only when all the specified Promises are resolved. The returned Promise will
+     * be successfully resolved, with a List of the values in the order of the specified Promises, if all the specified
+     * Promises are successfully resolved. The List in the returned Promise is the property of the caller and is
+     * modifiable. The returned Promise will be resolved with a failure of FailedPromisesException if any of the
+     * specified Promises are resolved with a failure. The failure FailedPromisesException must contain all of the
+     * specified Promises which resolved with a failure.
+     */
+    public static <T> Promise<List<T>> all(final Promise<? extends T>... promises) {
+        if (promises == null)
+            throw new NullPointerException();
+        List<Promise<T>> list = new ArrayList<Promise<T>>();
+        for (Promise<? extends T> promise : promises) {
+            @SuppressWarnings("unchecked")
+            Promise<T> pt = (Promise<T>) promise;
+            list.add(pt);
+        }
+        return all(list);
+    }
+}

Added: aries/trunk/async/promise-api/src/main/java/org/osgi/util/promise/Success.java
URL: http://svn.apache.org/viewvc/aries/trunk/async/promise-api/src/main/java/org/osgi/util/promise/Success.java?rev=1678227&view=auto
==============================================================================
--- aries/trunk/async/promise-api/src/main/java/org/osgi/util/promise/Success.java (added)
+++ aries/trunk/async/promise-api/src/main/java/org/osgi/util/promise/Success.java Thu May  7 15:00:53 2015
@@ -0,0 +1,29 @@
+package org.osgi.util.promise;
+
+/**
+ * Success callback for a Promise.
+ * <p>
+ * A Success callback is registered with a Promise using the Promise.then(Success) method and is called if the Promise is resolved successfully.
+ * <p>
+ * This is a functional interface and can be used as the assignment target for a lambda expression or method reference.
+ * <p>
+ * @param <T> The value type of the resolved Promise passed as input to this callback.
+ * @param <R> The value type of the returned Promise from this callback.
+ */
+//@org.osgi.annotation.versioning.ConsumerType
+public interface Success<T,R> {
+    /**
+     * Success callback for a Promise.
+     * <p>
+     * This method is called if the Promise with which it is registered resolves successfully.
+     * <p>
+     * In the remainder of this description we will refer to the Promise returned by this method as the returned Promise and the Promise returned by Promise.then(Success) when this Success callback was registered as the chained Promise.
+     * <p>
+     * If the returned Promise is null then the chained Promise will resolve immediately with a successful value of null. If the returned Promise is not null then the chained Promise will be resolved when the returned Promise is resolved.
+     *
+     * @param resolved The successfully resolved Promise.
+     * @return The Promise to use to resolve the chained Promise, or null if the chained Promise is to be resolved immediately with the value null.
+     * @throws Exception The chained Promise will be failed with the thrown exception.
+     */
+    Promise<R> call(Promise<T> resolved) throws Exception;
+}

Added: aries/trunk/async/promise-api/src/main/java/org/osgi/util/promise/packageinfo
URL: http://svn.apache.org/viewvc/aries/trunk/async/promise-api/src/main/java/org/osgi/util/promise/packageinfo?rev=1678227&view=auto
==============================================================================
--- aries/trunk/async/promise-api/src/main/java/org/osgi/util/promise/packageinfo (added)
+++ aries/trunk/async/promise-api/src/main/java/org/osgi/util/promise/packageinfo Thu May  7 15:00:53 2015
@@ -0,0 +1 @@
+version 1.0.0

Added: aries/trunk/async/promise-api/src/test/java/org/apache/aries/async/promise/test/CallbackTest.java
URL: http://svn.apache.org/viewvc/aries/trunk/async/promise-api/src/test/java/org/apache/aries/async/promise/test/CallbackTest.java?rev=1678227&view=auto
==============================================================================
--- aries/trunk/async/promise-api/src/test/java/org/apache/aries/async/promise/test/CallbackTest.java (added)
+++ aries/trunk/async/promise-api/src/test/java/org/apache/aries/async/promise/test/CallbackTest.java Thu May  7 15:00:53 2015
@@ -0,0 +1,70 @@
+package org.apache.aries.async.promise.test;
+
+import org.osgi.util.promise.Deferred;
+import org.osgi.util.promise.Promise;
+import org.junit.Test;
+
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+public class CallbackTest {
+
+    @Test
+    public void testCallbacks() throws Exception {
+        Deferred<String> def = new Deferred<String>();
+        final Promise<String> promise = def.getPromise();
+
+        Callback cb1 = new Callback(promise, "Hello");
+        assertEquals("onResolve returns promise", promise, promise.onResolve(cb1));
+
+        Callback cb2 = new Callback(promise, "Hello");
+        promise.onResolve(cb2);
+
+        def.resolve("Hello");
+
+        assertTrue("callback1 executed", cb1.latch.await(1, TimeUnit.SECONDS));
+        assertEquals("callback1 succeeded", null, cb1.error);
+
+        assertTrue("callback2 executed", cb2.latch.await(1, TimeUnit.SECONDS));
+        assertEquals("callback2 succeeded", null, cb2.error);
+
+        // register callback after promise is resolved
+        Callback cb3 = new Callback(promise, "Hello");
+        promise.onResolve(cb3);
+        assertTrue("callback3 executed", cb3.latch.await(1, TimeUnit.SECONDS));
+        assertEquals("callback3 succeeded", null, cb3.error);
+    }
+
+
+    class Callback implements Runnable {
+        final CountDownLatch latch = new CountDownLatch(1);
+        Throwable error = null;
+
+        private final Promise promise;
+        private final Object value;
+
+        Callback(Promise promise, Object value) {
+            this.promise = promise;
+            this.value = value;
+        }
+
+        @Override
+        public void run() {
+            try {
+                assertTrue("Promise resolved", promise.isDone());
+                assertEquals("Value matches", value, promise.getValue());
+            }
+            catch (Throwable t) {
+                error = t;
+            }
+            finally {
+                latch.countDown();
+            }
+        }
+    }
+
+
+}

Added: aries/trunk/async/promise-api/src/test/java/org/apache/aries/async/promise/test/ChainTest.java
URL: http://svn.apache.org/viewvc/aries/trunk/async/promise-api/src/test/java/org/apache/aries/async/promise/test/ChainTest.java?rev=1678227&view=auto
==============================================================================
--- aries/trunk/async/promise-api/src/test/java/org/apache/aries/async/promise/test/ChainTest.java (added)
+++ aries/trunk/async/promise-api/src/test/java/org/apache/aries/async/promise/test/ChainTest.java Thu May  7 15:00:53 2015
@@ -0,0 +1,254 @@
+package org.apache.aries.async.promise.test;
+
+import org.junit.Test;
+import org.osgi.util.promise.*;
+
+import static org.junit.Assert.*;
+
+public class ChainTest {
+
+    @Test
+    public void testThenSuccess() throws Exception {
+        Deferred<String> def = new Deferred<String>();
+
+        Promise<String> chain = def.getPromise().then(new Success<String, String>() {
+            @Override
+            public Promise<String> call(Promise<String> resolved) throws Exception {
+                return Promises.resolved("success!");
+            }
+        });
+        assertFalse("chain not resolved", chain.isDone());
+
+        def.resolve("ok");
+        assertTrue("chain resolved", chain.isDone());
+        assertEquals("chain value matches", "success!", chain.getValue());
+    }
+
+    @Test
+    public void testThenSuccessDeferred() throws Exception {
+        Deferred<String> def = new Deferred<String>();
+        final Deferred<String> def2 = new Deferred<String>();
+
+        Promise<String> chain = def.getPromise().then(new Success<String, String>() {
+            @Override
+            public Promise<String> call(Promise<String> resolved) throws Exception {
+                return def2.getPromise();
+            }
+        });
+        assertFalse("chain not resolved", chain.isDone());
+
+        def.resolve("ok");
+        assertFalse("chain still not resolved", chain.isDone());
+
+        def2.resolve("success!");
+        assertTrue("chain resolved", chain.isDone());
+        assertEquals("chain value matches", "success!", chain.getValue());
+    }
+
+    @Test
+    public void testThenSuccessFailed() throws Exception {
+        Deferred<String> def = new Deferred<String>();
+        final Promise<String> promise = def.getPromise();
+        final Throwable thenFail = new Throwable("failed!");
+
+        Promise<String> chain = promise.then(new Success<String, String>() {
+            @Override
+            public Promise<String> call(Promise<String> resolved) throws Exception {
+                return Promises.failed(thenFail);
+            }
+        });
+        assertFalse("chain not resolved", chain.isDone());
+
+        def.resolve("ok");
+        assertTrue("chain resolved", chain.isDone());
+        assertEquals("chain failure matches", thenFail, chain.getFailure());
+    }
+
+    @Test
+    public void testThenSuccessException() throws Exception {
+        Deferred<String> def = new Deferred<String>();
+        final Promise<String> promise = def.getPromise();
+        final Exception thenException = new Exception("then exception!");
+
+        Promise<String> chain = promise.then(new Success<String, String>() {
+            @Override
+            public Promise<String> call(Promise<String> resolved) throws Exception {
+                throw thenException;
+            }
+        });
+        assertFalse("chain not resolved", chain.isDone());
+
+        def.resolve("ok");
+        assertTrue("chain resolved", chain.isDone());
+        assertEquals("chain failure matches", thenException, chain.getFailure());
+    }
+
+    @Test
+    public void testThenFail() throws Exception {
+        Deferred<String> def = new Deferred<String>();
+        final Promise<String> promise = def.getPromise();
+
+        Promise<String> chain = promise.then(null, new Failure() {
+            @Override
+            public void fail(Promise<?> resolved) throws Exception {
+            }
+        });
+        assertFalse("chain not resolved", chain.isDone());
+
+        Throwable failure = new Throwable("fail!");
+        def.fail(failure);
+        assertTrue("chain resolved", chain.isDone());
+        assertEquals("chain failure matches", failure, chain.getFailure());
+    }
+
+    @Test
+    public void testThenFailNoCallback() throws Exception {
+        Deferred<String> def = new Deferred<String>();
+        final Promise<String> promise = def.getPromise();
+
+        Promise<String> chain = promise.then(null);
+        assertFalse("chain not resolved", chain.isDone());
+
+        Throwable failure = new Throwable("fail!");
+        def.fail(failure);
+        assertTrue("chain resolved", chain.isDone());
+        assertEquals("chain failure matches", failure, chain.getFailure());
+    }
+
+    @Test
+    public void testThenFailException() throws Exception {
+        Deferred<String> def = new Deferred<String>();
+        final Promise<String> promise = def.getPromise();
+        final Exception thenException = new Exception("eek!");
+
+        Promise<String> chain = promise.then(null, new Failure() {
+            @Override
+            public void fail(Promise<?> resolved) throws Exception {
+                throw thenException;
+            }
+        });
+        assertFalse("chain not resolved", chain.isDone());
+
+        def.fail(new Throwable("failed"));
+        assertTrue("chain resolved", chain.isDone());
+        assertEquals("chain failure matches", thenException, chain.getFailure());
+    }
+
+    @Test
+    public void testThenNull() throws Exception {
+        Deferred<String> def = new Deferred<String>();
+        final Promise<String> promise = def.getPromise();
+        Promise<String> chain = promise.then(null);
+        assertFalse("chain not resolved", chain.isDone());
+
+        def.resolve("ok");
+        assertTrue("chain resolved", chain.isDone());
+        assertNull("chain value null", chain.getValue());
+    }
+
+    @Test
+    public void testThenNullResolved() throws Exception {
+        Deferred<String> def = new Deferred<String>();
+        def.resolve("ok");
+        Promise<String> chain = def.getPromise().then(null);
+
+        assertTrue("chain resolved", chain.isDone());
+        assertNull("chain value null", chain.getValue());
+    }
+
+    @Test
+    public void testThenAlreadyResolved() throws Exception {
+        Deferred<String> def = new Deferred<String>();
+        final Promise<String> promise = def.getPromise();
+        def.resolve("ok");
+
+        Promise<String> chain = promise.then(new Success<String, String>() {
+            @Override
+            public Promise<String> call(Promise<String> resolved) throws Exception {
+                return Promises.resolved("success!");
+            }
+        });
+        assertTrue("chain resolved", chain.isDone());
+        assertEquals("chain value matches", "success!", chain.getValue());
+    }
+
+    @Test
+    public void testExampleChain() throws Exception {
+        Success<String, String> doubler = new Success<String, String>() {
+            public Promise<String> call(Promise<String> p) throws Exception {
+                return Promises.resolved(p.getValue() + p.getValue());
+            }
+        };
+
+        Deferred<String> def = new Deferred<String>();
+        final Promise<String> foo = def.getPromise().then(doubler).then(doubler);
+
+        def.resolve("hello!");
+        assertEquals("doubler matches", "hello!hello!hello!hello!", foo.getValue());
+    }
+
+    @Test
+    public void testThen2() throws Exception {
+        Deferred<String> def = new Deferred<String>();
+
+        Promise<String> chain1 = def.getPromise().then(new Success<String, String>() {
+            @Override
+            public Promise<String> call(Promise<String> resolved) throws Exception {
+                return Promises.resolved("success1");
+            }
+        });
+        assertFalse("chain not resolved", chain1.isDone());
+
+        Promise<String> chain2 = def.getPromise().then(new Success<String, String>() {
+            @Override
+            public Promise<String> call(Promise<String> resolved) throws Exception {
+                return Promises.resolved("success2");
+            }
+        });
+        assertFalse("chain not resolved", chain2.isDone());
+
+        def.resolve("ok");
+        assertTrue("chain1 resolved", chain1.isDone());
+        assertEquals("chain1 value matches", "success1", chain1.getValue());
+        assertTrue("chain2 resolved", chain2.isDone());
+        assertEquals("chain2 value matches", "success2", chain2.getValue());
+    }
+
+    @Test
+    public void testThenResolved() throws Exception {
+        Deferred<String> def = new Deferred<String>();
+        def.resolve("already resolved");
+
+        Promise<String> chain = def.getPromise().then(new Success<String, String>() {
+            @Override
+            public Promise<String> call(Promise<String> resolved) throws Exception {
+                return Promises.resolved("success!");
+            }
+        });
+        assertTrue("chain resolved", chain.isDone());
+
+        assertEquals("chain value matches", "success!", chain.getValue());
+    }
+
+    @Test
+    public void testThenResolved2() throws Exception {
+        Deferred<String> def = new Deferred<String>();
+        def.resolve("already resolved");
+
+        Promise<String> chain = def.getPromise().then(new Success<String, String>() {
+            @Override
+            public Promise<String> call(Promise<String> resolved) throws Exception {
+                return Promises.resolved("success1");
+            }
+        }).then(new Success<String, String>() {
+            @Override
+            public Promise<String> call(Promise<String> resolved) throws Exception {
+                return Promises.resolved("success2");
+            }
+        });
+
+        assertTrue("chain resolved", chain.isDone());
+
+        assertEquals("chain value matches", "success2", chain.getValue());
+    }
+}

Added: aries/trunk/async/promise-api/src/test/java/org/apache/aries/async/promise/test/DeferredTest.java
URL: http://svn.apache.org/viewvc/aries/trunk/async/promise-api/src/test/java/org/apache/aries/async/promise/test/DeferredTest.java?rev=1678227&view=auto
==============================================================================
--- aries/trunk/async/promise-api/src/test/java/org/apache/aries/async/promise/test/DeferredTest.java (added)
+++ aries/trunk/async/promise-api/src/test/java/org/apache/aries/async/promise/test/DeferredTest.java Thu May  7 15:00:53 2015
@@ -0,0 +1,128 @@
+package org.apache.aries.async.promise.test;
+
+import org.osgi.util.promise.Deferred;
+import org.osgi.util.promise.Promise;
+import org.junit.Test;
+
+import java.lang.reflect.InvocationTargetException;
+
+import static org.junit.Assert.*;
+
+public class DeferredTest {
+
+    @Test
+    public void testResolve() throws Exception {
+        Deferred<String> def = new Deferred<String>();
+        Promise<String> promise = def.getPromise();
+        assertFalse("Initial Promise not resolved", promise.isDone());
+
+        def.resolve("Hello");
+        assertTrue("Promise resolved", promise.isDone());
+        assertEquals("Value matches", "Hello", promise.getValue());
+        assertNull("Failure is null", promise.getFailure());
+
+        try {
+            def.resolve("Again");
+            fail("Already resolved didn't throw IllegalStateException");
+        } catch (IllegalStateException e) {
+            // suppress empty catch block warning
+        }
+    }
+
+    @Test
+    public void testResolveWithSuccess() throws Exception {
+        Deferred<String> def = new Deferred<String>();
+        Promise<String> promise = def.getPromise();
+
+        Deferred<String> with = new Deferred<String>();
+        Promise<Void> resolvedWith = def.resolveWith(with.getPromise());
+
+        // If the specified Promise is successfully resolved,
+        // the associated Promise is resolved with the value of the specified Promise.
+        with.resolve("resolveWith");
+        assertTrue("Promise resolved", promise.isDone());
+        assertEquals("Value matches", "resolveWith", promise.getValue());
+
+        // The returned Promise will be successfully resolved, with the value null,
+        // if the associated Promise was resolved by the specified Promise.
+        assertNull("resolveWith null", resolvedWith.getValue());
+    }
+
+    @Test
+    public void testResolveWithAlreadyResolved() throws Exception {
+        Deferred<String> def = new Deferred<String>();
+        Deferred<String> with = new Deferred<String>();
+        Promise<Void> resolvedWith = def.resolveWith(with.getPromise());
+
+        // The returned Promise will be resolved with a failure of IllegalStateException
+        // if the associated Promise was already resolved when the specified Promise was resolved.
+        def.resolve("Already resolved");
+        with.resolve("resolveWith");
+        @SuppressWarnings({"not thrown", "all"})
+        Throwable failure = resolvedWith.getFailure();
+        assertTrue("resolveWith IllegalStateException", failure instanceof IllegalStateException);
+    }
+
+    @Test
+    public void testResolveWithAlreadyFailed() throws Exception {
+        Deferred<String> def = new Deferred<String>();
+        Deferred<String> with = new Deferred<String>();
+        Promise<Void> resolvedWith = def.resolveWith(with.getPromise());
+
+        // The returned Promise will be resolved with a failure of IllegalStateException
+        // if the associated Promise was already resolved when the specified Promise was resolved.
+        def.resolve("Already resolved");
+        with.fail(new Throwable("failed"));
+        @SuppressWarnings({"not thrown", "all"})
+        Throwable failure = resolvedWith.getFailure();
+        assertTrue("resolveWith IllegalStateException", failure instanceof IllegalStateException);
+    }
+
+    @Test
+    public void testResolveWithFailure() throws Exception {
+        Deferred<String> def = new Deferred<String>();
+        Promise<String> promise = def.getPromise();
+
+        Deferred<String> def2 = new Deferred<String>();
+        Promise<String> promise2 = def2.getPromise();
+        Promise<Void> with = def.resolveWith(promise2);
+
+        // If the specified Promise is resolved with a failure,
+        // the associated Promise is resolved with the failure of the specified Promise.
+        Exception failure = new Exception("resolveWithFailure");
+        def2.fail(failure);
+        assertTrue("Promise resolved", promise.isDone());
+        assertEquals("Failure matches", failure, promise.getFailure());
+
+        // The returned Promise will be successfully resolved, with the value null,
+        // if the associated Promise was resolved by the specified Promise.
+        assertNull("resolveWith null", with.getValue());
+    }
+
+    @Test
+    public void testFail() throws Exception {
+        Deferred<String> def = new Deferred<String>();
+        Promise<String> promise = def.getPromise();
+        Exception failure = new Exception("Oops");
+
+        def.fail(failure);
+        assertTrue("Promise resolved", promise.isDone());
+        assertEquals("Failure matches", failure, promise.getFailure());
+
+        try {
+            promise.getValue();
+            fail("getValue didn't throw InvocationTargetException");
+        } catch (InvocationTargetException e) {
+            assertEquals("Failure matches", failure, e.getCause());
+        }
+
+        try {
+            def.fail(failure);
+            fail("Already failed didn't throw IllegalStateException");
+        } catch (IllegalStateException e) {
+            assertNotNull(e);
+        }
+    }
+
+
+}



Mime
View raw message