View Javadoc
1   /*
2    * Copyright (C) 2009 Christian Schulte <cs@schulte.it>
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *   o Redistributions of source code must retain the above copyright
10   *     notice, this list of conditions and the following disclaimer.
11   *
12   *   o Redistributions in binary form must reproduce the above copyright
13   *     notice, this list of conditions and the following disclaimer in
14   *     the documentation and/or other materials provided with the
15   *     distribution.
16   *
17   * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
18   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
19   * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
20   * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT, INDIRECT,
21   * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23   * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24   * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26   * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27   *
28   * $JOMC: ValidateClassesCommand.java 5251 2016-04-25 19:46:04Z schulte $
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   * {@code validate-classes} command implementation.
54   *
55   * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
56   */
57  public final class ValidateClassesCommand extends AbstractClassFileProcessorCommand
58  {
59  
60      /**
61       * Creates a new {@code ValidateClassesCommand} instance.
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 }