View Javadoc
1   /*
2    * Copyright 2009 (C) 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: ManageSourcesCommand.java 5251 2016-04-25 19:46:04Z schulte $
29   *
30   */
31  package org.jomc.cli.commands;
32  
33  import java.io.File;
34  import java.io.IOException;
35  import java.util.Locale;
36  import java.util.logging.Level;
37  import javax.xml.bind.JAXBContext;
38  import javax.xml.bind.JAXBException;
39  import javax.xml.bind.Marshaller;
40  import javax.xml.bind.util.JAXBSource;
41  import javax.xml.transform.Source;
42  import org.apache.commons.cli.CommandLine;
43  import org.jomc.model.Implementation;
44  import org.jomc.model.Module;
45  import org.jomc.model.Specification;
46  import org.jomc.modlet.Model;
47  import org.jomc.modlet.ModelContext;
48  import org.jomc.modlet.ModelException;
49  import org.jomc.modlet.ModelValidationReport;
50  import org.jomc.modlet.ObjectFactory;
51  import org.jomc.tools.SourceFileProcessor;
52  
53  /**
54   * {@code manage-sources} command implementation.
55   *
56   * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
57   */
58  public final class ManageSourcesCommand extends AbstractSourceFileProcessorCommand
59  {
60  
61      /**
62       * Creates a new {@code ManageSourcesCommand} instance.
63       */
64      public ManageSourcesCommand()
65      {
66          super();
67      }
68  
69      public String getName()
70      {
71          return "manage-sources";
72      }
73  
74      public String getAbbreviatedName()
75      {
76          return "ms";
77      }
78  
79      public String getShortDescription( final Locale locale )
80      {
81          return Messages.getMessage( "manageSourcesShortDescription" );
82      }
83  
84      public String getLongDescription( final Locale locale )
85      {
86          return null;
87      }
88  
89      protected void processSourceFiles( final CommandLine commandLine ) throws CommandExecutionException
90      {
91          if ( commandLine == null )
92          {
93              throw new NullPointerException( "commandLine" );
94          }
95  
96          CommandLineClassLoader classLoader = null;
97  
98          try
99          {
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 }