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