001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache license, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the license for the specific language governing permissions and
015 * limitations under the license.
016 */
017package org.apache.logging.log4j.core.net.ssl;
018
019import java.util.Arrays;
020import java.util.Objects;
021
022import org.apache.logging.log4j.status.StatusLogger;
023
024/**
025 *
026 */
027public class StoreConfiguration<T> {
028    protected static final StatusLogger LOGGER = StatusLogger.getLogger();
029
030    private String location;
031    private PasswordProvider passwordProvider;
032
033    public StoreConfiguration(final String location, final PasswordProvider passwordProvider) {
034        this.location = location;
035        this.passwordProvider = Objects.requireNonNull(passwordProvider, "passwordProvider");
036    }
037
038    /**
039     * @deprecated Use {@link #StoreConfiguration(String, PasswordProvider)}
040     */
041    @Deprecated
042    public StoreConfiguration(final String location, final char[] password) {
043        this(location, new MemoryPasswordProvider(password));
044    }
045
046    /**
047     * @deprecated Use {@link #StoreConfiguration(String, PasswordProvider)}
048     */
049    @Deprecated
050    public StoreConfiguration(final String location, final String password) {
051        this(location, new MemoryPasswordProvider(password == null ? null : password.toCharArray()));
052    }
053
054    /**
055     * Clears the secret fields in this object.
056     */
057    public void clearSecrets() {
058        this.location = null;
059        this.passwordProvider = null;
060    }
061
062    public String getLocation() {
063        return this.location;
064    }
065
066    public void setLocation(final String location) {
067        this.location = location;
068    }
069
070    /**
071     *
072     * @deprecated Use getPasswordAsCharArray()
073     */
074    @Deprecated
075    public String getPassword() {
076        return String.valueOf(this.passwordProvider.getPassword());
077    }
078
079    public char[] getPasswordAsCharArray() {
080        return this.passwordProvider.getPassword();
081    }
082
083    public void setPassword(final char[] password) {
084        this.passwordProvider = new MemoryPasswordProvider(password);
085    }
086
087    /**
088     *
089     * @deprecated Use getPasswordAsCharArray()
090     */
091    @Deprecated
092    public void setPassword(final String password) {
093        this.passwordProvider = new MemoryPasswordProvider(password == null ? null : password.toCharArray());
094    }
095
096    /**
097     * @throws StoreConfigurationException May be thrown by subclasses
098     */
099    protected T load() throws StoreConfigurationException {
100        return null;
101    }
102
103    @Override
104    public int hashCode() {
105        final int prime = 31;
106        int result = 1;
107        result = prime * result + ((location == null) ? 0 : location.hashCode());
108        result = prime * result + Arrays.hashCode(passwordProvider.getPassword());
109        return result;
110    }
111
112    @Override
113    public boolean equals(final Object obj) {
114        if (this == obj) {
115            return true;
116        }
117        if (obj == null) {
118            return false;
119        }
120        if (getClass() != obj.getClass()) {
121            return false;
122        }
123        final StoreConfiguration<?> other = (StoreConfiguration<?>) obj;
124        if (location == null) {
125            if (other.location != null) {
126                return false;
127            }
128        } else if (!location.equals(other.location)) {
129            return false;
130        }
131        if (!Arrays.equals(passwordProvider.getPassword(), other.passwordProvider.getPassword())) {
132            return false;
133        }
134        return true;
135    }
136}