001 package org.cumulus4j.keymanager.api; 002 003 import java.io.IOException; 004 005 import org.cumulus4j.keymanager.api.internal.AbstractKeyManagerAPI; 006 import org.cumulus4j.keymanager.api.internal.local.LocalKeyManagerAPI; 007 import org.cumulus4j.keymanager.api.internal.remote.RemoteKeyManagerAPI; 008 009 /** 010 * <p> 011 * Implementation of {@link KeyManagerAPI} that delegates to a specific implementation. 012 * </p><p> 013 * Instantiate an instance of this class and then use the <code>KeyManagerAPI</code> interface 014 * everywhere to reference it. 015 * </p><p> 016 * When using dependency injection, there should be no reference 017 * to this class at all except for the configuration of the dependency injection. 018 * </p> 019 * 020 * @author Marco หงุ่ยตระกูล-Schulze - marco at nightlabs dot de 021 */ 022 public class DefaultKeyManagerAPI extends AbstractKeyManagerAPI 023 { 024 private volatile KeyManagerAPI delegate; 025 026 @Override 027 public void setConfiguration(KeyManagerAPIConfiguration configuration) throws IllegalArgumentException, KeyManagerAPIInstantiationException 028 { 029 // We do *not* call super.setConfiguration(...), because the delegates might replace the configuration and 030 // thus this instance should not have a copy of it at all! Instead we override getConfiguration() as well. 031 032 if (configuration == null) 033 throw new IllegalArgumentException("configuration == null"); 034 035 configuration.markReadOnly(); 036 037 // In case, we already had a delegate before, we null it now (so we don't end up with some half-initialised stuff. 038 this.delegate = null; 039 040 String keyManagerBaseURL = configuration.getKeyManagerBaseURL(); 041 042 KeyManagerAPI delegate; 043 if (keyManagerBaseURL == null || keyManagerBaseURL.startsWith(FILE_URL_PREFIX)) { 044 try { 045 delegate = new LocalKeyManagerAPI(); 046 } catch (KeyManagerAPIInstantiationException x) { 047 throw x; 048 } catch (Throwable t) { 049 throw new KeyManagerAPIInstantiationException("The LocalKeyManagerAPI could not be instantiated! If you really want to use a local key-store, make sure all required libs are deployed. If you want to use a key-server instead of a local key-store, you must specify different arguments. " + t, t); 050 } 051 } 052 else { 053 try { 054 delegate = new RemoteKeyManagerAPI(); 055 } catch (KeyManagerAPIInstantiationException x) { 056 throw x; 057 } catch (Throwable t) { 058 throw new KeyManagerAPIInstantiationException("The RemoteKeyManagerAPI could not be instantiated! If you really want to use a key-server, make sure all required libs are deployed. If you want to use a local key-store instead of a key-server, you must specify different arguments. " + t, t); 059 } 060 } 061 062 delegate.setConfiguration(configuration); 063 this.delegate = delegate; 064 } 065 066 @Override 067 public KeyManagerAPIConfiguration getConfiguration() 068 { 069 KeyManagerAPI delegate = this.delegate; 070 if (delegate == null) 071 return null; 072 else 073 return delegate.getConfiguration(); 074 } 075 076 private KeyManagerAPI getDelegate() 077 { 078 KeyManagerAPI delegate = this.delegate; 079 if (delegate == null) 080 throw new IllegalStateException("setConfiguration(...) was not yet called!"); 081 082 return delegate; 083 } 084 085 @Override 086 public DateDependentKeyStrategyInitResult initDateDependentKeyStrategy(DateDependentKeyStrategyInitParam param) throws KeyStoreNotEmptyException, IOException 087 { 088 return getDelegate().initDateDependentKeyStrategy(param); 089 } 090 091 @Override 092 public void putUser(String userName, char[] password) throws AuthenticationException, IOException 093 { 094 getDelegate().putUser(userName, password); 095 } 096 097 @Override 098 public void deleteUser(String userName) throws AuthenticationException, CannotDeleteLastUserException, IOException 099 { 100 getDelegate().deleteUser(userName); 101 } 102 103 @Override 104 public CryptoSession getCryptoSession(String appServerBaseURL) throws AuthenticationException, IOException 105 { 106 return getDelegate().getCryptoSession(appServerBaseURL); 107 } 108 109 }