samples/messagedigest/src-new-api/org/apidesign/impl/DigestorAccessor.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 14 Jun 2008 09:52:25 +0200
changeset 47 f464a16d553a
permissions -rw-r--r--
Simplified to does not contain the friend API, instead the SPI is directly define by the API
     1 /*
     2  * To change this template, choose Tools | Templates
     3  * and open the template in the editor.
     4  */
     5 
     6 package org.apidesign.impl;
     7 
     8 import java.nio.ByteBuffer;
     9 import org.apidesign.spi.security.Digestor;
    10 
    11 /**
    12  *
    13  * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
    14  */
    15 public abstract class DigestorAccessor {
    16     private static DigestorAccessor INSTANCE;
    17     
    18     protected DigestorAccessor() {
    19         if (this.getClass().getName().equals("org.apidesign.spi.security.DigestorAccessorImpl")) {
    20             assert INSTANCE == null;
    21             INSTANCE = this;
    22             return;
    23         }
    24         if (this.getClass().getName().equals("org.apidesign.api.security.DigestorAccessorImpl")) {
    25             return;
    26         }
    27         throw new IllegalStateException();
    28     }
    29     
    30     final DigestorAccessor getDefault() {
    31         try {
    32             Class.forName(Digestor.class.getName(), true, DigestorAccessor.class.getClassLoader());
    33             return INSTANCE;
    34         } catch (ClassNotFoundException ex) {
    35             throw new IllegalStateException(ex);
    36         }
    37     }
    38     
    39     protected abstract <Data> byte[] digest(Digestor<Data> dig, Data data);
    40     protected abstract <Data> Data create(Digestor<Data> dig, String algorithm); 
    41     protected abstract <Data> void update(Digestor<Data> dig, Data data, ByteBuffer input);
    42     
    43     
    44     protected final <Data> byte[] defaultDigest(Digestor<Data> dig, Data data) {
    45         return getDefault().digest(dig, data);
    46     }
    47     protected final <Data> Data defaultCreate(Digestor<Data> dig, String algorithm) {
    48         return getDefault().create(dig, algorithm);
    49     }
    50     protected final <Data> void defaultUpdate(Digestor<Data> dig, Data data, ByteBuffer input) {
    51         getDefault().update(dig, data, input);
    52     }
    53 }