forked from apache/cayenne
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DbImporterMojo.java
242 lines (205 loc) · 7.49 KB
/
DbImporterMojo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*****************************************************************
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
****************************************************************/
package org.apache.cayenne.tools;
import org.apache.cayenne.dbimport.ReverseEngineering;
import org.apache.cayenne.dbsync.DbSyncModule;
import org.apache.cayenne.dbsync.reverse.filters.FiltersConfigBuilder;
import org.apache.cayenne.di.DIBootstrap;
import org.apache.cayenne.di.Injector;
import org.apache.cayenne.tools.configuration.ToolsModule;
import org.apache.cayenne.tools.dbimport.DbImportAction;
import org.apache.cayenne.tools.dbimport.DbImportConfiguration;
import org.apache.cayenne.tools.dbimport.DbImportModule;
import org.apache.cayenne.util.Util;
import org.apache.commons.logging.Log;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import java.io.File;
/**
* Maven mojo to reverse engineer datamap from DB.
*
* @phase generate-sources
* @goal cdbimport
* @since 3.0
*/
public class DbImporterMojo extends AbstractMojo {
/**
* Java class implementing org.apache.cayenne.dba.DbAdapter. This attribute
* is optional, the default is AutoAdapter, i.e. Cayenne would try to guess
* the DB type.
*
* @parameter adapter="adapter"
* default-value="org.apache.cayenne.dba.AutoAdapter"
*/
private String adapter;
/**
* A default package for ObjEntity Java classes. If not specified, and the
* existing DataMap already has the default package, the existing package
* will be used.
*
* @parameter defaultPackage="defaultPackage"
* @since 4.0
*/
private String defaultPackage;
/**
* A class of JDBC driver to use for the target database.
*
* @parameter driver="driver"
* @required
*/
private String driver;
/**
* @parameter forceDataMapCatalog="forceDataMapCatalog" default-value="false"
*/
private boolean forceDataMapCatalog;
/**
* @parameter forceDataMapSchema="forceDataMapSchema" default-value="false"
*/
private boolean forceDataMapSchema;
/**
* DataMap XML file to use as a base for DB importing.
*
* @parameter map="map"
* @required
*/
private File map;
/**
* A comma-separated list of Perl5 patterns that defines which imported tables should have their primary key columns
* mapped as ObjAttributes. "*" would indicate all tables.
*
* @parameter meaningfulPkTables="meaningfulPkTables"
* @since 4.0
*/
private String meaningfulPkTables;
/**
* Object layer naming generator implementation. Should be fully qualified Java class name implementing
* "org.apache.cayenne.dbsync.naming.ObjectNameGenerator". The default is
* "org.apache.cayenne.dbsync.naming.DefaultObjectNameGenerator".
*
* @parameter namingStrategy="namingStrategy"
* default-value="org.apache.cayenne.dbsync.naming.DefaultObjectNameGenerator"
*/
private String namingStrategy;
/**
* Database user password.
*
* @parameter password="password"
*/
private String password;
/**
* An object that contains reverse engineering rules.
*
* @parameter reverseEngineering="reverseEngineering"
*/
private ReverseEngineering reverseEngineering = new ReverseEngineering();
/**
* A regular expression that should match the part of the table name to strip before generating DB names.
*
* @parameter stripFromTableNames="stripFromTableNames"
* default-value=""
*/
private String stripFromTableNames;
/**
* JDBC connection URL of a target database.
*
* @parameter url="url"
* @required
*/
private String url;
/**
* If true, would use primitives instead of numeric and boolean classes.
*
* @parameter usePrimitives="usePrimitives" default-value="true"
*/
private boolean usePrimitives;
/**
* Database user name.
*
* @parameter username="username"
*/
private String username;
public void execute() throws MojoExecutionException, MojoFailureException {
Log logger = new MavenLogger(this);
DbImportConfiguration config = createConfig(logger);
Injector injector = DIBootstrap.createInjector(
new DbSyncModule(), new ToolsModule(logger), new DbImportModule());
DbImportConfigurationValidator validator = new DbImportConfigurationValidator(
reverseEngineering, config, injector);
try {
validator.validate();
} catch (Exception ex) {
throw new MojoExecutionException(ex.getMessage(), ex);
}
try {
injector.getInstance(DbImportAction.class).execute(config);
} catch (Exception ex) {
Throwable th = Util.unwindException(ex);
String message = "Error importing database schema";
if (th.getLocalizedMessage() != null) {
message += ": " + th.getLocalizedMessage();
}
getLog().error(message);
throw new MojoExecutionException(message, th);
}
}
DbImportConfiguration createConfig(Log logger) {
DbImportConfiguration config = new DbImportConfiguration();
config.setAdapter(adapter);
config.setDefaultPackage(defaultPackage);
config.setDriver(driver);
config.setFiltersConfig(new FiltersConfigBuilder(reverseEngineering).build());
config.setForceDataMapCatalog(forceDataMapCatalog);
config.setForceDataMapSchema(forceDataMapSchema);
config.setLogger(logger);
config.setMeaningfulPkTables(meaningfulPkTables);
config.setNamingStrategy(namingStrategy);
config.setPassword(password);
config.setSkipRelationshipsLoading(reverseEngineering.getSkipRelationshipsLoading());
config.setSkipPrimaryKeyLoading(reverseEngineering.getSkipPrimaryKeyLoading());
config.setStripFromTableNames(stripFromTableNames);
config.setTableTypes(reverseEngineering.getTableTypes());
config.setTargetDataMap(map);
config.setUrl(url);
config.setUsername(username);
config.setUsePrimitives(usePrimitives);
return config;
}
public File getMap() {
return map;
}
public void setMap(File map) {
this.map = map;
}
public String getDriver() {
return driver;
}
public void setDriver(String driver) {
this.driver = driver;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public ReverseEngineering getReverseEngineering() {
return reverseEngineering;
}
}