Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2012 Mirko Caserta
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this software except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *  http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package com.springcryptoutils.core.key;
 
 
 import java.util.Map;

A registry for selection of private keys at runtime.

Author(s):
Mirko Caserta (mirko.caserta@gmail.com)
 
 
 
     private Map<CacheKeyPrivateKeycache = new HashMap<CacheKeyPrivateKey>();

    
Sets the keystore registry.

Parameters:
keyStoreRegistry the keystore registry
 
     public void setKeyStoreRegistry(KeyStoreRegistry keyStoreRegistry) {
         this. = keyStoreRegistry;
     }

    
Returns the selected private key or null if not found.

Parameters:
keyStoreChooser the keystore chooser
privateKeyChooserByAlias the private key chooser by alias
Returns:
the selected private key or null if not found
 
     public PrivateKey get(KeyStoreChooser keyStoreChooserPrivateKeyChooserByAlias privateKeyChooserByAlias) {
         CacheKey cacheKey = new CacheKey(keyStoreChooser.getKeyStoreName(), privateKeyChooserByAlias.getAlias());
         PrivateKey retrievedPrivateKey = .get(cacheKey);
 
         if (retrievedPrivateKey != null) {
             return retrievedPrivateKey;
         }
 
         KeyStore keyStore = .get(keyStoreChooser);
 
         if (keyStore != null) {
             PrivateKeyFactoryBean factory = new PrivateKeyFactoryBean();
             factory.setKeystore(keyStore);
             factory.setAlias(privateKeyChooserByAlias.getAlias());
             factory.setPassword(privateKeyChooserByAlias.getPassword());
             try {
                 factory.afterPropertiesSet();
                 PrivateKey privateKey = (PrivateKeyfactory.getObject();
 
                 if (privateKey != null) {
                     .put(cacheKeyprivateKey);
                 }
                 return privateKey;
             } catch (Exception e) {
                 throw new PrivateKeyException("error initializing private key factory bean"e);
             }
         }
 
         return null;
     }
 
     private static final class CacheKey {
 
         private static final int INT_HASHCODE_BASE = 31;
 
         private String keyStoreName;
         private String privateKeyAlias;
 
         private CacheKey(String keyStoreNameString privateKeyAlias) {
             this. = keyStoreName;
             this. = privateKeyAlias;
         }
 
         @Override
         public boolean equals(Object o) {
             if (this == o) {
                 return true;
            }
            if (o == null || getClass() != o.getClass()) {
                return false;
            }
            CacheKey cacheKey = (CacheKeyo;
            return !( != null ? !.equals(cacheKey.keyStoreName) : cacheKey.keyStoreName != null) && !( != null ? !.equals(cacheKey.privateKeyAlias) : cacheKey.privateKeyAlias != null);
        }
        @Override
        public int hashCode() {
            int result =  != null ? .hashCode() : 0;
            result =  * result + ( != null ? .hashCode() : 0);
            return result;
        }
        @Override
        public String toString() {
            final StringBuilder sb = new StringBuilder();
            sb.append("CacheKey");
            sb.append("{keyStoreName='").append().append('\'');
            sb.append(", privateKeyAlias='").append().append('\'');
            sb.append('}');
            return sb.toString();
        }
    }
New to GrepCode? Check out our FAQ X