View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.syncope.common.keymaster.client.api.model;
20  
21  import java.io.IOException;
22  import java.io.Serializable;
23  import org.apache.commons.lang3.StringUtils;
24  import org.apache.commons.lang3.builder.EqualsBuilder;
25  import org.apache.commons.lang3.builder.HashCodeBuilder;
26  import org.apache.cxf.helpers.IOUtils;
27  import org.apache.syncope.common.lib.types.CipherAlgorithm;
28  import org.slf4j.Logger;
29  import org.slf4j.LoggerFactory;
30  
31  public class Domain implements Serializable {
32  
33      private static final long serialVersionUID = -5881851479361505961L;
34  
35      private static final Logger LOG = LoggerFactory.getLogger(Domain.class);
36  
37      public enum TransactionIsolation {
38          TRANSACTION_NONE,
39          TRANSACTION_READ_COMMITTED,
40          TRANSACTION_READ_UNCOMMITTED,
41          TRANSACTION_REPEATABLE_READ,
42          TRANSACTION_SERIALIZABLE
43  
44      }
45  
46      public static class Builder {
47  
48          private final Domain domain;
49  
50          public Builder(final String key) {
51              this.domain = new Domain();
52              this.domain.key = key;
53          }
54  
55          public Builder jdbcDriver(final String jdbcDriver) {
56              this.domain.jdbcDriver = jdbcDriver;
57              return this;
58          }
59  
60          public Builder jdbcURL(final String jdbcURL) {
61              this.domain.jdbcURL = jdbcURL;
62              return this;
63          }
64  
65          public Builder dbSchema(final String dbSchema) {
66              if (StringUtils.isNotBlank(dbSchema)) {
67                  this.domain.dbSchema = dbSchema;
68              }
69              return this;
70          }
71  
72          public Builder dbUsername(final String dbUsername) {
73              this.domain.dbUsername = dbUsername;
74              return this;
75          }
76  
77          public Builder dbPassword(final String dbPassword) {
78              this.domain.dbPassword = dbPassword;
79              return this;
80          }
81  
82          public Builder transactionIsolation(final TransactionIsolation transactionIsolation) {
83              this.domain.transactionIsolation = transactionIsolation;
84              return this;
85          }
86  
87          public Builder poolMaxActive(final int poolMaxActive) {
88              this.domain.poolMaxActive = poolMaxActive;
89              return this;
90          }
91  
92          public Builder poolMinIdle(final int poolMinIdle) {
93              this.domain.poolMinIdle = poolMinIdle;
94              return this;
95          }
96  
97          public Builder auditSql(final String auditSql) {
98              this.domain.auditSql = auditSql;
99              return this;
100         }
101 
102         public Builder orm(final String orm) {
103             this.domain.orm = orm;
104             return this;
105         }
106 
107         public Builder databasePlatform(final String databasePlatform) {
108             this.domain.databasePlatform = databasePlatform;
109             return this;
110         }
111 
112         public Builder adminPassword(final String adminPassword) {
113             this.domain.adminPassword = adminPassword;
114             return this;
115         }
116 
117         public Builder adminCipherAlgorithm(final CipherAlgorithm adminCipherAlgorithm) {
118             this.domain.adminCipherAlgorithm = adminCipherAlgorithm;
119             return this;
120         }
121 
122         public Builder content(final String content) {
123             this.domain.content = content;
124             return this;
125         }
126 
127         public Builder keymasterConfParams(final String keymasterConfParams) {
128             this.domain.keymasterConfParams = keymasterConfParams;
129             return this;
130         }
131 
132         public Domain build() {
133             return this.domain;
134         }
135     }
136 
137     private String key;
138 
139     private String jdbcDriver;
140 
141     private String jdbcURL;
142 
143     private String dbSchema;
144 
145     private String dbUsername;
146 
147     private String dbPassword;
148 
149     private TransactionIsolation transactionIsolation = TransactionIsolation.TRANSACTION_READ_COMMITTED;
150 
151     private int poolMaxActive = 10;
152 
153     private int poolMinIdle = 2;
154 
155     private String auditSql = "audit.sql";
156 
157     private String orm = "META-INF/spring-orm.xml";
158 
159     private String databasePlatform;
160 
161     private String adminPassword;
162 
163     private CipherAlgorithm adminCipherAlgorithm = CipherAlgorithm.SHA512;
164 
165     private String content;
166 
167     private String keymasterConfParams;
168 
169     public String getKey() {
170         return key;
171     }
172 
173     public String getJdbcDriver() {
174         return jdbcDriver;
175     }
176 
177     public String getJdbcURL() {
178         return jdbcURL;
179     }
180 
181     public String getDbSchema() {
182         return dbSchema;
183     }
184 
185     public String getDbUsername() {
186         return dbUsername;
187     }
188 
189     public String getDbPassword() {
190         return dbPassword;
191     }
192 
193     public TransactionIsolation getTransactionIsolation() {
194         return transactionIsolation;
195     }
196 
197     public int getPoolMaxActive() {
198         return poolMaxActive;
199     }
200 
201     public void setPoolMaxActive(final int poolMaxActive) {
202         this.poolMaxActive = poolMaxActive;
203     }
204 
205     public int getPoolMinIdle() {
206         return poolMinIdle;
207     }
208 
209     public void setPoolMinIdle(final int poolMinIdle) {
210         this.poolMinIdle = poolMinIdle;
211     }
212 
213     public String getAuditSql() {
214         return auditSql;
215     }
216 
217     public String getOrm() {
218         return orm;
219     }
220 
221     public String getDatabasePlatform() {
222         return databasePlatform;
223     }
224 
225     public String getAdminPassword() {
226         return adminPassword;
227     }
228 
229     public void setAdminPassword(final String adminPassword) {
230         this.adminPassword = adminPassword;
231     }
232 
233     public CipherAlgorithm getAdminCipherAlgorithm() {
234         return adminCipherAlgorithm;
235     }
236 
237     public void setAdminCipherAlgorithm(final CipherAlgorithm adminCipherAlgorithm) {
238         this.adminCipherAlgorithm = adminCipherAlgorithm;
239     }
240 
241     private String read(final String filename) {
242         String read = null;
243         try {
244             read = IOUtils.toString(Domain.class.getResourceAsStream('/' + filename));
245         } catch (IOException e) {
246             LOG.error("Could not read {}", filename, e);
247         }
248 
249         return read;
250     }
251 
252     public String getContent() {
253         if (content == null) {
254             content = read("defaultContent.xml");
255         }
256 
257         return content;
258     }
259 
260     public String getKeymasterConfParams() {
261         if (keymasterConfParams == null) {
262             keymasterConfParams = read("defaultKeymasterConfParams.json");
263         }
264 
265         return keymasterConfParams;
266     }
267 
268     @Override
269     public int hashCode() {
270         return new HashCodeBuilder().
271                 append(key).
272                 append(jdbcDriver).
273                 append(jdbcURL).
274                 append(dbSchema).
275                 append(dbUsername).
276                 append(dbPassword).
277                 append(transactionIsolation).
278                 append(poolMaxActive).
279                 append(poolMinIdle).
280                 append(auditSql).
281                 append(orm).
282                 append(databasePlatform).
283                 append(adminPassword).
284                 append(adminCipherAlgorithm).
285                 append(content).
286                 append(keymasterConfParams).
287                 build();
288     }
289 
290     @Override
291     public boolean equals(final Object obj) {
292         if (this == obj) {
293             return true;
294         }
295         if (obj == null) {
296             return false;
297         }
298         if (getClass() != obj.getClass()) {
299             return false;
300         }
301         final Domain other = (Domain) obj;
302         return new EqualsBuilder().
303                 append(key, other.key).
304                 append(jdbcDriver, other.jdbcDriver).
305                 append(jdbcURL, other.jdbcURL).
306                 append(dbSchema, other.dbSchema).
307                 append(dbUsername, other.dbUsername).
308                 append(dbPassword, other.dbPassword).
309                 append(transactionIsolation, other.transactionIsolation).
310                 append(poolMaxActive, other.poolMaxActive).
311                 append(poolMinIdle, other.poolMinIdle).
312                 append(auditSql, other.auditSql).
313                 append(orm, other.orm).
314                 append(databasePlatform, other.databasePlatform).
315                 append(adminPassword, other.adminPassword).
316                 append(adminCipherAlgorithm, other.adminCipherAlgorithm).
317                 append(content, other.content).
318                 append(keymasterConfParams, other.keymasterConfParams).
319                 build();
320     }
321 
322     @Override
323     public String toString() {
324         return "Domain{"
325                 + "key=" + key
326                 + ", jdbcDriver=" + jdbcDriver
327                 + ", jdbcURL=" + jdbcURL
328                 + ", dbSchema=" + dbSchema
329                 + ", dbUsername=" + dbUsername
330                 + ", dbPassword=" + dbPassword
331                 + ", transactionIsolation=" + transactionIsolation
332                 + ", poolMaxSize=" + poolMaxActive
333                 + ", poolMinIdle=" + poolMinIdle
334                 + ", auditSql=" + auditSql
335                 + ", orm=" + orm
336                 + ", databasePlatform=" + databasePlatform
337                 + ", adminPassword=" + adminPassword
338                 + ", adminCipherAlgorithm=" + adminCipherAlgorithm
339                 + ", content=" + content
340                 + ", keymasterConfParams=" + keymasterConfParams
341                 + '}';
342     }
343 }