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 package org.jomc.cli.commands;
32
33 import java.io.IOException;
34 import java.util.Locale;
35 import java.util.logging.Level;
36 import org.apache.commons.cli.CommandLine;
37 import org.jomc.modlet.Model;
38 import org.jomc.modlet.ModelContext;
39 import org.jomc.modlet.ModelException;
40 import org.jomc.modlet.ModelValidationReport;
41
42
43
44
45
46
47 public final class ValidateModelCommand extends AbstractModelCommand
48 {
49
50
51
52
53 public ValidateModelCommand()
54 {
55 super();
56 }
57
58 public String getName()
59 {
60 return "validate-model";
61 }
62
63 public String getAbbreviatedName()
64 {
65 return "vm";
66 }
67
68 public String getShortDescription( final Locale locale )
69 {
70 return Messages.getMessage( "validateModelShortDescription" );
71 }
72
73 public String getLongDescription( final Locale locale )
74 {
75 return null;
76 }
77
78 protected void executeCommand( final CommandLine commandLine ) throws CommandExecutionException
79 {
80 if ( commandLine == null )
81 {
82 throw new NullPointerException( "commandLine" );
83 }
84
85 CommandLineClassLoader classLoader = null;
86
87 try
88 {
89 classLoader = new CommandLineClassLoader( commandLine );
90 final ModelContext context = this.createModelContext( commandLine, classLoader );
91 final Model model = this.getModel( context, commandLine );
92 final ModelValidationReport validationReport = context.validateModel( model );
93 this.log( validationReport, context.createMarshaller( model.getIdentifier() ) );
94
95 if ( !validationReport.isModelValid() )
96 {
97 throw new CommandExecutionException( Messages.getMessage( "invalidModel",
98 this.getModel( commandLine ) ) );
99
100 }
101
102 classLoader.close();
103 classLoader = null;
104 }
105 catch ( final IOException e )
106 {
107 throw new CommandExecutionException( Messages.getMessage( e ), e );
108 }
109 catch ( final ModelException e )
110 {
111 throw new CommandExecutionException( Messages.getMessage( e ), e );
112 }
113 finally
114 {
115 try
116 {
117 if ( classLoader != null )
118 {
119 classLoader.close();
120 }
121 }
122 catch ( final IOException e )
123 {
124 this.log( Level.SEVERE, Messages.getMessage( e ), e );
125 }
126 }
127 }
128
129 }