Simplified to does not contain the friend API, instead the SPI is directly define by the API
authorJaroslav Tulach <jtulach@netbeans.org>
Sat, 14 Jun 2008 09:52:25 +0200
changeset 47f464a16d553a
parent 46 c75861f07646
child 48 c5742322dbc8
Simplified to does not contain the friend API, instead the SPI is directly define by the API
samples/messagedigest/build.xml
samples/messagedigest/nbproject/project.xml
samples/messagedigest/src-bridge/org/apidesign/impl/security/extension/BridgeToNew.java
samples/messagedigest/src-new-api/org/apidesign/api/security/Digest.java
samples/messagedigest/src-new-api/org/apidesign/api/security/DigestImplementation.java
samples/messagedigest/src-new-api/org/apidesign/api/security/DigestorAccessorImpl.java
samples/messagedigest/src-new-api/org/apidesign/impl/DigestorAccessor.java
samples/messagedigest/src-new-api/org/apidesign/spi/security/Digestor.java
samples/messagedigest/src-new-api/org/apidesign/spi/security/DigestorAccessorImpl.java
     1.1 --- a/samples/messagedigest/build.xml	Sat Jun 14 09:52:23 2008 +0200
     1.2 +++ b/samples/messagedigest/build.xml	Sat Jun 14 09:52:25 2008 +0200
     1.3 @@ -9,28 +9,24 @@
     1.4              <param name="version" value="new-api"/>
     1.5          </antcall>
     1.6          <antcall target="-build-one">
     1.7 -            <param name="version" value="new-spi"/>
     1.8 +            <param name="version" value="bridge"/>
     1.9              <param name="cp" value="build/new-api/classes"/>
    1.10          </antcall>
    1.11 -        <antcall target="-build-one">
    1.12 -            <param name="version" value="bridge"/>
    1.13 -            <param name="cp" value="build/new-api/classes:build/new-spi/classes"/>
    1.14 -        </antcall>
    1.15          
    1.16          <antcall target="-build-one">
    1.17              <param name="version" value="test"/>
    1.18 -            <param name="cp" value="build/new-api/classes:build/new-spi/classes:${junit.jar}"/>
    1.19 +            <param name="cp" value="build/new-api/classes:${junit.jar}"/>
    1.20          </antcall>
    1.21      </target>
    1.22      
    1.23      <target name="test" depends="build">
    1.24          <echo level="info" message="Running the tests with bridge enabled. This should succeeds."/>
    1.25          <antcall target="-run-one">
    1.26 -            <param name="test.cp" value="build/new-api/classes:build/new-spi/classes:build/bridge/classes"/>
    1.27 +            <param name="test.cp" value="build/new-api/classes:build/bridge/classes"/>
    1.28          </antcall>
    1.29          <echo level="info" message="Running the tests without bridge module. This should fail."/>
    1.30          <antcall target="-run-one">
    1.31 -            <param name="test.cp" value="build/new-api/classes:build/new-spi/classes"/>
    1.32 +            <param name="test.cp" value="build/new-api/classes"/>
    1.33          </antcall>
    1.34      </target>
    1.35      
     2.1 --- a/samples/messagedigest/nbproject/project.xml	Sat Jun 14 09:52:23 2008 +0200
     2.2 +++ b/samples/messagedigest/nbproject/project.xml	Sat Jun 14 09:52:25 2008 +0200
     2.3 @@ -3,7 +3,7 @@
     2.4      <type>org.netbeans.modules.ant.freeform</type>
     2.5      <configuration>
     2.6          <general-data xmlns="http://www.netbeans.org/ns/freeform-project/1">
     2.7 -            <name>bridges</name>
     2.8 +            <name>MessageDigest and Bridges</name>
     2.9          </general-data>
    2.10          <general-data xmlns="http://www.netbeans.org/ns/freeform-project/2">
    2.11              <!-- Do not use Project Properties customizer when editing this file manually. -->
    2.12 @@ -17,12 +17,6 @@
    2.13                      <encoding>UTF-8</encoding>
    2.14                  </source-folder>
    2.15                  <source-folder>
    2.16 -                    <label>src-new-spi</label>
    2.17 -                    <type>java</type>
    2.18 -                    <location>src-new-spi</location>
    2.19 -                    <encoding>UTF-8</encoding>
    2.20 -                </source-folder>
    2.21 -                <source-folder>
    2.22                      <label>src-bridge</label>
    2.23                      <type>java</type>
    2.24                      <location>src-bridge</location>
    2.25 @@ -34,6 +28,11 @@
    2.26                      <location>src-test</location>
    2.27                      <encoding>UTF-8</encoding>
    2.28                  </source-folder>
    2.29 +                <source-folder>
    2.30 +                    <label>MessageDigest and Bridges</label>
    2.31 +                    <location>.</location>
    2.32 +                    <encoding>UTF-8</encoding>
    2.33 +                </source-folder>
    2.34              </folders>
    2.35              <ide-actions>
    2.36                  <action name="build">
    2.37 @@ -50,22 +49,33 @@
    2.38                      <target>build</target>
    2.39                  </action>
    2.40              </ide-actions>
    2.41 +            <export>
    2.42 +                <type>folder</type>
    2.43 +                <location>build/bridge/classes</location>
    2.44 +                <build-target>build</build-target>
    2.45 +            </export>
    2.46 +            <export>
    2.47 +                <type>folder</type>
    2.48 +                <location>build/new-api/classes</location>
    2.49 +                <build-target>build</build-target>
    2.50 +            </export>
    2.51 +            <export>
    2.52 +                <type>folder</type>
    2.53 +                <location>build/test/classes</location>
    2.54 +                <build-target>build</build-target>
    2.55 +            </export>
    2.56              <view>
    2.57                  <items>
    2.58                      <source-folder style="packages">
    2.59 -                        <label>New API for Clients</label>
    2.60 +                        <label>src-new-api</label>
    2.61                          <location>src-new-api</location>
    2.62                      </source-folder>
    2.63                      <source-folder style="packages">
    2.64 -                        <label>New API for Providers</label>
    2.65 -                        <location>src-new-spi</location>
    2.66 -                    </source-folder>
    2.67 -                    <source-folder style="packages">
    2.68 -                        <label>Bridge</label>
    2.69 +                        <label>src-bridge</label>
    2.70                          <location>src-bridge</location>
    2.71                      </source-folder>
    2.72                      <source-folder style="packages">
    2.73 -                        <label>Tests</label>
    2.74 +                        <label>test</label>
    2.75                          <location>src-test</location>
    2.76                      </source-folder>
    2.77                      <source-file>
    2.78 @@ -79,25 +89,24 @@
    2.79                      <ide-action name="test"/>
    2.80                  </context-menu>
    2.81              </view>
    2.82 +            <subprojects/>
    2.83          </general-data>
    2.84          <java-data xmlns="http://www.netbeans.org/ns/freeform-project-java/1">
    2.85              <compilation-unit>
    2.86                  <package-root>src-new-api</package-root>
    2.87 +                <built-to>build/bridge/classes</built-to>
    2.88 +                <built-to>build/new-api/classes</built-to>
    2.89 +                <built-to>build/test/classes</built-to>
    2.90                  <source-level>1.5</source-level>
    2.91              </compilation-unit>
    2.92              <compilation-unit>
    2.93 -                <package-root>src-new-spi</package-root>
    2.94 +                <package-root>src-bridge</package-root>
    2.95                  <classpath mode="compile">src-new-api</classpath>
    2.96                  <source-level>1.5</source-level>
    2.97              </compilation-unit>
    2.98              <compilation-unit>
    2.99 -                <package-root>src-bridge</package-root>
   2.100 -                <classpath mode="compile">src-new-api:src-new-spi</classpath>
   2.101 -                <source-level>1.5</source-level>
   2.102 -            </compilation-unit>
   2.103 -            <compilation-unit>
   2.104                  <package-root>src-test</package-root>
   2.105 -                <classpath mode="compile">src-new-api:src-new-spi:../libs/dist/junit-4.4.jar</classpath>
   2.106 +                <classpath mode="compile">src-new-api:../libs/dist/junit-4.4.jar</classpath>
   2.107                  <source-level>1.5</source-level>
   2.108              </compilation-unit>
   2.109          </java-data>
     3.1 --- a/samples/messagedigest/src-bridge/org/apidesign/impl/security/extension/BridgeToNew.java	Sat Jun 14 09:52:23 2008 +0200
     3.2 +++ b/samples/messagedigest/src-bridge/org/apidesign/impl/security/extension/BridgeToNew.java	Sat Jun 14 09:52:25 2008 +0200
     3.3 @@ -5,40 +5,37 @@
     3.4  
     3.5  package org.apidesign.impl.security.extension;
     3.6  
     3.7 -import org.apidesign.impl.security.friendapi.DigestImplementation;
     3.8 -import org.apidesign.impl.security.friendapi.DigestProvider;
     3.9  import java.nio.ByteBuffer;
    3.10  import java.security.MessageDigest;
    3.11  import java.security.NoSuchAlgorithmException;
    3.12  import java.util.logging.Level;
    3.13  import java.util.logging.Logger;
    3.14 +import org.apidesign.spi.security.Digestor;
    3.15  
    3.16  /**
    3.17   *
    3.18   * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
    3.19   */
    3.20  // BEGIN: day.end.bridges.BridgeToNew
    3.21 -public class BridgeToNew implements DigestProvider {
    3.22 -
    3.23 -    public DigestImplementation create(String algorithm) {
    3.24 +public class BridgeToNew extends Digestor<MessageDigest> {
    3.25 +    @Override
    3.26 +    protected MessageDigest create(String algorithm) {
    3.27          try {
    3.28 -            final MessageDigest md = MessageDigest.getInstance(algorithm);
    3.29 -            return new DigestImplementation(algorithm) {
    3.30 -
    3.31 -                @Override
    3.32 -                public void update(ByteBuffer bb) {
    3.33 -                    md.update(bb);
    3.34 -                }
    3.35 -
    3.36 -                @Override
    3.37 -                public byte[] digest() {
    3.38 -                    return md.digest();
    3.39 -                }
    3.40 -            };
    3.41 +            return MessageDigest.getInstance(algorithm);
    3.42          } catch (NoSuchAlgorithmException ex) {
    3.43 -            Logger.getLogger(BridgeToNew.class.getName()).log(Level.INFO, null, ex);
    3.44 +            Logger.getLogger(BridgeToNew.class.getName()).log(Level.FINE, "Cannot find " + algorithm, ex);
    3.45              return null;
    3.46          }
    3.47      }
    3.48 +
    3.49 +    @Override
    3.50 +    protected byte[] digest(MessageDigest data) {
    3.51 +        return data.digest();
    3.52 +    }
    3.53 +
    3.54 +    @Override
    3.55 +    protected void update(MessageDigest data, ByteBuffer input) {
    3.56 +        data.update(input);
    3.57 +    }
    3.58  }
    3.59  // END: day.end.bridges.BridgeToNew
     4.1 --- a/samples/messagedigest/src-new-api/org/apidesign/api/security/Digest.java	Sat Jun 14 09:52:23 2008 +0200
     4.2 +++ b/samples/messagedigest/src-new-api/org/apidesign/api/security/Digest.java	Sat Jun 14 09:52:25 2008 +0200
     4.3 @@ -2,28 +2,28 @@
     4.4  package org.apidesign.api.security;
     4.5  
     4.6  import java.nio.ByteBuffer;
     4.7 -import org.apidesign.impl.security.friendapi.DigestImplementation;
     4.8 -import org.apidesign.impl.security.friendapi.DigestProvider;
     4.9  import java.util.ServiceLoader;
    4.10 +import org.apidesign.spi.security.Digestor;
    4.11  
    4.12 -/** MessageDigest extends MessageDigestSpi, that means the javadoc
    4.13 +/** Simplified version of a Digest class that allows to compute a fingerprint
    4.14 + * for buffer of data.
    4.15   *
    4.16 - * @author Jaroslav Tulach
    4.17 + * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
    4.18   */
    4.19  // BEGIN: day.end.bridges.Digest
    4.20  public final class Digest {
    4.21 -    private final DigestImplementation impl;
    4.22 +    private final DigestImplementation<?> impl;
    4.23      
    4.24      /** Factory method is better than constructor */
    4.25 -    private Digest(DigestImplementation impl) {
    4.26 +    private Digest(DigestImplementation<?> impl) {
    4.27          this.impl = impl;
    4.28      }
    4.29      
    4.30      /** Factory method to create digest for an algorithm.
    4.31       */
    4.32      public static Digest getInstance(String algorithm) {
    4.33 -        for (DigestProvider dp : ServiceLoader.load(DigestProvider.class)) {
    4.34 -            DigestImplementation impl = dp.create(algorithm);
    4.35 +        for (Digestor<?> digestor : ServiceLoader.load(Digestor.class)) {
    4.36 +            DigestImplementation<?> impl = DigestImplementation.create(digestor, algorithm);
    4.37              if (impl != null) {
    4.38                  return new Digest(impl);
    4.39              }
    4.40 @@ -37,8 +37,7 @@
    4.41      //
    4.42      
    4.43      public byte[] digest(ByteBuffer bb) {
    4.44 -        impl.update(bb);
    4.45 -        return impl.digest();
    4.46 +        return impl.digest(bb);
    4.47      }
    4.48  }
    4.49  // END: day.end.bridges.Digest
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/samples/messagedigest/src-new-api/org/apidesign/api/security/DigestImplementation.java	Sat Jun 14 09:52:25 2008 +0200
     5.3 @@ -0,0 +1,41 @@
     5.4 +/*
     5.5 + * To change this template, choose Tools | Templates
     5.6 + * and open the template in the editor.
     5.7 + */
     5.8 +
     5.9 +package org.apidesign.api.security;
    5.10 +
    5.11 +import java.nio.ByteBuffer;
    5.12 +import org.apidesign.spi.security.Digestor;
    5.13 +
    5.14 +/**
    5.15 + *
    5.16 + * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
    5.17 + */
    5.18 +final class DigestImplementation<Data> {
    5.19 +    private static final DigestorAccessorImpl ACCESSOR = new DigestorAccessorImpl();
    5.20 +    
    5.21 +    private final Digestor<Data> digestor;
    5.22 +    private final String algorithm;
    5.23 +    private Data data;
    5.24 +    
    5.25 +    private DigestImplementation(Digestor<Data> digestor, String algorithm, Data d) {
    5.26 +        this.digestor = digestor;
    5.27 +        this.algorithm = algorithm;
    5.28 +        this.data = d;
    5.29 +    }
    5.30 +    
    5.31 +    static <Data> DigestImplementation create(Digestor<Data> digestor, String algorithm) {
    5.32 +        Data d = ACCESSOR.create(digestor, algorithm);
    5.33 +        if (d == null) {
    5.34 +            return null;
    5.35 +        } else {
    5.36 +            return new DigestImplementation(digestor, algorithm, d);
    5.37 +        }
    5.38 +    }
    5.39 +
    5.40 +    byte[] digest(ByteBuffer bb) {
    5.41 +        ACCESSOR.update(digestor, data, bb);
    5.42 +        return ACCESSOR.digest(digestor, data);
    5.43 +    }
    5.44 +}
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/samples/messagedigest/src-new-api/org/apidesign/api/security/DigestorAccessorImpl.java	Sat Jun 14 09:52:25 2008 +0200
     6.3 @@ -0,0 +1,26 @@
     6.4 +package org.apidesign.api.security;
     6.5 +
     6.6 +import org.apidesign.spi.security.*;
     6.7 +import java.nio.ByteBuffer;
     6.8 +import org.apidesign.impl.DigestorAccessor;
     6.9 +
    6.10 +/**
    6.11 + *
    6.12 + * @author  Jaroslav Tulach <jaroslav.tulach@apidesign.org>
    6.13 + */
    6.14 +final class DigestorAccessorImpl extends DigestorAccessor {
    6.15 +    @Override
    6.16 +    public <Data> byte[] digest(Digestor<Data> dig, Data data) {
    6.17 +        return defaultDigest(dig, data);
    6.18 +    }
    6.19 +
    6.20 +    @Override
    6.21 +    public <Data> Data create(Digestor<Data> dig, String algorithm) {
    6.22 +        return defaultCreate(dig, algorithm);
    6.23 +    }
    6.24 +
    6.25 +    @Override
    6.26 +    public <Data> void update(Digestor<Data> dig, Data data, ByteBuffer input) {
    6.27 +        defaultUpdate(dig, data, input);
    6.28 +    }
    6.29 +}
     7.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.2 +++ b/samples/messagedigest/src-new-api/org/apidesign/impl/DigestorAccessor.java	Sat Jun 14 09:52:25 2008 +0200
     7.3 @@ -0,0 +1,53 @@
     7.4 +/*
     7.5 + * To change this template, choose Tools | Templates
     7.6 + * and open the template in the editor.
     7.7 + */
     7.8 +
     7.9 +package org.apidesign.impl;
    7.10 +
    7.11 +import java.nio.ByteBuffer;
    7.12 +import org.apidesign.spi.security.Digestor;
    7.13 +
    7.14 +/**
    7.15 + *
    7.16 + * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
    7.17 + */
    7.18 +public abstract class DigestorAccessor {
    7.19 +    private static DigestorAccessor INSTANCE;
    7.20 +    
    7.21 +    protected DigestorAccessor() {
    7.22 +        if (this.getClass().getName().equals("org.apidesign.spi.security.DigestorAccessorImpl")) {
    7.23 +            assert INSTANCE == null;
    7.24 +            INSTANCE = this;
    7.25 +            return;
    7.26 +        }
    7.27 +        if (this.getClass().getName().equals("org.apidesign.api.security.DigestorAccessorImpl")) {
    7.28 +            return;
    7.29 +        }
    7.30 +        throw new IllegalStateException();
    7.31 +    }
    7.32 +    
    7.33 +    final DigestorAccessor getDefault() {
    7.34 +        try {
    7.35 +            Class.forName(Digestor.class.getName(), true, DigestorAccessor.class.getClassLoader());
    7.36 +            return INSTANCE;
    7.37 +        } catch (ClassNotFoundException ex) {
    7.38 +            throw new IllegalStateException(ex);
    7.39 +        }
    7.40 +    }
    7.41 +    
    7.42 +    protected abstract <Data> byte[] digest(Digestor<Data> dig, Data data);
    7.43 +    protected abstract <Data> Data create(Digestor<Data> dig, String algorithm); 
    7.44 +    protected abstract <Data> void update(Digestor<Data> dig, Data data, ByteBuffer input);
    7.45 +    
    7.46 +    
    7.47 +    protected final <Data> byte[] defaultDigest(Digestor<Data> dig, Data data) {
    7.48 +        return getDefault().digest(dig, data);
    7.49 +    }
    7.50 +    protected final <Data> Data defaultCreate(Digestor<Data> dig, String algorithm) {
    7.51 +        return getDefault().create(dig, algorithm);
    7.52 +    }
    7.53 +    protected final <Data> void defaultUpdate(Digestor<Data> dig, Data data, ByteBuffer input) {
    7.54 +        getDefault().update(dig, data, input);
    7.55 +    }
    7.56 +}
     8.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     8.2 +++ b/samples/messagedigest/src-new-api/org/apidesign/spi/security/Digestor.java	Sat Jun 14 09:52:25 2008 +0200
     8.3 @@ -0,0 +1,29 @@
     8.4 +/*
     8.5 + * To change this template, choose Tools | Templates
     8.6 + * and open the template in the editor.
     8.7 + */
     8.8 +
     8.9 +package org.apidesign.spi.security;
    8.10 +
    8.11 +import java.nio.ByteBuffer;
    8.12 +
    8.13 +/**
    8.14 + *
    8.15 + * @author  Jaroslav Tulach <jaroslav.tulach@apidesign.org>
    8.16 + */
    8.17 +// BEGIN: day.end.bridges.Digestor
    8.18 +public abstract class Digestor<Data> {
    8.19 +   protected abstract byte[] digest(Data data);
    8.20 +   protected abstract Data create(String algorithm); 
    8.21 +   protected abstract void update(Data data, ByteBuffer input);
    8.22 +   
    8.23 +// END: day.end.bridges.Digestor   
    8.24 +   
    8.25 +   
    8.26 +   static {
    8.27 +       // initializes the accessor, so the api package can call protected
    8.28 +       // methods from this class
    8.29 +       new DigestorAccessorImpl();
    8.30 +   }
    8.31 +}
    8.32 +
     9.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     9.2 +++ b/samples/messagedigest/src-new-api/org/apidesign/spi/security/DigestorAccessorImpl.java	Sat Jun 14 09:52:25 2008 +0200
     9.3 @@ -0,0 +1,25 @@
     9.4 +package org.apidesign.spi.security;
     9.5 +
     9.6 +import java.nio.ByteBuffer;
     9.7 +import org.apidesign.impl.DigestorAccessor;
     9.8 +
     9.9 +/**
    9.10 + *
    9.11 + * @author  Jaroslav Tulach <jaroslav.tulach@apidesign.org>
    9.12 + */
    9.13 +final class DigestorAccessorImpl extends DigestorAccessor {
    9.14 +    @Override
    9.15 +    public <Data> byte[] digest(Digestor<Data> dig, Data data) {
    9.16 +        return dig.digest(data);
    9.17 +    }
    9.18 +
    9.19 +    @Override
    9.20 +    public <Data> Data create(Digestor<Data> dig, String algorithm) {
    9.21 +        return dig.create(algorithm);
    9.22 +    }
    9.23 +
    9.24 +    @Override
    9.25 +    public <Data> void update(Digestor<Data> dig, Data data, ByteBuffer input) {
    9.26 +        dig.update(data, input);
    9.27 +    }
    9.28 +}