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 javax.xml.bind.JAXBContext;
37 import javax.xml.bind.JAXBException;
38 import javax.xml.bind.Marshaller;
39 import javax.xml.bind.util.JAXBSource;
40 import javax.xml.transform.Source;
41 import org.apache.commons.cli.CommandLine;
42 import org.jomc.model.Implementation;
43 import org.jomc.model.Module;
44 import org.jomc.model.Specification;
45 import org.jomc.modlet.Model;
46 import org.jomc.modlet.ModelContext;
47 import org.jomc.modlet.ModelException;
48 import org.jomc.modlet.ModelValidationReport;
49 import org.jomc.modlet.ObjectFactory;
50 import org.jomc.tools.ClassFileProcessor;
51
52
53
54
55
56
57 public final class ValidateClassesCommand extends AbstractClassFileProcessorCommand
58 {
59
60
61
62
63 public ValidateClassesCommand()
64 {
65 super();
66 }
67
68 public String getName()
69 {
70 return "validate-classes";
71 }
72
73 public String getAbbreviatedName()
74 {
75 return "vc";
76 }
77
78 public String getShortDescription( final Locale locale )
79 {
80 return Messages.getMessage( "validateClassesShortDescription" );
81 }
82
83 public String getLongDescription( final Locale locale )
84 {
85 return null;
86 }
87
88 protected void processClassFiles( final CommandLine commandLine ) throws CommandExecutionException
89 {
90 if ( commandLine == null )
91 {
92 throw new NullPointerException( "commandLine" );
93 }
94
95 CommandLineClassLoader classLoader = null;
96
97 try
98 {
99 classLoader = new CommandLineClassLoader( commandLine );
100 final ModelContext context = this.createModelContext( commandLine, classLoader );
101 final Model model = this.getModel( context, commandLine );
102 final JAXBContext jaxbContext = context.createContext( model.getIdentifier() );
103 final Marshaller marshaller = context.createMarshaller( model.getIdentifier() );
104 final Source source = new JAXBSource( jaxbContext, new ObjectFactory().createModel( model ) );
105 ModelValidationReport validationReport = context.validateModel( model.getIdentifier(), source );
106 this.log( validationReport, marshaller );
107
108 if ( !validationReport.isModelValid() )
109 {
110 throw new CommandExecutionException( Messages.getMessage( "invalidModel",
111 this.getModel( commandLine ) ) );
112
113 }
114
115 final ClassFileProcessor tool = this.createClassFileProcessor( commandLine );
116 tool.setModel( model );
117
118 final Specification specification = this.getSpecification( commandLine, model );
119 final Implementation implementation = this.getImplementation( commandLine, model );
120 final Module module = this.getModule( commandLine, model );
121
122 if ( specification != null )
123 {
124 validationReport = tool.validateModelObjects( specification, context );
125
126 if ( validationReport != null )
127 {
128 this.log( validationReport, marshaller );
129
130 if ( !validationReport.isModelValid() )
131 {
132 throw new CommandExecutionException( Messages.getMessage( "invalidClasses" ) );
133 }
134 }
135 }
136
137 if ( implementation != null )
138 {
139 validationReport = tool.validateModelObjects( implementation, context );
140
141 if ( validationReport != null )
142 {
143 this.log( validationReport, marshaller );
144
145 if ( !validationReport.isModelValid() )
146 {
147 throw new CommandExecutionException( Messages.getMessage( "invalidClasses" ) );
148 }
149 }
150 }
151
152 if ( module != null )
153 {
154 validationReport = tool.validateModelObjects( module, context );
155
156 if ( validationReport != null )
157 {
158 this.log( validationReport, marshaller );
159
160 if ( !validationReport.isModelValid() )
161 {
162 throw new CommandExecutionException( Messages.getMessage( "invalidClasses" ) );
163 }
164 }
165 }
166
167 if ( this.isModulesProcessingRequested( commandLine ) )
168 {
169 validationReport = tool.validateModelObjects( context );
170
171 if ( validationReport != null )
172 {
173 this.log( validationReport, marshaller );
174
175 if ( !validationReport.isModelValid() )
176 {
177 throw new CommandExecutionException( Messages.getMessage( "invalidClasses" ) );
178 }
179 }
180 }
181
182 classLoader.close();
183 classLoader = null;
184 }
185 catch ( final JAXBException e )
186 {
187 String message = Messages.getMessage( e );
188 if ( message == null )
189 {
190 message = Messages.getMessage( e.getLinkedException() );
191 }
192
193 throw new CommandExecutionException( message, e );
194 }
195 catch ( final ModelException e )
196 {
197 throw new CommandExecutionException( Messages.getMessage( e ), e );
198 }
199 catch ( final IOException e )
200 {
201 throw new CommandExecutionException( Messages.getMessage( e ), e );
202 }
203 finally
204 {
205 try
206 {
207 if ( classLoader != null )
208 {
209 classLoader.close();
210 }
211 }
212 catch ( final IOException e )
213 {
214 this.log( Level.SEVERE, Messages.getMessage( e ), e );
215 }
216 }
217 }
218
219 }