Return-Path: X-Original-To: apmail-abdera-user-archive@www.apache.org Delivered-To: apmail-abdera-user-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 7D42B9B1D for ; Sat, 31 Dec 2011 00:26:02 +0000 (UTC) Received: (qmail 60384 invoked by uid 500); 31 Dec 2011 00:26:02 -0000 Delivered-To: apmail-abdera-user-archive@abdera.apache.org Received: (qmail 60365 invoked by uid 500); 31 Dec 2011 00:26:02 -0000 Mailing-List: contact user-help@abdera.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: user@abdera.apache.org Delivered-To: mailing list user@abdera.apache.org Received: (qmail 60357 invoked by uid 99); 31 Dec 2011 00:26:02 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 31 Dec 2011 00:26:02 +0000 X-ASF-Spam-Status: No, hits=1.5 required=5.0 tests=HTML_MESSAGE,RCVD_IN_DNSWL_LOW,SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (athena.apache.org: domain of chad.lung@gmail.com designates 209.85.215.170 as permitted sender) Received: from [209.85.215.170] (HELO mail-ey0-f170.google.com) (209.85.215.170) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 31 Dec 2011 00:25:55 +0000 Received: by eaa13 with SMTP id 13so3735683eaa.15 for ; Fri, 30 Dec 2011 16:25:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=CvUgHxkX5hWQrWZ45Fs03E7ihs21FCmEVQMhAmeYWxc=; b=ju+Pla9Lkl8jSC5kSPqPe8/WAXyJAs8pPN2CSdCTvG0sS5ql6PMgAVG4JAkB6p389x uuVXNJXZRrgCpqnqMPhjACjarP/JN/bFaUKEXnovLJjiBSfu851i4vpTiQmU9GSM8YX5 pmEoNiQxTuTVjPB7D3txkNvrDjg4ofFHR0FHs= MIME-Version: 1.0 Received: by 10.205.125.10 with SMTP id gq10mr9545901bkc.11.1325291134342; Fri, 30 Dec 2011 16:25:34 -0800 (PST) Received: by 10.204.175.73 with HTTP; Fri, 30 Dec 2011 16:25:34 -0800 (PST) In-Reply-To: References: Date: Fri, 30 Dec 2011 18:25:34 -0600 Message-ID: Subject: Re: Non-blocking operations in Abdera2... From: Chad Lung To: user@abdera.apache.org Content-Type: multipart/alternative; boundary=000e0ce0b5b449585a04b558631e --000e0ce0b5b449585a04b558631e Content-Type: text/plain; charset=ISO-8859-1 Wow, this is some powerful stuff! Thanks for the post James. Chad On Fri, Dec 30, 2011 at 5:24 PM, James Snell wrote: > I've been going through an updating the documentation for Abdera2 to > highlight the various new features. As part of that effort, I've been > playing around with a few of the new capabilities. One of the > particularly interesting ones is that the integration with the Guava > Libraries Function and Concurrency utilities allows applications built > with Abdera2 to leverage a variety of non-blocking mechanisms. For > instance, it's now possible to encrypt, decrypt, digitally sign and > verify signatures on Atom documents without blocking the current > thread of execution. Here's a quick example... > > First, we need to prepare the encryption provider (we use Bouncy > Castle by default) > > > KeyHelper.prepareDefaultJceProvider(); > > Now the cipher key... > String jceAlgorithmName = "AES"; > KeyGenerator keyGenerator = > KeyGenerator.getInstance(jceAlgorithmName); > keyGenerator.init(128); > SecretKey key = keyGenerator.generateKey(); > > Create the entry to be encrypted... > Abdera abdera = Abdera.getInstance(); > Entry entry = abdera.newEntry(); > entry.setId("http://example.org/foo/entry"); > entry.setUpdatedNow(); > entry.setTitle("This is an entry"); > entry.setContentAsXhtml("This is markup"); > entry.addAuthor("James"); > entry.addLink("http://www.example.org"); > > Here's where it starts to get fun... The security api has been > revamped and simplified in Abdera2... > > Security absec = new Security(abdera); > > The EncryptionOptions class is now immutable and threadsafe using the > new common factory pattern... > EncryptionOptions options = > absec.getEncryption().getDefaultEncryptionOptions() > .dataEncryptionKey(key).get(); > > Abdera2 uses the Guava Library to provide a Function object that wraps > the encryption logic, we can then in turn wrap that function with a > "Future Function", an Abdera2 concept that allows a Guava Function to > be be executed within a separate thread. All we need to do is pass in > an ExecutorService instance... > > ExecutorService exec = MoreExecutors2.getExitingExecutor(); > > Function,Future>> ff = > MoreFunctions.futureFunction( > absec.encryptor(options), > exec); > > Now we can call our non-blocking encryption function... > Future> future = > ff.apply(entry.getDocument()); > > The Future returned by the function is an instance of the Guava > ListenableFuture interface, allowing us to attach a listener that will > wait for the completion of the encryption operation without blocking > the current thread... > > com.google.common.util.concurrent.Futures.addCallback( > (ListenableFuture>) future, > new FutureCallback>() { > public void onSuccess(Document result) { > try { > System.out.println("The results:"); > result.writeTo(System.out); > } catch (Throwable t) { > t.printStackTrace(); > } > } > public void onFailure(Throwable t) { > t.printStackTrace(); > } > },exec); > > And that's it... non-blocking encryption of an Atom document. There > are ways that I can compose that together with the digital signature > capability to perform signing and encrypting in a single non-blocking > operation. > > This is good stuff :-) > --000e0ce0b5b449585a04b558631e--