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