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: GenerateResourcesCommand.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.ResourceFileProcessor;
52  
53  // SECTION-START[Documentation]
54  // <editor-fold defaultstate="collapsed" desc=" Generated Documentation ">
55  /**
56   * {@code generate-resources} command implementation.
57   *
58   * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
59   */
60  public final class GenerateResourcesCommand extends AbstractResourceFileProcessorCommand
61  {
62  
63      /**
64       * Creates a new {@code GenerateResourcesCommand} instance.
65       */
66      public GenerateResourcesCommand()
67      {
68          super();
69      }
70  
71      public String getName()
72      {
73          return "generate-resources";
74      }
75  
76      public String getAbbreviatedName()
77      {
78          return "gr";
79      }
80  
81      public String getShortDescription( final Locale locale )
82      {
83          return Messages.getMessage( "generateResourcesShortDescription" );
84      }
85  
86      public String getLongDescription( final Locale locale )
87      {
88          return null;
89      }
90  
91      protected void processResourceFiles( final CommandLine commandLine ) throws CommandExecutionException
92      {
93          if ( commandLine == null )
94          {
95              throw new NullPointerException( "commandLine" );
96          }
97  
98          CommandLineClassLoader classLoader = null;
99  
100         try
101         {
102             classLoader = new CommandLineClassLoader( commandLine );
103             final ModelContext context = this.createModelContext( commandLine, classLoader );
104             final Model model = this.getModel( context, commandLine );
105             final JAXBContext jaxbContext = context.createContext( model.getIdentifier() );
106             final Marshaller marshaller = context.createMarshaller( model.getIdentifier() );
107             final Source source = new JAXBSource( jaxbContext, new ObjectFactory().createModel( model ) );
108             final ModelValidationReport validationReport = context.validateModel( model.getIdentifier(), source );
109             this.log( validationReport, marshaller );
110 
111             if ( !validationReport.isModelValid() )
112             {
113                 throw new CommandExecutionException( Messages.getMessage( "invalidModel",
114                                                                           this.getModel( commandLine ) ) );
115 
116             }
117 
118             final ResourceFileProcessor tool = this.createResourceFileProcessor( commandLine );
119             tool.setModel( model );
120 
121             final File resourcesDirectory =
122                 new File( commandLine.getOptionValue( Options.RESOURCE_DIRECTORY_OPTION.getOpt() ) );
123 
124             final Specification specification = this.getSpecification( commandLine, model );
125             final Implementation implementation = this.getImplementation( commandLine, model );
126             final Module module = this.getModule( commandLine, model );
127 
128             if ( specification != null )
129             {
130                 tool.writeResourceBundleResourceFiles( specification, resourcesDirectory );
131             }
132 
133             if ( implementation != null )
134             {
135                 tool.writeResourceBundleResourceFiles( implementation, resourcesDirectory );
136             }
137 
138             if ( module != null )
139             {
140                 tool.writeResourceBundleResourceFiles( module, resourcesDirectory );
141             }
142 
143             if ( this.isModulesProcessingRequested( commandLine ) )
144             {
145                 tool.writeResourceBundleResourceFiles( resourcesDirectory );
146             }
147 
148             classLoader.close();
149             classLoader = null;
150         }
151         catch ( final JAXBException e )
152         {
153             String message = Messages.getMessage( e );
154             if ( message == null )
155             {
156                 message = Messages.getMessage( e.getLinkedException() );
157             }
158 
159             throw new CommandExecutionException( message, e );
160         }
161         catch ( final ModelException e )
162         {
163             throw new CommandExecutionException( Messages.getMessage( e ), e );
164         }
165         catch ( final IOException e )
166         {
167             throw new CommandExecutionException( Messages.getMessage( e ), e );
168         }
169         finally
170         {
171             try
172             {
173                 if ( classLoader != null )
174                 {
175                     classLoader.close();
176                 }
177             }
178             catch ( final IOException e )
179             {
180                 this.log( Level.SEVERE, Messages.getMessage( e ), e );
181             }
182         }
183     }
184 
185 }