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: ValidateModelCommand.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 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   * {@code validate-model} command implementation.
44   *
45   * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
46   */
47  public final class ValidateModelCommand extends AbstractModelCommand
48  {
49  
50      /**
51       * Creates a new {@code ValidateModelCommand} instance.
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 }