Ignore:
Timestamp:
08/31/10 21:21:38 (21 months ago)
Author:
kasper
Message:

Added CLI capability to list analyzers, transformers, datastores, schemas, tables and columns.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • AnalyzerBeans/trunk/src/main/java/org/eobjects/analyzer/cli/Main.java

    r983 r986  
    22 
    33import java.io.File; 
     4import java.util.Collection; 
    45import java.util.List; 
     6import java.util.Set; 
    57 
    68import org.eobjects.analyzer.configuration.AnalyzerBeansConfiguration; 
    79import org.eobjects.analyzer.configuration.JaxbConfigurationFactory; 
     10import org.eobjects.analyzer.connection.Datastore; 
     11import org.eobjects.analyzer.descriptors.AnalyzerBeanDescriptor; 
     12import org.eobjects.analyzer.descriptors.BeanDescriptor; 
     13import org.eobjects.analyzer.descriptors.ConfiguredPropertyDescriptor; 
     14import org.eobjects.analyzer.descriptors.TransformerBeanDescriptor; 
    815import org.eobjects.analyzer.job.AnalysisJobBuilder; 
    916import org.eobjects.analyzer.job.JaxbJobFactory; 
    10 import org.eobjects.analyzer.job.concurrent.TaskRunner; 
    1117import org.eobjects.analyzer.job.runner.AnalysisRunnerImpl; 
    1218import org.eobjects.analyzer.result.AnalyzerResult; 
     
    1420import org.kohsuke.args4j.CmdLineParser; 
    1521import org.kohsuke.args4j.Option; 
     22import org.slf4j.Logger; 
     23import org.slf4j.LoggerFactory; 
     24 
     25import dk.eobjects.metamodel.DataContext; 
     26import dk.eobjects.metamodel.schema.Schema; 
     27import dk.eobjects.metamodel.schema.Table; 
    1628 
    1729public final class Main { 
    1830 
    19         @Option(name = "-job", usage = "XML file describing the analysis job", required = true) 
     31        public static enum ListType { 
     32                ANALYZERS, TRANSFORMERS, DATASTORES, SCHEMAS, TABLES, COLUMNS 
     33        } 
     34 
     35        private final static Logger logger = LoggerFactory.getLogger(Main.class); 
     36 
     37        @Option(name = "-conf", aliases = { "-configuration", 
     38                        "--configuration-file" }, usage = "XML file describing the configuration of AnalyzerBeans", required = true) 
     39        private File configurationFile; 
     40 
     41        @Option(name = "-job", aliases = { "--job-file" }, usage = "An analysis job XML file to execute") 
    2042        private File jobFile; 
    2143 
    22         @Option(name = "-configuration", usage = "XML file describing the configuration of AnalyzerBeans", required = true) 
    23         private File configurationFile; 
     44        @Option(name = "-list", usage = "Used to print a list of various elements available in the configuration") 
     45        private ListType listType; 
     46 
     47        @Option(name = "-ds", aliases = { "-datastore", "--datastore-name" }, usage = "Name of datastore when printing a list of schemas, tables or columns") 
     48        private String datastoreName; 
     49 
     50        @Option(name = "-s", aliases = { "-schema", "--schema-name" }, usage = "Name of schema when printing a list of tables or columns") 
     51        private String schemaName; 
     52 
     53        @Option(name = "-t", aliases = { "-table", "--table-name" }, usage = "Name of table when printing a list of columns") 
     54        private String tableName; 
    2455 
    2556        public static void main(String[] args) { 
     
    3061                        main.run(); 
    3162                } catch (CmdLineException e) { 
    32                         e.printStackTrace(); 
     63                        parser.setUsageWidth(120); 
    3364                        parser.printUsage(System.out); 
    3465                } 
     
    3970                                .create(configurationFile); 
    4071                try { 
    41                         AnalysisJobBuilder analysisJobBuilder = new JaxbJobFactory( 
    42                                         configuration).create(jobFile); 
    43  
    44                         List<AnalyzerResult> results = new AnalysisRunnerImpl(configuration) 
    45                                         .run(analysisJobBuilder.toAnalysisJob()).getResults(); 
    46  
    47                         for (AnalyzerResult analyzerResult : results) { 
    48                                 System.out.println("\nRESULT:"); 
    49                                 System.out.println(analyzerResult); 
    50                         } 
     72                        if (jobFile != null) { 
     73                                runJob(configuration); 
     74                        } else if (listType != null) { 
     75                                switch (listType) { 
     76                                case ANALYZERS: 
     77                                        printAnalyzers(configuration); 
     78                                        break; 
     79                                case TRANSFORMERS: 
     80                                        printTransformers(configuration); 
     81                                        break; 
     82                                case DATASTORES: 
     83                                        printDatastores(configuration); 
     84                                        break; 
     85                                case SCHEMAS: 
     86                                        printSchemas(configuration); 
     87                                        break; 
     88                                case TABLES: 
     89                                        printTables(configuration); 
     90                                        break; 
     91                                case COLUMNS: 
     92                                        printColumns(configuration); 
     93                                        break; 
     94                                default: 
     95                                        throw new IllegalArgumentException("Unknown list type: " 
     96                                                        + listType); 
     97                                } 
     98                        } else { 
     99                                throw new IllegalArgumentException( 
     100                                                "Neither --job-file nor --list-type is specified. Try running with -usage to see usage help."); 
     101                        } 
     102                } catch (Exception e) { 
     103                        logger.error("Exception thrown in {}", e, this); 
     104                        System.err.println("Error: " + e.getMessage()); 
    51105                } finally { 
    52                         TaskRunner taskRunner = configuration.getTaskRunner(); 
    53                         taskRunner.shutdown(); 
    54                 } 
     106                        configuration.getTaskRunner().shutdown(); 
     107                } 
     108        } 
     109 
     110        private void printColumns(AnalyzerBeansConfiguration configuration) { 
     111                if (datastoreName == null) { 
     112                        System.err.println("You need to specify the datastore name!"); 
     113                } else if (tableName == null) { 
     114                        System.err.println("You need to specify a table name!"); 
     115                } else { 
     116                        Datastore ds = configuration.getDatastoreCatalog().getDatastore( 
     117                                        datastoreName); 
     118                        if (ds == null) { 
     119                                System.err.println("No such datastore: " + datastoreName); 
     120                        } else { 
     121                                DataContext dc = ds.getDataContextProvider().getDataContext(); 
     122                                Schema schema; 
     123                                if (schemaName == null) { 
     124                                        schema = dc.getDefaultSchema(); 
     125                                } else { 
     126                                        schema = dc.getSchemaByName(schemaName); 
     127                                } 
     128                                if (schema == null) { 
     129                                        System.err.println("No such schema: " + schemaName); 
     130                                } else { 
     131                                        Table table = schema.getTableByName(tableName); 
     132                                        if (table == null) { 
     133                                                System.out.println("No such table: " + tableName); 
     134                                        } else { 
     135                                                String[] columnNames = table.getColumnNames(); 
     136                                                System.out.println("Columns:"); 
     137                                                System.out.println("--------"); 
     138                                                for (String columnName : columnNames) { 
     139                                                        System.out.println(columnName); 
     140                                                } 
     141                                        } 
     142                                } 
     143                        } 
     144                } 
     145        } 
     146 
     147        private void printTables(AnalyzerBeansConfiguration configuration) { 
     148                if (datastoreName == null) { 
     149                        System.err.println("You need to specify the datastore name!"); 
     150                } else { 
     151                        Datastore ds = configuration.getDatastoreCatalog().getDatastore( 
     152                                        datastoreName); 
     153                        if (ds == null) { 
     154                                System.err.println("No such datastore: " + datastoreName); 
     155                        } else { 
     156                                DataContext dc = ds.getDataContextProvider().getDataContext(); 
     157                                Schema schema; 
     158                                if (schemaName == null) { 
     159                                        schema = dc.getDefaultSchema(); 
     160                                } else { 
     161                                        schema = dc.getSchemaByName(schemaName); 
     162                                } 
     163                                if (schema == null) { 
     164                                        System.err.println("No such schema: " + schemaName); 
     165                                } else { 
     166                                        String[] tableNames = schema.getTableNames(); 
     167                                        if (tableNames == null || tableNames.length == 0) { 
     168                                                System.err.println("No tables in schema!"); 
     169                                        } else { 
     170                                                System.out.println("Tables:"); 
     171                                                System.out.println("-------"); 
     172                                                for (String tableName : tableNames) { 
     173                                                        System.out.println(tableName); 
     174                                                } 
     175                                        } 
     176                                } 
     177                        } 
     178                } 
     179        } 
     180 
     181        private void printSchemas(AnalyzerBeansConfiguration configuration) { 
     182                if (datastoreName == null) { 
     183                        System.err.println("You need to specify the datastore name!"); 
     184                } else { 
     185                        Datastore ds = configuration.getDatastoreCatalog().getDatastore( 
     186                                        datastoreName); 
     187                        if (ds == null) { 
     188                                System.err.println("No such datastore: " + datastoreName); 
     189                        } else { 
     190                                String[] schemaNames = ds.getDataContextProvider() 
     191                                                .getDataContext().getSchemaNames(); 
     192                                if (schemaNames == null || schemaNames.length == 0) { 
     193                                        System.out.println("No schemas in datastore!"); 
     194                                } else { 
     195                                        System.out.println("Schemas:"); 
     196                                        System.out.println("--------"); 
     197                                        for (String schemaName : schemaNames) { 
     198                                                System.out.println(schemaName); 
     199                                        } 
     200                                } 
     201                        } 
     202                } 
     203        } 
     204 
     205        private void printDatastores(AnalyzerBeansConfiguration configuration) { 
     206                String[] datastoreNames = configuration.getDatastoreCatalog() 
     207                                .getDatastoreNames(); 
     208                if (datastoreNames == null || datastoreNames.length == 0) { 
     209                        System.out.println("No datastores configured!"); 
     210                } else { 
     211                        System.out.println("Datastores:"); 
     212                        System.out.println("-----------"); 
     213                        for (String datastoreName : datastoreNames) { 
     214                                System.out.println(datastoreName); 
     215                        } 
     216                } 
     217        } 
     218 
     219        protected void runJob(AnalyzerBeansConfiguration configuration) { 
     220                AnalysisJobBuilder analysisJobBuilder = new JaxbJobFactory( 
     221                                configuration).create(jobFile); 
     222 
     223                List<AnalyzerResult> results = new AnalysisRunnerImpl(configuration) 
     224                                .run(analysisJobBuilder.toAnalysisJob()).getResults(); 
     225 
     226                for (AnalyzerResult analyzerResult : results) { 
     227                        System.out.println("\nRESULT:"); 
     228                        System.out.println(analyzerResult); 
     229                } 
     230        } 
     231 
     232        protected void printAnalyzers(AnalyzerBeansConfiguration configuration) { 
     233                Collection<AnalyzerBeanDescriptor<?>> descriptors = configuration 
     234                                .getDescriptorProvider().getAnalyzerBeanDescriptors(); 
     235                if (descriptors == null || descriptors.isEmpty()) { 
     236                        System.out.println("No analyzers configured!"); 
     237                } else { 
     238                        System.out.println("Analyzers:"); 
     239                        System.out.println("----------"); 
     240                        printBeanDescriptors(descriptors); 
     241                } 
     242        } 
     243 
     244        private void printTransformers(AnalyzerBeansConfiguration configuration) { 
     245                Collection<TransformerBeanDescriptor<?>> descriptors = configuration 
     246                                .getDescriptorProvider().getTransformerBeanDescriptors(); 
     247                if (descriptors == null || descriptors.isEmpty()) { 
     248                        System.out.println("No transformers configured!"); 
     249                } else { 
     250                        System.out.println("Transformers:"); 
     251                        System.out.println("-------------"); 
     252                        printBeanDescriptors(descriptors); 
     253                } 
     254        } 
     255 
     256        protected void printBeanDescriptors( 
     257                        Collection<? extends BeanDescriptor<?>> descriptors) { 
     258                for (BeanDescriptor<?> descriptor : descriptors) { 
     259                        System.out.println("name: " + descriptor.getDisplayName()); 
     260                        ConfiguredPropertyDescriptor propertyForInput = descriptor 
     261                                        .getConfiguredPropertyForInput(); 
     262                        if (propertyForInput != null) { 
     263                                if (propertyForInput.isArray()) { 
     264                                        System.out.println(" - Consumes multiple input columns"); 
     265                                } else { 
     266                                        System.out.println(" - Consumes a single input column"); 
     267                                } 
     268                        } 
     269                        Set<ConfiguredPropertyDescriptor> properties = descriptor 
     270                                        .getConfiguredProperties(); 
     271                        for (ConfiguredPropertyDescriptor property : properties) { 
     272                                if (property != propertyForInput) { 
     273                                        System.out.println(" - Property: name=" 
     274                                                        + property.getName() + ", type=" 
     275                                                        + property.getBaseType().getSimpleName() 
     276                                                        + ", required=" + property.isRequired()); 
     277                                } 
     278                        } 
     279                } 
     280        } 
     281 
     282        @Override 
     283        public String toString() { 
     284                return "Main[configurationFile=" 
     285                                + (configurationFile == null ? "null" : configurationFile 
     286                                                .getName()) + ", jobFile=" 
     287                                + (jobFile == null ? "null" : jobFile.getName()) 
     288                                + ", listType=" + listType + ", datastoreName=" + datastoreName 
     289                                + ", schemaName=" + schemaName + ", tableName=" + tableName 
     290                                + "]"; 
    55291        } 
    56292} 
Note: See TracChangeset for help on using the changeset viewer.