samples/messagedigest/src-bridge/org/apidesign/impl/security/extension/BridgeToOld.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 14 Jun 2008 09:56:12 +0200
changeset 132 3bc4c54f4bcc
parent 51 130e099942d8
child 133 50bf1b976c0d
permissions -rw-r--r--
Truncating all examples to 80 characters per line
jtulach@44
     1
package org.apidesign.impl.security.extension;
jtulach@44
     2
jtulach@44
     3
import org.apidesign.api.security.Digest;
jtulach@44
     4
import java.nio.ByteBuffer;
jtulach@44
     5
import java.security.MessageDigest;
jtulach@44
     6
import java.security.NoSuchAlgorithmException;
jtulach@44
     7
import java.security.Provider;
jtulach@44
     8
import java.security.Security;
jtulach@44
     9
import java.util.Collections;
jtulach@44
    10
import java.util.List;
jtulach@44
    11
import java.util.Map;
jtulach@44
    12
jtulach@44
    13
/**
jtulach@44
    14
 *
jtulach@44
    15
 * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jtulach@44
    16
 */
jtulach@51
    17
// BEGIN: day.end.bridges.BridgeToOld
jtulach@51
    18
public final class BridgeToOld extends Provider {
jtulach@44
    19
jtulach@44
    20
    public BridgeToOld() {
jtulach@44
    21
        super("spi.Digestor", 1.0, "");
jtulach@44
    22
        Security.addProvider(this);
jtulach@44
    23
    }
jtulach@51
    24
jtulach@51
    25
    // BEGIN: day.end.bridges.cycle
jtulach@51
    26
    private ThreadLocal<Boolean> searching = new ThreadLocal<Boolean>();
jtulach@51
    27
    final boolean isSearching() {
jtulach@51
    28
        return Boolean.TRUE.equals(searching.get());
jtulach@51
    29
    }
jtulach@44
    30
    
jtulach@44
    31
    @Override
jtulach@44
    32
    public synchronized Service getService(String type, String algorithm) {
jtulach@51
    33
        Boolean prev = searching.get();
jtulach@51
    34
        try {
jtulach@51
    35
            searching.set(Boolean.TRUE);
jtulach@51
    36
            if ("MessageDigest".equals(type)) {
jtulach@51
    37
                Digest dig = Digest.getInstance(algorithm);
jtulach@51
    38
                if (dig != null) {
jtulach@132
    39
                    return new ServiceImpl(
jtulach@132
    40
                        dig, this, type, algorithm, "", 
jtulach@132
    41
                        Collections.<String>emptyList(), 
jtulach@132
    42
                        Collections.<String,String>emptyMap());
jtulach@51
    43
                }
jtulach@44
    44
            }
jtulach@51
    45
            return null;
jtulach@51
    46
        } finally {
jtulach@51
    47
            searching.set(prev);
jtulach@44
    48
        }
jtulach@44
    49
    }
jtulach@51
    50
    // END: day.end.bridges.cycle
jtulach@44
    51
jtulach@44
    52
    private static class ServiceImpl<Data> extends Service {
jtulach@44
    53
        Digest dig;
jtulach@44
    54
jtulach@132
    55
        public ServiceImpl(Digest dig, Provider provider, 
jtulach@132
    56
            String type, String algorithm, String className, 
jtulach@132
    57
            List<String> aliases, Map<String, String> attributes
jtulach@132
    58
        ) {
jtulach@44
    59
            super(provider, type, algorithm, className, aliases, attributes);
jtulach@44
    60
            this.dig = dig;
jtulach@44
    61
        }
jtulach@44
    62
jtulach@44
    63
        @Override
jtulach@132
    64
        public Object newInstance(Object constructorParameter) 
jtulach@132
    65
        throws NoSuchAlgorithmException {
jtulach@44
    66
            return new MessageDigest(getAlgorithm()) {
jtulach@44
    67
                private byte[] res;
jtulach@44
    68
                
jtulach@44
    69
                @Override
jtulach@44
    70
                protected void engineUpdate(byte input) {
jtulach@44
    71
                    ByteBuffer bb = ByteBuffer.wrap(new byte[] { input });
jtulach@44
    72
                    res = dig.digest(bb);
jtulach@44
    73
                }
jtulach@44
    74
jtulach@44
    75
                @Override
jtulach@132
    76
                protected void engineUpdate(
jtulach@132
    77
                    byte[] input, int offset, int len
jtulach@132
    78
                ) {
jtulach@44
    79
                    ByteBuffer bb = ByteBuffer.wrap(input);
jtulach@44
    80
                    bb.position(offset);
jtulach@44
    81
                    bb.limit(offset + len);
jtulach@44
    82
                    res = dig.digest(bb);
jtulach@44
    83
                }
jtulach@44
    84
jtulach@44
    85
                @Override
jtulach@44
    86
                protected byte[] engineDigest() {
jtulach@44
    87
                    return res;
jtulach@44
    88
                }
jtulach@44
    89
jtulach@44
    90
                @Override
jtulach@44
    91
                protected void engineReset() {
jtulach@44
    92
                    dig = Digest.getInstance(getAlgorithm());
jtulach@44
    93
                }
jtulach@44
    94
            };
jtulach@44
    95
        }
jtulach@44
    96
        
jtulach@44
    97
        
jtulach@44
    98
    }
jtulach@44
    99
jtulach@44
   100
}
jtulach@51
   101
// END: day.end.bridges.BridgeToOld