View Javadoc
1   package com.wakaleo.schemaspy;
2   
3   import org.schemaspy.LayoutFolder;
4   import org.schemaspy.SchemaAnalyzer;
5   import org.schemaspy.cli.CommandLineArgumentParser;
6   import org.schemaspy.cli.CommandLineArguments;
7   import org.schemaspy.connection.ScNullChecked;
8   import org.schemaspy.connection.ScSimple;
9   import org.schemaspy.connection.SqlConnection;
10  import org.schemaspy.input.dbms.ConnectionConfig;
11  import org.schemaspy.input.dbms.ConnectionURLBuilder;
12  import org.schemaspy.input.dbms.DriverFromConfig;
13  import org.schemaspy.input.dbms.service.DatabaseServiceFactory;
14  import org.schemaspy.input.dbms.service.SqlService;
15  import org.schemaspy.output.OutputProducer;
16  import org.schemaspy.output.xml.dom.XmlProducerUsingDOM;
17  
18  import java.io.IOException;
19  import java.sql.SQLException;
20  import java.util.List;
21  import java.util.Objects;
22  
23  /**
24   * Wrapper around the {@link SchemaAnalyzer} to hide the initialization details from the {@link SchemaSpyReport}
25   * maven mojo.
26   */
27  public class MavenSchemaAnalyzer {
28      private SchemaAnalyzer analyzer;
29  
30      private CommandLineArguments cliArgs;
31  
32      /**
33       * Adds the schemaspy plugin configuration properties. This is necessary before calling {@link #analyze()}.
34       * @param argList a list of property-value pairs.
35       */
36      public void applyConfiguration(List<String> argList) {
37          CommandLineArgumentParser parser = new CommandLineArgumentParser(argList.toArray(new String[0]));
38          cliArgs = parser.commandLineArguments();
39      }
40  
41      /**
42       * Executes the schemaspy analyzer process.
43       */
44      public void analyze() throws SQLException, IOException {
45          cliArgs = Objects.requireNonNull(cliArgs, "The field 'commandLineArguments' need to reference an instance. Call 'applyConfiguration(...) to initiate command line arguments");
46          ConnectionConfig connectionConfig = cliArgs.getConnectionConfig();
47          SqlConnection connection = new ScSimple(connectionConfig, new ConnectionURLBuilder(connectionConfig), new DriverFromConfig(connectionConfig));
48          SqlService sqlService = new SqlService();
49          DatabaseServiceFactory databaseServiceFactory = new DatabaseServiceFactory(sqlService);
50          OutputProducer outputProducer = new XmlProducerUsingDOM();
51          LayoutFolder layoutFolder = new LayoutFolder(SchemaAnalyzer.class.getClassLoader());
52          analyzer = new SchemaAnalyzer(sqlService, databaseServiceFactory, cliArgs, outputProducer, layoutFolder, connection);
53          analyzer.analyze();
54      }
55  }