View Javadoc
1   /*
2    *    Copyright 2006-2026 the original author or authors.
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *       https://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
15   */
16  package org.mybatis.generator.config;
17  
18  import static org.mybatis.generator.internal.util.StringUtility.stringHasValue;
19  import static org.mybatis.generator.internal.util.messages.Messages.getString;
20  
21  import java.util.List;
22  import java.util.Objects;
23  import java.util.Optional;
24  
25  import org.jspecify.annotations.Nullable;
26  
27  public class JDBCConnectionConfiguration extends PropertyHolder {
28      private final String driverClass;
29      private final String connectionURL;
30      private final @Nullable String userId;
31      private final @Nullable String password;
32  
33      protected JDBCConnectionConfiguration(Builder builder) {
34          super(builder);
35          this.driverClass = Objects.requireNonNull(builder.driverClass);
36          this.connectionURL = Objects.requireNonNull(builder.connectionURL);
37          this.userId = builder.userId;
38          this.password = builder.password;
39      }
40  
41      public String getConnectionURL() {
42          return connectionURL;
43      }
44  
45      public Optional<String> getPassword() {
46          return Optional.ofNullable(password);
47      }
48  
49      public Optional<String> getUserId() {
50          return Optional.ofNullable(userId);
51      }
52  
53      public String getDriverClass() {
54          return driverClass;
55      }
56  
57      public void validate(List<String> errors) {
58          if (!stringHasValue(driverClass)) {
59              errors.add(getString("ValidationError.4")); //$NON-NLS-1$
60          }
61  
62          if (!stringHasValue(connectionURL)) {
63              errors.add(getString("ValidationError.5")); //$NON-NLS-1$
64          }
65      }
66  
67      public static class Builder extends AbstractBuilder<Builder> {
68          private @Nullable String driverClass;
69          private @Nullable String connectionURL;
70          private @Nullable String userId;
71          private @Nullable String password;
72  
73          public Builder withDriverClass(@Nullable String driverClass) {
74              this.driverClass = driverClass;
75              return this;
76          }
77  
78          public Builder withConnectionURL(@Nullable String connectionURL) {
79              this.connectionURL = connectionURL;
80              return this;
81          }
82  
83          public Builder withUserId(@Nullable String userId) {
84              this.userId = userId;
85              return this;
86          }
87  
88          public Builder withPassword(@Nullable String password) {
89              this.password = password;
90              return this;
91          }
92  
93          public JDBCConnectionConfiguration build() {
94              return new JDBCConnectionConfiguration(this);
95          }
96  
97          @Override
98          protected Builder getThis() {
99              return this;
100         }
101     }
102 }