001    package org.cumulus4j.keymanager.api.internal;
002    
003    import org.cumulus4j.keymanager.api.KeyManagerAPI;
004    import org.cumulus4j.keymanager.api.KeyManagerAPIConfiguration;
005    import org.cumulus4j.keymanager.api.KeyManagerAPIInstantiationException;
006    
007    /**
008     * @author Marco หงุ่ยตระกูล-Schulze - marco at nightlabs dot de
009     */
010    public abstract class AbstractKeyManagerAPI
011    implements KeyManagerAPI
012    {
013            protected static final String FILE_URL_PREFIX = "file:";
014    
015            private volatile KeyManagerAPIConfiguration configuration;
016    
017            /**
018             * Get the configuration. If there is no configuration, yet, an
019             * {@link IllegalStateException} is thrown.
020             * @return the configuration, never <code>null</code>.
021             * @throws IllegalStateException if there is no configuration, yet, i.e. setConfiguration(...) was not yet called.
022             */
023            public KeyManagerAPIConfiguration getConf()
024            throws IllegalStateException
025            {
026                    KeyManagerAPIConfiguration configuration = getConfiguration();
027                    if (configuration == null)
028                            throw new IllegalStateException("There is no configuration, yet! Call setConfiguration(...) first!");
029    
030                    return configuration;
031            }
032    
033            /**
034             * Convenience method delegating to {@link KeyManagerAPIConfiguration#getAuthUserName()}.
035             * @return the authUserName.
036             * @throws IllegalStateException if there is no configuration, yet, i.e. setConfiguration(...) was not yet called.
037             */
038            public String getAuthUserName() throws IllegalStateException {
039                    return getConf().getAuthUserName();
040            }
041    
042            /**
043             * Convenience method delegating to {@link KeyManagerAPIConfiguration#getAuthPassword()}.
044             * @return the authPassword.
045             * @throws IllegalStateException if there is no configuration, yet, i.e. setConfiguration(...) was not yet called.
046             */
047            public char[] getAuthPassword() throws IllegalStateException {
048                    return getConf().getAuthPassword();
049            }
050    
051            /**
052             * Convenience method delegating to {@link KeyManagerAPIConfiguration#getKeyStoreID()}.
053             * @return the keyStoreID.
054             * @throws IllegalStateException if there is no configuration, yet, i.e. setConfiguration(...) was not yet called.
055             */
056            public String getKeyStoreID() throws IllegalStateException {
057                    return getConf().getKeyStoreID();
058            }
059    
060            /**
061             * Convenience method delegating to {@link KeyManagerAPIConfiguration#getKeyManagerBaseURL()}.
062             * @return the keyManagerBaseURL.
063             * @throws IllegalStateException if there is no configuration, yet, i.e. setConfiguration(...) was not yet called.
064             */
065            public String getKeyManagerBaseURL() throws IllegalStateException {
066                    return getConf().getKeyManagerBaseURL();
067            }
068    
069            @Override
070            public void setConfiguration(KeyManagerAPIConfiguration configuration) throws IllegalArgumentException, KeyManagerAPIInstantiationException
071            {
072                    if (configuration == null)
073                            throw new IllegalArgumentException("configuration == null");
074    
075                    // Mark it read-only to prevent any configuration change besides calling this method again.
076                    configuration.markReadOnly();
077    
078                    // The authUserName and authPassword is not necessarily required for all operations.
079    //              if (configuration.getAuthUserName() == null)
080    //                      throw new IllegalArgumentException("configuration.authUserName == null");
081    //
082    //              if (configuration.getAuthPassword() == null)
083    //                      throw new IllegalArgumentException("configuration.authPassword == null");
084    
085                    if (configuration.getKeyStoreID() == null)
086                            throw new IllegalArgumentException("configuration.keyStoreID == null");
087    
088                    this.configuration = configuration;
089            }
090    
091            @Override
092            public KeyManagerAPIConfiguration getConfiguration() {
093                    return configuration;
094            }
095    
096            protected static boolean equals(Object o1, Object o2)
097            {
098                    return o1 == o2 || (o1 != null && o1.equals(o2));
099            }
100    }