View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  package org.apache.logging.log4j.core.net.ssl;
18  
19  import java.util.Arrays;
20  import java.util.Objects;
21  
22  import org.apache.logging.log4j.status.StatusLogger;
23  
24  /**
25   *
26   */
27  public class StoreConfiguration<T> {
28      protected static final StatusLogger LOGGER = StatusLogger.getLogger();
29  
30      private String location;
31      private PasswordProvider passwordProvider;
32  
33      public StoreConfiguration(final String location, final PasswordProvider passwordProvider) {
34          this.location = location;
35          this.passwordProvider = Objects.requireNonNull(passwordProvider, "passwordProvider");
36      }
37  
38      /**
39       * @deprecated Use {@link #StoreConfiguration(String, PasswordProvider)}
40       */
41      @Deprecated
42      public StoreConfiguration(final String location, final char[] password) {
43          this(location, new MemoryPasswordProvider(password));
44      }
45  
46      /**
47       * @deprecated Use {@link #StoreConfiguration(String, PasswordProvider)}
48       */
49      @Deprecated
50      public StoreConfiguration(final String location, final String password) {
51          this(location, new MemoryPasswordProvider(password == null ? null : password.toCharArray()));
52      }
53  
54      /**
55       * Clears the secret fields in this object.
56       */
57      public void clearSecrets() {
58          this.location = null;
59          this.passwordProvider = null;
60      }
61  
62      public String getLocation() {
63          return this.location;
64      }
65  
66      public void setLocation(final String location) {
67          this.location = location;
68      }
69  
70      /**
71       *
72       * @deprecated Use getPasswordAsCharArray()
73       */
74      @Deprecated
75      public String getPassword() {
76          return String.valueOf(this.passwordProvider.getPassword());
77      }
78  
79      public char[] getPasswordAsCharArray() {
80          return this.passwordProvider.getPassword();
81      }
82  
83      public void setPassword(final char[] password) {
84          this.passwordProvider = new MemoryPasswordProvider(password);
85      }
86  
87      /**
88       *
89       * @deprecated Use getPasswordAsCharArray()
90       */
91      @Deprecated
92      public void setPassword(final String password) {
93          this.passwordProvider = new MemoryPasswordProvider(password == null ? null : password.toCharArray());
94      }
95  
96      /**
97       * @throws StoreConfigurationException May be thrown by subclasses
98       */
99      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 }