samples/messagedigest/src-bridge/org/apidesign/impl/security/extension/BridgeToNew.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 14 Jun 2008 09:58:08 +0200
changeset 153 b5cbb797ec0a
parent 132 3bc4c54f4bcc
child 154 0fd5e9c500b9
permissions -rw-r--r--
up to line 2000
     1 /*
     2  * To change this template, choose Tools | Templates
     3  * and open the template in the editor.
     4  */
     5 
     6 package org.apidesign.impl.security.extension;
     7 
     8 import java.nio.ByteBuffer;
     9 import java.security.MessageDigest;
    10 import java.security.NoSuchAlgorithmException;
    11 import java.util.logging.Level;
    12 import java.util.logging.Logger;
    13 import org.apidesign.spi.security.Digestor;
    14 
    15 /**
    16  *
    17  * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
    18  */
    19 // BEGIN: day.end.bridges.BridgeToNew
    20 public class BridgeToNew extends Digestor<MessageDigest> {
    21     /** initializes the other bridge, and allow us to eliminate stack overflow */
    22     private static final BridgeToOld oldBridge = new BridgeToOld();
    23     
    24     @Override
    25     protected MessageDigest create(String algorithm) {
    26         // BEGIN: day.end.bridges.cyclecheck
    27         if (oldBridge.isSearching()) {
    28             // if the call is initiated from the other bridge, do not do
    29             // any delegation
    30             return null;
    31         }
    32         // END: day.end.bridges.cyclecheck
    33         
    34         try {
    35             return MessageDigest.getInstance(algorithm);
    36         } catch (NoSuchAlgorithmException ex) {
    37             Logger.getLogger(BridgeToNew.class.getName()).log(Level.FINE, "Cannot find " + algorithm, ex);
    38             return null;
    39         }
    40     }
    41 
    42     @Override
    43     protected byte[] digest(MessageDigest data) {
    44         return data.digest();
    45     }
    46 
    47     @Override
    48     protected void update(MessageDigest data, ByteBuffer input) {
    49         data.update(input);
    50     }
    51     
    52     static {
    53         new BridgeToOld();
    54     }
    55 }
    56 // END: day.end.bridges.BridgeToNew