001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *     http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    
019    package org.apache.hadoop.crypto.key;
020    
021    import java.io.IOException;
022    import java.security.NoSuchAlgorithmException;
023    import java.util.List;
024    
025    /**
026     * This is a utility class used to extend the functionality of KeyProvider, that
027     * takes a KeyProvider and an Extension. It implements all the required methods
028     * of the KeyProvider by delegating it to the provided KeyProvider.
029     */
030    public abstract class KeyProviderExtension
031    <E extends KeyProviderExtension.Extension> extends KeyProvider {
032    
033      /**
034       * A marker interface for the KeyProviderExtension subclass implement.
035       */
036      public static interface Extension {
037      }
038    
039      private KeyProvider keyProvider;
040      private E extension;
041    
042      public KeyProviderExtension(KeyProvider keyProvider, E extensions) {
043        super(keyProvider.getConf());
044        this.keyProvider = keyProvider;
045        this.extension = extensions;
046      }
047    
048      protected E getExtension() {
049        return extension;
050      }
051    
052      protected KeyProvider getKeyProvider() {
053        return keyProvider;
054      }
055    
056      @Override
057      public boolean isTransient() {
058        return keyProvider.isTransient();
059      }
060    
061      @Override
062      public Metadata[] getKeysMetadata(String... names) throws IOException {
063        return keyProvider.getKeysMetadata(names);
064      }
065    
066      @Override
067      public KeyVersion getCurrentKey(String name) throws IOException {
068        return keyProvider.getCurrentKey(name);
069      }
070    
071      @Override
072      public KeyVersion createKey(String name, Options options)
073          throws NoSuchAlgorithmException, IOException {
074        return keyProvider.createKey(name, options);
075      }
076    
077      @Override
078      public KeyVersion rollNewVersion(String name)
079          throws NoSuchAlgorithmException, IOException {
080        return keyProvider.rollNewVersion(name);
081      }
082    
083      @Override
084      public KeyVersion getKeyVersion(String versionName) throws IOException {
085        return keyProvider.getKeyVersion(versionName);
086      }
087    
088      @Override
089      public List<String> getKeys() throws IOException {
090        return keyProvider.getKeys();
091      }
092    
093      @Override
094      public List<KeyVersion> getKeyVersions(String name) throws IOException {
095        return keyProvider.getKeyVersions(name);
096      }
097    
098      @Override
099      public Metadata getMetadata(String name) throws IOException {
100        return keyProvider.getMetadata(name);
101      }
102    
103      @Override
104      public KeyVersion createKey(String name, byte[] material, Options options)
105          throws IOException {
106        return keyProvider.createKey(name, material, options);
107      }
108    
109      @Override
110      public void deleteKey(String name) throws IOException {
111        keyProvider.deleteKey(name);
112      }
113    
114      @Override
115      public KeyVersion rollNewVersion(String name, byte[] material)
116          throws IOException {
117        return keyProvider.rollNewVersion(name, material);
118      }
119    
120      @Override
121      public void flush() throws IOException {
122        keyProvider.flush();
123      }
124    
125      @Override
126      public String toString() {
127        return getClass().getSimpleName() + ": " + keyProvider.toString();
128      }
129    }