001/*
002 * Copyright 2009 (C) 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: ManageSourcesCommand.java 5251 2016-04-25 19:46:04Z schulte $
029 *
030 */
031package org.jomc.cli.commands;
032
033import java.io.File;
034import java.io.IOException;
035import java.util.Locale;
036import java.util.logging.Level;
037import javax.xml.bind.JAXBContext;
038import javax.xml.bind.JAXBException;
039import javax.xml.bind.Marshaller;
040import javax.xml.bind.util.JAXBSource;
041import javax.xml.transform.Source;
042import org.apache.commons.cli.CommandLine;
043import org.jomc.model.Implementation;
044import org.jomc.model.Module;
045import org.jomc.model.Specification;
046import org.jomc.modlet.Model;
047import org.jomc.modlet.ModelContext;
048import org.jomc.modlet.ModelException;
049import org.jomc.modlet.ModelValidationReport;
050import org.jomc.modlet.ObjectFactory;
051import org.jomc.tools.SourceFileProcessor;
052
053/**
054 * {@code manage-sources} command implementation.
055 *
056 * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
057 */
058public final class ManageSourcesCommand extends AbstractSourceFileProcessorCommand
059{
060
061    /**
062     * Creates a new {@code ManageSourcesCommand} instance.
063     */
064    public ManageSourcesCommand()
065    {
066        super();
067    }
068
069    public String getName()
070    {
071        return "manage-sources";
072    }
073
074    public String getAbbreviatedName()
075    {
076        return "ms";
077    }
078
079    public String getShortDescription( final Locale locale )
080    {
081        return Messages.getMessage( "manageSourcesShortDescription" );
082    }
083
084    public String getLongDescription( final Locale locale )
085    {
086        return null;
087    }
088
089    protected void processSourceFiles( final CommandLine commandLine ) throws CommandExecutionException
090    {
091        if ( commandLine == null )
092        {
093            throw new NullPointerException( "commandLine" );
094        }
095
096        CommandLineClassLoader classLoader = null;
097
098        try
099        {
100            classLoader = new CommandLineClassLoader( commandLine );
101            final ModelContext context = this.createModelContext( commandLine, classLoader );
102            final Model model = this.getModel( context, commandLine );
103            final JAXBContext jaxbContext = context.createContext( model.getIdentifier() );
104            final Marshaller marshaller = context.createMarshaller( model.getIdentifier() );
105            final Source source = new JAXBSource( jaxbContext, new ObjectFactory().createModel( model ) );
106            final ModelValidationReport validationReport = context.validateModel( model.getIdentifier(), source );
107            this.log( validationReport, marshaller );
108
109            if ( !validationReport.isModelValid() )
110            {
111                throw new CommandExecutionException( Messages.getMessage( "invalidModel",
112                                                                          this.getModel( commandLine ) ) );
113
114            }
115
116            final SourceFileProcessor tool = this.createSourceFileProcessor( commandLine );
117            tool.setModel( model );
118
119            final File sourcesDirectory =
120                new File( commandLine.getOptionValue( Options.SOURCE_DIRECTORY_OPTION.getOpt() ) );
121
122            final Specification specification = this.getSpecification( commandLine, model );
123            final Implementation implementation = this.getImplementation( commandLine, model );
124            final Module module = this.getModule( commandLine, model );
125
126            if ( specification != null )
127            {
128                tool.manageSourceFiles( specification, sourcesDirectory );
129            }
130
131            if ( implementation != null )
132            {
133                tool.manageSourceFiles( implementation, sourcesDirectory );
134            }
135
136            if ( module != null )
137            {
138                tool.manageSourceFiles( module, sourcesDirectory );
139            }
140
141            if ( this.isModulesProcessingRequested( commandLine ) )
142            {
143                tool.manageSourceFiles( sourcesDirectory );
144            }
145
146            classLoader.close();
147            classLoader = null;
148        }
149        catch ( final JAXBException e )
150        {
151            String message = Messages.getMessage( e );
152            if ( message == null )
153            {
154                message = Messages.getMessage( e.getLinkedException() );
155            }
156
157            throw new CommandExecutionException( message, e );
158        }
159        catch ( final ModelException e )
160        {
161            throw new CommandExecutionException( Messages.getMessage( e ), e );
162        }
163        catch ( final IOException e )
164        {
165            throw new CommandExecutionException( Messages.getMessage( e ), e );
166        }
167        finally
168        {
169            try
170            {
171                if ( classLoader != null )
172                {
173                    classLoader.close();
174                }
175            }
176            catch ( final IOException e )
177            {
178                this.log( Level.SEVERE, Messages.getMessage( e ), e );
179            }
180        }
181    }
182
183}