001/*
002 * Copyright (C) 2009 Christian Schulte <cs@schulte.it>
003 * All rights reserved.
004 *
005 * Redistribution and use in source and binary forms, with or without
006 * modification, are permitted provided that the following conditions
007 * are met:
008 *
009 *   o Redistributions of source code must retain the above copyright
010 *     notice, this list of conditions and the following disclaimer.
011 *
012 *   o Redistributions in binary form must reproduce the above copyright
013 *     notice, this list of conditions and the following disclaimer in
014 *     the documentation and/or other materials provided with the
015 *     distribution.
016 *
017 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
018 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
019 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
020 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT, INDIRECT,
021 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
022 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
023 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
024 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
026 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027 *
028 * $JOMC: ValidateModelCommand.java 5251 2016-04-25 19:46:04Z schulte $
029 *
030 */
031package org.jomc.cli.commands;
032
033import java.io.IOException;
034import java.util.Locale;
035import java.util.logging.Level;
036import org.apache.commons.cli.CommandLine;
037import org.jomc.modlet.Model;
038import org.jomc.modlet.ModelContext;
039import org.jomc.modlet.ModelException;
040import org.jomc.modlet.ModelValidationReport;
041
042/**
043 * {@code validate-model} command implementation.
044 *
045 * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
046 */
047public final class ValidateModelCommand extends AbstractModelCommand
048{
049
050    /**
051     * Creates a new {@code ValidateModelCommand} instance.
052     */
053    public ValidateModelCommand()
054    {
055        super();
056    }
057
058    public String getName()
059    {
060        return "validate-model";
061    }
062
063    public String getAbbreviatedName()
064    {
065        return "vm";
066    }
067
068    public String getShortDescription( final Locale locale )
069    {
070        return Messages.getMessage( "validateModelShortDescription" );
071    }
072
073    public String getLongDescription( final Locale locale )
074    {
075        return null;
076    }
077
078    protected void executeCommand( final CommandLine commandLine ) throws CommandExecutionException
079    {
080        if ( commandLine == null )
081        {
082            throw new NullPointerException( "commandLine" );
083        }
084
085        CommandLineClassLoader classLoader = null;
086
087        try
088        {
089            classLoader = new CommandLineClassLoader( commandLine );
090            final ModelContext context = this.createModelContext( commandLine, classLoader );
091            final Model model = this.getModel( context, commandLine );
092            final ModelValidationReport validationReport = context.validateModel( model );
093            this.log( validationReport, context.createMarshaller( model.getIdentifier() ) );
094
095            if ( !validationReport.isModelValid() )
096            {
097                throw new CommandExecutionException( Messages.getMessage( "invalidModel",
098                                                                          this.getModel( commandLine ) ) );
099
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}